[Python-checkins] bpo-34266: [pdb] handle ValueError from shlex.split() (GH-26656) (GH-27005)

iritkatriel webhook-mailer at python.org
Sat Jul 3 12:05:56 EDT 2021


https://github.com/python/cpython/commit/21be6cb0304ba143980b7e4c875cb5b9db30952b
commit: 21be6cb0304ba143980b7e4c875cb5b9db30952b
branch: 3.9
author: Miss Islington (bot) <31488909+miss-islington at users.noreply.github.com>
committer: iritkatriel <1055913+iritkatriel at users.noreply.github.com>
date: 2021-07-03T17:05:33+01:00
summary:

bpo-34266: [pdb] handle ValueError from shlex.split() (GH-26656) (GH-27005)

(cherry picked from commit d968a638fcbf9030c999cfacd4c9bf0656e779c4)

Co-authored-by: Irit Katriel <1055913+iritkatriel at users.noreply.github.com>

files:
A Misc/NEWS.d/next/Library/2021-06-10-21-53-46.bpo-34266.k3fxnm.rst
M Lib/pdb.py
M Lib/test/test_pdb.py

diff --git a/Lib/pdb.py b/Lib/pdb.py
index ff40f7b2476a3..1b4ff54833fcb 100755
--- a/Lib/pdb.py
+++ b/Lib/pdb.py
@@ -1026,7 +1026,11 @@ def do_run(self, arg):
         if arg:
             import shlex
             argv0 = sys.argv[0:1]
-            sys.argv = shlex.split(arg)
+            try:
+                sys.argv = shlex.split(arg)
+            except ValueError as e:
+                self.error('Cannot run %s: %s' % (arg, e))
+                return
             sys.argv[:0] = argv0
         # this is caught in the main debugger loop
         raise Restart
diff --git a/Lib/test/test_pdb.py b/Lib/test/test_pdb.py
index 1c007a2b6fc8d..193207a9aba50 100644
--- a/Lib/test/test_pdb.py
+++ b/Lib/test/test_pdb.py
@@ -1736,6 +1736,21 @@ def test_errors_in_command(self):
             '(Pdb) ',
         ])
 
+    def test_issue34266(self):
+        '''do_run handles exceptions from parsing its arg'''
+        def check(bad_arg, msg):
+            commands = "\n".join([
+                f'run {bad_arg}',
+                'q',
+            ])
+            stdout, _ = self.run_pdb_script('pass', commands + '\n')
+            self.assertEqual(stdout.splitlines()[1:], [
+                '-> pass',
+                f'(Pdb) *** Cannot run {bad_arg}: {msg}',
+                '(Pdb) ',
+            ])
+        check('\\', 'No escaped character')
+        check('"', 'No closing quotation')
 
     def test_issue42384(self):
         '''When running `python foo.py` sys.path[0] is an absolute path. `python -m pdb foo.py` should behave the same'''
diff --git a/Misc/NEWS.d/next/Library/2021-06-10-21-53-46.bpo-34266.k3fxnm.rst b/Misc/NEWS.d/next/Library/2021-06-10-21-53-46.bpo-34266.k3fxnm.rst
new file mode 100644
index 0000000000000..22ef84e9626ad
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2021-06-10-21-53-46.bpo-34266.k3fxnm.rst
@@ -0,0 +1 @@
+Handle exceptions from parsing the arg of :mod:`pdb`'s run/restart command.



More information about the Python-checkins mailing list