comparison Python/Python-ast.c @ 58250:edfed0e32ced legacy-trunk

Issue #2333: Backport set and dict comprehensions syntax.
author Alexandre Vassalotti <alexandre@peadrop.com>
date Mon, 11 Jan 2010 22:36:12 +0000
parents 2cff3bce4096
children 8a5ac9ff5057
comparison
equal deleted inserted replaced
58249:33e402909cf2 58250:edfed0e32ced
195 static PyTypeObject *ListComp_type; 195 static PyTypeObject *ListComp_type;
196 static char *ListComp_fields[]={ 196 static char *ListComp_fields[]={
197 "elt", 197 "elt",
198 "generators", 198 "generators",
199 }; 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",
209 "generators",
210 };
200 static PyTypeObject *GeneratorExp_type; 211 static PyTypeObject *GeneratorExp_type;
201 static char *GeneratorExp_fields[]={ 212 static char *GeneratorExp_fields[]={
202 "elt", 213 "elt",
203 "generators", 214 "generators",
204 }; 215 };
724 if (!Dict_type) return 0; 735 if (!Dict_type) return 0;
725 Set_type = make_type("Set", expr_type, Set_fields, 1); 736 Set_type = make_type("Set", expr_type, Set_fields, 1);
726 if (!Set_type) return 0; 737 if (!Set_type) return 0;
727 ListComp_type = make_type("ListComp", expr_type, ListComp_fields, 2); 738 ListComp_type = make_type("ListComp", expr_type, ListComp_fields, 2);
728 if (!ListComp_type) return 0; 739 if (!ListComp_type) return 0;
740 SetComp_type = make_type("SetComp", expr_type, SetComp_fields, 2);
741 if (!SetComp_type) return 0;
742 DictComp_type = make_type("DictComp", expr_type, DictComp_fields, 3);
743 if (!DictComp_type) return 0;
729 GeneratorExp_type = make_type("GeneratorExp", expr_type, 744 GeneratorExp_type = make_type("GeneratorExp", expr_type,
730 GeneratorExp_fields, 2); 745 GeneratorExp_fields, 2);
731 if (!GeneratorExp_type) return 0; 746 if (!GeneratorExp_type) return 0;
732 Yield_type = make_type("Yield", expr_type, Yield_fields, 1); 747 Yield_type = make_type("Yield", expr_type, Yield_fields, 1);
733 if (!Yield_type) return 0; 748 if (!Yield_type) return 0;
1629 p->col_offset = col_offset; 1644 p->col_offset = col_offset;
1630 return p; 1645 return p;
1631 } 1646 }
1632 1647
1633 expr_ty 1648 expr_ty
1649 SetComp(expr_ty elt, asdl_seq * generators, int lineno, int col_offset, PyArena
1650 *arena)
1651 {
1652 expr_ty p;
1653 if (!elt) {
1654 PyErr_SetString(PyExc_ValueError,
1655 "field elt is required for SetComp");
1656 return NULL;
1657 }
1658 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1659 if (!p)
1660 return NULL;
1661 p->kind = SetComp_kind;
1662 p->v.SetComp.elt = elt;
1663 p->v.SetComp.generators = generators;
1664 p->lineno = lineno;
1665 p->col_offset = col_offset;
1666 return p;
1667 }
1668
1669 expr_ty
1670 DictComp(expr_ty key, expr_ty value, asdl_seq * generators, int lineno, int
1671 col_offset, PyArena *arena)
1672 {
1673 expr_ty p;
1674 if (!key) {
1675 PyErr_SetString(PyExc_ValueError,
1676 "field key is required for DictComp");
1677 return NULL;
1678 }
1679 if (!value) {
1680 PyErr_SetString(PyExc_ValueError,
1681 "field value is required for DictComp");
1682 return NULL;
1683 }
1684 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1685 if (!p)
1686 return NULL;
1687 p->kind = DictComp_kind;
1688 p->v.DictComp.key = key;
1689 p->v.DictComp.value = value;
1690 p->v.DictComp.generators = generators;
1691 p->lineno = lineno;
1692 p->col_offset = col_offset;
1693 return p;
1694 }
1695
1696 expr_ty
1634 GeneratorExp(expr_ty elt, asdl_seq * generators, int lineno, int col_offset, 1697 GeneratorExp(expr_ty elt, asdl_seq * generators, int lineno, int col_offset,
1635 PyArena *arena) 1698 PyArena *arena)
1636 { 1699 {
1637 expr_ty p; 1700 expr_ty p;
1638 if (!elt) { 1701 if (!elt) {
2602 if (!value) goto failed; 2665 if (!value) goto failed;
2603 if (PyObject_SetAttrString(result, "elt", value) == -1) 2666 if (PyObject_SetAttrString(result, "elt", value) == -1)
2604 goto failed; 2667 goto failed;
2605 Py_DECREF(value); 2668 Py_DECREF(value);
2606 value = ast2obj_list(o->v.ListComp.generators, 2669 value = ast2obj_list(o->v.ListComp.generators,
2670 ast2obj_comprehension);
2671 if (!value) goto failed;
2672 if (PyObject_SetAttrString(result, "generators", value) == -1)
2673 goto failed;
2674 Py_DECREF(value);
2675 break;
2676 case SetComp_kind:
2677 result = PyType_GenericNew(SetComp_type, NULL, NULL);
2678 if (!result) goto failed;
2679 value = ast2obj_expr(o->v.SetComp.elt);
2680 if (!value) goto failed;
2681 if (PyObject_SetAttrString(result, "elt", value) == -1)
2682 goto failed;
2683 Py_DECREF(value);
2684 value = ast2obj_list(o->v.SetComp.generators,
2685 ast2obj_comprehension);
2686 if (!value) goto failed;
2687 if (PyObject_SetAttrString(result, "generators", value) == -1)
2688 goto failed;
2689 Py_DECREF(value);
2690 break;
2691 case DictComp_kind:
2692 result = PyType_GenericNew(DictComp_type, NULL, NULL);
2693 if (!result) goto failed;
2694 value = ast2obj_expr(o->v.DictComp.key);
2695 if (!value) goto failed;
2696 if (PyObject_SetAttrString(result, "key", value) == -1)
2697 goto failed;
2698 Py_DECREF(value);
2699 value = ast2obj_expr(o->v.DictComp.value);
2700 if (!value) goto failed;
2701 if (PyObject_SetAttrString(result, "value", value) == -1)
2702 goto failed;
2703 Py_DECREF(value);
2704 value = ast2obj_list(o->v.DictComp.generators,
2607 ast2obj_comprehension); 2705 ast2obj_comprehension);
2608 if (!value) goto failed; 2706 if (!value) goto failed;
2609 if (PyObject_SetAttrString(result, "generators", value) == -1) 2707 if (PyObject_SetAttrString(result, "generators", value) == -1)
2610 goto failed; 2708 goto failed;
2611 Py_DECREF(value); 2709 Py_DECREF(value);
4969 } else { 5067 } else {
4970 PyErr_SetString(PyExc_TypeError, "required field \"generators\" missing from ListComp"); 5068 PyErr_SetString(PyExc_TypeError, "required field \"generators\" missing from ListComp");
4971 return 1; 5069 return 1;
4972 } 5070 }
4973 *out = ListComp(elt, generators, lineno, col_offset, arena); 5071 *out = ListComp(elt, generators, lineno, col_offset, arena);
5072 if (*out == NULL) goto failed;
5073 return 0;
5074 }
5075 isinstance = PyObject_IsInstance(obj, (PyObject*)SetComp_type);
5076 if (isinstance == -1) {
5077 return 1;
5078 }
5079 if (isinstance) {
5080 expr_ty elt;
5081 asdl_seq* generators;
5082
5083 if (PyObject_HasAttrString(obj, "elt")) {
5084 int res;
5085 tmp = PyObject_GetAttrString(obj, "elt");
5086 if (tmp == NULL) goto failed;
5087 res = obj2ast_expr(tmp, &elt, arena);
5088 if (res != 0) goto failed;
5089 Py_XDECREF(tmp);
5090 tmp = NULL;
5091 } else {
5092 PyErr_SetString(PyExc_TypeError, "required field \"elt\" missing from SetComp");
5093 return 1;
5094 }
5095 if (PyObject_HasAttrString(obj, "generators")) {
5096 int res;
5097 Py_ssize_t len;
5098 Py_ssize_t i;
5099 tmp = PyObject_GetAttrString(obj, "generators");
5100 if (tmp == NULL) goto failed;
5101 if (!PyList_Check(tmp)) {
5102 PyErr_Format(PyExc_TypeError, "SetComp field \"generators\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5103 goto failed;
5104 }
5105 len = PyList_GET_SIZE(tmp);
5106 generators = asdl_seq_new(len, arena);
5107 if (generators == NULL) goto failed;
5108 for (i = 0; i < len; i++) {
5109 comprehension_ty value;
5110 res = obj2ast_comprehension(PyList_GET_ITEM(tmp, i), &value, arena);
5111 if (res != 0) goto failed;
5112 asdl_seq_SET(generators, i, value);
5113 }
5114 Py_XDECREF(tmp);
5115 tmp = NULL;
5116 } else {
5117 PyErr_SetString(PyExc_TypeError, "required field \"generators\" missing from SetComp");
5118 return 1;
5119 }
5120 *out = SetComp(elt, generators, lineno, col_offset, arena);
5121 if (*out == NULL) goto failed;
5122 return 0;
5123 }
5124 isinstance = PyObject_IsInstance(obj, (PyObject*)DictComp_type);
5125 if (isinstance == -1) {
5126 return 1;
5127 }
5128 if (isinstance) {
5129 expr_ty key;
5130 expr_ty value;
5131 asdl_seq* generators;
5132
5133 if (PyObject_HasAttrString(obj, "key")) {
5134 int res;
5135 tmp = PyObject_GetAttrString(obj, "key");
5136 if (tmp == NULL) goto failed;
5137 res = obj2ast_expr(tmp, &key, arena);
5138 if (res != 0) goto failed;
5139 Py_XDECREF(tmp);
5140 tmp = NULL;
5141 } else {
5142 PyErr_SetString(PyExc_TypeError, "required field \"key\" missing from DictComp");
5143 return 1;
5144 }
5145 if (PyObject_HasAttrString(obj, "value")) {
5146 int res;
5147 tmp = PyObject_GetAttrString(obj, "value");
5148 if (tmp == NULL) goto failed;
5149 res = obj2ast_expr(tmp, &value, arena);
5150 if (res != 0) goto failed;
5151 Py_XDECREF(tmp);
5152 tmp = NULL;
5153 } else {
5154 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from DictComp");
5155 return 1;
5156 }
5157 if (PyObject_HasAttrString(obj, "generators")) {
5158 int res;
5159 Py_ssize_t len;
5160 Py_ssize_t i;
5161 tmp = PyObject_GetAttrString(obj, "generators");
5162 if (tmp == NULL) goto failed;
5163 if (!PyList_Check(tmp)) {
5164 PyErr_Format(PyExc_TypeError, "DictComp field \"generators\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5165 goto failed;
5166 }
5167 len = PyList_GET_SIZE(tmp);
5168 generators = asdl_seq_new(len, arena);
5169 if (generators == NULL) goto failed;
5170 for (i = 0; i < len; i++) {
5171 comprehension_ty value;
5172 res = obj2ast_comprehension(PyList_GET_ITEM(tmp, i), &value, arena);
5173 if (res != 0) goto failed;
5174 asdl_seq_SET(generators, i, value);
5175 }
5176 Py_XDECREF(tmp);
5177 tmp = NULL;
5178 } else {
5179 PyErr_SetString(PyExc_TypeError, "required field \"generators\" missing from DictComp");
5180 return 1;
5181 }
5182 *out = DictComp(key, value, generators, lineno, col_offset,
5183 arena);
4974 if (*out == NULL) goto failed; 5184 if (*out == NULL) goto failed;
4975 return 0; 5185 return 0;
4976 } 5186 }
4977 isinstance = PyObject_IsInstance(obj, (PyObject*)GeneratorExp_type); 5187 isinstance = PyObject_IsInstance(obj, (PyObject*)GeneratorExp_type);
4978 if (isinstance == -1) { 5188 if (isinstance == -1) {
6416 return; 6626 return;
6417 if (PyDict_SetItemString(d, "IfExp", (PyObject*)IfExp_type) < 0) return; 6627 if (PyDict_SetItemString(d, "IfExp", (PyObject*)IfExp_type) < 0) return;
6418 if (PyDict_SetItemString(d, "Dict", (PyObject*)Dict_type) < 0) return; 6628 if (PyDict_SetItemString(d, "Dict", (PyObject*)Dict_type) < 0) return;
6419 if (PyDict_SetItemString(d, "Set", (PyObject*)Set_type) < 0) return; 6629 if (PyDict_SetItemString(d, "Set", (PyObject*)Set_type) < 0) return;
6420 if (PyDict_SetItemString(d, "ListComp", (PyObject*)ListComp_type) < 0) 6630 if (PyDict_SetItemString(d, "ListComp", (PyObject*)ListComp_type) < 0)
6631 return;
6632 if (PyDict_SetItemString(d, "SetComp", (PyObject*)SetComp_type) < 0)
6633 return;
6634 if (PyDict_SetItemString(d, "DictComp", (PyObject*)DictComp_type) < 0)
6421 return; 6635 return;
6422 if (PyDict_SetItemString(d, "GeneratorExp", 6636 if (PyDict_SetItemString(d, "GeneratorExp",
6423 (PyObject*)GeneratorExp_type) < 0) return; 6637 (PyObject*)GeneratorExp_type) < 0) return;
6424 if (PyDict_SetItemString(d, "Yield", (PyObject*)Yield_type) < 0) return; 6638 if (PyDict_SetItemString(d, "Yield", (PyObject*)Yield_type) < 0) return;
6425 if (PyDict_SetItemString(d, "Compare", (PyObject*)Compare_type) < 0) 6639 if (PyDict_SetItemString(d, "Compare", (PyObject*)Compare_type) < 0)