comparison Python/Python-ast.c @ 58229:c2fdf25329ff legacy-trunk

Issue #2335: Backport set literals syntax from Python 3.x.
author Alexandre Vassalotti <alexandre@peadrop.com>
date Sat, 09 Jan 2010 23:35:54 +0000
parents 20b7deb53d4e
children 2cff3bce4096
comparison
equal deleted inserted replaced
58220:4675f11fb125 58229:c2fdf25329ff
185 }; 185 };
186 static PyTypeObject *Dict_type; 186 static PyTypeObject *Dict_type;
187 static char *Dict_fields[]={ 187 static char *Dict_fields[]={
188 "keys", 188 "keys",
189 "values", 189 "values",
190 };
191 static PyTypeObject *Set_type;
192 static char *Set_fields[]={
193 "elts",
190 }; 194 };
191 static PyTypeObject *ListComp_type; 195 static PyTypeObject *ListComp_type;
192 static char *ListComp_fields[]={ 196 static char *ListComp_fields[]={
193 "elt", 197 "elt",
194 "generators", 198 "generators",
716 if (!Lambda_type) return 0; 720 if (!Lambda_type) return 0;
717 IfExp_type = make_type("IfExp", expr_type, IfExp_fields, 3); 721 IfExp_type = make_type("IfExp", expr_type, IfExp_fields, 3);
718 if (!IfExp_type) return 0; 722 if (!IfExp_type) return 0;
719 Dict_type = make_type("Dict", expr_type, Dict_fields, 2); 723 Dict_type = make_type("Dict", expr_type, Dict_fields, 2);
720 if (!Dict_type) return 0; 724 if (!Dict_type) return 0;
725 Set_type = make_type("Set", expr_type, Set_fields, 1);
726 if (!Set_type) return 0;
721 ListComp_type = make_type("ListComp", expr_type, ListComp_fields, 2); 727 ListComp_type = make_type("ListComp", expr_type, ListComp_fields, 2);
722 if (!ListComp_type) return 0; 728 if (!ListComp_type) return 0;
723 GeneratorExp_type = make_type("GeneratorExp", expr_type, 729 GeneratorExp_type = make_type("GeneratorExp", expr_type,
724 GeneratorExp_fields, 2); 730 GeneratorExp_fields, 2);
725 if (!GeneratorExp_type) return 0; 731 if (!GeneratorExp_type) return 0;
1588 p->col_offset = col_offset; 1594 p->col_offset = col_offset;
1589 return p; 1595 return p;
1590 } 1596 }
1591 1597
1592 expr_ty 1598 expr_ty
1599 Set(asdl_seq * elts, int lineno, int col_offset, PyArena *arena)
1600 {
1601 expr_ty p;
1602 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1603 if (!p)
1604 return NULL;
1605 p->kind = Set_kind;
1606 p->v.Set.elts = elts;
1607 p->lineno = lineno;
1608 p->col_offset = col_offset;
1609 return p;
1610 }
1611
1612 expr_ty
1593 ListComp(expr_ty elt, asdl_seq * generators, int lineno, int col_offset, 1613 ListComp(expr_ty elt, asdl_seq * generators, int lineno, int col_offset,
1594 PyArena *arena) 1614 PyArena *arena)
1595 { 1615 {
1596 expr_ty p; 1616 expr_ty p;
1597 if (!elt) { 1617 if (!elt) {
2561 goto failed; 2581 goto failed;
2562 Py_DECREF(value); 2582 Py_DECREF(value);
2563 value = ast2obj_list(o->v.Dict.values, ast2obj_expr); 2583 value = ast2obj_list(o->v.Dict.values, ast2obj_expr);
2564 if (!value) goto failed; 2584 if (!value) goto failed;
2565 if (PyObject_SetAttrString(result, "values", value) == -1) 2585 if (PyObject_SetAttrString(result, "values", value) == -1)
2586 goto failed;
2587 Py_DECREF(value);
2588 break;
2589 case Set_kind:
2590 result = PyType_GenericNew(Set_type, NULL, NULL);
2591 if (!result) goto failed;
2592 value = ast2obj_list(o->v.Set.elts, ast2obj_expr);
2593 if (!value) goto failed;
2594 if (PyObject_SetAttrString(result, "elts", value) == -1)
2566 goto failed; 2595 goto failed;
2567 Py_DECREF(value); 2596 Py_DECREF(value);
2568 break; 2597 break;
2569 case ListComp_kind: 2598 case ListComp_kind:
2570 result = PyType_GenericNew(ListComp_type, NULL, NULL); 2599 result = PyType_GenericNew(ListComp_type, NULL, NULL);
4855 } else { 4884 } else {
4856 PyErr_SetString(PyExc_TypeError, "required field \"values\" missing from Dict"); 4885 PyErr_SetString(PyExc_TypeError, "required field \"values\" missing from Dict");
4857 return 1; 4886 return 1;
4858 } 4887 }
4859 *out = Dict(keys, values, lineno, col_offset, arena); 4888 *out = Dict(keys, values, lineno, col_offset, arena);
4889 if (*out == NULL) goto failed;
4890 return 0;
4891 }
4892 isinstance = PyObject_IsInstance(obj, (PyObject*)Set_type);
4893 if (isinstance == -1) {
4894 return 1;
4895 }
4896 if (isinstance) {
4897 asdl_seq* elts;
4898
4899 if (PyObject_HasAttrString(obj, "elts")) {
4900 int res;
4901 Py_ssize_t len;
4902 Py_ssize_t i;
4903 tmp = PyObject_GetAttrString(obj, "elts");
4904 if (tmp == NULL) goto failed;
4905 if (!PyList_Check(tmp)) {
4906 PyErr_Format(PyExc_TypeError, "Set field \"elts\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4907 goto failed;
4908 }
4909 len = PyList_GET_SIZE(tmp);
4910 elts = asdl_seq_new(len, arena);
4911 if (elts == NULL) goto failed;
4912 for (i = 0; i < len; i++) {
4913 expr_ty value;
4914 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
4915 if (res != 0) goto failed;
4916 asdl_seq_SET(elts, i, value);
4917 }
4918 Py_XDECREF(tmp);
4919 tmp = NULL;
4920 } else {
4921 PyErr_SetString(PyExc_TypeError, "required field \"elts\" missing from Set");
4922 return 1;
4923 }
4924 *out = Set(elts, lineno, col_offset, arena);
4860 if (*out == NULL) goto failed; 4925 if (*out == NULL) goto failed;
4861 return 0; 4926 return 0;
4862 } 4927 }
4863 isinstance = PyObject_IsInstance(obj, (PyObject*)ListComp_type); 4928 isinstance = PyObject_IsInstance(obj, (PyObject*)ListComp_type);
4864 if (isinstance == -1) { 4929 if (isinstance == -1) {
6349 return; 6414 return;
6350 if (PyDict_SetItemString(d, "Lambda", (PyObject*)Lambda_type) < 0) 6415 if (PyDict_SetItemString(d, "Lambda", (PyObject*)Lambda_type) < 0)
6351 return; 6416 return;
6352 if (PyDict_SetItemString(d, "IfExp", (PyObject*)IfExp_type) < 0) return; 6417 if (PyDict_SetItemString(d, "IfExp", (PyObject*)IfExp_type) < 0) return;
6353 if (PyDict_SetItemString(d, "Dict", (PyObject*)Dict_type) < 0) return; 6418 if (PyDict_SetItemString(d, "Dict", (PyObject*)Dict_type) < 0) return;
6419 if (PyDict_SetItemString(d, "Set", (PyObject*)Set_type) < 0) return;
6354 if (PyDict_SetItemString(d, "ListComp", (PyObject*)ListComp_type) < 0) 6420 if (PyDict_SetItemString(d, "ListComp", (PyObject*)ListComp_type) < 0)
6355 return; 6421 return;
6356 if (PyDict_SetItemString(d, "GeneratorExp", 6422 if (PyDict_SetItemString(d, "GeneratorExp",
6357 (PyObject*)GeneratorExp_type) < 0) return; 6423 (PyObject*)GeneratorExp_type) < 0) return;
6358 if (PyDict_SetItemString(d, "Yield", (PyObject*)Yield_type) < 0) return; 6424 if (PyDict_SetItemString(d, "Yield", (PyObject*)Yield_type) < 0) return;