Skip to content

Commit b72406b

Browse files
committed
refactor to fix refleaks
1 parent 6fba3db commit b72406b

2 files changed

Lines changed: 49 additions & 27 deletions

File tree

Parser/asdl_c.py

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -499,12 +499,10 @@ def isSimpleType(self, field):
499499
def visitField(self, field, name, sum=None, prod=None, depth=0):
500500
ctype = get_c_type(field.type)
501501
if field.opt:
502-
add_check = " && _PyObject_GetAttrId(obj, &PyId_%s) != Py_None" \
503-
% (field.name)
502+
check = "exists_not_none(obj, &PyId_%s)" % (field.name,)
504503
else:
505-
add_check = str()
506-
self.emit("if (_PyObject_HasAttrId(obj, &PyId_%s)%s) {"
507-
% (field.name, add_check), depth, reflow=False)
504+
check = "_PyObject_HasAttrId(obj, &PyId_%s)" % (field.name,)
505+
self.emit("if (%s) {" % (check,), depth, reflow=False)
508506
self.emit("int res;", depth+1)
509507
if field.seq:
510508
self.emit("Py_ssize_t len;", depth+1)
@@ -931,6 +929,18 @@ def visitModule(self, mod):
931929
return 0;
932930
}
933931
932+
static int exists_not_none(PyObject *obj, _Py_Identifier *id)
933+
{
934+
PyObject *attr = _PyObject_GetAttrId(obj, id);
935+
if (!attr) {
936+
PyErr_Clear();
937+
return 0;
938+
}
939+
int isnone = attr == Py_None;
940+
Py_DECREF(attr);
941+
return !isnone;
942+
}
943+
934944
""", 0, reflow=False)
935945

936946
self.emit("static int init_types(void)",0)

Python/Python-ast.c

Lines changed: 34 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -770,6 +770,18 @@ static int add_ast_fields(void)
770770
return 0;
771771
}
772772

773+
static int exists_not_none(PyObject *obj, _Py_Identifier *id)
774+
{
775+
PyObject *attr = _PyObject_GetAttrId(obj, id);
776+
if (!attr) {
777+
PyErr_Clear();
778+
return 0;
779+
}
780+
int isnone = attr == Py_None;
781+
Py_DECREF(attr);
782+
return !isnone;
783+
}
784+
773785

774786
static int init_types(void)
775787
{
@@ -3846,7 +3858,7 @@ obj2ast_stmt(PyObject* obj, stmt_ty* out, PyArena* arena)
38463858
PyErr_SetString(PyExc_TypeError, "required field \"decorator_list\" missing from FunctionDef");
38473859
return 1;
38483860
}
3849-
if (_PyObject_HasAttrId(obj, &PyId_returns) && _PyObject_GetAttrId(obj, &PyId_returns) != Py_None) {
3861+
if (exists_not_none(obj, &PyId_returns)) {
38503862
int res;
38513863
tmp = _PyObject_GetAttrId(obj, &PyId_returns);
38523864
if (tmp == NULL) goto failed;
@@ -3937,7 +3949,7 @@ obj2ast_stmt(PyObject* obj, stmt_ty* out, PyArena* arena)
39373949
PyErr_SetString(PyExc_TypeError, "required field \"keywords\" missing from ClassDef");
39383950
return 1;
39393951
}
3940-
if (_PyObject_HasAttrId(obj, &PyId_starargs) && _PyObject_GetAttrId(obj, &PyId_starargs) != Py_None) {
3952+
if (exists_not_none(obj, &PyId_starargs)) {
39413953
int res;
39423954
tmp = _PyObject_GetAttrId(obj, &PyId_starargs);
39433955
if (tmp == NULL) goto failed;
@@ -3948,7 +3960,7 @@ obj2ast_stmt(PyObject* obj, stmt_ty* out, PyArena* arena)
39483960
} else {
39493961
starargs = NULL;
39503962
}
3951-
if (_PyObject_HasAttrId(obj, &PyId_kwargs) && _PyObject_GetAttrId(obj, &PyId_kwargs) != Py_None) {
3963+
if (exists_not_none(obj, &PyId_kwargs)) {
39523964
int res;
39533965
tmp = _PyObject_GetAttrId(obj, &PyId_kwargs);
39543966
if (tmp == NULL) goto failed;
@@ -4021,7 +4033,7 @@ obj2ast_stmt(PyObject* obj, stmt_ty* out, PyArena* arena)
40214033
if (isinstance) {
40224034
expr_ty value;
40234035

4024-
if (_PyObject_HasAttrId(obj, &PyId_value) && _PyObject_GetAttrId(obj, &PyId_value) != Py_None) {
4036+
if (exists_not_none(obj, &PyId_value)) {
40254037
int res;
40264038
tmp = _PyObject_GetAttrId(obj, &PyId_value);
40274039
if (tmp == NULL) goto failed;
@@ -4479,7 +4491,7 @@ obj2ast_stmt(PyObject* obj, stmt_ty* out, PyArena* arena)
44794491
expr_ty exc;
44804492
expr_ty cause;
44814493

4482-
if (_PyObject_HasAttrId(obj, &PyId_exc) && _PyObject_GetAttrId(obj, &PyId_exc) != Py_None) {
4494+
if (exists_not_none(obj, &PyId_exc)) {
44834495
int res;
44844496
tmp = _PyObject_GetAttrId(obj, &PyId_exc);
44854497
if (tmp == NULL) goto failed;
@@ -4490,7 +4502,7 @@ obj2ast_stmt(PyObject* obj, stmt_ty* out, PyArena* arena)
44904502
} else {
44914503
exc = NULL;
44924504
}
4493-
if (_PyObject_HasAttrId(obj, &PyId_cause) && _PyObject_GetAttrId(obj, &PyId_cause) != Py_None) {
4505+
if (exists_not_none(obj, &PyId_cause)) {
44944506
int res;
44954507
tmp = _PyObject_GetAttrId(obj, &PyId_cause);
44964508
if (tmp == NULL) goto failed;
@@ -4640,7 +4652,7 @@ obj2ast_stmt(PyObject* obj, stmt_ty* out, PyArena* arena)
46404652
PyErr_SetString(PyExc_TypeError, "required field \"test\" missing from Assert");
46414653
return 1;
46424654
}
4643-
if (_PyObject_HasAttrId(obj, &PyId_msg) && _PyObject_GetAttrId(obj, &PyId_msg) != Py_None) {
4655+
if (exists_not_none(obj, &PyId_msg)) {
46444656
int res;
46454657
tmp = _PyObject_GetAttrId(obj, &PyId_msg);
46464658
if (tmp == NULL) goto failed;
@@ -4700,7 +4712,7 @@ obj2ast_stmt(PyObject* obj, stmt_ty* out, PyArena* arena)
47004712
asdl_seq* names;
47014713
int level;
47024714

4703-
if (_PyObject_HasAttrId(obj, &PyId_module) && _PyObject_GetAttrId(obj, &PyId_module) != Py_None) {
4715+
if (exists_not_none(obj, &PyId_module)) {
47044716
int res;
47054717
tmp = _PyObject_GetAttrId(obj, &PyId_module);
47064718
if (tmp == NULL) goto failed;
@@ -4736,7 +4748,7 @@ obj2ast_stmt(PyObject* obj, stmt_ty* out, PyArena* arena)
47364748
PyErr_SetString(PyExc_TypeError, "required field \"names\" missing from ImportFrom");
47374749
return 1;
47384750
}
4739-
if (_PyObject_HasAttrId(obj, &PyId_level) && _PyObject_GetAttrId(obj, &PyId_level) != Py_None) {
4751+
if (exists_not_none(obj, &PyId_level)) {
47404752
int res;
47414753
tmp = _PyObject_GetAttrId(obj, &PyId_level);
47424754
if (tmp == NULL) goto failed;
@@ -5455,7 +5467,7 @@ obj2ast_expr(PyObject* obj, expr_ty* out, PyArena* arena)
54555467
if (isinstance) {
54565468
expr_ty value;
54575469

5458-
if (_PyObject_HasAttrId(obj, &PyId_value) && _PyObject_GetAttrId(obj, &PyId_value) != Py_None) {
5470+
if (exists_not_none(obj, &PyId_value)) {
54595471
int res;
54605472
tmp = _PyObject_GetAttrId(obj, &PyId_value);
54615473
if (tmp == NULL) goto failed;
@@ -5642,7 +5654,7 @@ obj2ast_expr(PyObject* obj, expr_ty* out, PyArena* arena)
56425654
PyErr_SetString(PyExc_TypeError, "required field \"keywords\" missing from Call");
56435655
return 1;
56445656
}
5645-
if (_PyObject_HasAttrId(obj, &PyId_starargs) && _PyObject_GetAttrId(obj, &PyId_starargs) != Py_None) {
5657+
if (exists_not_none(obj, &PyId_starargs)) {
56465658
int res;
56475659
tmp = _PyObject_GetAttrId(obj, &PyId_starargs);
56485660
if (tmp == NULL) goto failed;
@@ -5653,7 +5665,7 @@ obj2ast_expr(PyObject* obj, expr_ty* out, PyArena* arena)
56535665
} else {
56545666
starargs = NULL;
56555667
}
5656-
if (_PyObject_HasAttrId(obj, &PyId_kwargs) && _PyObject_GetAttrId(obj, &PyId_kwargs) != Py_None) {
5668+
if (exists_not_none(obj, &PyId_kwargs)) {
56575669
int res;
56585670
tmp = _PyObject_GetAttrId(obj, &PyId_kwargs);
56595671
if (tmp == NULL) goto failed;
@@ -6124,7 +6136,7 @@ obj2ast_slice(PyObject* obj, slice_ty* out, PyArena* arena)
61246136
expr_ty upper;
61256137
expr_ty step;
61266138

6127-
if (_PyObject_HasAttrId(obj, &PyId_lower) && _PyObject_GetAttrId(obj, &PyId_lower) != Py_None) {
6139+
if (exists_not_none(obj, &PyId_lower)) {
61286140
int res;
61296141
tmp = _PyObject_GetAttrId(obj, &PyId_lower);
61306142
if (tmp == NULL) goto failed;
@@ -6135,7 +6147,7 @@ obj2ast_slice(PyObject* obj, slice_ty* out, PyArena* arena)
61356147
} else {
61366148
lower = NULL;
61376149
}
6138-
if (_PyObject_HasAttrId(obj, &PyId_upper) && _PyObject_GetAttrId(obj, &PyId_upper) != Py_None) {
6150+
if (exists_not_none(obj, &PyId_upper)) {
61396151
int res;
61406152
tmp = _PyObject_GetAttrId(obj, &PyId_upper);
61416153
if (tmp == NULL) goto failed;
@@ -6146,7 +6158,7 @@ obj2ast_slice(PyObject* obj, slice_ty* out, PyArena* arena)
61466158
} else {
61476159
upper = NULL;
61486160
}
6149-
if (_PyObject_HasAttrId(obj, &PyId_step) && _PyObject_GetAttrId(obj, &PyId_step) != Py_None) {
6161+
if (exists_not_none(obj, &PyId_step)) {
61506162
int res;
61516163
tmp = _PyObject_GetAttrId(obj, &PyId_step);
61526164
if (tmp == NULL) goto failed;
@@ -6601,7 +6613,7 @@ obj2ast_excepthandler(PyObject* obj, excepthandler_ty* out, PyArena* arena)
66016613
identifier name;
66026614
asdl_seq* body;
66036615

6604-
if (_PyObject_HasAttrId(obj, &PyId_type) && _PyObject_GetAttrId(obj, &PyId_type) != Py_None) {
6616+
if (exists_not_none(obj, &PyId_type)) {
66056617
int res;
66066618
tmp = _PyObject_GetAttrId(obj, &PyId_type);
66076619
if (tmp == NULL) goto failed;
@@ -6612,7 +6624,7 @@ obj2ast_excepthandler(PyObject* obj, excepthandler_ty* out, PyArena* arena)
66126624
} else {
66136625
type = NULL;
66146626
}
6615-
if (_PyObject_HasAttrId(obj, &PyId_name) && _PyObject_GetAttrId(obj, &PyId_name) != Py_None) {
6627+
if (exists_not_none(obj, &PyId_name)) {
66166628
int res;
66176629
tmp = _PyObject_GetAttrId(obj, &PyId_name);
66186630
if (tmp == NULL) goto failed;
@@ -6696,7 +6708,7 @@ obj2ast_arguments(PyObject* obj, arguments_ty* out, PyArena* arena)
66966708
PyErr_SetString(PyExc_TypeError, "required field \"args\" missing from arguments");
66976709
return 1;
66986710
}
6699-
if (_PyObject_HasAttrId(obj, &PyId_vararg) && _PyObject_GetAttrId(obj, &PyId_vararg) != Py_None) {
6711+
if (exists_not_none(obj, &PyId_vararg)) {
67006712
int res;
67016713
tmp = _PyObject_GetAttrId(obj, &PyId_vararg);
67026714
if (tmp == NULL) goto failed;
@@ -6757,7 +6769,7 @@ obj2ast_arguments(PyObject* obj, arguments_ty* out, PyArena* arena)
67576769
PyErr_SetString(PyExc_TypeError, "required field \"kw_defaults\" missing from arguments");
67586770
return 1;
67596771
}
6760-
if (_PyObject_HasAttrId(obj, &PyId_kwarg) && _PyObject_GetAttrId(obj, &PyId_kwarg) != Py_None) {
6772+
if (exists_not_none(obj, &PyId_kwarg)) {
67616773
int res;
67626774
tmp = _PyObject_GetAttrId(obj, &PyId_kwarg);
67636775
if (tmp == NULL) goto failed;
@@ -6820,7 +6832,7 @@ obj2ast_arg(PyObject* obj, arg_ty* out, PyArena* arena)
68206832
PyErr_SetString(PyExc_TypeError, "required field \"arg\" missing from arg");
68216833
return 1;
68226834
}
6823-
if (_PyObject_HasAttrId(obj, &PyId_annotation) && _PyObject_GetAttrId(obj, &PyId_annotation) != Py_None) {
6835+
if (exists_not_none(obj, &PyId_annotation)) {
68246836
int res;
68256837
tmp = _PyObject_GetAttrId(obj, &PyId_annotation);
68266838
if (tmp == NULL) goto failed;
@@ -6895,7 +6907,7 @@ obj2ast_alias(PyObject* obj, alias_ty* out, PyArena* arena)
68956907
PyErr_SetString(PyExc_TypeError, "required field \"name\" missing from alias");
68966908
return 1;
68976909
}
6898-
if (_PyObject_HasAttrId(obj, &PyId_asname) && _PyObject_GetAttrId(obj, &PyId_asname) != Py_None) {
6910+
if (exists_not_none(obj, &PyId_asname)) {
68996911
int res;
69006912
tmp = _PyObject_GetAttrId(obj, &PyId_asname);
69016913
if (tmp == NULL) goto failed;
@@ -6932,7 +6944,7 @@ obj2ast_withitem(PyObject* obj, withitem_ty* out, PyArena* arena)
69326944
PyErr_SetString(PyExc_TypeError, "required field \"context_expr\" missing from withitem");
69336945
return 1;
69346946
}
6935-
if (_PyObject_HasAttrId(obj, &PyId_optional_vars) && _PyObject_GetAttrId(obj, &PyId_optional_vars) != Py_None) {
6947+
if (exists_not_none(obj, &PyId_optional_vars)) {
69366948
int res;
69376949
tmp = _PyObject_GetAttrId(obj, &PyId_optional_vars);
69386950
if (tmp == NULL) goto failed;

0 commit comments

Comments
 (0)