Skip to content

Commit 31acfd7

Browse files
gh-103578: Fix pdb reading code with non-utf8 encoding (#103581)
`pdb` should use `io.open_code` to open code to avoid encoding issue.
1 parent c986412 commit 31acfd7

File tree

3 files changed

+8
-1
lines changed

3 files changed

+8
-1
lines changed

Lib/pdb.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ def namespace(self):
154154

155155
@property
156156
def code(self):
157-
with io.open(self) as fp:
157+
with io.open_code(self) as fp:
158158
return f"exec(compile({fp.read()!r}, {self!r}, 'exec'))"
159159

160160

Lib/test/test_pdb.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2396,6 +2396,12 @@ def _create_fake_frozen_module():
23962396
# verify that pdb found the source of the "frozen" function
23972397
self.assertIn('x = "Sentinel string for gh-93696"', stdout, "Sentinel statement not found")
23982398

2399+
def test_non_utf8_encoding(self):
2400+
script_dir = os.path.join(os.path.dirname(__file__), 'encoded_modules')
2401+
for filename in os.listdir(script_dir):
2402+
if filename.endswith(".py"):
2403+
self._run_pdb([os.path.join(script_dir, filename)], 'q')
2404+
23992405
class ChecklineTests(unittest.TestCase):
24002406
def setUp(self):
24012407
linecache.clearcache() # Pdb.checkline() uses linecache.getline()
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
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`.

0 commit comments

Comments
 (0)