@@ -615,7 +615,7 @@ def check_same_constant(const):
615
615
exec (code , ns )
616
616
f1 = ns ['f1' ]
617
617
f2 = ns ['f2' ]
618
- self .assertIs (f1 .__code__ , f2 .__code__ )
618
+ self .assertIs (f1 .__code__ . co_consts , f2 .__code__ . co_consts )
619
619
self .check_constant (f1 , const )
620
620
self .assertEqual (repr (f1 ()), repr (const ))
621
621
@@ -628,7 +628,7 @@ def check_same_constant(const):
628
628
# Note: "lambda: ..." emits "LOAD_CONST Ellipsis",
629
629
# whereas "lambda: Ellipsis" emits "LOAD_GLOBAL Ellipsis"
630
630
f1 , f2 = lambda : ..., lambda : ...
631
- self .assertIs (f1 .__code__ , f2 .__code__ )
631
+ self .assertIs (f1 .__code__ . co_consts , f2 .__code__ . co_consts )
632
632
self .check_constant (f1 , Ellipsis )
633
633
self .assertEqual (repr (f1 ()), repr (Ellipsis ))
634
634
@@ -643,7 +643,7 @@ def check_same_constant(const):
643
643
# {0} is converted to a constant frozenset({0}) by the peephole
644
644
# optimizer
645
645
f1 , f2 = lambda x : x in {0 }, lambda x : x in {0 }
646
- self .assertIs (f1 .__code__ , f2 .__code__ )
646
+ self .assertIs (f1 .__code__ . co_consts , f2 .__code__ . co_consts )
647
647
self .check_constant (f1 , frozenset ({0 }))
648
648
self .assertTrue (f1 (0 ))
649
649
@@ -1302,6 +1302,27 @@ def f():
1302
1302
self .assertIsNotNone (end_column )
1303
1303
self .assertLessEqual ((line , column ), (end_line , end_column ))
1304
1304
1305
+ @support .cpython_only
1306
+ def test_column_offset_deduplication (self ):
1307
+ # GH-95150: Code with different column offsets shouldn't be merged!
1308
+ for source in [
1309
+ "lambda: a" ,
1310
+ "(a for b in c)" ,
1311
+ "[a for b in c]" ,
1312
+ "{a for b in c}" ,
1313
+ "{a: b for c in d}" ,
1314
+ ]:
1315
+ with self .subTest (source ):
1316
+ code = compile (f"{ source } , { source } " , "<test>" , "eval" )
1317
+ self .assertEqual (len (code .co_consts ), 2 )
1318
+ self .assertIsInstance (code .co_consts [0 ], types .CodeType )
1319
+ self .assertIsInstance (code .co_consts [1 ], types .CodeType )
1320
+ self .assertNotEqual (code .co_consts [0 ], code .co_consts [1 ])
1321
+ self .assertNotEqual (
1322
+ list (code .co_consts [0 ].co_positions ()),
1323
+ list (code .co_consts [1 ].co_positions ()),
1324
+ )
1325
+
1305
1326
1306
1327
class TestExpressionStackSize (unittest .TestCase ):
1307
1328
# These tests check that the computed stack size for a code object
0 commit comments