comparison Python/Python-ast.c @ 74356:d64ac9ab4cd0

Implement PEP 380 - 'yield from' (closes #11682)
author Nick Coghlan <ncoghlan@gmail.com>
date Fri, 13 Jan 2012 21:43:40 +1000
parents 53cc2668d54f
children 87331661042b
comparison
equal deleted inserted replaced
74355:f46c1007b626 74356:d64ac9ab4cd0
229 static char *GeneratorExp_fields[]={ 229 static char *GeneratorExp_fields[]={
230 "elt", 230 "elt",
231 "generators", 231 "generators",
232 }; 232 };
233 static PyTypeObject *Yield_type; 233 static PyTypeObject *Yield_type;
234 _Py_IDENTIFIER(is_from);
234 static char *Yield_fields[]={ 235 static char *Yield_fields[]={
236 "is_from",
235 "value", 237 "value",
236 }; 238 };
237 static PyTypeObject *Compare_type; 239 static PyTypeObject *Compare_type;
238 _Py_IDENTIFIER(ops); 240 _Py_IDENTIFIER(ops);
239 _Py_IDENTIFIER(comparators); 241 _Py_IDENTIFIER(comparators);
808 DictComp_type = make_type("DictComp", expr_type, DictComp_fields, 3); 810 DictComp_type = make_type("DictComp", expr_type, DictComp_fields, 3);
809 if (!DictComp_type) return 0; 811 if (!DictComp_type) return 0;
810 GeneratorExp_type = make_type("GeneratorExp", expr_type, 812 GeneratorExp_type = make_type("GeneratorExp", expr_type,
811 GeneratorExp_fields, 2); 813 GeneratorExp_fields, 2);
812 if (!GeneratorExp_type) return 0; 814 if (!GeneratorExp_type) return 0;
813 Yield_type = make_type("Yield", expr_type, Yield_fields, 1); 815 Yield_type = make_type("Yield", expr_type, Yield_fields, 2);
814 if (!Yield_type) return 0; 816 if (!Yield_type) return 0;
815 Compare_type = make_type("Compare", expr_type, Compare_fields, 3); 817 Compare_type = make_type("Compare", expr_type, Compare_fields, 3);
816 if (!Compare_type) return 0; 818 if (!Compare_type) return 0;
817 Call_type = make_type("Call", expr_type, Call_fields, 5); 819 Call_type = make_type("Call", expr_type, Call_fields, 5);
818 if (!Call_type) return 0; 820 if (!Call_type) return 0;
1745 p->col_offset = col_offset; 1747 p->col_offset = col_offset;
1746 return p; 1748 return p;
1747 } 1749 }
1748 1750
1749 expr_ty 1751 expr_ty
1750 Yield(expr_ty value, int lineno, int col_offset, PyArena *arena) 1752 Yield(int is_from, expr_ty value, int lineno, int col_offset, PyArena *arena)
1751 { 1753 {
1752 expr_ty p; 1754 expr_ty p;
1753 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p)); 1755 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1754 if (!p) 1756 if (!p)
1755 return NULL; 1757 return NULL;
1756 p->kind = Yield_kind; 1758 p->kind = Yield_kind;
1759 p->v.Yield.is_from = is_from;
1757 p->v.Yield.value = value; 1760 p->v.Yield.value = value;
1758 p->lineno = lineno; 1761 p->lineno = lineno;
1759 p->col_offset = col_offset; 1762 p->col_offset = col_offset;
1760 return p; 1763 return p;
1761 } 1764 }
2793 Py_DECREF(value); 2796 Py_DECREF(value);
2794 break; 2797 break;
2795 case Yield_kind: 2798 case Yield_kind:
2796 result = PyType_GenericNew(Yield_type, NULL, NULL); 2799 result = PyType_GenericNew(Yield_type, NULL, NULL);
2797 if (!result) goto failed; 2800 if (!result) goto failed;
2801 value = ast2obj_int(o->v.Yield.is_from);
2802 if (!value) goto failed;
2803 if (PyObject_SetAttrString(result, "is_from", value) == -1)
2804 goto failed;
2805 Py_DECREF(value);
2798 value = ast2obj_expr(o->v.Yield.value); 2806 value = ast2obj_expr(o->v.Yield.value);
2799 if (!value) goto failed; 2807 if (!value) goto failed;
2800 if (_PyObject_SetAttrId(result, &PyId_value, value) == -1) 2808 if (_PyObject_SetAttrId(result, &PyId_value, value) == -1)
2801 goto failed; 2809 goto failed;
2802 Py_DECREF(value); 2810 Py_DECREF(value);
5335 isinstance = PyObject_IsInstance(obj, (PyObject*)Yield_type); 5343 isinstance = PyObject_IsInstance(obj, (PyObject*)Yield_type);
5336 if (isinstance == -1) { 5344 if (isinstance == -1) {
5337 return 1; 5345 return 1;
5338 } 5346 }
5339 if (isinstance) { 5347 if (isinstance) {
5348 int is_from;
5340 expr_ty value; 5349 expr_ty value;
5341 5350
5351 if (_PyObject_HasAttrId(obj, &PyId_is_from)) {
5352 int res;
5353 tmp = _PyObject_GetAttrId(obj, &PyId_is_from);
5354 if (tmp == NULL) goto failed;
5355 res = obj2ast_int(tmp, &is_from, arena);
5356 if (res != 0) goto failed;
5357 Py_XDECREF(tmp);
5358 tmp = NULL;
5359 } else {
5360 PyErr_SetString(PyExc_TypeError, "required field \"is_from\" missing from Yield");
5361 return 1;
5362 }
5342 if (_PyObject_HasAttrId(obj, &PyId_value)) { 5363 if (_PyObject_HasAttrId(obj, &PyId_value)) {
5343 int res; 5364 int res;
5344 tmp = _PyObject_GetAttrId(obj, &PyId_value); 5365 tmp = _PyObject_GetAttrId(obj, &PyId_value);
5345 if (tmp == NULL) goto failed; 5366 if (tmp == NULL) goto failed;
5346 res = obj2ast_expr(tmp, &value, arena); 5367 res = obj2ast_expr(tmp, &value, arena);
5348 Py_XDECREF(tmp); 5369 Py_XDECREF(tmp);
5349 tmp = NULL; 5370 tmp = NULL;
5350 } else { 5371 } else {
5351 value = NULL; 5372 value = NULL;
5352 } 5373 }
5353 *out = Yield(value, lineno, col_offset, arena); 5374 *out = Yield(is_from, value, lineno, col_offset, arena);
5354 if (*out == NULL) goto failed; 5375 if (*out == NULL) goto failed;
5355 return 0; 5376 return 0;
5356 } 5377 }
5357 isinstance = PyObject_IsInstance(obj, (PyObject*)Compare_type); 5378 isinstance = PyObject_IsInstance(obj, (PyObject*)Compare_type);
5358 if (isinstance == -1) { 5379 if (isinstance == -1) {