Skip to content

Commit

Permalink
pythongh-103578: Fix pdb reading code with non-utf8 encoding (python#…
Browse files Browse the repository at this point in the history
…103581)

`pdb` should use `io.open_code` to open code to avoid encoding issue.
  • Loading branch information
gaogaotiantian authored and itamaro committed Apr 26, 2023
1 parent 2848e41 commit 8c02f95
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Lib/pdb.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'))"


Expand Down
6 changes: 6 additions & 0 deletions Lib/test/test_pdb.py
Original file line number Diff line number Diff line change
Expand Up @@ -2396,6 +2396,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()
Expand Down
Original file line number Diff line number Diff line change
@@ -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`.

0 comments on commit 8c02f95

Please sign in to comment.