From bfaf1d2655b8915da649c6eb65407cbbd642235f Mon Sep 17 00:00:00 2001 From: Tian Gao Date: Sun, 26 Mar 2023 13:46:07 -0700 Subject: [PATCH 1/5] Display current line correctly with CACHE --- Lib/dis.py | 7 ++++++- Lib/test/test_dis.py | 11 +++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/Lib/dis.py b/Lib/dis.py index c3d152b4de0469..b39b2835330135 100644 --- a/Lib/dis.py +++ b/Lib/dis.py @@ -581,7 +581,12 @@ def _disassemble_bytes(code, lasti=-1, varname_from_oparg=None, instr.offset > 0) if new_source_line: print(file=file) - is_current_instr = instr.offset == lasti + if show_caches: + is_current_instr = instr.offset == lasti + else: + # Each CACHE takes 2 bytes + is_current_instr = instr.offset <= lasti \ + <= instr.offset + 2 * _inline_cache_entries[_deoptop(instr.opcode)] print(instr._disassemble(lineno_width, is_current_instr, offset_width), file=file) if exception_entries: diff --git a/Lib/test/test_dis.py b/Lib/test/test_dis.py index ed66b362b08080..bc3d17536c282b 100644 --- a/Lib/test/test_dis.py +++ b/Lib/test/test_dis.py @@ -1198,6 +1198,17 @@ def test_show_caches(self): self.assertEqual(caches.count(""), empty_caches) self.assertEqual(len(caches), total_caches) + @cpython_only + def test_show_currinstr_with_cache(self): + def f(): + print(a) + # The code above should generate a LOAD_GLOBAL which has CACHE instr after + # Make sure that with lasti pointing to CACHE, it still shows the current + # line correctly + self.assertEqual(self.get_disassembly(f.__code__, lasti=2, wrapper=False), + self.get_disassembly(f.__code__, lasti=4, wrapper=False)) + + class DisWithFileTests(DisTests): # Run the tests again, using the file arg instead of print From a5cd6d8d79e8e93bc91e096145686c8960a67c6b Mon Sep 17 00:00:00 2001 From: "blurb-it[bot]" <43283697+blurb-it[bot]@users.noreply.github.com> Date: Sun, 26 Mar 2023 20:54:58 +0000 Subject: [PATCH 2/5] =?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 --- .../next/Library/2023-03-26-20-54-57.gh-issue-103046.xBlA2l.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 Misc/NEWS.d/next/Library/2023-03-26-20-54-57.gh-issue-103046.xBlA2l.rst diff --git a/Misc/NEWS.d/next/Library/2023-03-26-20-54-57.gh-issue-103046.xBlA2l.rst b/Misc/NEWS.d/next/Library/2023-03-26-20-54-57.gh-issue-103046.xBlA2l.rst new file mode 100644 index 00000000000000..b88b076567e6aa --- /dev/null +++ b/Misc/NEWS.d/next/Library/2023-03-26-20-54-57.gh-issue-103046.xBlA2l.rst @@ -0,0 +1 @@ +Display current line label correctly for :mod:`dis` when CACHE entries exist From dfadcd69efd981dc614b33c549d2a629163e9cfd Mon Sep 17 00:00:00 2001 From: Tian Gao Date: Mon, 27 Mar 2023 10:05:50 -0700 Subject: [PATCH 3/5] Improve cache test to explicitly find CACHE entry --- Lib/test/test_dis.py | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/Lib/test/test_dis.py b/Lib/test/test_dis.py index bc3d17536c282b..cde4fd2538c697 100644 --- a/Lib/test/test_dis.py +++ b/Lib/test/test_dis.py @@ -1200,13 +1200,26 @@ def test_show_caches(self): @cpython_only def test_show_currinstr_with_cache(self): + """ + Make sure that with lasti pointing to CACHE, it still shows the current + line correctly + """ def f(): print(a) # The code above should generate a LOAD_GLOBAL which has CACHE instr after - # Make sure that with lasti pointing to CACHE, it still shows the current - # line correctly - self.assertEqual(self.get_disassembly(f.__code__, lasti=2, wrapper=False), - self.get_disassembly(f.__code__, lasti=4, wrapper=False)) + # However, this might change in the future. So we explicitly try to find + # a CACHE entry in the instructions. If we can't do that, fail the test + + for inst in dis.get_instructions(f, show_caches=True): + if inst.opname == "CACHE": + op_offset = inst.offset - 2 + cache_offset = inst.offset + break + else: + self.fail("Can't find a CACHE entry in the function provided to do the test") + + self.assertEqual(self.get_disassembly(f.__code__, lasti=op_offset, wrapper=False), + self.get_disassembly(f.__code__, lasti=cache_offset, wrapper=False)) class DisWithFileTests(DisTests): From 6a8b9ab94dc3fa8089f4ea524aab59b259721040 Mon Sep 17 00:00:00 2001 From: gaogaotiantian Date: Mon, 27 Mar 2023 12:09:57 -0700 Subject: [PATCH 4/5] Update Misc/NEWS.d/next/Library/2023-03-26-20-54-57.gh-issue-103046.xBlA2l.rst Co-authored-by: Irit Katriel <1055913+iritkatriel@users.noreply.github.com> --- .../next/Library/2023-03-26-20-54-57.gh-issue-103046.xBlA2l.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Misc/NEWS.d/next/Library/2023-03-26-20-54-57.gh-issue-103046.xBlA2l.rst b/Misc/NEWS.d/next/Library/2023-03-26-20-54-57.gh-issue-103046.xBlA2l.rst index b88b076567e6aa..f9bd0a10056ef1 100644 --- a/Misc/NEWS.d/next/Library/2023-03-26-20-54-57.gh-issue-103046.xBlA2l.rst +++ b/Misc/NEWS.d/next/Library/2023-03-26-20-54-57.gh-issue-103046.xBlA2l.rst @@ -1 +1 @@ -Display current line label correctly for :mod:`dis` when CACHE entries exist +Display current line label correctly in :mod:`dis` when ``show_caches`` is False and ``lasti`` points to a CACHE entry. From e27ffc2f91bbc0831432fa64f255884f3a6a6c7f Mon Sep 17 00:00:00 2001 From: Tian Gao Date: Mon, 27 Mar 2023 12:36:40 -0700 Subject: [PATCH 5/5] Add checks for the current line indicator --- Lib/test/test_dis.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/Lib/test/test_dis.py b/Lib/test/test_dis.py index cde4fd2538c697..7cad8d1bfe13ae 100644 --- a/Lib/test/test_dis.py +++ b/Lib/test/test_dis.py @@ -1218,8 +1218,13 @@ def f(): else: self.fail("Can't find a CACHE entry in the function provided to do the test") - self.assertEqual(self.get_disassembly(f.__code__, lasti=op_offset, wrapper=False), - self.get_disassembly(f.__code__, lasti=cache_offset, wrapper=False)) + assem_op = self.get_disassembly(f.__code__, lasti=op_offset, wrapper=False) + assem_cache = self.get_disassembly(f.__code__, lasti=cache_offset, wrapper=False) + + # Make sure --> exists and points to the correct offset + self.assertRegex(assem_op, fr"-->\s+{op_offset}") + # Make sure when lasti points to cache, it shows the same disassembly + self.assertEqual(assem_op, assem_cache) class DisWithFileTests(DisTests):