Changeset 388 for python/vendor/current/Python/Python-ast.c
- Timestamp:
- Mar 19, 2014, 11:11:30 AM (11 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
python/vendor/current/Python/Python-ast.c
r2 r388 3 3 4 4 /* 5 __version__ 62047.5 __version__ 82160. 6 6 7 7 This module must be committed separately after each AST grammar change; … … 189 189 "values", 190 190 }; 191 static PyTypeObject *Set_type; 192 static char *Set_fields[]={ 193 "elts", 194 }; 191 195 static PyTypeObject *ListComp_type; 192 196 static char *ListComp_fields[]={ 193 197 "elt", 198 "generators", 199 }; 200 static PyTypeObject *SetComp_type; 201 static char *SetComp_fields[]={ 202 "elt", 203 "generators", 204 }; 205 static PyTypeObject *DictComp_type; 206 static char *DictComp_fields[]={ 207 "key", 208 "value", 194 209 "generators", 195 210 }; … … 513 528 int i, result; 514 529 PyObject *s, *l = PyTuple_New(num_fields); 515 if (!l) return 0; 516 for(i = 0; i < num_fields; i++) { 530 if (!l) 531 return 0; 532 for (i = 0; i < num_fields; i++) { 517 533 s = PyString_FromString(attrs[i]); 518 534 if (!s) { … … 579 595 } 580 596 581 #define obj2ast_identifier obj2ast_object 582 #define obj2ast_string obj2ast_object 597 static int obj2ast_identifier(PyObject* obj, PyObject** out, PyArena* arena) 598 { 599 if (!PyString_CheckExact(obj) && obj != Py_None) { 600 PyErr_Format(PyExc_TypeError, 601 "AST identifier must be of type str"); 602 return 1; 603 } 604 return obj2ast_object(obj, out, arena); 605 } 606 607 static int obj2ast_string(PyObject* obj, PyObject** out, PyArena* arena) 608 { 609 if (!PyString_CheckExact(obj) && !PyUnicode_CheckExact(obj)) { 610 PyErr_SetString(PyExc_TypeError, 611 "AST string must be of type str or unicode"); 612 return 1; 613 } 614 return obj2ast_object(obj, out, arena); 615 } 583 616 584 617 static int obj2ast_int(PyObject* obj, int* out, PyArena* arena) … … 719 752 Dict_type = make_type("Dict", expr_type, Dict_fields, 2); 720 753 if (!Dict_type) return 0; 754 Set_type = make_type("Set", expr_type, Set_fields, 1); 755 if (!Set_type) return 0; 721 756 ListComp_type = make_type("ListComp", expr_type, ListComp_fields, 2); 722 757 if (!ListComp_type) return 0; 758 SetComp_type = make_type("SetComp", expr_type, SetComp_fields, 2); 759 if (!SetComp_type) return 0; 760 DictComp_type = make_type("DictComp", expr_type, DictComp_fields, 3); 761 if (!DictComp_type) return 0; 723 762 GeneratorExp_type = make_type("GeneratorExp", expr_type, 724 763 GeneratorExp_fields, 2); … … 1331 1370 { 1332 1371 stmt_ty p; 1333 if (!module) {1334 PyErr_SetString(PyExc_ValueError,1335 "field module is required for ImportFrom");1336 return NULL;1337 }1338 1372 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p)); 1339 1373 if (!p) … … 1596 1630 1597 1631 expr_ty 1632 Set(asdl_seq * elts, int lineno, int col_offset, PyArena *arena) 1633 { 1634 expr_ty p; 1635 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p)); 1636 if (!p) 1637 return NULL; 1638 p->kind = Set_kind; 1639 p->v.Set.elts = elts; 1640 p->lineno = lineno; 1641 p->col_offset = col_offset; 1642 return p; 1643 } 1644 1645 expr_ty 1598 1646 ListComp(expr_ty elt, asdl_seq * generators, int lineno, int col_offset, 1599 1647 PyArena *arena) … … 1611 1659 p->v.ListComp.elt = elt; 1612 1660 p->v.ListComp.generators = generators; 1661 p->lineno = lineno; 1662 p->col_offset = col_offset; 1663 return p; 1664 } 1665 1666 expr_ty 1667 SetComp(expr_ty elt, asdl_seq * generators, int lineno, int col_offset, PyArena 1668 *arena) 1669 { 1670 expr_ty p; 1671 if (!elt) { 1672 PyErr_SetString(PyExc_ValueError, 1673 "field elt is required for SetComp"); 1674 return NULL; 1675 } 1676 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p)); 1677 if (!p) 1678 return NULL; 1679 p->kind = SetComp_kind; 1680 p->v.SetComp.elt = elt; 1681 p->v.SetComp.generators = generators; 1682 p->lineno = lineno; 1683 p->col_offset = col_offset; 1684 return p; 1685 } 1686 1687 expr_ty 1688 DictComp(expr_ty key, expr_ty value, asdl_seq * generators, int lineno, int 1689 col_offset, PyArena *arena) 1690 { 1691 expr_ty p; 1692 if (!key) { 1693 PyErr_SetString(PyExc_ValueError, 1694 "field key is required for DictComp"); 1695 return NULL; 1696 } 1697 if (!value) { 1698 PyErr_SetString(PyExc_ValueError, 1699 "field value is required for DictComp"); 1700 return NULL; 1701 } 1702 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p)); 1703 if (!p) 1704 return NULL; 1705 p->kind = DictComp_kind; 1706 p->v.DictComp.key = key; 1707 p->v.DictComp.value = value; 1708 p->v.DictComp.generators = generators; 1613 1709 p->lineno = lineno; 1614 1710 p->col_offset = col_offset; … … 2572 2668 Py_DECREF(value); 2573 2669 break; 2670 case Set_kind: 2671 result = PyType_GenericNew(Set_type, NULL, NULL); 2672 if (!result) goto failed; 2673 value = ast2obj_list(o->v.Set.elts, ast2obj_expr); 2674 if (!value) goto failed; 2675 if (PyObject_SetAttrString(result, "elts", value) == -1) 2676 goto failed; 2677 Py_DECREF(value); 2678 break; 2574 2679 case ListComp_kind: 2575 2680 result = PyType_GenericNew(ListComp_type, NULL, NULL); … … 2581 2686 Py_DECREF(value); 2582 2687 value = ast2obj_list(o->v.ListComp.generators, 2688 ast2obj_comprehension); 2689 if (!value) goto failed; 2690 if (PyObject_SetAttrString(result, "generators", value) == -1) 2691 goto failed; 2692 Py_DECREF(value); 2693 break; 2694 case SetComp_kind: 2695 result = PyType_GenericNew(SetComp_type, NULL, NULL); 2696 if (!result) goto failed; 2697 value = ast2obj_expr(o->v.SetComp.elt); 2698 if (!value) goto failed; 2699 if (PyObject_SetAttrString(result, "elt", value) == -1) 2700 goto failed; 2701 Py_DECREF(value); 2702 value = ast2obj_list(o->v.SetComp.generators, 2703 ast2obj_comprehension); 2704 if (!value) goto failed; 2705 if (PyObject_SetAttrString(result, "generators", value) == -1) 2706 goto failed; 2707 Py_DECREF(value); 2708 break; 2709 case DictComp_kind: 2710 result = PyType_GenericNew(DictComp_type, NULL, NULL); 2711 if (!result) goto failed; 2712 value = ast2obj_expr(o->v.DictComp.key); 2713 if (!value) goto failed; 2714 if (PyObject_SetAttrString(result, "key", value) == -1) 2715 goto failed; 2716 Py_DECREF(value); 2717 value = ast2obj_expr(o->v.DictComp.value); 2718 if (!value) goto failed; 2719 if (PyObject_SetAttrString(result, "value", value) == -1) 2720 goto failed; 2721 Py_DECREF(value); 2722 value = ast2obj_list(o->v.DictComp.generators, 2583 2723 ast2obj_comprehension); 2584 2724 if (!value) goto failed; … … 4360 4500 tmp = NULL; 4361 4501 } else { 4362 PyErr_SetString(PyExc_TypeError, "required field \"module\" missing from ImportFrom"); 4363 return 1; 4502 module = NULL; 4364 4503 } 4365 4504 if (PyObject_HasAttrString(obj, "names")) { … … 4867 5006 return 0; 4868 5007 } 5008 isinstance = PyObject_IsInstance(obj, (PyObject*)Set_type); 5009 if (isinstance == -1) { 5010 return 1; 5011 } 5012 if (isinstance) { 5013 asdl_seq* elts; 5014 5015 if (PyObject_HasAttrString(obj, "elts")) { 5016 int res; 5017 Py_ssize_t len; 5018 Py_ssize_t i; 5019 tmp = PyObject_GetAttrString(obj, "elts"); 5020 if (tmp == NULL) goto failed; 5021 if (!PyList_Check(tmp)) { 5022 PyErr_Format(PyExc_TypeError, "Set field \"elts\" must be a list, not a %.200s", tmp->ob_type->tp_name); 5023 goto failed; 5024 } 5025 len = PyList_GET_SIZE(tmp); 5026 elts = asdl_seq_new(len, arena); 5027 if (elts == NULL) goto failed; 5028 for (i = 0; i < len; i++) { 5029 expr_ty value; 5030 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena); 5031 if (res != 0) goto failed; 5032 asdl_seq_SET(elts, i, value); 5033 } 5034 Py_XDECREF(tmp); 5035 tmp = NULL; 5036 } else { 5037 PyErr_SetString(PyExc_TypeError, "required field \"elts\" missing from Set"); 5038 return 1; 5039 } 5040 *out = Set(elts, lineno, col_offset, arena); 5041 if (*out == NULL) goto failed; 5042 return 0; 5043 } 4869 5044 isinstance = PyObject_IsInstance(obj, (PyObject*)ListComp_type); 4870 5045 if (isinstance == -1) { … … 4913 5088 } 4914 5089 *out = ListComp(elt, generators, lineno, col_offset, arena); 5090 if (*out == NULL) goto failed; 5091 return 0; 5092 } 5093 isinstance = PyObject_IsInstance(obj, (PyObject*)SetComp_type); 5094 if (isinstance == -1) { 5095 return 1; 5096 } 5097 if (isinstance) { 5098 expr_ty elt; 5099 asdl_seq* generators; 5100 5101 if (PyObject_HasAttrString(obj, "elt")) { 5102 int res; 5103 tmp = PyObject_GetAttrString(obj, "elt"); 5104 if (tmp == NULL) goto failed; 5105 res = obj2ast_expr(tmp, &elt, arena); 5106 if (res != 0) goto failed; 5107 Py_XDECREF(tmp); 5108 tmp = NULL; 5109 } else { 5110 PyErr_SetString(PyExc_TypeError, "required field \"elt\" missing from SetComp"); 5111 return 1; 5112 } 5113 if (PyObject_HasAttrString(obj, "generators")) { 5114 int res; 5115 Py_ssize_t len; 5116 Py_ssize_t i; 5117 tmp = PyObject_GetAttrString(obj, "generators"); 5118 if (tmp == NULL) goto failed; 5119 if (!PyList_Check(tmp)) { 5120 PyErr_Format(PyExc_TypeError, "SetComp field \"generators\" must be a list, not a %.200s", tmp->ob_type->tp_name); 5121 goto failed; 5122 } 5123 len = PyList_GET_SIZE(tmp); 5124 generators = asdl_seq_new(len, arena); 5125 if (generators == NULL) goto failed; 5126 for (i = 0; i < len; i++) { 5127 comprehension_ty value; 5128 res = obj2ast_comprehension(PyList_GET_ITEM(tmp, i), &value, arena); 5129 if (res != 0) goto failed; 5130 asdl_seq_SET(generators, i, value); 5131 } 5132 Py_XDECREF(tmp); 5133 tmp = NULL; 5134 } else { 5135 PyErr_SetString(PyExc_TypeError, "required field \"generators\" missing from SetComp"); 5136 return 1; 5137 } 5138 *out = SetComp(elt, generators, lineno, col_offset, arena); 5139 if (*out == NULL) goto failed; 5140 return 0; 5141 } 5142 isinstance = PyObject_IsInstance(obj, (PyObject*)DictComp_type); 5143 if (isinstance == -1) { 5144 return 1; 5145 } 5146 if (isinstance) { 5147 expr_ty key; 5148 expr_ty value; 5149 asdl_seq* generators; 5150 5151 if (PyObject_HasAttrString(obj, "key")) { 5152 int res; 5153 tmp = PyObject_GetAttrString(obj, "key"); 5154 if (tmp == NULL) goto failed; 5155 res = obj2ast_expr(tmp, &key, arena); 5156 if (res != 0) goto failed; 5157 Py_XDECREF(tmp); 5158 tmp = NULL; 5159 } else { 5160 PyErr_SetString(PyExc_TypeError, "required field \"key\" missing from DictComp"); 5161 return 1; 5162 } 5163 if (PyObject_HasAttrString(obj, "value")) { 5164 int res; 5165 tmp = PyObject_GetAttrString(obj, "value"); 5166 if (tmp == NULL) goto failed; 5167 res = obj2ast_expr(tmp, &value, arena); 5168 if (res != 0) goto failed; 5169 Py_XDECREF(tmp); 5170 tmp = NULL; 5171 } else { 5172 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from DictComp"); 5173 return 1; 5174 } 5175 if (PyObject_HasAttrString(obj, "generators")) { 5176 int res; 5177 Py_ssize_t len; 5178 Py_ssize_t i; 5179 tmp = PyObject_GetAttrString(obj, "generators"); 5180 if (tmp == NULL) goto failed; 5181 if (!PyList_Check(tmp)) { 5182 PyErr_Format(PyExc_TypeError, "DictComp field \"generators\" must be a list, not a %.200s", tmp->ob_type->tp_name); 5183 goto failed; 5184 } 5185 len = PyList_GET_SIZE(tmp); 5186 generators = asdl_seq_new(len, arena); 5187 if (generators == NULL) goto failed; 5188 for (i = 0; i < len; i++) { 5189 comprehension_ty value; 5190 res = obj2ast_comprehension(PyList_GET_ITEM(tmp, i), &value, arena); 5191 if (res != 0) goto failed; 5192 asdl_seq_SET(generators, i, value); 5193 } 5194 Py_XDECREF(tmp); 5195 tmp = NULL; 5196 } else { 5197 PyErr_SetString(PyExc_TypeError, "required field \"generators\" missing from DictComp"); 5198 return 1; 5199 } 5200 *out = DictComp(key, value, generators, lineno, col_offset, 5201 arena); 4915 5202 if (*out == NULL) goto failed; 4916 5203 return 0; … … 6301 6588 if (PyModule_AddIntConstant(m, "PyCF_ONLY_AST", PyCF_ONLY_AST) < 0) 6302 6589 return; 6303 if (PyModule_AddStringConstant(m, "__version__", " 62047") < 0)6590 if (PyModule_AddStringConstant(m, "__version__", "82160") < 0) 6304 6591 return; 6305 6592 if (PyDict_SetItemString(d, "mod", (PyObject*)mod_type) < 0) return; … … 6358 6645 if (PyDict_SetItemString(d, "IfExp", (PyObject*)IfExp_type) < 0) return; 6359 6646 if (PyDict_SetItemString(d, "Dict", (PyObject*)Dict_type) < 0) return; 6647 if (PyDict_SetItemString(d, "Set", (PyObject*)Set_type) < 0) return; 6360 6648 if (PyDict_SetItemString(d, "ListComp", (PyObject*)ListComp_type) < 0) 6649 return; 6650 if (PyDict_SetItemString(d, "SetComp", (PyObject*)SetComp_type) < 0) 6651 return; 6652 if (PyDict_SetItemString(d, "DictComp", (PyObject*)DictComp_type) < 0) 6361 6653 return; 6362 6654 if (PyDict_SetItemString(d, "GeneratorExp", … … 6458 6750 { 6459 6751 mod_ty res; 6460 PyObject *req_type[] = {(PyObject*)Module_type, (PyObject*)Expression_type, 6461 (PyObject*)Interactive_type}; 6462 char *req_name[] = {"Module", "Expression", "Interactive"}; 6752 PyObject *req_type[3]; 6753 char *req_name[3]; 6463 6754 int isinstance; 6755 6756 req_type[0] = (PyObject*)Module_type; 6757 req_type[1] = (PyObject*)Expression_type; 6758 req_type[2] = (PyObject*)Interactive_type; 6759 6760 req_name[0] = "Module"; 6761 req_name[1] = "Expression"; 6762 req_name[2] = "Interactive"; 6763 6464 6764 assert(0 <= mode && mode <= 2); 6465 6765
Note:
See TracChangeset
for help on using the changeset viewer.