]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
[3.11] gh-108791: Fix pdb CLI invalid argument handling (GH-108816) (#111063)
authorRadislav Chugunov <52372310+chgnrdv@users.noreply.github.com>
Thu, 19 Oct 2023 13:26:40 +0000 (16:26 +0300)
committerGitHub <noreply@github.com>
Thu, 19 Oct 2023 13:26:40 +0000 (14:26 +0100)
* [3.11] gh-108791: Fix `pdb` CLI invalid argument handling (GH-108816)
(cherry picked from commit 162213f2db3835e1115178d38741544f4b4db416)

Co-authored-by: Radislav Chugunov <52372310+chgnrdv@users.noreply.github.com>
Lib/pdb.py
Lib/test/test_pdb.py
Misc/NEWS.d/next/Library/2023-09-02-16-07-23.gh-issue-108791.fBcAqh.rst [new file with mode: 0644]

index 5dfe5077654c21d59d6f8277c7b0461d9a72a46a..4a4a0b9d90f6d2090afdb692af97d08f79ac18df 100755 (executable)
@@ -136,6 +136,9 @@ class _ScriptTarget(str):
         if not os.path.exists(self):
             print('Error:', self.orig, 'does not exist')
             sys.exit(1)
+        if os.path.isdir(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)
@@ -162,6 +165,9 @@ class _ModuleTarget(str):
     def check(self):
         try:
             self._details
+        except ImportError as e:
+            print(f"ImportError: {e}")
+            sys.exit(1)
         except Exception:
             traceback.print_exc()
             sys.exit(1)
index 8a0350b8590d8e823cfb4e5807663ed302eb5616..5f9bcc5af82ad6feb001325317bffc1d0c207b7b 100644 (file)
@@ -2159,8 +2159,7 @@ def bœr():
         stdout, stderr = self._run_pdb(
             ['-m', module_name], "", expected_returncode=1
         )
-        self.assertIn("ImportError: No module named t_main.__main__",
-                      stdout.splitlines())
+        self.assertIn("ImportError: No module named t_main.__main__;", stdout)
 
     def test_package_without_a_main(self):
         pkg_name = 't_pkg'
@@ -2178,6 +2177,22 @@ def bœr():
             "'t_pkg.t_main' is a package and cannot be directly executed",
             stdout)
 
+    def test_nonexistent_module(self):
+        assert not os.path.exists(os_helper.TESTFN)
+        stdout, stderr = self._run_pdb(["-m", os_helper.TESTFN], "", expected_returncode=1)
+        self.assertIn(f"ImportError: No module named {os_helper.TESTFN}", stdout)
+
+    def test_dir_as_script(self):
+        with os_helper.temp_dir() as temp_dir:
+            stdout, stderr = self._run_pdb([temp_dir], "", expected_returncode=1)
+            self.assertIn(f"Error: {temp_dir} is a directory", stdout)
+
+    def test_invalid_cmd_line_options(self):
+        stdout, stderr = self._run_pdb(["-c"], "", expected_returncode=1)
+        self.assertIn(f"Error: option -c requires argument", stdout)
+        stdout, stderr = self._run_pdb(["--spam"], "", expected_returncode=1)
+        self.assertIn(f"Error: option --spam not recognized", stdout)
+
     def test_blocks_at_first_code_line(self):
         script = """
                 #This is a comment, on line 2
diff --git a/Misc/NEWS.d/next/Library/2023-09-02-16-07-23.gh-issue-108791.fBcAqh.rst b/Misc/NEWS.d/next/Library/2023-09-02-16-07-23.gh-issue-108791.fBcAqh.rst
new file mode 100644 (file)
index 0000000..84a2cd5
--- /dev/null
@@ -0,0 +1 @@
+Improved error handling in :mod:`pdb` command line interface, making it produce more concise error messages.