Skip to content

Commit bec0789

Browse files
committed
remove unused docstring const
1 parent 12bd15f commit bec0789

File tree

7 files changed

+31
-30
lines changed

7 files changed

+31
-30
lines changed

Include/internal/pycore_flowgraph.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ void _PyCfgBuilder_Free(struct _PyCfgBuilder *g);
2222
int _PyCfgBuilder_CheckSize(struct _PyCfgBuilder* g);
2323

2424
int _PyCfg_OptimizeCodeUnit(struct _PyCfgBuilder *g, PyObject *consts, PyObject *const_cache,
25-
int nlocals, int nparams, int firstlineno);
25+
int nlocals, int nparams, int firstlineno, unsigned has_docstring);
2626

2727
struct _PyCfgBuilder* _PyCfg_FromInstructionSequence(_PyInstructionSequence *seq);
2828
int _PyCfg_ToInstructionSequence(struct _PyCfgBuilder *g, _PyInstructionSequence *seq);

Lib/test/test_code.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
freevars: ('x',)
3131
nlocals: 1
3232
flags: 19
33-
consts: ('None',)
33+
consts: ()
3434
3535
>>> def h(x, y):
3636
... a = x + y
@@ -50,7 +50,7 @@
5050
freevars: ()
5151
nlocals: 5
5252
flags: 3
53-
consts: ('None',)
53+
consts: ()
5454
5555
>>> def attrs(obj):
5656
... print(obj.attr1)
@@ -104,7 +104,7 @@
104104
freevars: ()
105105
nlocals: 3
106106
flags: 3
107-
consts: ('None',)
107+
consts: ()
108108
109109
>>> def posonly_args(a,b,/,c):
110110
... return a,b,c
@@ -121,7 +121,7 @@
121121
freevars: ()
122122
nlocals: 3
123123
flags: 3
124-
consts: ('None',)
124+
consts: ()
125125
126126
>>> def has_docstring(x: str):
127127
... 'This is a one-line doc string'

Lib/test/test_compile.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -841,7 +841,7 @@ def f():
841841
return "unused"
842842

843843
self.assertEqual(f.__code__.co_consts,
844-
(True, "used"))
844+
("used", ))
845845

846846
@support.cpython_only
847847
def test_remove_unused_consts_extended_args(self):

Lib/test/test_dis.py

+3-6
Original file line numberDiff line numberDiff line change
@@ -1425,9 +1425,9 @@ def get_disassembly(self, func, lasti=-1, wrapper=True, **kwargs):
14251425

14261426

14271427
if dis.code_info.__doc__ is None:
1428-
code_info_consts = "0: None"
1428+
code_info_consts = ""
14291429
else:
1430-
code_info_consts = "0: 'Formatted details of methods, functions, or code.'"
1430+
code_info_consts = "Constants:\n 0: 'Formatted details of methods, functions, or code.'"
14311431

14321432
code_info_code_info = f"""\
14331433
Name: code_info
@@ -1438,8 +1438,7 @@ def get_disassembly(self, func, lasti=-1, wrapper=True, **kwargs):
14381438
Number of locals: 1
14391439
Stack size: \\d+
14401440
Flags: OPTIMIZED, NEWLOCALS, HAS_DOCSTRING
1441-
Constants:
1442-
{code_info_consts}
1441+
{code_info_consts}
14431442
Names:
14441443
0: _format_code_info
14451444
1: _get_code_object
@@ -1546,8 +1545,6 @@ def f(c=c):
15461545
Number of locals: 0
15471546
Stack size: \\d+
15481547
Flags: 0x0
1549-
Constants:
1550-
0: None
15511548
Names:
15521549
0: x"""
15531550

Lib/test/test_peepholer.py

+11-11
Original file line numberDiff line numberDiff line change
@@ -1057,16 +1057,16 @@ def test_conditional_jump_forward_non_const_condition(self):
10571057
expected_insts = [
10581058
('LOAD_NAME', 1, 11),
10591059
('POP_JUMP_IF_TRUE', lbl := self.Label(), 12),
1060-
('LOAD_CONST', 1, 13),
1060+
('LOAD_CONST', 0, 13),
10611061
('RETURN_VALUE', None, 13),
10621062
lbl,
1063-
('LOAD_CONST', 2, 14),
1063+
('LOAD_CONST', 1, 14),
10641064
('RETURN_VALUE', None, 14),
10651065
]
10661066
self.cfg_optimization_test(insts,
10671067
expected_insts,
10681068
consts=[0, 1, 2, 3, 4],
1069-
expected_consts=[0, 2, 3])
1069+
expected_consts=[2, 3])
10701070

10711071
def test_list_exceeding_stack_use_guideline(self):
10721072
def f():
@@ -1107,10 +1107,10 @@ def test_multiple_foldings(self):
11071107
('RETURN_VALUE', None, 0)
11081108
]
11091109
after = [
1110-
('LOAD_CONST', 1, 0),
1110+
('LOAD_CONST', 0, 0),
11111111
('RETURN_VALUE', None, 0)
11121112
]
1113-
self.cfg_optimization_test(before, after, consts=[], expected_consts=[(2,), (1, 2)])
1113+
self.cfg_optimization_test(before, after, consts=[], expected_consts=[(1, 2)])
11141114

11151115
def test_build_empty_tuple(self):
11161116
before = [
@@ -1277,13 +1277,13 @@ def test_conditional_jump_forward_const_condition(self):
12771277
expected_insts = [
12781278
('NOP', None, 11),
12791279
('NOP', None, 12),
1280-
('LOAD_CONST', 1, 14),
1280+
('LOAD_CONST', 0, 14),
12811281
('RETURN_VALUE', None, 14),
12821282
]
12831283
self.cfg_optimization_test(insts,
12841284
expected_insts,
12851285
consts=[0, 1, 2, 3, 4],
1286-
expected_consts=[0, 3])
1286+
expected_consts=[3])
12871287

12881288
def test_conditional_jump_backward_non_const_condition(self):
12891289
insts = [
@@ -1322,18 +1322,18 @@ def test_except_handler_label(self):
13221322
insts = [
13231323
('SETUP_FINALLY', handler := self.Label(), 10),
13241324
('POP_BLOCK', None, -1),
1325-
('LOAD_CONST', 1, 11),
1325+
('LOAD_CONST', 0, 11),
13261326
('RETURN_VALUE', None, 11),
13271327
handler,
1328-
('LOAD_CONST', 2, 12),
1328+
('LOAD_CONST', 1, 12),
13291329
('RETURN_VALUE', None, 12),
13301330
]
13311331
expected_insts = [
13321332
('SETUP_FINALLY', handler := self.Label(), 10),
1333-
('LOAD_CONST', 1, 11),
1333+
('LOAD_CONST', 0, 11),
13341334
('RETURN_VALUE', None, 11),
13351335
handler,
1336-
('LOAD_CONST', 2, 12),
1336+
('LOAD_CONST', 1, 12),
13371337
('RETURN_VALUE', None, 12),
13381338
]
13391339
self.cfg_optimization_test(insts, expected_insts, consts=list(range(5)))

Python/compile.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -1325,7 +1325,7 @@ optimize_and_assemble_code_unit(struct compiler_unit *u, PyObject *const_cache,
13251325
assert(u->u_metadata.u_firstlineno);
13261326

13271327
if (_PyCfg_OptimizeCodeUnit(g, consts, const_cache, nlocals,
1328-
nparams, u->u_metadata.u_firstlineno) < 0) {
1328+
nparams, u->u_metadata.u_firstlineno, u->u_ste->ste_has_docstring) < 0) {
13291329
goto error;
13301330
}
13311331

Python/flowgraph.c

+10-6
Original file line numberDiff line numberDiff line change
@@ -2281,7 +2281,7 @@ fast_scan_many_locals(basicblock *entryblock, int nlocals)
22812281
}
22822282

22832283
static int
2284-
remove_unused_consts(basicblock *entryblock, PyObject *consts)
2284+
remove_unused_consts(basicblock *entryblock, PyObject *consts, int has_docstring)
22852285
{
22862286
assert(PyList_CheckExact(consts));
22872287
Py_ssize_t nconsts = PyList_GET_SIZE(consts);
@@ -2297,11 +2297,15 @@ remove_unused_consts(basicblock *entryblock, PyObject *consts)
22972297
if (index_map == NULL) {
22982298
goto end;
22992299
}
2300-
for (Py_ssize_t i = 1; i < nconsts; i++) {
2300+
for (Py_ssize_t i = 0; i < nconsts; i++) {
23012301
index_map[i] = -1;
23022302
}
23032303
// The first constant may be docstring; keep it always.
2304-
index_map[0] = 0;
2304+
if (has_docstring) {
2305+
assert(PyList_Size(consts) >= 1);
2306+
assert(PyUnicode_CheckExact(PyList_GET_ITEM(consts, 0)));
2307+
index_map[0] = 0;
2308+
}
23052309

23062310
/* mark used consts */
23072311
for (basicblock *b = entryblock; b != NULL; b = b->b_next) {
@@ -2764,7 +2768,7 @@ resolve_line_numbers(cfg_builder *g, int firstlineno)
27642768

27652769
int
27662770
_PyCfg_OptimizeCodeUnit(cfg_builder *g, PyObject *consts, PyObject *const_cache,
2767-
int nlocals, int nparams, int firstlineno)
2771+
int nlocals, int nparams, int firstlineno, unsigned has_docstring)
27682772
{
27692773
assert(cfg_builder_check(g));
27702774
/** Preprocessing **/
@@ -2775,7 +2779,7 @@ _PyCfg_OptimizeCodeUnit(cfg_builder *g, PyObject *consts, PyObject *const_cache,
27752779

27762780
/** Optimization **/
27772781
RETURN_IF_ERROR(optimize_cfg(g, consts, const_cache, firstlineno));
2778-
RETURN_IF_ERROR(remove_unused_consts(g->g_entryblock, consts));
2782+
RETURN_IF_ERROR(remove_unused_consts(g->g_entryblock, consts, has_docstring));
27792783
RETURN_IF_ERROR(
27802784
add_checks_for_loads_of_uninitialized_variables(
27812785
g->g_entryblock, nlocals, nparams));
@@ -3191,7 +3195,7 @@ _PyCompile_OptimizeCfg(PyObject *seq, PyObject *consts, int nlocals)
31913195
}
31923196
int nparams = 0, firstlineno = 1;
31933197
if (_PyCfg_OptimizeCodeUnit(g, consts, const_cache, nlocals,
3194-
nparams, firstlineno) < 0) {
3198+
nparams, firstlineno, 0) < 0) {
31953199
goto error;
31963200
}
31973201
res = cfg_to_instruction_sequence(g);

0 commit comments

Comments
 (0)