8000 bpo-46314: Remove extra RESUME when compiling a lamdba. (GH-30513) · python/cpython@ec0c392 · GitHub
[go: up one dir, main page]

Skip to content

Commit ec0c392

Browse files
authored
bpo-46314: Remove extra RESUME when compiling a lamdba. (GH-30513)
1 parent d24cd49 commit ec0c392

File tree

2 files changed

+157
-144
lines changed

2 files changed

+157
-144
lines changed

Lib/test/test_sys_settrace.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1388,6 +1388,21 @@ def func():
13881388
(19, 'line'),
13891389
(19, 'return')])
13901390

1391+
def test_notrace_lambda(self):
1392+
#Regression test for issue 46314
1393+
1394+
def func():
139 8000 5+
1
1396+
lambda x: 2
1397+
3
1398+
1399+
self.run_and_compare(func,
1400+
[(0, 'call'),
1401+
(1, 'line'),
1402+
(2, 'line'),
1403+
(3, 'line'),
1404+
(3, 'return')])
1405+
13911406

13921407
class SkipLineEventsTraceTestCase(TraceTestCase):
13931408
"""Repeat the trace tests, but with per-line events skipped"""

Python/compile.c

Lines changed: 142 additions & 144 deletions
Original file line numberDiff line numberDiff line change
@@ -630,140 +630,6 @@ compiler_unit_free(struct compiler_unit *u)
630630
PyObject_Free(u);
631631
}
632632

633-
static int
634-
compiler_enter_scope(struct compiler *c, identifier name,
635-
int scope_type, void *key, int lineno)
636-
{
637-
struct compiler_unit *u;
638-
basicblock *block;
639-
640-
u = (struct compiler_unit *)PyObject_Calloc(1, sizeof(
641-
struct compiler_unit));
642-
if (!u) {
643-
PyErr_NoMemory();
644-
return 0;
645-
}
646-
u->u_scope_type = scope_type;
647-
u->u_argcount = 0;
648-
u->u_posonlyargcount = 0;
649-
u->u_kwonlyargcount = 0;
650-
u->u_ste = PySymtable_Lookup(c->c_st, key);
651-
if (!u->u_ste) {
652-
compiler_unit_free(u);
653-
return 0;
654-
}
655-
Py_INCREF(name);
656-
u->u_name = name;
657-
u->u_varnames = list2dict(u->u_ste->ste_varnames);
658-
u->u_cellvars = dictbytype(u->u_ste->ste_symbols, CELL, 0, 0);
659-
if (!u->u_varnames || !u->u_cellvars) {
660-
compiler_unit_free(u);
661-
return 0;
662-
}
663-
if (u->u_ste->ste_needs_class_closure) {
664-
/* Cook up an implicit __class__ cell. */
665-
_Py_IDENTIFIER(__class__);
666-
PyObject *name;
667-
int res;
668-
assert(u->u_scope_type == COMPILER_SCOPE_CLASS);
669-
assert(PyDict_GET_SIZE(u->u_cellvars) == 0);
670-
name = _PyUnicode_FromId(&PyId___class__);
671-
if (!name) {
672-
compiler_unit_free(u);
673-
return 0;
674-
}
675-
res = PyDict_SetItem(u->u_cellvars, name, _PyLong_GetZero());
676-
if (res < 0) {
677-
compiler_unit_free(u);
678-
return 0;
679-
}
680-
}
681-
682-
u->u_freevars = dictbytype(u->u_ste->ste_symbols, FREE, DEF_FREE_CLASS,
683-
PyDict_GET_SIZE(u->u_cellvars));
684-
if (!u->u_freevars) {
685-
compiler_unit_free(u);
686-
return 0;
687-
}
688-
689-
u->u_blocks = NULL;
690-
u->u_nfblocks = 0;
691-
u->u_firstlineno = lineno;
692-
u->u_lineno = lineno;
693-
u->u_col_offset = 0;
694-
u->u_end_lineno = lineno;
695-
u->u_end_col_offset = 0;
696-
u->u_consts = PyDict_New();
697-
if (!u->u_consts) {
698-
compiler_unit_free(u);
699-
return 0;
700-
}
701-
u->u_names = PyDict_New();
702-
if (!u->u_names) {
703-
compiler_unit_free(u);
704-
return 0;
705-
}
706-
707-
u->u_private = NULL;
708-
709-
/* Push the old compiler_unit on the stack. */
710-
if (c->u) {
711-
PyObject *capsule = PyCapsule_New(c->u, CAPSULE_NAME, NULL);
712-
if (!capsule || PyList_Append(c->c_stack, capsule) < 0) {
713-
Py_XDECREF(capsule);
714-
compiler_unit_free(u);
715-
return 0;
716-
}
717-
Py_DECREF(capsule);
718-
u->u_private = c->u->u_private;
719-
Py_XINCREF(u->u_private);
720-
}
721-
c->u = u;
722-
723-
c->c_nestlevel++;
724-
725-
block = compiler_new_block(c);
726-
if (block == NULL)
727-
return 0;
728-
c->u->u_curblock = block;
729-
730-
if (u->u_scope_type != COMPILER_SCOPE_MODULE) {
731-
if (!compiler_set_qualname(c))
732-
return 0;
733-
}
734-
735-
return 1;
736-
}
737-
738-
static void
739-
compiler_exit_scope(struct compiler *c)
740-
{
741-
// Don't call PySequence_DelItem() with an exception raised
742-
PyObject *exc_type, *exc_val, *exc_tb;
743-
PyErr_Fetch(&exc_type, &exc_val, &exc_tb);
744-
745-
c->c_nestlevel--;
746-
compiler_unit_free(c->u);
747-
/* Restore c->u to the parent unit. */
748-
Py_ssize_t n = PyList_GET_SIZE(c->c_stack) - 1;
749-
if (n >= 0) {
750-
PyObject *capsule = PyList_GET_ITEM(c->c_stack, n);
751-
c->u = (struct compiler_unit *)PyCapsule_GetPointer(capsule, CAPSULE_NAME);
752-
assert(c->u);
753-
/* we are deleting from a list so this really shouldn't fail */
754-
if (PySequence_DelItem(c->c_stack, n) < 0) {
755-
_PyErr_WriteUnraisableMsg("on removing the last compiler "
756-
"stack item", NULL);
757-
}
758-
compiler_unit_check(c->u);
759-
}
760-
else {
761-
c->u = NULL;
762-
}
763-
764-
PyErr_Restore(exc_type, exc_val, exc_tb);
765-
}
766-
767633
static int
768634
compiler_set_qualname(struct compiler *c)
769635
{
@@ -1715,6 +1581,144 @@ compiler_addop_j_noline(struct compiler *c, int opcode, basicblock *b)
17151581
return 0; \
17161582
}
17171583

1584+
static int
1585+
compiler_enter_scope(struct compiler *c, identifier name,
1586+
int scope_type, void *key, int lineno)
1587+
{
1588+
struct compiler_unit *u;
1589+
basicblock *block;
1590+
1591+
u = (struct compiler_unit *)PyObject_Calloc(1, sizeof(
1592+
struct compiler_unit));
1593+
if (!u) {
1594+
PyErr_NoMemory();
1595+
return 0;
1596+
}
1597+
u->u_scope_type = scope_type;
1598+
u->u_argcount = 0;
1599+
u->u_posonlyargcount = 0;
1600+
u->u_kwonlyargcount = 0;
1601+
u->u_ste = PySymtable_Lookup(c->c_st, key);
1602+
if (!u->u_ste) {
1603+
compiler_unit_free(u);
1604+
return 0;
1605+
}
1606+
Py_INCREF(name);
1607+
u->u_name = name;
1608+
u->u_varnames = list2dict(u->u_ste->ste_varnames);
1609+
u->u_cellvars = dictbytype(u->u_ste->ste_symbols, CELL, 0, 0);
1610+
if (!u->u_varnames || !u->u_cellvars) {
1611+
compiler_unit_free(u);
1612+
return 0;
1613+
}
1614+
if (u->u_ste->ste_needs_class_closure) {
1615+
/* Cook up an implicit __class__ cell. */
1616+
_Py_IDENTIFIER(__class__);
1617+
PyObject *name;
1618+
int res;
1619+
assert(u->u_scope_type == COMPILER_SCOPE_CLASS);
1620+
assert(PyDict_GET_SIZE(u->u_cellvars) == 0);
1621+
name = _PyUnicode_FromId(&PyId___class__);
1622+
if (!name) {
1623+
compiler_unit_free(u);
1624+
return 0;
1625+
}
1626+
res = PyDict_SetItem(u->u_cellvars, name, _PyLong_GetZero());
1627+
if (res < 0) {
1628+
compiler_unit_free(u);
1629+
return 0;
1630+
}
1631+
}
1632+
1633+
u->u_freevars = dictbytype(u->u_ste->ste_symbols, FREE, DEF_FREE_CLASS,
1634+
PyDict_GET_SIZE(u->u_cellvars));
1635+
if (!u->u_freevars) {
1636+
compiler_unit_free(u);
1637+
return 0;
1638+
}
1639+
1640+
u->u_blocks = NULL;
1641+
u->u_nfblocks = 0;
1642+
u->u_firstlineno = lineno;
1643+
u->u_lineno = lineno;
1644+
u->u_col_offset = 0;
1645+
u->u_end_lineno = lineno;
1646+
u->u_end_col_offset = 0;
1647+
u->u_consts = PyDict_New();
1648+
if (!u->u_consts) {
1649+
compiler_unit_free(u);
1650+
return 0;
1651+
}
1652+
u->u_names = PyDict_New();
1653+
if (!u->u_names) {
1654+
compiler_unit_free(u);
1655+
return 0;
1656+
}
1657+
1658+
u->u_private = NULL;
1659+
1660+
/* Push the old compiler_unit on the stack. */
1661+
if (c->u) {
1662+
PyObject *capsule = PyCapsule_New(c->u, CAPSULE_NAME, NULL);
1663+
if (!capsule || PyList_Append(c->c_stack, capsule) < 0) {
1664+
Py_XDECREF(capsule);
1665+
compiler_unit_free(u);
1666+
return 0;
1667+
}
1668+
Py_DECREF(capsule);
1669+
u->u_private = c->u->u_private;
1670+
Py_XINCREF(u->u_private);
1671+
}
1672+
c->u = u;
1673+
1674+
c->c_nestlevel++;
1675+
1676+
block = compiler_new_block(c);
1677+
if (block == NULL)
1678+
return 0;
1679+
c->u->u_curblock = block;
1680+
1681+
if (u->u_scope_type == COMPILER_SCOPE_MODULE) {
1682+
c->u->u_lineno = -1;
1683+
}
1684+
else {
1685+
if (!compiler_set_qualname(c))
1686+
return 0;
1687+
}
1688+
ADDOP_I(c, RESUME, 0);
1689+
1690+
return 1;
1691+
}
1692+
1693+
static void
1694+
compiler_exit_scope(struct compiler *c)
1695+
{
1696+
// Don't call PySequence_DelItem() with an exception raised
1697+
PyObject *exc_type, *exc_val, *exc_tb;
1698+
PyErr_Fetch(&exc_type, &exc_val, &exc_tb);
1699+
1700+
c->c_nestlevel--;
1701+
compiler_unit_free(c->u);
1702+
/* Restore c->u to the parent unit. */
1703+
Py_ssize_t n = PyList_GET_SIZE(c->c_stack) - 1;
1704+
if (n >= 0) {
1705+
PyObject *capsule = PyList_GET_ITEM(c->c_stack, n);
1706+
c->u = (struct compiler_unit *)PyCapsule_GetPointer(capsule, CAPSULE_NAME);
1707+
assert(c->u);
1708+
/* we are deleting from a list so this really shouldn't fail */
1709+
if (PySequence_DelItem(c->c_stack, n) < 0) {
1710+
_PyErr_WriteUnraisableMsg("on removing the last compiler "
1711+
"stack item", NULL);
1712+
}
1713+
compiler_unit_check(c->u);
1714+
}
1715+
else {
1716+
c->u = NULL;
1717+
}
1718+
1719+
PyErr_Restore(exc_type, exc_val, exc_tb);
1720+
}
1721+
17181722
/* Search if variable annotations are present statically in a block. */
17191723

17201724
static int
@@ -2049,10 +2053,9 @@ compiler_mod(struct compiler *c, mod_ty mod)
20492053
if (module == NULL) {
20502054
return 0;
20512055
}
2052-
if (!compiler_enter_scope(c, module, COMPILER_SCOPE_MODULE, mod, 1))
2056+
if (!compiler_enter_scope(c, module, COMPILER_SCOPE_MODULE, mod, 1)) {
20532057
return NULL;
2054-
c->u->u_lineno = -1;
2055-
ADDOP_I(c, RESUME, 0);
2058+
}
20562059
c->u->u_lineno = 1;
20572060
switch (mod->kind) {
20582061
case Module_kind:
@@ -2508,7 +2511,6 @@ compiler_function(struct compiler *c, stmt_ty s, int is_async)
25082511
if (!compiler_enter_scope(c, name, scope_type, (void *)s, firstlineno)) {
25092512
return 0;
25102513
}
2511-
ADDOP_I(c, RESUME, 0);
25122514

25132515
/* if not -OO mode, add docstring */
25142516
if (c->c_optimize < 2) {
@@ -2581,7 +2583,6 @@ compiler_class(struct compiler *c, stmt_ty s)
25812583
COMPILER_SCOPE_CLASS, (void *)s, firstlineno)) {
25822584
return 0;
25832585
}
2584-
ADDOP_I(c, RESUME, 0);
25852586
/* this block represents what we do in the new scope */
25862587
{
25872588
/* use the class name for name mangling */
@@ -2914,13 +2915,11 @@ compiler_lambda(struct compiler *c, expr_ty e)
29142915
if (funcflags == -1) {
29152916
return 0;
29162917
}
2917-
ADDOP_I(c, RESUME, 0);
29182918

29192919
if (!compiler_enter_scope(c, name, COMPILER_SCOPE_LAMBDA,
2920-
(void *)e, e->lineno))
2920+
(void *)e, e->lineno)) {
29212921
return 0;
2922-
2923-
ADDOP_I(c, RESUME, 0);
2922+
}
29242923
/* Make None the first constant, so the lambda can't have a
29252924
docstring. */
2926 6AF1 2925
if (compiler_add_const(c, Py_None) < 0)
@@ -5292,7 +5291,6 @@ compiler_comprehension(struct compiler *c, expr_ty e, int type,
52925291
{
52935292
goto error;
52945293
}
5295-
ADDOP_I(c, RESUME, 0);
52965294
SET_LOC(c, e);
52975295

52985296
is_async_generator = c->u->u_ste->ste_coroutine;

0 commit comments

Comments
 (0)
0