]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-103578: Fix pdb reading code with non-utf8 encoding (#103581) 31302/head
authorTian Gao <gaogaotiantian@hotmail.com>
Wed, 26 Apr 2023 05:04:51 +0000 (22:04 -0700)
committerGitHub <noreply@github.com>
Wed, 26 Apr 2023 05:04:51 +0000 (23:04 -0600)
`pdb` should use `io.open_code` to open code to avoid encoding issue.

Lib/pdb.py
Lib/test/test_pdb.py
Misc/NEWS.d/next/Library/2023-04-16-18-29-04.gh-issue-103578.fly1wc.rst [new file with mode: 0644]

index a3553b345a8dd3b865443b269784c03320cf2508..645cbf518e58e35a2d127f8234e915676500d932 100755 (executable)
@@ -154,7 +154,7 @@ class _ScriptTarget(str):
 
     @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'))"
 
 
index b5c413af344c93decb5a04a929312bdf5901bdaf..2f712a1025798405006dae39675278b4f7f494ef 100644 (file)
@@ -2396,6 +2396,12 @@ def bœr():
         # 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 (file)
index 0000000..69986c2
--- /dev/null
@@ -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`.