Skip to content

Commit 3210e3c

Browse files
[3.12] GH-109052: Use the base opcode when comparing code objects (GH-112329)
1 parent bfc6d91 commit 3210e3c

File tree

3 files changed

+22
-2
lines changed

3 files changed

+22
-2
lines changed

Lib/test/test_code.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -498,6 +498,25 @@ def test_code_hash_uses_bytecode(self):
498498
self.assertNotEqual(c, c1)
499499
self.assertNotEqual(hash(c), hash(c1))
500500

501+
@cpython_only
502+
def test_code_equal_with_instrumentation(self):
503+
""" GH-109052
504+
505+
Make sure the instrumentation doesn't affect the code equality
506+
The validity of this test relies on the fact that "x is x" and
507+
"x in x" have only one different instruction and the instructions
508+
have the same argument.
509+
510+
"""
511+
code1 = compile("x is x", "example.py", "eval")
512+
code2 = compile("x in x", "example.py", "eval")
513+
sys._getframe().f_trace_opcodes = True
514+
sys.settrace(lambda *args: None)
515+
exec(code1, {'x': []})
516+
exec(code2, {'x': []})
517+
self.assertNotEqual(code1, code2)
518+
sys.settrace(None)
519+
501520

502521
def isinterned(s):
503522
return s is sys.intern(('_' + s + '_')[1:-1])
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Use the base opcode when comparing code objects to avoid interference from instrumentation

Objects/codeobject.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1800,8 +1800,8 @@ code_richcompare(PyObject *self, PyObject *other, int op)
18001800
for (int i = 0; i < Py_SIZE(co); i++) {
18011801
_Py_CODEUNIT co_instr = _PyCode_CODE(co)[i];
18021802
_Py_CODEUNIT cp_instr = _PyCode_CODE(cp)[i];
1803-
co_instr.op.code = _PyOpcode_Deopt[co_instr.op.code];
1804-
cp_instr.op.code = _PyOpcode_Deopt[cp_instr.op.code];
1803+
co_instr.op.code = _Py_GetBaseOpcode(co, i);
1804+
cp_instr.op.code = _Py_GetBaseOpcode(cp, i);
18051805
eq = co_instr.cache == cp_instr.cache;
18061806
if (!eq) {
18071807
goto unequal;

0 commit comments

Comments
 (0)