From 58248d94379b202ccce3e45b1d1830ca47683273 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Srinivas=20Reddy=20Thatiparthy=20=28=E0=B0=B6=E0=B1=8D?= =?UTF-8?q?=E0=B0=B0=E0=B1=80=E0=B0=A8=E0=B0=BF=E0=B0=B5=E0=B0=BE=E0=B0=B8?= =?UTF-8?q?=E0=B1=8D=20=20=E0=B0=B0=E0=B1=86=E0=B0=A1=E0=B1=8D=E0=B0=A1?= =?UTF-8?q?=E0=B0=BF=20=E0=B0=A4=E0=B0=BE=E0=B0=9F=E0=B0=BF=E0=B0=AA?= =?UTF-8?q?=E0=B0=B0=E0=B1=8D=E0=B0=A4=E0=B0=BF=29?= Date: Thu, 8 Jul 2021 12:46:08 +0530 Subject: bpo-41137: Use utf-8 encoding while reading .pdbrc files (GH-21263) --- Doc/library/pdb.rst | 12 +++++--- Doc/whatsnew/3.11.rst | 3 ++ Lib/pdb.py | 4 +-- Lib/test/test_pdb.py | 34 ++++++++++++++++++++++ .../2020-07-01-17-42-41.bpo-41137.AnqbP-.rst | 1 + 5 files changed, 48 insertions(+), 6 deletions(-) create mode 100644 Misc/NEWS.d/next/Library/2020-07-01-17-42-41.bpo-41137.AnqbP-.rst diff --git a/Doc/library/pdb.rst b/Doc/library/pdb.rst index ed1e971..6d1dba1 100644 --- a/Doc/library/pdb.rst +++ b/Doc/library/pdb.rst @@ -241,10 +241,14 @@ middle of a quoted string. triple: debugger; configuration; file If a file :file:`.pdbrc` exists in the user's home directory or in the current -directory, it is read in and executed as if it had been typed at the debugger -prompt. This is particularly useful for aliases. If both files exist, the one -in the home directory is read first and aliases defined there can be overridden -by the local file. +directory, it is read with ``'utf-8'`` encoding and executed as if it had been +typed at the debugger prompt. This is particularly useful for aliases. If both +files exist, the one in the home directory is read first and aliases defined there +can be overridden by the local file. + +.. versionchanged:: 3.11 + :file:`.pdbrc` is now read with ``'utf-8'`` encoding. Previously, it was read + with the system locale encoding. .. versionchanged:: 3.2 :file:`.pdbrc` can now contain commands that continue debugging, such as diff --git a/Doc/whatsnew/3.11.rst b/Doc/whatsnew/3.11.rst index 5b51273..5b58ef6 100644 --- a/Doc/whatsnew/3.11.rst +++ b/Doc/whatsnew/3.11.rst @@ -151,6 +151,9 @@ Optimizations (Contributed by Ken Jin and Mark Shannon in :issue:`26110`, based on ideas implemented in PyPy.) +* :file:`.pdbrc` is now read with ``'utf-8'`` encoding. + + CPython bytecode changes ======================== diff --git a/Lib/pdb.py b/Lib/pdb.py index 1b4ff54..72ebd71 100755 --- a/Lib/pdb.py +++ b/Lib/pdb.py @@ -166,12 +166,12 @@ class Pdb(bdb.Bdb, cmd.Cmd): self.rcLines = [] if readrc: try: - with open(os.path.expanduser('~/.pdbrc')) as rcFile: + with open(os.path.expanduser('~/.pdbrc'), encoding='utf-8') as rcFile: self.rcLines.extend(rcFile) except OSError: pass try: - with open(".pdbrc") as rcFile: + with open(".pdbrc", encoding='utf-8') as rcFile: self.rcLines.extend(rcFile) except OSError: pass diff --git a/Lib/test/test_pdb.py b/Lib/test/test_pdb.py index 63afb81..17634c7 100644 --- a/Lib/test/test_pdb.py +++ b/Lib/test/test_pdb.py @@ -1627,6 +1627,40 @@ def bœr(): if save_home is not None: os.environ["HOME"] = save_home + def test_read_pdbrc_with_ascii_encoding(self): + script = textwrap.dedent(""" + import pdb; pdb.Pdb().set_trace() + print('hello') + """) + save_home = os.environ.pop('HOME', None) + try: + with os_helper.temp_cwd(): + with open('.pdbrc', 'w', encoding='utf-8') as f: + f.write("Fran\u00E7ais") + + with open('main.py', 'w', encoding='utf-8') as f: + f.write(script) + + cmd = [sys.executable, 'main.py'] + env = {'PYTHONIOENCODING': 'ascii'} + if sys.platform == 'win32': + env['PYTHONLEGACYWINDOWSSTDIO'] = 'non-empty-string' + proc = subprocess.Popen( + cmd, + stdout=subprocess.PIPE, + stdin=subprocess.PIPE, + stderr=subprocess.PIPE, + env={**os.environ, **env} + ) + with proc: + stdout, stderr = proc.communicate(b'c\n') + self.assertIn(b"UnicodeEncodeError: \'ascii\' codec can\'t encode character " + b"\'\\xe7\' in position 21: ordinal not in range(128)", stderr) + + finally: + if save_home is not None: + os.environ['HOME'] = save_home + def test_header(self): stdout = StringIO() header = 'Nobody expects... blah, blah, blah' diff --git a/Misc/NEWS.d/next/Library/2020-07-01-17-42-41.bpo-41137.AnqbP-.rst b/Misc/NEWS.d/next/Library/2020-07-01-17-42-41.bpo-41137.AnqbP-.rst new file mode 100644 index 0000000..f91b47d --- /dev/null +++ b/Misc/NEWS.d/next/Library/2020-07-01-17-42-41.bpo-41137.AnqbP-.rst @@ -0,0 +1 @@ +Use utf-8 encoding while reading .pdbrc files. Patch by Srinivas Reddy Thatiparthy \ No newline at end of file -- cgit v0.12