From b18459fe8c3b3bdf78846a5d708ad7fb984a3f8c Mon Sep 17 00:00:00 2001 From: Tian Gao Date: Thu, 7 Sep 2023 11:44:30 -0700 Subject: [PATCH 1/3] Use the base opcode when comparing code objects --- Lib/test/test_code.py | 19 +++++++++++++++++++ Objects/codeobject.c | 4 ++-- 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/Lib/test/test_code.py b/Lib/test/test_code.py index 812c0682569207..a961ddbe17a3d3 100644 --- a/Lib/test/test_code.py +++ b/Lib/test/test_code.py @@ -505,6 +505,25 @@ def test_code_hash_uses_bytecode(self): self.assertNotEqual(c, c1) self.assertNotEqual(hash(c), hash(c1)) + @cpython_only + def test_code_equal_with_instrumentation(self): + """ GH-109052 + + Make sure the instrumentation doesn't affect the code equality + The validity of this test relies on the fact that "x is x" and + "x in x" have only one different instruction and the instructions + have the same argument. + + """ + code1 = compile("x is x", "example.py", "eval") + code2 = compile("x in x", "example.py", "eval") + sys._getframe().f_trace_opcodes = True + sys.settrace(lambda *args: None) + exec(code1, {'x': []}) + exec(code2, {'x': []}) + self.assertNotEqual(code1, code2) + sys.settrace(None) + def isinterned(s): return s is sys.intern(('_' + s + '_')[1:-1]) diff --git a/Objects/codeobject.c b/Objects/codeobject.c index 1443027ff293ac..de33885d9efb38 100644 --- a/Objects/codeobject.c +++ b/Objects/codeobject.c @@ -1797,9 +1797,9 @@ code_richcompare(PyObject *self, PyObject *other, int op) for (int i = 0; i < Py_SIZE(co); i++) { _Py_CODEUNIT co_instr = _PyCode_CODE(co)[i]; _Py_CODEUNIT cp_instr = _PyCode_CODE(cp)[i]; - uint8_t co_code = co_instr.op.code; + uint8_t co_code = _Py_GetBaseOpcode(co, i); uint8_t co_arg = co_instr.op.arg; - uint8_t cp_code = cp_instr.op.code; + uint8_t cp_code = _Py_GetBaseOpcode(cp, i); uint8_t cp_arg = cp_instr.op.arg; if (co_code == ENTER_EXECUTOR) { From 805abf335f04ffc4a416d240a37d7562ce941e51 Mon Sep 17 00:00:00 2001 From: "blurb-it[bot]" <43283697+blurb-it[bot]@users.noreply.github.com> Date: Thu, 7 Sep 2023 18:49:03 +0000 Subject: [PATCH 2/3] =?UTF-8?q?=F0=9F=93=9C=F0=9F=A4=96=20Added=20by=20blu?= =?UTF-8?q?rb=5Fit.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../2023-09-07-18-49-01.gh-issue-109052.TBU4nC.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 Misc/NEWS.d/next/Core and Builtins/2023-09-07-18-49-01.gh-issue-109052.TBU4nC.rst diff --git a/Misc/NEWS.d/next/Core and Builtins/2023-09-07-18-49-01.gh-issue-109052.TBU4nC.rst b/Misc/NEWS.d/next/Core and Builtins/2023-09-07-18-49-01.gh-issue-109052.TBU4nC.rst new file mode 100644 index 00000000000000..175046c771cdf3 --- /dev/null +++ b/Misc/NEWS.d/next/Core and Builtins/2023-09-07-18-49-01.gh-issue-109052.TBU4nC.rst @@ -0,0 +1 @@ +Use the base opcode when comparing code objects to avoid interference from instrumentation From 70472dbfeb002112273686787d9184d8879c4f5c Mon Sep 17 00:00:00 2001 From: Tian Gao Date: Thu, 7 Sep 2023 23:51:01 -0700 Subject: [PATCH 3/3] Do Deopt only once --- Objects/codeobject.c | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/Objects/codeobject.c b/Objects/codeobject.c index de33885d9efb38..d65605dbf05413 100644 --- a/Objects/codeobject.c +++ b/Objects/codeobject.c @@ -1805,20 +1805,18 @@ code_richcompare(PyObject *self, PyObject *other, int op) if (co_code == ENTER_EXECUTOR) { const int exec_index = co_arg; _PyExecutorObject *exec = co->co_executors->executors[exec_index]; - co_code = exec->vm_data.opcode; + co_code = _PyOpcode_Deopt[exec->vm_data.opcode]; co_arg = exec->vm_data.oparg; } assert(co_code != ENTER_EXECUTOR); - co_code = _PyOpcode_Deopt[co_code]; if (cp_code == ENTER_EXECUTOR) { const int exec_index = cp_arg; _PyExecutorObject *exec = cp->co_executors->executors[exec_index]; - cp_code = exec->vm_data.opcode; + cp_code = _PyOpcode_Deopt[exec->vm_data.opcode]; cp_arg = exec->vm_data.oparg; } assert(cp_code != ENTER_EXECUTOR); - cp_code = _PyOpcode_Deopt[cp_code]; if (co_code != cp_code || co_arg != cp_arg) { goto unequal;