Skip to content

Commit 044efd0

Browse files
gaogaotiantianmiss-islington
authored andcommitted
pythongh-103578: Fix pdb reading code with non-utf8 encoding (pythonGH-103581)
`pdb` should use `io.open_code` to open code to avoid encoding issue. (cherry picked from commit 31acfd7) Co-authored-by: Tian Gao <[email protected]>
1 parent d9aafe2 commit 044efd0

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
@@ -2348,6 +2348,12 @@ def _create_fake_frozen_module():
23482348
# verify that pdb found the source of the "frozen" function
23492349
self.assertIn('x = "Sentinel string for gh-93696"', stdout, "Sentinel statement not found")
23502350

2351+
def test_non_utf8_encoding(self):
2352+
script_dir = os.path.join(os.path.dirname(__file__), 'encoded_modules')
2353+
for filename in os.listdir(script_dir):
2354+
if filename.endswith(".py"):
2355+
self._run_pdb([os.path.join(script_dir, filename)], 'q')
2356+
23512357
class ChecklineTests(unittest.TestCase):
23522358
def setUp(self):
23532359
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)