Commit b72406b8 authored by Benjamin Peterson's avatar Benjamin Peterson

refactor to fix refleaks

parent 6fba3dbc
......@@ -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)
......
This diff is collapsed.
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment