Skip to content

Commit 26a0282

Browse files
authored
[3.11] gh-108791: Fix pdb CLI invalid argument handling (GH-108816) (#111063)
* [3.11] gh-108791: Fix `pdb` CLI invalid argument handling (GH-108816) (cherry picked from commit 162213f) Co-authored-by: Radislav Chugunov <52372310+chgnrdv@users.noreply.github.com>
1 parent 2258d6c commit 26a0282

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
@@ -136,6 +136,9 @@ def check(self):
136136
if not os.path.exists(self):
137137
print('Error:', self.orig, 'does not exist')
138138
sys.exit(1)
139+
if os.path.isdir(self):
140+
print('Error:', self.orig, 'is a directory')
141+
sys.exit(1)
139142

140143
# Replace pdb's dir with script's dir in front of module search path.
141144
sys.path[0] = os.path.dirname(self)
@@ -162,6 +165,9 @@ class _ModuleTarget(str):
162165
def check(self):
163166
try:
164167
self._details
168+
except ImportError as e:
169+
print(f"ImportError: {e}")
170+
sys.exit(1)
165171
except Exception:
166172
traceback.print_exc()
167173
sys.exit(1)

Lib/test/test_pdb.py

+17-2
Original file line numberDiff line numberDiff line change
@@ -2159,8 +2159,7 @@ def test_module_without_a_main(self):
21592159
stdout, stderr = self._run_pdb(
21602160
['-m', module_name], "", expected_returncode=1
21612161
)
2162-
self.assertIn("ImportError: No module named t_main.__main__",
2163-
stdout.splitlines())
2162+
self.assertIn("ImportError: No module named t_main.__main__;", stdout)
21642163

21652164
def test_package_without_a_main(self):
21662165
pkg_name = 't_pkg'
@@ -2178,6 +2177,22 @@ def test_package_without_a_main(self):
21782177
"'t_pkg.t_main' is a package and cannot be directly executed",
21792178
stdout)
21802179

2180+
def test_nonexistent_module(self):
2181+
assert not os.path.exists(os_helper.TESTFN)
2182+
stdout, stderr = self._run_pdb(["-m", os_helper.TESTFN], "", expected_returncode=1)
2183+
self.assertIn(f"ImportError: No module named {os_helper.TESTFN}", stdout)
2184+
2185+
def test_dir_as_script(self):
2186+
with os_helper.temp_dir() as temp_dir:
2187+
stdout, stderr = self._run_pdb([temp_dir], "", expected_returncode=1)
2188+
self.assertIn(f"Error: {temp_dir} is a directory", stdout)
2189+
2190+
def test_invalid_cmd_line_options(self):
2191+
stdout, stderr = self._run_pdb(["-c"], "", expected_returncode=1)
2192+
self.assertIn(f"Error: option -c requires argument", stdout)
2193+
stdout, stderr = self._run_pdb(["--spam"], "", expected_returncode=1)
2194+
self.assertIn(f"Error: option --spam not recognized", stdout)
2195+
21812196
def test_blocks_at_first_code_line(self):
21822197
script = """
21832198
#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)