Mercurial > cpython-lru_cache-weakref
comparison Python/Python-ast.c @ 46200:2ff88ae9850c legacy-trunk
Patch #2511: Give the "excepthandler" AST item proper attributes by making it a Sum.
| author | Georg Brandl <georg@python.org> |
|---|---|
| date | Sun, 30 Mar 2008 06:40:17 +0000 |
| parents | b0bf06d49647 |
| children | 42c5d287a852 |
comparison
equal
deleted
inserted
replaced
| 46198:e4486c343f7f | 46200:2ff88ae9850c |
|---|---|
| 334 "target", | 334 "target", |
| 335 "iter", | 335 "iter", |
| 336 "ifs", | 336 "ifs", |
| 337 }; | 337 }; |
| 338 static PyTypeObject *excepthandler_type; | 338 static PyTypeObject *excepthandler_type; |
| 339 static char *excepthandler_attributes[] = { | |
| 340 "lineno", | |
| 341 "col_offset", | |
| 342 }; | |
| 339 static PyObject* ast2obj_excepthandler(void*); | 343 static PyObject* ast2obj_excepthandler(void*); |
| 340 static char *excepthandler_fields[]={ | 344 static PyTypeObject *ExceptHandler_type; |
| 345 static char *ExceptHandler_fields[]={ | |
| 341 "type", | 346 "type", |
| 342 "name", | 347 "name", |
| 343 "body", | 348 "body", |
| 344 "lineno", | |
| 345 "col_offset", | |
| 346 }; | 349 }; |
| 347 static PyTypeObject *arguments_type; | 350 static PyTypeObject *arguments_type; |
| 348 static PyObject* ast2obj_arguments(void*); | 351 static PyObject* ast2obj_arguments(void*); |
| 349 static char *arguments_fields[]={ | 352 static char *arguments_fields[]={ |
| 350 "args", | 353 "args", |
| 774 NotIn_singleton = PyType_GenericNew(NotIn_type, NULL, NULL); | 777 NotIn_singleton = PyType_GenericNew(NotIn_type, NULL, NULL); |
| 775 if (!NotIn_singleton) return 0; | 778 if (!NotIn_singleton) return 0; |
| 776 comprehension_type = make_type("comprehension", AST_type, | 779 comprehension_type = make_type("comprehension", AST_type, |
| 777 comprehension_fields, 3); | 780 comprehension_fields, 3); |
| 778 if (!comprehension_type) return 0; | 781 if (!comprehension_type) return 0; |
| 779 excepthandler_type = make_type("excepthandler", AST_type, | 782 excepthandler_type = make_type("excepthandler", AST_type, NULL, 0); |
| 780 excepthandler_fields, 5); | |
| 781 if (!excepthandler_type) return 0; | 783 if (!excepthandler_type) return 0; |
| 784 if (!add_attributes(excepthandler_type, excepthandler_attributes, 2)) | |
| 785 return 0; | |
| 786 ExceptHandler_type = make_type("ExceptHandler", excepthandler_type, | |
| 787 ExceptHandler_fields, 3); | |
| 788 if (!ExceptHandler_type) return 0; | |
| 782 arguments_type = make_type("arguments", AST_type, arguments_fields, 4); | 789 arguments_type = make_type("arguments", AST_type, arguments_fields, 4); |
| 783 if (!arguments_type) return 0; | 790 if (!arguments_type) return 0; |
| 784 keyword_type = make_type("keyword", AST_type, keyword_fields, 2); | 791 keyword_type = make_type("keyword", AST_type, keyword_fields, 2); |
| 785 if (!keyword_type) return 0; | 792 if (!keyword_type) return 0; |
| 786 alias_type = make_type("alias", AST_type, alias_fields, 2); | 793 alias_type = make_type("alias", AST_type, alias_fields, 2); |
| 1823 p->ifs = ifs; | 1830 p->ifs = ifs; |
| 1824 return p; | 1831 return p; |
| 1825 } | 1832 } |
| 1826 | 1833 |
| 1827 excepthandler_ty | 1834 excepthandler_ty |
| 1828 excepthandler(expr_ty type, expr_ty name, asdl_seq * body, int lineno, int | 1835 ExceptHandler(expr_ty type, expr_ty name, asdl_seq * body, int lineno, int |
| 1829 col_offset, PyArena *arena) | 1836 col_offset, PyArena *arena) |
| 1830 { | 1837 { |
| 1831 excepthandler_ty p; | 1838 excepthandler_ty p; |
| 1832 p = (excepthandler_ty)PyArena_Malloc(arena, sizeof(*p)); | 1839 p = (excepthandler_ty)PyArena_Malloc(arena, sizeof(*p)); |
| 1833 if (!p) | 1840 if (!p) |
| 1834 return NULL; | 1841 return NULL; |
| 1835 p->type = type; | 1842 p->kind = ExceptHandler_kind; |
| 1836 p->name = name; | 1843 p->v.ExceptHandler.type = type; |
| 1837 p->body = body; | 1844 p->v.ExceptHandler.name = name; |
| 1845 p->v.ExceptHandler.body = body; | |
| 1838 p->lineno = lineno; | 1846 p->lineno = lineno; |
| 1839 p->col_offset = col_offset; | 1847 p->col_offset = col_offset; |
| 1840 return p; | 1848 return p; |
| 1841 } | 1849 } |
| 1842 | 1850 |
| 2899 if (!o) { | 2907 if (!o) { |
| 2900 Py_INCREF(Py_None); | 2908 Py_INCREF(Py_None); |
| 2901 return Py_None; | 2909 return Py_None; |
| 2902 } | 2910 } |
| 2903 | 2911 |
| 2904 result = PyType_GenericNew(excepthandler_type, NULL, NULL); | 2912 switch (o->kind) { |
| 2905 if (!result) return NULL; | 2913 case ExceptHandler_kind: |
| 2906 value = ast2obj_expr(o->type); | 2914 result = PyType_GenericNew(ExceptHandler_type, NULL, NULL); |
| 2907 if (!value) goto failed; | 2915 if (!result) goto failed; |
| 2908 if (PyObject_SetAttrString(result, "type", value) == -1) | 2916 value = ast2obj_expr(o->v.ExceptHandler.type); |
| 2909 goto failed; | 2917 if (!value) goto failed; |
| 2910 Py_DECREF(value); | 2918 if (PyObject_SetAttrString(result, "type", value) == -1) |
| 2911 value = ast2obj_expr(o->name); | 2919 goto failed; |
| 2912 if (!value) goto failed; | 2920 Py_DECREF(value); |
| 2913 if (PyObject_SetAttrString(result, "name", value) == -1) | 2921 value = ast2obj_expr(o->v.ExceptHandler.name); |
| 2914 goto failed; | 2922 if (!value) goto failed; |
| 2915 Py_DECREF(value); | 2923 if (PyObject_SetAttrString(result, "name", value) == -1) |
| 2916 value = ast2obj_list(o->body, ast2obj_stmt); | 2924 goto failed; |
| 2917 if (!value) goto failed; | 2925 Py_DECREF(value); |
| 2918 if (PyObject_SetAttrString(result, "body", value) == -1) | 2926 value = ast2obj_list(o->v.ExceptHandler.body, ast2obj_stmt); |
| 2919 goto failed; | 2927 if (!value) goto failed; |
| 2920 Py_DECREF(value); | 2928 if (PyObject_SetAttrString(result, "body", value) == -1) |
| 2929 goto failed; | |
| 2930 Py_DECREF(value); | |
| 2931 break; | |
| 2932 } | |
| 2921 value = ast2obj_int(o->lineno); | 2933 value = ast2obj_int(o->lineno); |
| 2922 if (!value) goto failed; | 2934 if (!value) goto failed; |
| 2923 if (PyObject_SetAttrString(result, "lineno", value) == -1) | 2935 if (PyObject_SetAttrString(result, "lineno", value) < 0) |
| 2924 goto failed; | 2936 goto failed; |
| 2925 Py_DECREF(value); | 2937 Py_DECREF(value); |
| 2926 value = ast2obj_int(o->col_offset); | 2938 value = ast2obj_int(o->col_offset); |
| 2927 if (!value) goto failed; | 2939 if (!value) goto failed; |
| 2928 if (PyObject_SetAttrString(result, "col_offset", value) == -1) | 2940 if (PyObject_SetAttrString(result, "col_offset", value) < 0) |
| 2929 goto failed; | 2941 goto failed; |
| 2930 Py_DECREF(value); | 2942 Py_DECREF(value); |
| 2931 return result; | 2943 return result; |
| 2932 failed: | 2944 failed: |
| 2933 Py_XDECREF(value); | 2945 Py_XDECREF(value); |
| 5532 | 5544 |
| 5533 int | 5545 int |
| 5534 obj2ast_excepthandler(PyObject* obj, excepthandler_ty* out, PyArena* arena) | 5546 obj2ast_excepthandler(PyObject* obj, excepthandler_ty* out, PyArena* arena) |
| 5535 { | 5547 { |
| 5536 PyObject* tmp = NULL; | 5548 PyObject* tmp = NULL; |
| 5537 expr_ty type; | 5549 |
| 5538 expr_ty name; | |
| 5539 asdl_seq* body; | |
| 5540 int lineno; | 5550 int lineno; |
| 5541 int col_offset; | 5551 int col_offset; |
| 5542 | 5552 |
| 5543 if (PyObject_HasAttrString(obj, "type")) { | 5553 if (obj == Py_None) { |
| 5544 int res; | 5554 *out = NULL; |
| 5545 tmp = PyObject_GetAttrString(obj, "type"); | 5555 return 0; |
| 5546 if (tmp == NULL) goto failed; | |
| 5547 res = obj2ast_expr(tmp, &type, arena); | |
| 5548 if (res != 0) goto failed; | |
| 5549 Py_XDECREF(tmp); | |
| 5550 tmp = NULL; | |
| 5551 } else { | |
| 5552 type = NULL; | |
| 5553 } | |
| 5554 if (PyObject_HasAttrString(obj, "name")) { | |
| 5555 int res; | |
| 5556 tmp = PyObject_GetAttrString(obj, "name"); | |
| 5557 if (tmp == NULL) goto failed; | |
| 5558 res = obj2ast_expr(tmp, &name, arena); | |
| 5559 if (res != 0) goto failed; | |
| 5560 Py_XDECREF(tmp); | |
| 5561 tmp = NULL; | |
| 5562 } else { | |
| 5563 name = NULL; | |
| 5564 } | |
| 5565 if (PyObject_HasAttrString(obj, "body")) { | |
| 5566 int res; | |
| 5567 Py_ssize_t len; | |
| 5568 Py_ssize_t i; | |
| 5569 tmp = PyObject_GetAttrString(obj, "body"); | |
| 5570 if (tmp == NULL) goto failed; | |
| 5571 if (!PyList_Check(tmp)) { | |
| 5572 PyErr_Format(PyExc_TypeError, "excepthandler field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name); | |
| 5573 goto failed; | |
| 5574 } | |
| 5575 len = PyList_GET_SIZE(tmp); | |
| 5576 body = asdl_seq_new(len, arena); | |
| 5577 if (body == NULL) goto failed; | |
| 5578 for (i = 0; i < len; i++) { | |
| 5579 stmt_ty value; | |
| 5580 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena); | |
| 5581 if (res != 0) goto failed; | |
| 5582 asdl_seq_SET(body, i, value); | |
| 5583 } | |
| 5584 Py_XDECREF(tmp); | |
| 5585 tmp = NULL; | |
| 5586 } else { | |
| 5587 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from excepthandler"); | |
| 5588 return 1; | |
| 5589 } | 5556 } |
| 5590 if (PyObject_HasAttrString(obj, "lineno")) { | 5557 if (PyObject_HasAttrString(obj, "lineno")) { |
| 5591 int res; | 5558 int res; |
| 5592 tmp = PyObject_GetAttrString(obj, "lineno"); | 5559 tmp = PyObject_GetAttrString(obj, "lineno"); |
| 5593 if (tmp == NULL) goto failed; | 5560 if (tmp == NULL) goto failed; |
| 5609 tmp = NULL; | 5576 tmp = NULL; |
| 5610 } else { | 5577 } else { |
| 5611 PyErr_SetString(PyExc_TypeError, "required field \"col_offset\" missing from excepthandler"); | 5578 PyErr_SetString(PyExc_TypeError, "required field \"col_offset\" missing from excepthandler"); |
| 5612 return 1; | 5579 return 1; |
| 5613 } | 5580 } |
| 5614 *out = excepthandler(type, name, body, lineno, col_offset, arena); | 5581 if (PyObject_IsInstance(obj, (PyObject*)ExceptHandler_type)) { |
| 5615 return 0; | 5582 expr_ty type; |
| 5583 expr_ty name; | |
| 5584 asdl_seq* body; | |
| 5585 | |
| 5586 if (PyObject_HasAttrString(obj, "type")) { | |
| 5587 int res; | |
| 5588 tmp = PyObject_GetAttrString(obj, "type"); | |
| 5589 if (tmp == NULL) goto failed; | |
| 5590 res = obj2ast_expr(tmp, &type, arena); | |
| 5591 if (res != 0) goto failed; | |
| 5592 Py_XDECREF(tmp); | |
| 5593 tmp = NULL; | |
| 5594 } else { | |
| 5595 type = NULL; | |
| 5596 } | |
| 5597 if (PyObject_HasAttrString(obj, "name")) { | |
| 5598 int res; | |
| 5599 tmp = PyObject_GetAttrString(obj, "name"); | |
| 5600 if (tmp == NULL) goto failed; | |
| 5601 res = obj2ast_expr(tmp, &name, arena); | |
| 5602 if (res != 0) goto failed; | |
| 5603 Py_XDECREF(tmp); | |
| 5604 tmp = NULL; | |
| 5605 } else { | |
| 5606 name = NULL; | |
| 5607 } | |
| 5608 if (PyObject_HasAttrString(obj, "body")) { | |
| 5609 int res; | |
| 5610 Py_ssize_t len; | |
| 5611 Py_ssize_t i; | |
| 5612 tmp = PyObject_GetAttrString(obj, "body"); | |
| 5613 if (tmp == NULL) goto failed; | |
| 5614 if (!PyList_Check(tmp)) { | |
| 5615 PyErr_Format(PyExc_TypeError, "ExceptHandler field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name); | |
| 5616 goto failed; | |
| 5617 } | |
| 5618 len = PyList_GET_SIZE(tmp); | |
| 5619 body = asdl_seq_new(len, arena); | |
| 5620 if (body == NULL) goto failed; | |
| 5621 for (i = 0; i < len; i++) { | |
| 5622 stmt_ty value; | |
| 5623 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena); | |
| 5624 if (res != 0) goto failed; | |
| 5625 asdl_seq_SET(body, i, value); | |
| 5626 } | |
| 5627 Py_XDECREF(tmp); | |
| 5628 tmp = NULL; | |
| 5629 } else { | |
| 5630 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from ExceptHandler"); | |
| 5631 return 1; | |
| 5632 } | |
| 5633 *out = ExceptHandler(type, name, body, lineno, col_offset, | |
| 5634 arena); | |
| 5635 if (*out == NULL) goto failed; | |
| 5636 return 0; | |
| 5637 } | |
| 5638 | |
| 5639 tmp = PyObject_Repr(obj); | |
| 5640 if (tmp == NULL) goto failed; | |
| 5641 PyErr_Format(PyExc_TypeError, "expected some sort of excepthandler, but got %.400s", PyString_AS_STRING(tmp)); | |
| 5616 failed: | 5642 failed: |
| 5617 Py_XDECREF(tmp); | 5643 Py_XDECREF(tmp); |
| 5618 return 1; | 5644 return 1; |
| 5619 } | 5645 } |
| 5620 | 5646 |
| 5928 if (PyDict_SetItemString(d, "NotIn", (PyObject*)NotIn_type) < 0) return; | 5954 if (PyDict_SetItemString(d, "NotIn", (PyObject*)NotIn_type) < 0) return; |
| 5929 if (PyDict_SetItemString(d, "comprehension", | 5955 if (PyDict_SetItemString(d, "comprehension", |
| 5930 (PyObject*)comprehension_type) < 0) return; | 5956 (PyObject*)comprehension_type) < 0) return; |
| 5931 if (PyDict_SetItemString(d, "excepthandler", | 5957 if (PyDict_SetItemString(d, "excepthandler", |
| 5932 (PyObject*)excepthandler_type) < 0) return; | 5958 (PyObject*)excepthandler_type) < 0) return; |
| 5959 if (PyDict_SetItemString(d, "ExceptHandler", | |
| 5960 (PyObject*)ExceptHandler_type) < 0) return; | |
| 5933 if (PyDict_SetItemString(d, "arguments", (PyObject*)arguments_type) < | 5961 if (PyDict_SetItemString(d, "arguments", (PyObject*)arguments_type) < |
| 5934 0) return; | 5962 0) return; |
| 5935 if (PyDict_SetItemString(d, "keyword", (PyObject*)keyword_type) < 0) | 5963 if (PyDict_SetItemString(d, "keyword", (PyObject*)keyword_type) < 0) |
| 5936 return; | 5964 return; |
| 5937 if (PyDict_SetItemString(d, "alias", (PyObject*)alias_type) < 0) return; | 5965 if (PyDict_SetItemString(d, "alias", (PyObject*)alias_type) < 0) return; |
