8000 [3.12] gh-108791: Fix pdb CLI invalid argument handling (GH-108816) (… · python/cpython@68a5640 · GitHub
[go: up one dir, main page]

Skip to content

Commit 68a5640

Browse files
authored
[3.12] gh-108791: Fix pdb CLI invalid argument handling (GH-108816) (#111064)
* [3.12] 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 40db34c commit 68a5640

File tree

3 files changed

+24
-2
lines changed

3 files changed

+24
-2
lines changed

Lib/pdb.py

Lines changed: 6 additions & 0 deletions
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

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2301,8 +2301,7 @@ def test_module_without_a_main(self):
23012301
stdout, stderr = self._run_pdb(
23022302
['-m', module_name], "", expected_returncode=1
23032303
)
2304-
self.assertIn("ImportError: No module named t_main.__main__",
2305-
stdout.splitlines())
2304+
self.assertIn("ImportError: No module named t_main.__main__;", stdout)
23062305

23072306
def test_package_without_a_main(self):
23082307
pkg_name = 't_pkg'
@@ -2320,6 +2319,22 @@ def test_package_without_a_main(self):
23202319
"'t_pkg.t_main' is a package and cannot be directly executed",
23212320
stdout)
23222321

2322+
def test_nonexistent_module(self):
2323+
assert not os.path.exists(os_helper.TESTFN)
2324+
stdout, stderr = self._run_pdb(["-m", os_helper.TESTFN], "", expected_returncode=1)
2325+
self.assertIn(f"ImportError: No module named {os_helper.TESTFN}", stdout)
2326+
2327+
def test_dir_as_script(self):
2328+
with os_helper.temp_dir() as temp_dir:
2329+
stdout, stderr = self._run_pdb([temp_dir], "", expected_returncode=1)
2330+
self.assertIn(f"Error: {temp_dir} is a directory", stdout)
2331+
2332+
def test_invalid_cmd_line_options(self):
2333+
stdout, stderr = self._run_pdb(["-c"], "", expected_returncode=1)
2334+
self.assertIn(f"Error: option -c requires argument", stdout)
2335+
stdout, stderr = self._run_pdb(["--spam"], "", expected_returncode=1)
2336+
self.assertIn(f"Error: option --spam not recognized", stdout)
2337+
23232338
def test_blocks_at_first_code_line(self):
23242339
script = """
23252340
#This is a comment, on line 2
Lines changed: 1 addition & 0 deletions
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)
0