summaryrefslogtreecommitdiffstats
path: root/Lib/test/test_compile.py
diff options
context:
space:
mode:
authorBrandt Bucher <brandtbucher@gmail.com>2022-08-01 18:02:56 (GMT)
committerGitHub <noreply@github.com>2022-08-01 18:02:56 (GMT)
commitc7e5bbaee88a71dc6e633e3cd451ed1798436382 (patch)
treebc1fd93fbb18b2745ebc1956ea5448a19f5bbdf1 /Lib/test/test_compile.py
parenta95e60db748ec6f2c19b5710c11f62e1e4d669f4 (diff)
downloadcpython-c7e5bbaee88a71dc6e633e3cd451ed1798436382.zip
cpython-c7e5bbaee88a71dc6e633e3cd451ed1798436382.tar.gz
cpython-c7e5bbaee88a71dc6e633e3cd451ed1798436382.tar.bz2
GH-95150: Use position and exception tables for code hashing and equality (GH-95509)
Diffstat (limited to 'Lib/test/test_compile.py')
-rw-r--r--Lib/test/test_compile.py27
1 files changed, 24 insertions, 3 deletions
diff --git a/Lib/test/test_compile.py b/Lib/test/test_compile.py
index e619446..c64e4e5 100644
--- a/Lib/test/test_compile.py
+++ b/Lib/test/test_compile.py
@@ -615,7 +615,7 @@ if 1:
exec(code, ns)
f1 = ns['f1']
f2 = ns['f2']
- self.assertIs(f1.__code__, f2.__code__)
+ self.assertIs(f1.__code__.co_consts, f2.__code__.co_consts)
self.check_constant(f1, const)
self.assertEqual(repr(f1()), repr(const))
@@ -628,7 +628,7 @@ if 1:
# Note: "lambda: ..." emits "LOAD_CONST Ellipsis",
# whereas "lambda: Ellipsis" emits "LOAD_GLOBAL Ellipsis"
f1, f2 = lambda: ..., lambda: ...
- self.assertIs(f1.__code__, f2.__code__)
+ self.assertIs(f1.__code__.co_consts, f2.__code__.co_consts)
self.check_constant(f1, Ellipsis)
self.assertEqual(repr(f1()), repr(Ellipsis))
@@ -643,7 +643,7 @@ if 1:
# {0} is converted to a constant frozenset({0}) by the peephole
# optimizer
f1, f2 = lambda x: x in {0}, lambda x: x in {0}
- self.assertIs(f1.__code__, f2.__code__)
+ self.assertIs(f1.__code__.co_consts, f2.__code__.co_consts)
self.check_constant(f1, frozenset({0}))
self.assertTrue(f1(0))
@@ -1302,6 +1302,27 @@ f(
self.assertIsNotNone(end_column)
self.assertLessEqual((line, column), (end_line, end_column))
+ @support.cpython_only
+ def test_column_offset_deduplication(self):
+ # GH-95150: Code with different column offsets shouldn't be merged!
+ for source in [
+ "lambda: a",
+ "(a for b in c)",
+ "[a for b in c]",
+ "{a for b in c}",
+ "{a: b for c in d}",
+ ]:
+ with self.subTest(source):
+ code = compile(f"{source}, {source}", "<test>", "eval")
+ self.assertEqual(len(code.co_consts), 2)
+ self.assertIsInstance(code.co_consts[0], types.CodeType)
+ self.assertIsInstance(code.co_consts[1], types.CodeType)
+ self.assertNotEqual(code.co_consts[0], code.co_consts[1])
+ self.assertNotEqual(
+ list(code.co_consts[0].co_positions()),
+ list(code.co_consts[1].co_positions()),
+ )
+
class TestExpressionStackSize(unittest.TestCase):
# These tests check that the computed stack size for a code object