Skip to content

Commit 58248d9

Browse files
bpo-41137: Use utf-8 encoding while reading .pdbrc files (GH-21263)
1 parent fed2fc4 commit 58248d9

File tree

5 files changed

+48
-6
lines changed

5 files changed

+48
-6
lines changed

Doc/library/pdb.rst

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -241,10 +241,14 @@ middle of a quoted string.
241241
triple: debugger; configuration; file
242242

243243
If a file :file:`.pdbrc` exists in the user's home directory or in the current
244-
directory, it is read in and executed as if it had been typed at the debugger
245-
prompt. This is particularly useful for aliases. If both files exist, the one
246-
in the home directory is read first and aliases defined there can be overridden
247-
by the local file.
244+
directory, it is read with ``'utf-8'`` encoding and executed as if it had been
245+
typed at the debugger prompt. This is particularly useful for aliases. If both
246+
files exist, the one in the home directory is read first and aliases defined there
247+
can be overridden by the local file.
248+
249+
.. versionchanged:: 3.11
250+
:file:`.pdbrc` is now read with ``'utf-8'`` encoding. Previously, it was read
251+
with the system locale encoding.
248252

249253
.. versionchanged:: 3.2
250254
:file:`.pdbrc` can now contain commands that continue debugging, such as

Doc/whatsnew/3.11.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,9 @@ Optimizations
151151
(Contributed by Ken Jin and Mark Shannon in :issue:`26110`, based on ideas
152152
implemented in PyPy.)
153153

154+
* :file:`.pdbrc` is now read with ``'utf-8'`` encoding.
155+
156+
154157
CPython bytecode changes
155158
========================
156159

Lib/pdb.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -166,12 +166,12 @@ def __init__(self, completekey='tab', stdin=None, stdout=None, skip=None,
166166
self.rcLines = []
167167
if readrc:
168168
try:
169-
with open(os.path.expanduser('~/.pdbrc')) as rcFile:
169+
with open(os.path.expanduser('~/.pdbrc'), encoding='utf-8') as rcFile:
170170
self.rcLines.extend(rcFile)
171171
except OSError:
172172
pass
173173
try:
174-
with open(".pdbrc") as rcFile:
174+
with open(".pdbrc", encoding='utf-8') as rcFile:
175175
self.rcLines.extend(rcFile)
176176
except OSError:
177177
pass

Lib/test/test_pdb.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1627,6 +1627,40 @@ def test_readrc_homedir(self):
16271627
if save_home is not None:
16281628
os.environ["HOME"] = save_home
16291629

1630+
def test_read_pdbrc_with_ascii_encoding(self):
1631+
script = textwrap.dedent("""
1632+
import pdb; pdb.Pdb().set_trace()
1633+
print('hello')
1634+
""")
1635+
save_home = os.environ.pop('HOME', None)
1636+
try:
1637+
with os_helper.temp_cwd():
1638+
with open('.pdbrc', 'w', encoding='utf-8') as f:
1639+
f.write("Fran\u00E7ais")
1640+
1641+
with open('main.py', 'w', encoding='utf-8') as f:
1642+
f.write(script)
1643+
1644+
cmd = [sys.executable, 'main.py']
1645+
env = {'PYTHONIOENCODING': 'ascii'}
1646+
if sys.platform == 'win32':
1647+
env['PYTHONLEGACYWINDOWSSTDIO'] = 'non-empty-string'
1648+
proc = subprocess.Popen(
1649+
cmd,
1650+
stdout=subprocess.PIPE,
1651+
stdin=subprocess.PIPE,
1652+
stderr=subprocess.PIPE,
1653+
env={**os.environ, **env}
1654+
)
1655+
with proc:
1656+
stdout, stderr = proc.communicate(b'c\n')
1657+
self.assertIn(b"UnicodeEncodeError: \'ascii\' codec can\'t encode character "
1658+
b"\'\\xe7\' in position 21: ordinal not in range(128)", stderr)
1659+
1660+
finally:
1661+
if save_home is not None:
1662+
os.environ['HOME'] = save_home
1663+
16301664
def test_header(self):
16311665
stdout = StringIO()
16321666
header = 'Nobody expects... blah, blah, blah'
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Use utf-8 encoding while reading .pdbrc files. Patch by Srinivas Reddy Thatiparthy

0 commit comments

Comments
 (0)