[3.10] bpo-45056: Remove trailing unused constants from co_consts (GH-28109) (GH-28125)
(cherry picked from commit 55c4a92fc1abfe388335071f1d64b3addfa5793f)
Co-authored-by: Inada Naoki <songofacandy@gmail.com>
diff --git a/Lib/test/test_compile.py b/Lib/test/test_compile.py
index 99ba487..4de5448 100644
--- a/Lib/test/test_compile.py
+++ b/Lib/test/test_compile.py
@@ -648,6 +648,17 @@ def test_merge_code_attrs(self):
self.assertIs(f1.__code__.co_linetable, f2.__code__.co_linetable)
self.assertIs(f1.__code__.co_code, f2.__code__.co_code)
+ # Stripping unused constants is not a strict requirement for the
+ # Python semantics, it's a more an implementation detail.
+ @support.cpython_only
+ def test_strip_unused_consts(self):
+ # Python 3.10rc1 appended None to co_consts when None is not used
+ # at all. See bpo-45056.
+ def f1():
+ "docstring"
+ return 42
+ self.assertEqual(f1.__code__.co_consts, ("docstring", 42))
+
# This is a regression test for a CPython specific peephole optimizer
# implementation bug present in a few releases. It's assertion verifies
# that peephole optimization was actually done though that isn't an
diff --git a/Lib/test/test_dis.py b/Lib/test/test_dis.py
index d03aa75..edda967 100644
--- a/Lib/test/test_dis.py
+++ b/Lib/test/test_dis.py
@@ -689,10 +689,7 @@ def get_disassembly(self, func, lasti=-1, wrapper=True, **kwargs):
if sys.flags.optimize:
code_info_consts = "0: None"
else:
- code_info_consts = (
- """0: 'Formatted details of methods, functions, or code.'
- 1: None"""
-)
+ code_info_consts = "0: 'Formatted details of methods, functions, or code.'"
code_info_code_info = f"""\
Name: code_info
@@ -816,7 +813,6 @@ def f(c=c):
Constants:
0: 0
1: 1
- 2: None
Names:
0: x"""