[Python-checkins] [3.11] gh-103578: Fix pdb reading code with non-utf8 encoding (GH-103581) (#103867)

hauntsaninja webhook-mailer at python.org
Wed Apr 26 01:28:36 EDT 2023


https://github.com/python/cpython/commit/f11ba1c2decd2fa6e9dfe93b9df4b80b46a290be
commit: f11ba1c2decd2fa6e9dfe93b9df4b80b46a290be
branch: 3.11
author: Miss Islington (bot) <31488909+miss-islington at users.noreply.github.com>
committer: hauntsaninja <12621235+hauntsaninja at users.noreply.github.com>
date: 2023-04-26T05:28:29Z
summary:

[3.11] gh-103578: Fix pdb reading code with non-utf8 encoding (GH-103581) (#103867)

`pdb` should use `io.open_code` to open code to avoid encoding issue.
(cherry picked from commit 31acfd78a0810f84898d36a8289e407d3754b823)

Co-authored-by: Tian Gao <gaogaotiantian at hotmail.com>

files:
A Misc/NEWS.d/next/Library/2023-04-16-18-29-04.gh-issue-103578.fly1wc.rst
M Lib/pdb.py
M Lib/test/test_pdb.py

diff --git a/Lib/pdb.py b/Lib/pdb.py
index 57d070a87e81..f9c5bee4c06c 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 1a0fbf405bff..9328ccaf3bce 100644
--- a/Lib/test/test_pdb.py
+++ b/Lib/test/test_pdb.py
@@ -2348,6 +2348,12 @@ 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):
+            if filename.endswith(".py"):
+                self._run_pdb([os.path.join(script_dir, filename)], 'q')
+
 class ChecklineTests(unittest.TestCase):
     def setUp(self):
         linecache.clearcache()  # Pdb.checkline() uses linecache.getline()
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 000000000000..69986c2a15b3
--- /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 by replacing :func:`io.open` with :func:`io.open_code`. The new method would also call into the hook set by :func:`PyFile_SetOpenCodeHook`.



More information about the Python-checkins mailing list