From abbc7d5f9f73a2c8cbd6e33a480ae475fdc72c84 Mon Sep 17 00:00:00 2001 From: Tian Gao Date: Sun, 16 Apr 2023 11:25:53 -0700 Subject: [PATCH 1/4] Fix pdb reading code with non-utf8 encoding issue --- Lib/pdb.py | 2 +- Lib/test/test_pdb.py | 5 +++++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/Lib/pdb.py b/Lib/pdb.py index a3553b345a8dd3..645cbf518e58e3 100755 --- a/Lib/pdb.py +++ b/Lib/pdb.py @@ -154,7 +154,7 @@ def namespace(self): @property def code(self): - with io.open(self) as fp: + with io.open_code(self) as fp: return f"exec(compile({fp.read()!r}, {self!r}, 'exec'))" diff --git a/Lib/test/test_pdb.py b/Lib/test/test_pdb.py index 94b441720f258c..acd70f98edfb4a 100644 --- a/Lib/test/test_pdb.py +++ b/Lib/test/test_pdb.py @@ -2396,6 +2396,11 @@ def _create_fake_frozen_module(): # verify that pdb found the source of the "frozen" function self.assertIn('x = "Sentinel string for gh-93696"', stdout, "Sentinel statement not found") + def test_non_utf8_encoding(self): + script_dir = os.path.join(os.path.dirname(__file__), 'encoded_modules') + for filename in os.listdir(script_dir): + self._run_pdb([os.path.join(script_dir, filename)], 'q') + class ChecklineTests(unittest.TestCase): def setUp(self): linecache.clearcache() # Pdb.checkline() uses linecache.getline() From cad5b05669ca2f712adc933f1f8458321a68accf Mon Sep 17 00:00:00 2001 From: "blurb-it[bot]" <43283697+blurb-it[bot]@users.noreply.github.com> Date: Sun, 16 Apr 2023 18:29:06 +0000 Subject: [PATCH 2/4] =?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-04-16-18-29-04.gh-issue-103578.fly1wc.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 Misc/NEWS.d/next/Library/2023-04-16-18-29-04.gh-issue-103578.fly1wc.rst diff --git a/Misc/NEWS.d/next/Library/2023-04-16-18-29-04.gh-issue-103578.fly1wc.rst b/Misc/NEWS.d/next/Library/2023-04-16-18-29-04.gh-issue-103578.fly1wc.rst new file mode 100644 index 00000000000000..10c460411a4887 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2023-04-16-18-29-04.gh-issue-103578.fly1wc.rst @@ -0,0 +1 @@ +Fixed a bug where :mod:`pdb` crashes when reading source file with different encoding. From aba11f907be5b68522b4bd278ca2619e9b015502 Mon Sep 17 00:00:00 2001 From: Tian Gao Date: Sun, 16 Apr 2023 13:13:03 -0700 Subject: [PATCH 3/4] Add python ext check --- Lib/test/test_pdb.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Lib/test/test_pdb.py b/Lib/test/test_pdb.py index acd70f98edfb4a..88eb180dd12af8 100644 --- a/Lib/test/test_pdb.py +++ b/Lib/test/test_pdb.py @@ -2399,7 +2399,8 @@ def _create_fake_frozen_module(): def test_non_utf8_encoding(self): script_dir = os.path.join(os.path.dirname(__file__), 'encoded_modules') for filename in os.listdir(script_dir): - self._run_pdb([os.path.join(script_dir, filename)], 'q') + if filename.endswith(".py"): + self._run_pdb([os.path.join(script_dir, filename)], 'q') class ChecklineTests(unittest.TestCase): def setUp(self): From 1d8fc05745a85755572d2a6c276899d988ae966f Mon Sep 17 00:00:00 2001 From: Tian Gao Date: Sun, 23 Apr 2023 22:37:22 -0700 Subject: [PATCH 4/4] Update 2023-04-16-18-29-04.gh-issue-103578.fly1wc.rst --- .../next/Library/2023-04-16-18-29-04.gh-issue-103578.fly1wc.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Misc/NEWS.d/next/Library/2023-04-16-18-29-04.gh-issue-103578.fly1wc.rst b/Misc/NEWS.d/next/Library/2023-04-16-18-29-04.gh-issue-103578.fly1wc.rst index 10c460411a4887..69986c2a15b39e 100644 --- a/Misc/NEWS.d/next/Library/2023-04-16-18-29-04.gh-issue-103578.fly1wc.rst +++ b/Misc/NEWS.d/next/Library/2023-04-16-18-29-04.gh-issue-103578.fly1wc.rst @@ -1 +1 @@ -Fixed a bug where :mod:`pdb` crashes when reading source file with different encoding. +Fixed a bug where :mod:`pdb` crashes when reading source file with different encoding by replacing :func:`io.open` with :func:`io.open_code`. The new method would also call into the hook set by :func:`PyFile_SetOpenCodeHook`.