comparison Python/Python-ast.c @ 40529:54cd47788811

PEP 3107 - Function Annotations thanks to Tony Lownds
author Neal Norwitz <nnorwitz@gmail.com>
date Thu, 28 Dec 2006 06:47:50 +0000
parents 1cce0526d9b0
children 6bd65cb5b952
comparison
equal deleted inserted replaced
40528:bce7e4a926a9 40529:54cd47788811
32 static char *FunctionDef_fields[]={ 32 static char *FunctionDef_fields[]={
33 "name", 33 "name",
34 "args", 34 "args",
35 "body", 35 "body",
36 "decorators", 36 "decorators",
37 "returns",
37 }; 38 };
38 static PyTypeObject *ClassDef_type; 39 static PyTypeObject *ClassDef_type;
39 static char *ClassDef_fields[]={ 40 static char *ClassDef_fields[]={
40 "name", 41 "name",
41 "bases", 42 "bases",
331 static PyTypeObject *arguments_type; 332 static PyTypeObject *arguments_type;
332 static PyObject* ast2obj_arguments(void*); 333 static PyObject* ast2obj_arguments(void*);
333 static char *arguments_fields[]={ 334 static char *arguments_fields[]={
334 "args", 335 "args",
335 "vararg", 336 "vararg",
337 "varargannotation",
336 "kwonlyargs", 338 "kwonlyargs",
337 "kwarg", 339 "kwarg",
340 "kwargannotation",
338 "defaults", 341 "defaults",
339 "kw_defaults", 342 "kw_defaults",
343 };
344 static PyTypeObject *arg_type;
345 static PyObject* ast2obj_arg(void*);
346 static PyTypeObject *SimpleArg_type;
347 static char *SimpleArg_fields[]={
348 "arg",
349 "annotation",
350 };
351 static PyTypeObject *NestedArgs_type;
352 static char *NestedArgs_fields[]={
353 "args",
340 }; 354 };
341 static PyTypeObject *keyword_type; 355 static PyTypeObject *keyword_type;
342 static PyObject* ast2obj_keyword(void*); 356 static PyObject* ast2obj_keyword(void*);
343 static char *keyword_fields[]={ 357 static char *keyword_fields[]={
344 "arg", 358 "arg",
452 if (!Suite_type) return 0; 466 if (!Suite_type) return 0;
453 stmt_type = make_type("stmt", AST_type, NULL, 0); 467 stmt_type = make_type("stmt", AST_type, NULL, 0);
454 if (!stmt_type) return 0; 468 if (!stmt_type) return 0;
455 if (!add_attributes(stmt_type, stmt_attributes, 2)) return 0; 469 if (!add_attributes(stmt_type, stmt_attributes, 2)) return 0;
456 FunctionDef_type = make_type("FunctionDef", stmt_type, 470 FunctionDef_type = make_type("FunctionDef", stmt_type,
457 FunctionDef_fields, 4); 471 FunctionDef_fields, 5);
458 if (!FunctionDef_type) return 0; 472 if (!FunctionDef_type) return 0;
459 ClassDef_type = make_type("ClassDef", stmt_type, ClassDef_fields, 3); 473 ClassDef_type = make_type("ClassDef", stmt_type, ClassDef_fields, 3);
460 if (!ClassDef_type) return 0; 474 if (!ClassDef_type) return 0;
461 Return_type = make_type("Return", stmt_type, Return_fields, 1); 475 Return_type = make_type("Return", stmt_type, Return_fields, 1);
462 if (!Return_type) return 0; 476 if (!Return_type) return 0;
708 comprehension_fields, 3); 722 comprehension_fields, 3);
709 if (!comprehension_type) return 0; 723 if (!comprehension_type) return 0;
710 excepthandler_type = make_type("excepthandler", AST_type, 724 excepthandler_type = make_type("excepthandler", AST_type,
711 excepthandler_fields, 5); 725 excepthandler_fields, 5);
712 if (!excepthandler_type) return 0; 726 if (!excepthandler_type) return 0;
713 arguments_type = make_type("arguments", AST_type, arguments_fields, 6); 727 arguments_type = make_type("arguments", AST_type, arguments_fields, 8);
714 if (!arguments_type) return 0; 728 if (!arguments_type) return 0;
729 arg_type = make_type("arg", AST_type, NULL, 0);
730 if (!arg_type) return 0;
731 if (!add_attributes(arg_type, NULL, 0)) return 0;
732 SimpleArg_type = make_type("SimpleArg", arg_type, SimpleArg_fields, 2);
733 if (!SimpleArg_type) return 0;
734 NestedArgs_type = make_type("NestedArgs", arg_type, NestedArgs_fields,
735 1);
736 if (!NestedArgs_type) return 0;
715 keyword_type = make_type("keyword", AST_type, keyword_fields, 2); 737 keyword_type = make_type("keyword", AST_type, keyword_fields, 2);
716 if (!keyword_type) return 0; 738 if (!keyword_type) return 0;
717 alias_type = make_type("alias", AST_type, alias_fields, 2); 739 alias_type = make_type("alias", AST_type, alias_fields, 2);
718 if (!alias_type) return 0; 740 if (!alias_type) return 0;
719 initialized = 1; 741 initialized = 1;
781 return p; 803 return p;
782 } 804 }
783 805
784 stmt_ty 806 stmt_ty
785 FunctionDef(identifier name, arguments_ty args, asdl_seq * body, asdl_seq * 807 FunctionDef(identifier name, arguments_ty args, asdl_seq * body, asdl_seq *
786 decorators, int lineno, int col_offset, PyArena *arena) 808 decorators, expr_ty returns, int lineno, int col_offset, PyArena
809 *arena)
787 { 810 {
788 stmt_ty p; 811 stmt_ty p;
789 if (!name) { 812 if (!name) {
790 PyErr_SetString(PyExc_ValueError, 813 PyErr_SetString(PyExc_ValueError,
791 "field name is required for FunctionDef"); 814 "field name is required for FunctionDef");
804 p->kind = FunctionDef_kind; 827 p->kind = FunctionDef_kind;
805 p->v.FunctionDef.name = name; 828 p->v.FunctionDef.name = name;
806 p->v.FunctionDef.args = args; 829 p->v.FunctionDef.args = args;
807 p->v.FunctionDef.body = body; 830 p->v.FunctionDef.body = body;
808 p->v.FunctionDef.decorators = decorators; 831 p->v.FunctionDef.decorators = decorators;
832 p->v.FunctionDef.returns = returns;
809 p->lineno = lineno; 833 p->lineno = lineno;
810 p->col_offset = col_offset; 834 p->col_offset = col_offset;
811 return p; 835 return p;
812 } 836 }
813 837
1828 p->col_offset = col_offset; 1852 p->col_offset = col_offset;
1829 return p; 1853 return p;
1830 } 1854 }
1831 1855
1832 arguments_ty 1856 arguments_ty
1833 arguments(asdl_seq * args, identifier vararg, asdl_seq * kwonlyargs, identifier 1857 arguments(asdl_seq * args, identifier vararg, expr_ty varargannotation,
1834 kwarg, asdl_seq * defaults, asdl_seq * kw_defaults, PyArena *arena) 1858 asdl_seq * kwonlyargs, identifier kwarg, expr_ty kwargannotation,
1859 asdl_seq * defaults, asdl_seq * kw_defaults, PyArena *arena)
1835 { 1860 {
1836 arguments_ty p; 1861 arguments_ty p;
1837 p = (arguments_ty)PyArena_Malloc(arena, sizeof(*p)); 1862 p = (arguments_ty)PyArena_Malloc(arena, sizeof(*p));
1838 if (!p) { 1863 if (!p) {
1839 PyErr_NoMemory(); 1864 PyErr_NoMemory();
1840 return NULL; 1865 return NULL;
1841 } 1866 }
1842 p->args = args; 1867 p->args = args;
1843 p->vararg = vararg; 1868 p->vararg = vararg;
1869 p->varargannotation = varargannotation;
1844 p->kwonlyargs = kwonlyargs; 1870 p->kwonlyargs = kwonlyargs;
1845 p->kwarg = kwarg; 1871 p->kwarg = kwarg;
1872 p->kwargannotation = kwargannotation;
1846 p->defaults = defaults; 1873 p->defaults = defaults;
1847 p->kw_defaults = kw_defaults; 1874 p->kw_defaults = kw_defaults;
1875 return p;
1876 }
1877
1878 arg_ty
1879 SimpleArg(identifier arg, expr_ty annotation, PyArena *arena)
1880 {
1881 arg_ty p;
1882 if (!arg) {
1883 PyErr_SetString(PyExc_ValueError,
1884 "field arg is required for SimpleArg");
1885 return NULL;
1886 }
1887 p = (arg_ty)PyArena_Malloc(arena, sizeof(*p));
1888 if (!p) {
1889 PyErr_NoMemory();
1890 return NULL;
1891 }
1892 p->kind = SimpleArg_kind;
1893 p->v.SimpleArg.arg = arg;
1894 p->v.SimpleArg.annotation = annotation;
1895 return p;
1896 }
1897
1898 arg_ty
1899 NestedArgs(asdl_seq * args, PyArena *arena)
1900 {
1901 arg_ty p;
1902 p = (arg_ty)PyArena_Malloc(arena, sizeof(*p));
1903 if (!p) {
1904 PyErr_NoMemory();
1905 return NULL;
1906 }
1907 p->kind = NestedArgs_kind;
1908 p->v.NestedArgs.args = args;
1848 return p; 1909 return p;
1849 } 1910 }
1850 1911
1851 keyword_ty 1912 keyword_ty
1852 keyword(identifier arg, expr_ty value, PyArena *arena) 1913 keyword(identifier arg, expr_ty value, PyArena *arena)
1977 goto failed; 2038 goto failed;
1978 Py_DECREF(value); 2039 Py_DECREF(value);
1979 value = ast2obj_list(o->v.FunctionDef.decorators, ast2obj_expr); 2040 value = ast2obj_list(o->v.FunctionDef.decorators, ast2obj_expr);
1980 if (!value) goto failed; 2041 if (!value) goto failed;
1981 if (PyObject_SetAttrString(result, "decorators", value) == -1) 2042 if (PyObject_SetAttrString(result, "decorators", value) == -1)
2043 goto failed;
2044 Py_DECREF(value);
2045 value = ast2obj_expr(o->v.FunctionDef.returns);
2046 if (!value) goto failed;
2047 if (PyObject_SetAttrString(result, "returns", value) == -1)
1982 goto failed; 2048 goto failed;
1983 Py_DECREF(value); 2049 Py_DECREF(value);
1984 break; 2050 break;
1985 case ClassDef_kind: 2051 case ClassDef_kind:
1986 result = PyType_GenericNew(ClassDef_type, NULL, NULL); 2052 result = PyType_GenericNew(ClassDef_type, NULL, NULL);
2899 return Py_None; 2965 return Py_None;
2900 } 2966 }
2901 2967
2902 result = PyType_GenericNew(arguments_type, NULL, NULL); 2968 result = PyType_GenericNew(arguments_type, NULL, NULL);
2903 if (!result) return NULL; 2969 if (!result) return NULL;
2904 value = ast2obj_list(o->args, ast2obj_expr); 2970 value = ast2obj_list(o->args, ast2obj_arg);
2905 if (!value) goto failed; 2971 if (!value) goto failed;
2906 if (PyObject_SetAttrString(result, "args", value) == -1) 2972 if (PyObject_SetAttrString(result, "args", value) == -1)
2907 goto failed; 2973 goto failed;
2908 Py_DECREF(value); 2974 Py_DECREF(value);
2909 value = ast2obj_identifier(o->vararg); 2975 value = ast2obj_identifier(o->vararg);
2910 if (!value) goto failed; 2976 if (!value) goto failed;
2911 if (PyObject_SetAttrString(result, "vararg", value) == -1) 2977 if (PyObject_SetAttrString(result, "vararg", value) == -1)
2912 goto failed; 2978 goto failed;
2913 Py_DECREF(value); 2979 Py_DECREF(value);
2914 value = ast2obj_list(o->kwonlyargs, ast2obj_expr); 2980 value = ast2obj_expr(o->varargannotation);
2981 if (!value) goto failed;
2982 if (PyObject_SetAttrString(result, "varargannotation", value) == -1)
2983 goto failed;
2984 Py_DECREF(value);
2985 value = ast2obj_list(o->kwonlyargs, ast2obj_arg);
2915 if (!value) goto failed; 2986 if (!value) goto failed;
2916 if (PyObject_SetAttrString(result, "kwonlyargs", value) == -1) 2987 if (PyObject_SetAttrString(result, "kwonlyargs", value) == -1)
2917 goto failed; 2988 goto failed;
2918 Py_DECREF(value); 2989 Py_DECREF(value);
2919 value = ast2obj_identifier(o->kwarg); 2990 value = ast2obj_identifier(o->kwarg);
2920 if (!value) goto failed; 2991 if (!value) goto failed;
2921 if (PyObject_SetAttrString(result, "kwarg", value) == -1) 2992 if (PyObject_SetAttrString(result, "kwarg", value) == -1)
2993 goto failed;
2994 Py_DECREF(value);
2995 value = ast2obj_expr(o->kwargannotation);
2996 if (!value) goto failed;
2997 if (PyObject_SetAttrString(result, "kwargannotation", value) == -1)
2922 goto failed; 2998 goto failed;
2923 Py_DECREF(value); 2999 Py_DECREF(value);
2924 value = ast2obj_list(o->defaults, ast2obj_expr); 3000 value = ast2obj_list(o->defaults, ast2obj_expr);
2925 if (!value) goto failed; 3001 if (!value) goto failed;
2926 if (PyObject_SetAttrString(result, "defaults", value) == -1) 3002 if (PyObject_SetAttrString(result, "defaults", value) == -1)
2937 Py_XDECREF(result); 3013 Py_XDECREF(result);
2938 return NULL; 3014 return NULL;
2939 } 3015 }
2940 3016
2941 PyObject* 3017 PyObject*
3018 ast2obj_arg(void* _o)
3019 {
3020 arg_ty o = (arg_ty)_o;
3021 PyObject *result = NULL, *value = NULL;
3022 if (!o) {
3023 Py_INCREF(Py_None);
3024 return Py_None;
3025 }
3026
3027 switch (o->kind) {
3028 case SimpleArg_kind:
3029 result = PyType_GenericNew(SimpleArg_type, NULL, NULL);
3030 if (!result) goto failed;
3031 value = ast2obj_identifier(o->v.SimpleArg.arg);
3032 if (!value) goto failed;
3033 if (PyObject_SetAttrString(result, "arg", value) == -1)
3034 goto failed;
3035 Py_DECREF(value);
3036 value = ast2obj_expr(o->v.SimpleArg.annotation);
3037 if (!value) goto failed;
3038 if (PyObject_SetAttrString(result, "annotation", value) == -1)
3039 goto failed;
3040 Py_DECREF(value);
3041 break;
3042 case NestedArgs_kind:
3043 result = PyType_GenericNew(NestedArgs_type, NULL, NULL);
3044 if (!result) goto failed;
3045 value = ast2obj_list(o->v.NestedArgs.args, ast2obj_arg);
3046 if (!value) goto failed;
3047 if (PyObject_SetAttrString(result, "args", value) == -1)
3048 goto failed;
3049 Py_DECREF(value);
3050 break;
3051 }
3052 return result;
3053 failed:
3054 Py_XDECREF(value);
3055 Py_XDECREF(result);
3056 return NULL;
3057 }
3058
3059 PyObject*
2942 ast2obj_keyword(void* _o) 3060 ast2obj_keyword(void* _o)
2943 { 3061 {
2944 keyword_ty o = (keyword_ty)_o; 3062 keyword_ty o = (keyword_ty)_o;
2945 PyObject *result = NULL, *value = NULL; 3063 PyObject *result = NULL, *value = NULL;
2946 if (!o) { 3064 if (!o) {
3006 if (!m) return; 3124 if (!m) return;
3007 d = PyModule_GetDict(m); 3125 d = PyModule_GetDict(m);
3008 if (PyDict_SetItemString(d, "AST", (PyObject*)AST_type) < 0) return; 3126 if (PyDict_SetItemString(d, "AST", (PyObject*)AST_type) < 0) return;
3009 if (PyModule_AddIntConstant(m, "PyCF_ONLY_AST", PyCF_ONLY_AST) < 0) 3127 if (PyModule_AddIntConstant(m, "PyCF_ONLY_AST", PyCF_ONLY_AST) < 0)
3010 return; 3128 return;
3011 if (PyModule_AddStringConstant(m, "__version__", "51773") < 0) 3129 if (PyModule_AddStringConstant(m, "__version__", "52491") < 0)
3012 return; 3130 return;
3013 if (PyDict_SetItemString(d, "mod", (PyObject*)mod_type) < 0) return; 3131 if (PyDict_SetItemString(d, "mod", (PyObject*)mod_type) < 0) return;
3014 if (PyDict_SetItemString(d, "Module", (PyObject*)Module_type) < 0) 3132 if (PyDict_SetItemString(d, "Module", (PyObject*)Module_type) < 0)
3015 return; 3133 return;
3016 if (PyDict_SetItemString(d, "Interactive", (PyObject*)Interactive_type) 3134 if (PyDict_SetItemString(d, "Interactive", (PyObject*)Interactive_type)
3144 (PyObject*)comprehension_type) < 0) return; 3262 (PyObject*)comprehension_type) < 0) return;
3145 if (PyDict_SetItemString(d, "excepthandler", 3263 if (PyDict_SetItemString(d, "excepthandler",
3146 (PyObject*)excepthandler_type) < 0) return; 3264 (PyObject*)excepthandler_type) < 0) return;
3147 if (PyDict_SetItemString(d, "arguments", (PyObject*)arguments_type) < 3265 if (PyDict_SetItemString(d, "arguments", (PyObject*)arguments_type) <
3148 0) return; 3266 0) return;
3267 if (PyDict_SetItemString(d, "arg", (PyObject*)arg_type) < 0) return;
3268 if (PyDict_SetItemString(d, "SimpleArg", (PyObject*)SimpleArg_type) <
3269 0) return;
3270 if (PyDict_SetItemString(d, "NestedArgs", (PyObject*)NestedArgs_type) <
3271 0) return;
3149 if (PyDict_SetItemString(d, "keyword", (PyObject*)keyword_type) < 0) 3272 if (PyDict_SetItemString(d, "keyword", (PyObject*)keyword_type) < 0)
3150 return; 3273 return;
3151 if (PyDict_SetItemString(d, "alias", (PyObject*)alias_type) < 0) return; 3274 if (PyDict_SetItemString(d, "alias", (PyObject*)alias_type) < 0) return;
3152 } 3275 }
3153 3276