summaryrefslogtreecommitdiffstats
path: root/Parser
diff options
context:
space:
mode:
Diffstat (limited to 'Parser')
-rwxr-xr-xParser/asdl_c.py20
1 files changed, 15 insertions, 5 deletions
diff --git a/Parser/asdl_c.py b/Parser/asdl_c.py
index 61747ed..80537ce 100755
--- a/Parser/asdl_c.py
+++ b/Parser/asdl_c.py
@@ -499,12 +499,10 @@ class Obj2ModVisitor(PickleVisitor):
def visitField(self, field, name, sum=None, prod=None, depth=0):
ctype = get_c_type(field.type)
if field.opt:
- add_check = " && _PyObject_GetAttrId(obj, &PyId_%s) != Py_None" \
- % (field.name)
+ check = "exists_not_none(obj, &PyId_%s)" % (field.name,)
else:
- add_check = str()
- self.emit("if (_PyObject_HasAttrId(obj, &PyId_%s)%s) {"
- % (field.name, add_check), depth, reflow=False)
+ check = "_PyObject_HasAttrId(obj, &PyId_%s)" % (field.name,)
+ self.emit("if (%s) {" % (check,), depth, reflow=False)
self.emit("int res;", depth+1)
if field.seq:
self.emit("Py_ssize_t len;", depth+1)
@@ -931,6 +929,18 @@ static int add_ast_fields(void)
return 0;
}
+static int exists_not_none(PyObject *obj, _Py_Identifier *id)
+{
+ PyObject *attr = _PyObject_GetAttrId(obj, id);
+ if (!attr) {
+ PyErr_Clear();
+ return 0;
+ }
+ int isnone = attr == Py_None;
+ Py_DECREF(attr);
+ return !isnone;
+}
+
""", 0, reflow=False)
self.emit("static int init_types(void)",0)