8000 [2.7] bpo-33132: Fix reference counting issues in the compiler. (GH-6… · python/cpython@72f3e08 · GitHub
[go: up one dir, main page]

Skip to content

Commit 72f3e08

Browse files
[2.7] bpo-33132: Fix reference counting issues in the compiler. (GH-6209). (GH-6322)
(cherry picked from commit a95d986) Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
1 parent 4a3c4ba commit 72f3e08

File tree

1 file changed

+19
-18
lines changed

1 file changed

+19
-18
lines changed

Python/compile.c

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1075,6 +1075,15 @@ compiler_addop_j(struct compiler *c, int opcode, basicblock *b, int absolute)
10751075
return 0; \
10761076
}
10771077

1078+
/* Same as ADDOP_O, but steals a reference. */
1079+
#define ADDOP_N(C, OP, O, TYPE) { \
1080+
if (!compiler_addop_o((C), (OP), (C)->u->u_ ## TYPE, (O))) { \
1081+
Py_DECREF((O)); \
1082+
return 0; \
1083+
} \
1084+
Py_DECREF((O)); \
1085+
}
1086+
10781087
#define ADDOP_NAME(C, OP, O, TYPE) { \
10791088
if (!compiler_addop_name((C), (OP), (C)->u->u_ ## TYPE, (O))) \
10801089
return 0; \
@@ -1890,8 +1899,7 @@ compiler_import_as(struct compiler *c, identifier name, identifier asname)
18901899
dot ? dot - src : strlen(src));
18911900
if (!attr)
18921901
return 0;
1893-
ADDOP_O(c, LOAD_ATTR, attr, names);
1894-
Py_DECREF(attr);
1902+
ADDOP_N(c, LOAD_ATTR, attr, names);
18951903
src = dot + 1;
18961904
}
18971905
}
@@ -1923,8 +1931,7 @@ compiler_import(struct compiler *c, stmt_ty s)
19231931
if (level == NULL)
19241932
return 0;
19251933

1926-
ADDOP_O(c, LOAD_CONST, level, consts);
1927-
Py_DECREF(level);
1934+
ADDOP_N(c, LOAD_CONST, level, consts);
19281935
ADDOP_O(c, LOAD_CONST, Py_None, consts);
19291936
ADDOP_NAME(c, IMPORT_NAME, alias->name, names);
19301937

@@ -1959,8 +1966,7 @@ compiler_from_import(struct compiler *c, stmt_ty s)
19591966
{
19601967
int i, n = asdl_seq_LEN(s->v.ImportFrom.names);
19611968

1962-
PyObject *names = PyTuple_New(n);
1963-
PyObject *level;
1969+
PyObject *level, *names;
19641970
static PyObject *empty_string;
19651971

19661972
if (!empty_string) {
@@ -1969,19 +1975,20 @@ compiler_from_import(struct compiler *c, stmt_ty s)
19691975
return 0;
19701976
}
19711977

1972-
if (!names)
1973-
return 0;
1974-
19751978
if (s->v.ImportFrom.level == 0 && c->c_flags &&
19761979
!(c->c_flags->cf_flags & CO_FUTURE_ABSOLUTE_IMPORT))
19771980
level = PyInt_FromLong(-1);
19781981
else
19791982
level = PyInt_FromLong(s->v.ImportFrom.level);
19801983

19811984
if (!level) {
1982-
Py_DECREF(names);
19831985
return 0;
19841986
}
1987+
ADDOP_N(c, LOAD_CONST, level, consts);
1988+
1989+
names = PyTuple_New(n);
1990+
if (!names)
1991+
return 0;
19851992

19861993
/* build up the names */
19871994
for (i = 0; i < n; i++) {
@@ -1992,16 +1999,12 @@ compiler_from_import(struct compiler *c, stmt_ty s)
19921999

19932000
if (s->lineno > c->c_future->ff_lineno && s->v.ImportFrom.module &&
19942001
!strcmp(PyString_AS_STRING(s->v.ImportFrom.module), "__future__")) {
1995-
Py_DECREF(level);
19962002
Py_DECREF(names);
19972003
return compiler_error(c, "from __future__ imports must occur "
19982004
"at the beginning of the file");
19992005
}
2006+
ADDOP_N(c, LOAD_CONST, names, consts);
20002007

2001-
ADDOP_O(c, LOAD_CONST, level, consts);
2002-
Py_DECREF(level);
2003-
ADDOP_O(c, LOAD_CONST, names, consts);
2004-
Py_DECREF(names);
20052008
if (s->v.ImportFrom.module) {
20062009
ADDOP_NAME(c, IMPORT_NAME, s->v.ImportFrom.module, names);
20072010
}
@@ -2024,7 +2027,6 @@ compiler_from_import(struct compiler *c, stmt_ty s)
20242027
store_name = alias->asname;
20252028

20262029
if (!compiler_nameop(c, store_name, Store)) {
2027-
Py_DECREF(names);
20282030
return 0;
20292031
}
20302032
}
@@ -2391,8 +2393,7 @@ compiler_nameop(struct compiler *c, identifier name, expr_context_ty ctx)
23912393
"param invalid for local variable");
23922394
return 0;
23932395
}
2394-
ADDOP_O(c, op, mangled, varnames);
2395-
Py_DECREF(mangled);
2396+
ADDOP_N(c, op, mangled, varnames);
23962397
return 1;
23972398
case OP_GLOBAL:
23982399
switch (ctx) {

0 commit comments

Comments
 (0)
0