Skip to content

Commit 162213f

Browse files
authored
gh-108791: Fix pdb CLI invalid argument handling (#108816)
1 parent b75186f commit 162213f

File tree

3 files changed

+24
-2
lines changed

3 files changed

+24
-2
lines changed

Lib/pdb.py

+6
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,9 @@ def check(self):
138138
if not os.path.exists(self):
139139
print('Error:', self.orig, 'does not exist')
140140
sys.exit(1)
141+
if os.path.isdir(self):
142+
print('Error:', self.orig, 'is a directory')
143+
sys.exit(1)
141144

142145
# Replace pdb's dir with script's dir in front of module search path.
143146
sys.path[0] = os.path.dirname(self)
@@ -164,6 +167,9 @@ class _ModuleTarget(str):
164167
def check(self):
165168
try:
166169
self._details
170+
except ImportError as e:
171+
print(f"ImportError: {e}")
172+
sys.exit(1)
167173
except Exception:
168174
traceback.print_exc()
169175
sys.exit(1)

Lib/test/test_pdb.py

+17-2
Original file line numberDiff line numberDiff line change
@@ -2820,8 +2820,7 @@ def test_module_without_a_main(self):
28202820
stdout, stderr = self._run_pdb(
28212821
['-m', module_name], "", expected_returncode=1
28222822
)
2823-
self.assertIn("ImportError: No module named t_main.__main__",
2824-
stdout.splitlines())
2823+
self.assertIn("ImportError: No module named t_main.__main__;", stdout)
28252824

28262825
def test_package_without_a_main(self):
28272826
pkg_name = 't_pkg'
@@ -2839,6 +2838,22 @@ def test_package_without_a_main(self):
28392838
"'t_pkg.t_main' is a package and cannot be directly executed",
28402839
stdout)
28412840

2841+
def test_nonexistent_module(self):
2842+
assert not os.path.exists(os_helper.TESTFN)
2843+
stdout, stderr = self._run_pdb(["-m", os_helper.TESTFN], "", expected_returncode=1)
2844+
self.assertIn(f"ImportError: No module named {os_helper.TESTFN}", stdout)
2845+
2846+
def test_dir_as_script(self):
2847+
with os_helper.temp_dir() as temp_dir:
2848+
stdout, stderr = self._run_pdb([temp_dir], "", expected_returncode=1)
2849+
self.assertIn(f"Error: {temp_dir} is a directory", stdout)
2850+
2851+
def test_invalid_cmd_line_options(self):
2852+
stdout, stderr = self._run_pdb(["-c"], "", expected_returncode=2)
2853+
self.assertIn(f"pdb: error: argument -c/--command: expected one argument", stdout.split('\n')[1])
2854+
stdout, stderr = self._run_pdb(["--spam", "-m", "pdb"], "", expected_returncode=2)
2855+
self.assertIn(f"pdb: error: unrecognized arguments: --spam", stdout.split('\n')[1])
2856+
28422857
def test_blocks_at_first_code_line(self):
28432858
script = """
28442859
#This is a comment, on line 2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Improved error handling in :mod:`pdb` command line interface, making it produce more concise error messages.

0 commit comments

Comments
 (0)