Skip to content

Commit 25c869e

Browse files
[3.6] bpo-33132: Fix reference counting issues in the compiler. (pythonGH-6209). (pythonGH-6321)
(cherry picked from commit a95d986)
1 parent ddc9971 commit 25c869e

File tree

1 file changed

+11
-20
lines changed

1 file changed

+11
-20
lines changed

Python/compile.c

Lines changed: 11 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2599,8 +2599,7 @@ compiler_import_as(struct compiler *c, identifier name, identifier asname)
25992599
PyUnicode_GET_LENGTH(name));
26002600
if (!attr)
26012601
return 0;
2602-
ADDOP_O(c, LOAD_ATTR, attr, names);
2603-
Py_DECREF(attr);
2602+
ADDOP_N(c, LOAD_ATTR, attr, names);
26042603
pos = dot + 1;
26052604
}
26062605
}
@@ -2628,8 +2627,7 @@ compiler_import(struct compiler *c, stmt_ty s)
26282627
if (level == NULL)
26292628
return 0;
26302629

2631-
ADDOP_O(c, LOAD_CONST, level, consts);
2632-
Py_DECREF(level);
2630+
ADDOP_N(c, LOAD_CONST, level, consts);
26332631
ADDOP_O(c, LOAD_CONST, Py_None, consts);
26342632
ADDOP_NAME(c, IMPORT_NAME, alias->name, names);
26352633

@@ -2662,9 +2660,7 @@ static int
26622660
compiler_from_import(struct compiler *c, stmt_ty s)
26632661
{
26642662
Py_ssize_t i, n = asdl_seq_LEN(s->v.ImportFrom.names);
2665-
2666-
PyObject *names = PyTuple_New(n);
2667-
PyObject *level;
2663+
PyObject *level, *names;
26682664
static PyObject *empty_string;
26692665

26702666
if (!empty_string) {
@@ -2673,14 +2669,15 @@ compiler_from_import(struct compiler *c, stmt_ty s)
26732669
return 0;
26742670
}
26752671

2676-
if (!names)
2677-
return 0;
2678-
26792672
level = PyLong_FromLong(s->v.ImportFrom.level);
26802673
if (!level) {
2681-
Py_DECREF(names);
26822674
return 0;
26832675
}
2676+
ADDOP_N(c, LOAD_CONST, level, consts);
2677+
2678+
names = PyTuple_New(n);
2679+
if (!names)
2680+
return 0;
26842681

26852682
/* build up the names */
26862683
for (i = 0; i < n; i++) {
@@ -2691,16 +2688,12 @@ compiler_from_import(struct compiler *c, stmt_ty s)
26912688

26922689
if (s->lineno > c->c_future->ff_lineno && s->v.ImportFrom.module &&
26932690
_PyUnicode_EqualToASCIIString(s->v.ImportFrom.module, "__future__")) {
2694-
Py_DECREF(level);
26952691
Py_DECREF(names);
26962692
return compiler_error(c, "from __future__ imports must occur "
26972693
"at the beginning of the file");
26982694
}
2695+
ADDOP_N(c, LOAD_CONST, names, consts);
26992696

2700-
ADDOP_O(c, LOAD_CONST, level, consts);
2701-
Py_DECREF(level);
2702-
ADDOP_O(c, LOAD_CONST, names, consts);
2703-
Py_DECREF(names);
27042697
if (s->v.ImportFrom.module) {
27052698
ADDOP_NAME(c, IMPORT_NAME, s->v.ImportFrom.module, names);
27062699
}
@@ -2723,7 +2716,6 @@ compiler_from_import(struct compiler *c, stmt_ty s)
27232716
store_name = alias->asname;
27242717

27252718
if (!compiler_nameop(c, store_name, Store)) {
2726-
Py_DECREF(names);
27272719
return 0;
27282720
}
27292721
}
@@ -3101,8 +3093,7 @@ compiler_nameop(struct compiler *c, identifier name, expr_context_ty ctx)
31013093
"param invalid for local variable");
31023094
return 0;
31033095
}
3104-
ADDOP_O(c, op, mangled, varnames);
3105-
Py_DECREF(mangled);
3096+
ADDOP_N(c, op, mangled, varnames);
31063097
return 1;
31073098
case OP_GLOBAL:
31083099
switch (ctx) {
@@ -4552,11 +4543,11 @@ compiler_annassign(struct compiler *c, stmt_ty s)
45524543
if (s->v.AnnAssign.simple &&
45534544
(c->u->u_scope_type == COMPILER_SCOPE_MODULE ||
45544545
c->u->u_scope_type == COMPILER_SCOPE_CLASS)) {
4546+
VISIT(c, expr, s->v.AnnAssign.annotation);
45554547
mangled = _Py_Mangle(c->u->u_private, targ->v.Name.id);
45564548
if (!mangled) {
45574549
return 0;
45584550
}
4559-
VISIT(c, expr, s->v.AnnAssign.annotation);
45604551
/* ADDOP_N decrefs its argument */
45614552
ADDOP_N(c, STORE_ANNOTATION, mangled, names);
45624553
}

0 commit comments

Comments
 (0)