summaryrefslogtreecommitdiffstats
path: root/Parser/asdl_c.py
diff options
context:
space:
mode:
authorBenjamin Peterson <benjamin@python.org>2013-03-19 06:24:41 (GMT)
committerBenjamin Peterson <benjamin@python.org>2013-03-19 06:24:41 (GMT)
commitb72406b8faca66a3a8c5ebcc9ec3baafbc5cc47c (patch)
treeea2c51ba125521c1db43c7c8b589ad3b7cec2449 /Parser/asdl_c.py
parent6fba3dbce0573e5b6fd17eceb116c11b5c825d76 (diff)
downloadcpython-b72406b8faca66a3a8c5ebcc9ec3baafbc5cc47c.zip
cpython-b72406b8faca66a3a8c5ebcc9ec3baafbc5cc47c.tar.gz
cpython-b72406b8faca66a3a8c5ebcc9ec3baafbc5cc47c.tar.bz2
refactor to fix refleaks
Diffstat (limited to 'Parser/asdl_c.py')
-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)