Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit ce1d5d2

Browse files
committed
Fix iterating over cmpop_ty lists.
1 parent bd260da commit ce1d5d2

2 files changed

Lines changed: 20 additions & 7 deletions

File tree

Parser/asdl_c.py

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -609,13 +609,21 @@ def emitSeq(self, field, value, depth, emit):
609609

610610
def set(self, field, value, depth):
611611
if field.seq:
612+
# XXX should really check for is_simple, but that requires a symbol table
612613
if field.type.value == "cmpop":
613-
# XXX check that this cast is safe, i.e. works independent on whether
614-
# sizeof(cmpop_ty) != sizeof(void*)
615-
cast = "(PyObject*(*)(void*))"
614+
# While the sequence elements are stored as void*,
615+
# ast2obj_cmpop expects an enum
616+
self.emit("{", depth)
617+
self.emit("int i, n = asdl_seq_LEN(%s);" % value, depth+1)
618+
self.emit("value = PyList_New(n);", depth+1)
619+
self.emit("if (!value) goto failed;", depth+1)
620+
self.emit("for(i = 0; i < n; i++)", depth+1)
621+
# This cannot fail, so no need for error handling
622+
self.emit("PyList_SET_ITEM(value, i, ast2obj_%s((%s_ty)asdl_seq_GET(%s, i)));" %
623+
(field.type, field.type, value), depth+2, reflow=False)
624+
self.emit("}", depth)
616625
else:
617-
cast = ""
618-
self.emit("value = ast2obj_list(%s, %sast2obj_%s);" % (value, cast, field.type), depth)
626+
self.emit("value = ast2obj_list(%s, ast2obj_%s);" % (value, field.type), depth)
619627
else:
620628
ctype = get_c_type(field.type)
621629
self.emit("value = ast2obj_%s(%s);" % (field.type, value), depth, reflow=False)

Python/Python-ast.c

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2216,8 +2216,13 @@ ast2obj_expr(void* _o)
22162216
if (PyObject_SetAttrString(result, "left", value) == -1)
22172217
goto failed;
22182218
Py_DECREF(value);
2219-
value = ast2obj_list(o->v.Compare.ops,
2220-
(PyObject*(*)(void*))ast2obj_cmpop);
2219+
{
2220+
int i, n = asdl_seq_LEN(o->v.Compare.ops);
2221+
value = PyList_New(n);
2222+
if (!value) goto failed;
2223+
for(i = 0; i < n; i++)
2224+
PyList_SET_ITEM(value, i, ast2obj_cmpop((cmpop_ty)asdl_seq_GET(o->v.Compare.ops, i)));
2225+
}
22212226
if (!value) goto failed;
22222227
if (PyObject_SetAttrString(result, "ops", value) == -1)
22232228
goto failed;

0 commit comments

Comments
 (0)