From 601ce64cd7cb521207c0d493f8881c31b07ae8be Mon Sep 17 00:00:00 2001 From: Srinivas Reddy Thatiparthy Date: Wed, 1 Jul 2020 22:07:15 +0530 Subject: [PATCH 1/7] bpo-41137: Use utf-8 encoding while reading .pdbrc files --- Lib/pdb.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Lib/pdb.py b/Lib/pdb.py index 1b4ff54833fcb4..72ebd711ea9765 100755 --- a/Lib/pdb.py +++ b/Lib/pdb.py @@ -166,12 +166,12 @@ def __init__(self, completekey='tab', stdin=None, stdout=None, skip=None, 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 From ab8ed087b690a22012b98775963dcc01cbf7914d Mon Sep 17 00:00:00 2001 From: Srinivas Reddy Thatiparthy Date: Thu, 2 Jul 2020 13:02:49 +0530 Subject: [PATCH 2/7] bpo-41137: Update the pdb module documentation for opening .pdbrc files with 'utf-8' encoding --- Doc/library/pdb.rst | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Doc/library/pdb.rst b/Doc/library/pdb.rst index ed1e9712c0e3de..6b360e548ea3e0 100644 --- a/Doc/library/pdb.rst +++ b/Doc/library/pdb.rst @@ -241,10 +241,10 @@ 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,irrespective of system locale 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.2 :file:`.pdbrc` can now contain commands that continue debugging, such as From 770bebc3d9d684cec76d2604880751a77b40d04e Mon Sep 17 00:00:00 2001 From: Srinivas Reddy Thatiparthy Date: Thu, 2 Jul 2020 22:34:57 +0530 Subject: [PATCH 3/7] =?UTF-8?q?bpo-41137:=20Add=20test=20case:=20Write=20a?= =?UTF-8?q?=20dummy=20unicode=20pdb=20command=20-=20'fran=C3=A7aise'=20to?= =?UTF-8?q?=20.pdbrc=20file=20and=20try=20to=20read=20the=20file=20with=20?= =?UTF-8?q?ascii=20encoding.=20And=20test=20the=20read=20failure=20in=20st?= =?UTF-8?q?derr?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Lib/test/test_pdb.py | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/Lib/test/test_pdb.py b/Lib/test/test_pdb.py index 63afb81cb168ff..11d3e284a7ab22 100644 --- a/Lib/test/test_pdb.py +++ b/Lib/test/test_pdb.py @@ -1627,6 +1627,38 @@ def test_readrc_homedir(self): 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 support.temp_cwd(): + with open('.pdbrc', 'w') as f: + f.write("Fran\u00E7ais\n") + + with open('main.py', 'w') as f: + f.write(script) + + cmd = [sys.executable, 'main.py'] + proc = subprocess.Popen( + cmd, + stdout=subprocess.PIPE, + stdin=subprocess.PIPE, + stderr=subprocess.PIPE, + env={**os.environ, 'PYTHONIOENCODING': 'ascii'} + ) + 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' From 1e60ef0fcf08048d6140aa169bf1183ed809e73a Mon Sep 17 00:00:00 2001 From: "blurb-it[bot]" <43283697+blurb-it[bot]@users.noreply.github.com> Date: Wed, 1 Jul 2020 17:42:42 +0000 Subject: [PATCH 4/7] =?UTF-8?q?=F0=9F=93=9C=F0=9F=A4=96=20Added=20by=20blu?= =?UTF-8?q?rb=5Fit.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../NEWS.d/next/Library/2020-07-01-17-42-41.bpo-41137.AnqbP-.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 Misc/NEWS.d/next/Library/2020-07-01-17-42-41.bpo-41137.AnqbP-.rst 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 00000000000000..f91b47dd724619 --- /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 From 660eb0ac382e0eb896dd67e7e3bbf2426bcb439a Mon Sep 17 00:00:00 2001 From: Srinivas Reddy Thatiparthy Date: Thu, 2 Jul 2020 23:29:16 +0530 Subject: [PATCH 5/7] bpo-41137: Fix the failing test case --- Lib/test/test_pdb.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/Lib/test/test_pdb.py b/Lib/test/test_pdb.py index 11d3e284a7ab22..67e3eaba6b67d5 100644 --- a/Lib/test/test_pdb.py +++ b/Lib/test/test_pdb.py @@ -1642,18 +1642,20 @@ def test_read_pdbrc_with_ascii_encoding(self): 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, 'PYTHONIOENCODING': 'ascii'} + 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) + b"\'\\xe7\' in position 21: ordinal not in range(128)", stderr) finally: if save_home is not None: From 4c764a260e4d6a73a048777eae04b5653e6014cc Mon Sep 17 00:00:00 2001 From: Srinivas Reddy Thatiparthy Date: Thu, 2 Jul 2020 23:34:34 +0530 Subject: [PATCH 6/7] bpo-41137: Fix docs failure --- Doc/library/pdb.rst | 12 ++++++++---- Doc/whatsnew/3.11.rst | 3 +++ Lib/test/test_pdb.py | 6 +++--- 3 files changed, 14 insertions(+), 7 deletions(-) diff --git a/Doc/library/pdb.rst b/Doc/library/pdb.rst index 6b360e548ea3e0..6d1dba1bf2eb01 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 with `utf-8` encoding,irrespective of system locale 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. +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 5b51273ed6b9a2..5b58ef681c671f 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/test/test_pdb.py b/Lib/test/test_pdb.py index 67e3eaba6b67d5..dec602a7f15074 100644 --- a/Lib/test/test_pdb.py +++ b/Lib/test/test_pdb.py @@ -1635,10 +1635,10 @@ def test_read_pdbrc_with_ascii_encoding(self): save_home = os.environ.pop('HOME', None) try: with support.temp_cwd(): - with open('.pdbrc', 'w') as f: - f.write("Fran\u00E7ais\n") + with open('.pdbrc', 'w', encoding='utf-8') as f: + f.write("Fran\u00E7ais") - with open('main.py', 'w') as f: + with open('main.py', 'w', encoding='utf-8') as f: f.write(script) cmd = [sys.executable, 'main.py'] From a79515fdfa63e1920f6b7cc79e395a11f3075fa5 Mon Sep 17 00:00:00 2001 From: Srinivas Reddy Thatiparthy Date: Thu, 8 Jul 2021 11:56:11 +0530 Subject: [PATCH 7/7] Fix test failure --- Lib/test/test_pdb.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/test/test_pdb.py b/Lib/test/test_pdb.py index dec602a7f15074..17634c707b6a19 100644 --- a/Lib/test/test_pdb.py +++ b/Lib/test/test_pdb.py @@ -1634,7 +1634,7 @@ def test_read_pdbrc_with_ascii_encoding(self): """) save_home = os.environ.pop('HOME', None) try: - with support.temp_cwd(): + with os_helper.temp_cwd(): with open('.pdbrc', 'w', encoding='utf-8') as f: f.write("Fran\u00E7ais")