Mercurial > cpython-lru_cache-weakref
comparison Python/Python-ast.c @ 46231:81ee270a6eb5
Merged revisions 62047 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r62047 | georg.brandl | 2008-03-29 23:40:17 -0700 (Sat, 29 Mar 2008) | 2 lines
Patch #2511: Give the "excepthandler" AST item proper attributes by making it a Sum.
........
| author | Neal Norwitz <nnorwitz@gmail.com> |
|---|---|
| date | Mon, 31 Mar 2008 05:14:30 +0000 |
| parents | 07b36d2b7534 |
| children | 61c92923cf7f |
comparison
equal
deleted
inserted
replaced
| 46230:e47481764e57 | 46231:81ee270a6eb5 |
|---|---|
| 349 "target", | 349 "target", |
| 350 "iter", | 350 "iter", |
| 351 "ifs", | 351 "ifs", |
| 352 }; | 352 }; |
| 353 static PyTypeObject *excepthandler_type; | 353 static PyTypeObject *excepthandler_type; |
| 354 static char *excepthandler_attributes[] = { | |
| 355 "lineno", | |
| 356 "col_offset", | |
| 357 }; | |
| 354 static PyObject* ast2obj_excepthandler(void*); | 358 static PyObject* ast2obj_excepthandler(void*); |
| 355 static char *excepthandler_fields[]={ | 359 static PyTypeObject *ExceptHandler_type; |
| 360 static char *ExceptHandler_fields[]={ | |
| 356 "type", | 361 "type", |
| 357 "name", | 362 "name", |
| 358 "body", | 363 "body", |
| 359 "lineno", | |
| 360 "col_offset", | |
| 361 }; | 364 }; |
| 362 static PyTypeObject *arguments_type; | 365 static PyTypeObject *arguments_type; |
| 363 static PyObject* ast2obj_arguments(void*); | 366 static PyObject* ast2obj_arguments(void*); |
| 364 static char *arguments_fields[]={ | 367 static char *arguments_fields[]={ |
| 365 "args", | 368 "args", |
| 879 NotIn_singleton = PyType_GenericNew(NotIn_type, NULL, NULL); | 882 NotIn_singleton = PyType_GenericNew(NotIn_type, NULL, NULL); |
| 880 if (!NotIn_singleton) return 0; | 883 if (!NotIn_singleton) return 0; |
| 881 comprehension_type = make_type("comprehension", &AST_type, | 884 comprehension_type = make_type("comprehension", &AST_type, |
| 882 comprehension_fields, 3); | 885 comprehension_fields, 3); |
| 883 if (!comprehension_type) return 0; | 886 if (!comprehension_type) return 0; |
| 884 excepthandler_type = make_type("excepthandler", &AST_type, | 887 excepthandler_type = make_type("excepthandler", &AST_type, NULL, 0); |
| 885 excepthandler_fields, 5); | |
| 886 if (!excepthandler_type) return 0; | 888 if (!excepthandler_type) return 0; |
| 889 if (!add_attributes(excepthandler_type, excepthandler_attributes, 2)) | |
| 890 return 0; | |
| 891 ExceptHandler_type = make_type("ExceptHandler", excepthandler_type, | |
| 892 ExceptHandler_fields, 3); | |
| 893 if (!ExceptHandler_type) return 0; | |
| 887 arguments_type = make_type("arguments", &AST_type, arguments_fields, 8); | 894 arguments_type = make_type("arguments", &AST_type, arguments_fields, 8); |
| 888 if (!arguments_type) return 0; | 895 if (!arguments_type) return 0; |
| 889 arg_type = make_type("arg", &AST_type, arg_fields, 2); | 896 arg_type = make_type("arg", &AST_type, arg_fields, 2); |
| 890 if (!arg_type) return 0; | 897 if (!arg_type) return 0; |
| 891 keyword_type = make_type("keyword", &AST_type, keyword_fields, 2); | 898 keyword_type = make_type("keyword", &AST_type, keyword_fields, 2); |
| 2000 p->ifs = ifs; | 2007 p->ifs = ifs; |
| 2001 return p; | 2008 return p; |
| 2002 } | 2009 } |
| 2003 | 2010 |
| 2004 excepthandler_ty | 2011 excepthandler_ty |
| 2005 excepthandler(expr_ty type, identifier name, asdl_seq * body, int lineno, int | 2012 ExceptHandler(expr_ty type, identifier name, asdl_seq * body, int lineno, int |
| 2006 col_offset, PyArena *arena) | 2013 col_offset, PyArena *arena) |
| 2007 { | 2014 { |
| 2008 excepthandler_ty p; | 2015 excepthandler_ty p; |
| 2009 p = (excepthandler_ty)PyArena_Malloc(arena, sizeof(*p)); | 2016 p = (excepthandler_ty)PyArena_Malloc(arena, sizeof(*p)); |
| 2010 if (!p) | 2017 if (!p) |
| 2011 return NULL; | 2018 return NULL; |
| 2012 p->type = type; | 2019 p->kind = ExceptHandler_kind; |
| 2013 p->name = name; | 2020 p->v.ExceptHandler.type = type; |
| 2014 p->body = body; | 2021 p->v.ExceptHandler.name = name; |
| 2022 p->v.ExceptHandler.body = body; | |
| 2015 p->lineno = lineno; | 2023 p->lineno = lineno; |
| 2016 p->col_offset = col_offset; | 2024 p->col_offset = col_offset; |
| 2017 return p; | 2025 return p; |
| 2018 } | 2026 } |
| 2019 | 2027 |
| 3142 if (!o) { | 3150 if (!o) { |
| 3143 Py_INCREF(Py_None); | 3151 Py_INCREF(Py_None); |
| 3144 return Py_None; | 3152 return Py_None; |
| 3145 } | 3153 } |
| 3146 | 3154 |
| 3147 result = PyType_GenericNew(excepthandler_type, NULL, NULL); | 3155 switch (o->kind) { |
| 3148 if (!result) return NULL; | 3156 case ExceptHandler_kind: |
| 3149 value = ast2obj_expr(o->type); | 3157 result = PyType_GenericNew(ExceptHandler_type, NULL, NULL); |
| 3150 if (!value) goto failed; | 3158 if (!result) goto failed; |
| 3151 if (PyObject_SetAttrString(result, "type", value) == -1) | 3159 value = ast2obj_expr(o->v.ExceptHandler.type); |
| 3152 goto failed; | 3160 if (!value) goto failed; |
| 3153 Py_DECREF(value); | 3161 if (PyObject_SetAttrString(result, "type", value) == -1) |
| 3154 value = ast2obj_identifier(o->name); | 3162 goto failed; |
| 3155 if (!value) goto failed; | 3163 Py_DECREF(value); |
| 3156 if (PyObject_SetAttrString(result, "name", value) == -1) | 3164 value = ast2obj_identifier(o->v.ExceptHandler.name); |
| 3157 goto failed; | 3165 if (!value) goto failed; |
| 3158 Py_DECREF(value); | 3166 if (PyObject_SetAttrString(result, "name", value) == -1) |
| 3159 value = ast2obj_list(o->body, ast2obj_stmt); | 3167 goto failed; |
| 3160 if (!value) goto failed; | 3168 Py_DECREF(value); |
| 3161 if (PyObject_SetAttrString(result, "body", value) == -1) | 3169 value = ast2obj_list(o->v.ExceptHandler.body, ast2obj_stmt); |
| 3162 goto failed; | 3170 if (!value) goto failed; |
| 3163 Py_DECREF(value); | 3171 if (PyObject_SetAttrString(result, "body", value) == -1) |
| 3172 goto failed; | |
| 3173 Py_DECREF(value); | |
| 3174 break; | |
| 3175 } | |
| 3164 value = ast2obj_int(o->lineno); | 3176 value = ast2obj_int(o->lineno); |
| 3165 if (!value) goto failed; | 3177 if (!value) goto failed; |
| 3166 if (PyObject_SetAttrString(result, "lineno", value) == -1) | 3178 if (PyObject_SetAttrString(result, "lineno", value) < 0) |
| 3167 goto failed; | 3179 goto failed; |
| 3168 Py_DECREF(value); | 3180 Py_DECREF(value); |
| 3169 value = ast2obj_int(o->col_offset); | 3181 value = ast2obj_int(o->col_offset); |
| 3170 if (!value) goto failed; | 3182 if (!value) goto failed; |
| 3171 if (PyObject_SetAttrString(result, "col_offset", value) == -1) | 3183 if (PyObject_SetAttrString(result, "col_offset", value) < 0) |
| 3172 goto failed; | 3184 goto failed; |
| 3173 Py_DECREF(value); | 3185 Py_DECREF(value); |
| 3174 return result; | 3186 return result; |
| 3175 failed: | 3187 failed: |
| 3176 Py_XDECREF(value); | 3188 Py_XDECREF(value); |
| 5974 | 5986 |
| 5975 int | 5987 int |
| 5976 obj2ast_excepthandler(PyObject* obj, excepthandler_ty* out, PyArena* arena) | 5988 obj2ast_excepthandler(PyObject* obj, excepthandler_ty* out, PyArena* arena) |
| 5977 { | 5989 { |
| 5978 PyObject* tmp = NULL; | 5990 PyObject* tmp = NULL; |
| 5979 expr_ty type; | 5991 |
| 5980 identifier name; | |
| 5981 asdl_seq* body; | |
| 5982 int lineno; | 5992 int lineno; |
| 5983 int col_offset; | 5993 int col_offset; |
| 5984 | 5994 |
| 5985 if (PyObject_HasAttrString(obj, "type")) { | 5995 if (obj == Py_None) { |
| 5986 int res; | 5996 *out = NULL; |
| 5987 tmp = PyObject_GetAttrString(obj, "type"); | 5997 return 0; |
| 5988 if (tmp == NULL) goto failed; | |
| 5989 res = obj2ast_expr(tmp, &type, arena); | |
| 5990 if (res != 0) goto failed; | |
| 5991 Py_XDECREF(tmp); | |
| 5992 tmp = NULL; | |
| 5993 } else { | |
| 5994 type = NULL; | |
| 5995 } | |
| 5996 if (PyObject_HasAttrString(obj, "name")) { | |
| 5997 int res; | |
| 5998 tmp = PyObject_GetAttrString(obj, "name"); | |
| 5999 if (tmp == NULL) goto failed; | |
| 6000 res = obj2ast_identifier(tmp, &name, arena); | |
| 6001 if (res != 0) goto failed; | |
| 6002 Py_XDECREF(tmp); | |
| 6003 tmp = NULL; | |
| 6004 } else { | |
| 6005 name = NULL; | |
| 6006 } | |
| 6007 if (PyObject_HasAttrString(obj, "body")) { | |
| 6008 int res; | |
| 6009 Py_ssize_t len; | |
| 6010 Py_ssize_t i; | |
| 6011 tmp = PyObject_GetAttrString(obj, "body"); | |
| 6012 if (tmp == NULL) goto failed; | |
| 6013 if (!PyList_Check(tmp)) { | |
| 6014 PyErr_Format(PyExc_TypeError, "excepthandler field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name); | |
| 6015 goto failed; | |
| 6016 } | |
| 6017 len = PyList_GET_SIZE(tmp); | |
| 6018 body = asdl_seq_new(len, arena); | |
| 6019 if (body == NULL) goto failed; | |
| 6020 for (i = 0; i < len; i++) { | |
| 6021 stmt_ty value; | |
| 6022 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena); | |
| 6023 if (res != 0) goto failed; | |
| 6024 asdl_seq_SET(body, i, value); | |
| 6025 } | |
| 6026 Py_XDECREF(tmp); | |
| 6027 tmp = NULL; | |
| 6028 } else { | |
| 6029 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from excepthandler"); | |
| 6030 return 1; | |
| 6031 } | 5998 } |
| 6032 if (PyObject_HasAttrString(obj, "lineno")) { | 5999 if (PyObject_HasAttrString(obj, "lineno")) { |
| 6033 int res; | 6000 int res; |
| 6034 tmp = PyObject_GetAttrString(obj, "lineno"); | 6001 tmp = PyObject_GetAttrString(obj, "lineno"); |
| 6035 if (tmp == NULL) goto failed; | 6002 if (tmp == NULL) goto failed; |
| 6051 tmp = NULL; | 6018 tmp = NULL; |
| 6052 } else { | 6019 } else { |
| 6053 PyErr_SetString(PyExc_TypeError, "required field \"col_offset\" missing from excepthandler"); | 6020 PyErr_SetString(PyExc_TypeError, "required field \"col_offset\" missing from excepthandler"); |
| 6054 return 1; | 6021 return 1; |
| 6055 } | 6022 } |
| 6056 *out = excepthandler(type, name, body, lineno, col_offset, arena); | 6023 if (PyObject_IsInstance(obj, (PyObject*)ExceptHandler_type)) { |
| 6057 return 0; | 6024 expr_ty type; |
| 6025 identifier name; | |
| 6026 asdl_seq* body; | |
| 6027 | |
| 6028 if (PyObject_HasAttrString(obj, "type")) { | |
| 6029 int res; | |
| 6030 tmp = PyObject_GetAttrString(obj, "type"); | |
| 6031 if (tmp == NULL) goto failed; | |
| 6032 res = obj2ast_expr(tmp, &type, arena); | |
| 6033 if (res != 0) goto failed; | |
| 6034 Py_XDECREF(tmp); | |
| 6035 tmp = NULL; | |
| 6036 } else { | |
| 6037 type = NULL; | |
| 6038 } | |
| 6039 if (PyObject_HasAttrString(obj, "name")) { | |
| 6040 int res; | |
| 6041 tmp = PyObject_GetAttrString(obj, "name"); | |
| 6042 if (tmp == NULL) goto failed; | |
| 6043 res = obj2ast_identifier(tmp, &name, arena); | |
| 6044 if (res != 0) goto failed; | |
| 6045 Py_XDECREF(tmp); | |
| 6046 tmp = NULL; | |
| 6047 } else { | |
| 6048 name = NULL; | |
| 6049 } | |
| 6050 if (PyObject_HasAttrString(obj, "body")) { | |
| 6051 int res; | |
| 6052 Py_ssize_t len; | |
| 6053 Py_ssize_t i; | |
| 6054 tmp = PyObject_GetAttrString(obj, "body"); | |
| 6055 if (tmp == NULL) goto failed; | |
| 6056 if (!PyList_Check(tmp)) { | |
| 6057 PyErr_Format(PyExc_TypeError, "ExceptHandler field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name); | |
| 6058 goto failed; | |
| 6059 } | |
| 6060 len = PyList_GET_SIZE(tmp); | |
| 6061 body = asdl_seq_new(len, arena); | |
| 6062 if (body == NULL) goto failed; | |
| 6063 for (i = 0; i < len; i++) { | |
| 6064 stmt_ty value; | |
| 6065 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena); | |
| 6066 if (res != 0) goto failed; | |
| 6067 asdl_seq_SET(body, i, value); | |
| 6068 } | |
| 6069 Py_XDECREF(tmp); | |
| 6070 tmp = NULL; | |
| 6071 } else { | |
| 6072 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from ExceptHandler"); | |
| 6073 return 1; | |
| 6074 } | |
| 6075 *out = ExceptHandler(type, name, body, lineno, col_offset, | |
| 6076 arena); | |
| 6077 if (*out == NULL) goto failed; | |
| 6078 return 0; | |
| 6079 } | |
| 6080 | |
| 6081 tmp = PyObject_Repr(obj); | |
| 6082 if (tmp == NULL) goto failed; | |
| 6083 PyErr_Format(PyExc_TypeError, "expected some sort of excepthandler, but got %.400s", PyString_AS_STRING(tmp)); | |
| 6058 failed: | 6084 failed: |
| 6059 Py_XDECREF(tmp); | 6085 Py_XDECREF(tmp); |
| 6060 return 1; | 6086 return 1; |
| 6061 } | 6087 } |
| 6062 | 6088 |
| 6491 if (PyDict_SetItemString(d, "NotIn", (PyObject*)NotIn_type) < 0) return; | 6517 if (PyDict_SetItemString(d, "NotIn", (PyObject*)NotIn_type) < 0) return; |
| 6492 if (PyDict_SetItemString(d, "comprehension", | 6518 if (PyDict_SetItemString(d, "comprehension", |
| 6493 (PyObject*)comprehension_type) < 0) return; | 6519 (PyObject*)comprehension_type) < 0) return; |
| 6494 if (PyDict_SetItemString(d, "excepthandler", | 6520 if (PyDict_SetItemString(d, "excepthandler", |
| 6495 (PyObject*)excepthandler_type) < 0) return; | 6521 (PyObject*)excepthandler_type) < 0) return; |
| 6522 if (PyDict_SetItemString(d, "ExceptHandler", | |
| 6523 (PyObject*)ExceptHandler_type) < 0) return; | |
| 6496 if (PyDict_SetItemString(d, "arguments", (PyObject*)arguments_type) < | 6524 if (PyDict_SetItemString(d, "arguments", (PyObject*)arguments_type) < |
| 6497 0) return; | 6525 0) return; |
| 6498 if (PyDict_SetItemString(d, "arg", (PyObject*)arg_type) < 0) return; | 6526 if (PyDict_SetItemString(d, "arg", (PyObject*)arg_type) < 0) return; |
| 6499 if (PyDict_SetItemString(d, "keyword", (PyObject*)keyword_type) < 0) | 6527 if (PyDict_SetItemString(d, "keyword", (PyObject*)keyword_type) < 0) |
| 6500 return; | 6528 return; |
