@@ -615,7 +615,7 @@ def check_same_constant(const):
615615 exec (code , ns )
616616 f1 = ns ['f1' ]
617617 f2 = ns ['f2' ]
618- self .assertIs (f1 .__code__ , f2 .__code__ )
618+ self .assertIs (f1 .__code__ . co_consts , f2 .__code__ . co_consts )
619619 self .check_constant (f1 , const )
620620 self .assertEqual (repr (f1 ()), repr (const ))
621621
@@ -628,7 +628,7 @@ def check_same_constant(const):
628628 # Note: "lambda: ..." emits "LOAD_CONST Ellipsis",
629629 # whereas "lambda: Ellipsis" emits "LOAD_GLOBAL Ellipsis"
630630 f1 , f2 = lambda : ..., lambda : ...
631- self .assertIs (f1 .__code__ , f2 .__code__ )
631+ self .assertIs (f1 .__code__ . co_consts , f2 .__code__ . co_consts )
632632 self .check_constant (f1 , Ellipsis )
633633 self .assertEqual (repr (f1 ()), repr (Ellipsis ))
634634
@@ -643,7 +643,7 @@ def check_same_constant(const):
643643 # {0} is converted to a constant frozenset({0}) by the peephole
644644 # optimizer
645645 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 )
647647 self .check_constant (f1 , frozenset ({0 }))
648648 self .assertTrue (f1 (0 ))
649649
@@ -1302,6 +1302,27 @@ def f():
13021302 self .assertIsNotNone (end_column )
13031303 self .assertLessEqual ((line , column ), (end_line , end_column ))
13041304
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+
13051326
13061327class TestExpressionStackSize (unittest .TestCase ):
13071328 # These tests check that the computed stack size for a code object
0 commit comments