Mercurial > cpython-lru_cache-weakref
comparison Python/Python-ast.c @ 45447:393822f2ef90 legacy-trunk
Patch #1759: Backport of PEP 3129 class decorators
with some help from Georg
| author | Christian Heimes <christian@cheimes.de> |
|---|---|
| date | Sat, 23 Feb 2008 15:01:05 +0000 |
| parents | 0376f13a1af9 |
| children | 3915d5f650fa |
comparison
equal
deleted
inserted
replaced
| 45445:dda92a097e8c | 45447:393822f2ef90 |
|---|---|
| 40 static PyTypeObject *FunctionDef_type; | 40 static PyTypeObject *FunctionDef_type; |
| 41 static char *FunctionDef_fields[]={ | 41 static char *FunctionDef_fields[]={ |
| 42 "name", | 42 "name", |
| 43 "args", | 43 "args", |
| 44 "body", | 44 "body", |
| 45 "decorators", | 45 "decorator_list", |
| 46 }; | 46 }; |
| 47 static PyTypeObject *ClassDef_type; | 47 static PyTypeObject *ClassDef_type; |
| 48 static char *ClassDef_fields[]={ | 48 static char *ClassDef_fields[]={ |
| 49 "name", | 49 "name", |
| 50 "bases", | 50 "bases", |
| 51 "body", | 51 "body", |
| 52 "decorator_list", | |
| 52 }; | 53 }; |
| 53 static PyTypeObject *Return_type; | 54 static PyTypeObject *Return_type; |
| 54 static char *Return_fields[]={ | 55 static char *Return_fields[]={ |
| 55 "value", | 56 "value", |
| 56 }; | 57 }; |
| 467 if (!stmt_type) return 0; | 468 if (!stmt_type) return 0; |
| 468 if (!add_attributes(stmt_type, stmt_attributes, 2)) return 0; | 469 if (!add_attributes(stmt_type, stmt_attributes, 2)) return 0; |
| 469 FunctionDef_type = make_type("FunctionDef", stmt_type, | 470 FunctionDef_type = make_type("FunctionDef", stmt_type, |
| 470 FunctionDef_fields, 4); | 471 FunctionDef_fields, 4); |
| 471 if (!FunctionDef_type) return 0; | 472 if (!FunctionDef_type) return 0; |
| 472 ClassDef_type = make_type("ClassDef", stmt_type, ClassDef_fields, 3); | 473 ClassDef_type = make_type("ClassDef", stmt_type, ClassDef_fields, 4); |
| 473 if (!ClassDef_type) return 0; | 474 if (!ClassDef_type) return 0; |
| 474 Return_type = make_type("Return", stmt_type, Return_fields, 1); | 475 Return_type = make_type("Return", stmt_type, Return_fields, 1); |
| 475 if (!Return_type) return 0; | 476 if (!Return_type) return 0; |
| 476 Delete_type = make_type("Delete", stmt_type, Delete_fields, 1); | 477 Delete_type = make_type("Delete", stmt_type, Delete_fields, 1); |
| 477 if (!Delete_type) return 0; | 478 if (!Delete_type) return 0; |
| 788 return p; | 789 return p; |
| 789 } | 790 } |
| 790 | 791 |
| 791 stmt_ty | 792 stmt_ty |
| 792 FunctionDef(identifier name, arguments_ty args, asdl_seq * body, asdl_seq * | 793 FunctionDef(identifier name, arguments_ty args, asdl_seq * body, asdl_seq * |
| 793 decorators, int lineno, int col_offset, PyArena *arena) | 794 decorator_list, int lineno, int col_offset, PyArena *arena) |
| 794 { | 795 { |
| 795 stmt_ty p; | 796 stmt_ty p; |
| 796 if (!name) { | 797 if (!name) { |
| 797 PyErr_SetString(PyExc_ValueError, | 798 PyErr_SetString(PyExc_ValueError, |
| 798 "field name is required for FunctionDef"); | 799 "field name is required for FunctionDef"); |
| 808 return NULL; | 809 return NULL; |
| 809 p->kind = FunctionDef_kind; | 810 p->kind = FunctionDef_kind; |
| 810 p->v.FunctionDef.name = name; | 811 p->v.FunctionDef.name = name; |
| 811 p->v.FunctionDef.args = args; | 812 p->v.FunctionDef.args = args; |
| 812 p->v.FunctionDef.body = body; | 813 p->v.FunctionDef.body = body; |
| 813 p->v.FunctionDef.decorators = decorators; | 814 p->v.FunctionDef.decorator_list = decorator_list; |
| 814 p->lineno = lineno; | 815 p->lineno = lineno; |
| 815 p->col_offset = col_offset; | 816 p->col_offset = col_offset; |
| 816 return p; | 817 return p; |
| 817 } | 818 } |
| 818 | 819 |
| 819 stmt_ty | 820 stmt_ty |
| 820 ClassDef(identifier name, asdl_seq * bases, asdl_seq * body, int lineno, int | 821 ClassDef(identifier name, asdl_seq * bases, asdl_seq * body, asdl_seq * |
| 821 col_offset, PyArena *arena) | 822 decorator_list, int lineno, int col_offset, PyArena *arena) |
| 822 { | 823 { |
| 823 stmt_ty p; | 824 stmt_ty p; |
| 824 if (!name) { | 825 if (!name) { |
| 825 PyErr_SetString(PyExc_ValueError, | 826 PyErr_SetString(PyExc_ValueError, |
| 826 "field name is required for ClassDef"); | 827 "field name is required for ClassDef"); |
| 831 return NULL; | 832 return NULL; |
| 832 p->kind = ClassDef_kind; | 833 p->kind = ClassDef_kind; |
| 833 p->v.ClassDef.name = name; | 834 p->v.ClassDef.name = name; |
| 834 p->v.ClassDef.bases = bases; | 835 p->v.ClassDef.bases = bases; |
| 835 p->v.ClassDef.body = body; | 836 p->v.ClassDef.body = body; |
| 837 p->v.ClassDef.decorator_list = decorator_list; | |
| 836 p->lineno = lineno; | 838 p->lineno = lineno; |
| 837 p->col_offset = col_offset; | 839 p->col_offset = col_offset; |
| 838 return p; | 840 return p; |
| 839 } | 841 } |
| 840 | 842 |
| 1904 value = ast2obj_list(o->v.FunctionDef.body, ast2obj_stmt); | 1906 value = ast2obj_list(o->v.FunctionDef.body, ast2obj_stmt); |
| 1905 if (!value) goto failed; | 1907 if (!value) goto failed; |
| 1906 if (PyObject_SetAttrString(result, "body", value) == -1) | 1908 if (PyObject_SetAttrString(result, "body", value) == -1) |
| 1907 goto failed; | 1909 goto failed; |
| 1908 Py_DECREF(value); | 1910 Py_DECREF(value); |
| 1909 value = ast2obj_list(o->v.FunctionDef.decorators, ast2obj_expr); | 1911 value = ast2obj_list(o->v.FunctionDef.decorator_list, |
| 1910 if (!value) goto failed; | 1912 ast2obj_expr); |
| 1911 if (PyObject_SetAttrString(result, "decorators", value) == -1) | 1913 if (!value) goto failed; |
| 1914 if (PyObject_SetAttrString(result, "decorator_list", value) == | |
| 1915 -1) | |
| 1912 goto failed; | 1916 goto failed; |
| 1913 Py_DECREF(value); | 1917 Py_DECREF(value); |
| 1914 break; | 1918 break; |
| 1915 case ClassDef_kind: | 1919 case ClassDef_kind: |
| 1916 result = PyType_GenericNew(ClassDef_type, NULL, NULL); | 1920 result = PyType_GenericNew(ClassDef_type, NULL, NULL); |
| 1926 goto failed; | 1930 goto failed; |
| 1927 Py_DECREF(value); | 1931 Py_DECREF(value); |
| 1928 value = ast2obj_list(o->v.ClassDef.body, ast2obj_stmt); | 1932 value = ast2obj_list(o->v.ClassDef.body, ast2obj_stmt); |
| 1929 if (!value) goto failed; | 1933 if (!value) goto failed; |
| 1930 if (PyObject_SetAttrString(result, "body", value) == -1) | 1934 if (PyObject_SetAttrString(result, "body", value) == -1) |
| 1935 goto failed; | |
| 1936 Py_DECREF(value); | |
| 1937 value = ast2obj_list(o->v.ClassDef.decorator_list, | |
| 1938 ast2obj_expr); | |
| 1939 if (!value) goto failed; | |
| 1940 if (PyObject_SetAttrString(result, "decorator_list", value) == | |
| 1941 -1) | |
| 1931 goto failed; | 1942 goto failed; |
| 1932 Py_DECREF(value); | 1943 Py_DECREF(value); |
| 1933 break; | 1944 break; |
| 1934 case Return_kind: | 1945 case Return_kind: |
| 1935 result = PyType_GenericNew(Return_type, NULL, NULL); | 1946 result = PyType_GenericNew(Return_type, NULL, NULL); |
