From 3fe225daeb1b743999a03b0b3234f3d9dc69e40c Mon Sep 17 00:00:00 2001 From: Tian Gao Date: Sun, 5 Nov 2023 12:03:02 -0800 Subject: [PATCH 1/7] Do not mangle sys.path[0] if safe_path is set --- Lib/pdb.py | 6 ++++-- Lib/test/test_pdb.py | 33 +++++++++++++++++++++++++++++---- 2 files changed, 33 insertions(+), 6 deletions(-) diff --git a/Lib/pdb.py b/Lib/pdb.py index a4b02e010a6be6..7376be075ad2a9 100755 --- a/Lib/pdb.py +++ b/Lib/pdb.py @@ -142,8 +142,10 @@ def check(self): print('Error:', self.orig, 'is a directory') sys.exit(1) - # Replace pdb's dir with script's dir in front of module search path. - sys.path[0] = os.path.dirname(self) + # Replace pdb's dir with script's dir in front of module search path + # if safe_path is not set, otherwise sys.path[0] is not pdb's dir + if not getattr(sys.flags, 'safe_path', None): + sys.path[0] = os.path.dirname(self) @property def filename(self): diff --git a/Lib/test/test_pdb.py b/Lib/test/test_pdb.py index 5508f7bff37994..480dd5bb9e09b7 100644 --- a/Lib/test/test_pdb.py +++ b/Lib/test/test_pdb.py @@ -2493,15 +2493,21 @@ def tearDown(self): @unittest.skipIf(sys.flags.safe_path, 'PYTHONSAFEPATH changes default sys.path') - def _run_pdb(self, pdb_args, commands, expected_returncode=0): + def _run_pdb(self, pdb_args, commands, + expected_returncode=0, + extra_env=None): self.addCleanup(os_helper.rmtree, '__pycache__') cmd = [sys.executable, '-m', 'pdb'] + pdb_args + if extra_env is not None: + env = os.environ | extra_env + else: + env = os.environ with subprocess.Popen( cmd, stdout=subprocess.PIPE, stdin=subprocess.PIPE, stderr=subprocess.STDOUT, - env = {**os.environ, 'PYTHONIOENCODING': 'utf-8'} + env = {**env, 'PYTHONIOENCODING': 'utf-8'} ) as proc: stdout, stderr = proc.communicate(str.encode(commands)) stdout = stdout and bytes.decode(stdout) @@ -2513,13 +2519,15 @@ def _run_pdb(self, pdb_args, commands, expected_returncode=0): ) return stdout, stderr - def run_pdb_script(self, script, commands, expected_returncode=0): + def run_pdb_script(self, script, commands, + expected_returncode=0, + extra_env=None): """Run 'script' lines with pdb and the pdb 'commands'.""" filename = 'main.py' with open(filename, 'w') as f: f.write(textwrap.dedent(script)) self.addCleanup(os_helper.unlink, filename) - return self._run_pdb([filename], commands, expected_returncode) + return self._run_pdb([filename], commands, expected_returncode, extra_env) def run_pdb_module(self, script, commands): """Runs the script code as part of a module""" @@ -3104,6 +3112,23 @@ def test_issue42384_symlink(self): self.assertEqual(stdout.split('\n')[2].rstrip('\r'), expected) + def test_safe_path(self): + """ With safe_path set, pdb should not mangle sys.path[0]""" + + script = textwrap.dedent(""" + import sys + import random + print('sys.path[0] is', sys.path[0]) + """) + commands = 'c\n' + + + with os_helper.temp_cwd() as cwd: + stdout, _ = self.run_pdb_script(script, commands, extra_env={'PYTHONSAFEPATH': '1'}) + + unexpected = f'sys.path[0] is {os.path.realpath(cwd)}' + self.assertNotIn(unexpected, stdout) + def test_issue42383(self): with os_helper.temp_cwd() as cwd: with open('foo.py', 'w') as f: From 1ec6724fb90853c2641d6d9b10a37cd1de198194 Mon Sep 17 00:00:00 2001 From: "blurb-it[bot]" <43283697+blurb-it[bot]@users.noreply.github.com> Date: Sun, 5 Nov 2023 20:09:29 +0000 Subject: [PATCH 2/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 --- .../next/Library/2023-11-05-20-09-27.gh-issue-99367.HLaWKo.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 Misc/NEWS.d/next/Library/2023-11-05-20-09-27.gh-issue-99367.HLaWKo.rst diff --git a/Misc/NEWS.d/next/Library/2023-11-05-20-09-27.gh-issue-99367.HLaWKo.rst b/Misc/NEWS.d/next/Library/2023-11-05-20-09-27.gh-issue-99367.HLaWKo.rst new file mode 100644 index 00000000000000..0920da221e423f --- /dev/null +++ b/Misc/NEWS.d/next/Library/2023-11-05-20-09-27.gh-issue-99367.HLaWKo.rst @@ -0,0 +1 @@ +Do not mangle ``sys.path[0]`` in :mod:`pdb` if safe_path is set From 9a8919eed3012f7e9d73846c6e530e065dad6b9d Mon Sep 17 00:00:00 2001 From: Tian Gao Date: Mon, 6 Nov 2023 10:48:23 -0800 Subject: [PATCH 3/7] Rephrase the comments and simplified the logic --- Lib/pdb.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Lib/pdb.py b/Lib/pdb.py index 7376be075ad2a9..c0d133e9338b31 100755 --- a/Lib/pdb.py +++ b/Lib/pdb.py @@ -142,9 +142,9 @@ def check(self): print('Error:', self.orig, 'is a directory') sys.exit(1) - # Replace pdb's dir with script's dir in front of module search path - # if safe_path is not set, otherwise sys.path[0] is not pdb's dir - if not getattr(sys.flags, 'safe_path', None): + # If safe_path(-P) is not set, sys.path[0] would be the directory + # of pdb, and we should replace it with the directory of the script + if not sys.flags.safe_path: sys.path[0] = os.path.dirname(self) @property From c5fabe62c2d77659eaf1c3e638c0fbbe055592d2 Mon Sep 17 00:00:00 2001 From: Tian Gao Date: Mon, 6 Nov 2023 12:25:11 -0800 Subject: [PATCH 4/7] Update Lib/pdb.py Co-authored-by: Irit Katriel <1055913+iritkatriel@users.noreply.github.com> --- Lib/pdb.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/pdb.py b/Lib/pdb.py index c0d133e9338b31..f72251ded6067d 100755 --- a/Lib/pdb.py +++ b/Lib/pdb.py @@ -142,7 +142,7 @@ def check(self): print('Error:', self.orig, 'is a directory') sys.exit(1) - # If safe_path(-P) is not set, sys.path[0] would be the directory + # If safe_path(-P) is not set, sys.path[0] is the directory # of pdb, and we should replace it with the directory of the script if not sys.flags.safe_path: sys.path[0] = os.path.dirname(self) From d73c2da9f5609bccb73326c578f6bbafce0638d7 Mon Sep 17 00:00:00 2001 From: Tian Gao Date: Mon, 27 Nov 2023 14:24:07 -0800 Subject: [PATCH 5/7] Add whatsnew entry --- Doc/whatsnew/3.13.rst | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/Doc/whatsnew/3.13.rst b/Doc/whatsnew/3.13.rst index e12c2a1b0454fd..88d4db2458e234 100644 --- a/Doc/whatsnew/3.13.rst +++ b/Doc/whatsnew/3.13.rst @@ -254,6 +254,11 @@ pdb identified and executed. (Contributed by Tian Gao in :gh:`108464`.) +* ``sys.path[0]`` will not be mangled if ``sys.flags.safe_path`` is set by + :option:`-P` command line option or :envvar:`PYTHONSAFEPATH` environment + variable. + (Contributed by Tian Gao and Christian Walther in :gh:`111762`.) + sqlite3 ------- From c810afc919a004cc8abfe011564fb750b9df698d Mon Sep 17 00:00:00 2001 From: Tian Gao Date: Mon, 27 Nov 2023 14:50:52 -0800 Subject: [PATCH 6/7] Update Doc/whatsnew/3.13.rst Co-authored-by: Irit Katriel <1055913+iritkatriel@users.noreply.github.com> --- Doc/whatsnew/3.13.rst | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Doc/whatsnew/3.13.rst b/Doc/whatsnew/3.13.rst index 88d4db2458e234..9bf93c2f7e4a09 100644 --- a/Doc/whatsnew/3.13.rst +++ b/Doc/whatsnew/3.13.rst @@ -254,9 +254,9 @@ pdb identified and executed. (Contributed by Tian Gao in :gh:`108464`.) -* ``sys.path[0]`` will not be mangled if ``sys.flags.safe_path`` is set by +* ``sys.path[0]`` will no longer be mangled when ``sys.flags.safe_path`` is set (via the :option:`-P` command line option or :envvar:`PYTHONSAFEPATH` environment - variable. + variable). (Contributed by Tian Gao and Christian Walther in :gh:`111762`.) sqlite3 From d40d3db19e1c36e057d65d71ac1b127a1927ff46 Mon Sep 17 00:00:00 2001 From: Tian Gao Date: Mon, 27 Nov 2023 14:54:45 -0800 Subject: [PATCH 7/7] Update phrasing --- Doc/whatsnew/3.13.rst | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Doc/whatsnew/3.13.rst b/Doc/whatsnew/3.13.rst index 9bf93c2f7e4a09..638e1f8e2e83b3 100644 --- a/Doc/whatsnew/3.13.rst +++ b/Doc/whatsnew/3.13.rst @@ -254,9 +254,9 @@ pdb identified and executed. (Contributed by Tian Gao in :gh:`108464`.) -* ``sys.path[0]`` will no longer be mangled when ``sys.flags.safe_path`` is set (via the - :option:`-P` command line option or :envvar:`PYTHONSAFEPATH` environment - variable). +* ``sys.path[0]`` will no longer be replaced by the directory of the script + being debugged when ``sys.flags.safe_path`` is set (via the :option:`-P` + command line option or :envvar:`PYTHONSAFEPATH` environment variable). (Contributed by Tian Gao and Christian Walther in :gh:`111762`.) sqlite3