Skip to content

Commit ee03bad

Browse files
jaracoiritkatrielblurb-it[bot]
authored
bpo-44461: Check early that a pdb target is valid for execution. (#27227)
* bpo-44461: Fix bug with pdb's handling of import error due to a package which does not have a __main__ module * 📜🤖 Added by blurb_it. * remove "else" Co-authored-by: Jason R. Coombs <[email protected]> * If running as a module, first check that it can run as a module. Alternate fix for bpo-44461. Co-authored-by: Irit Katriel <[email protected]> Co-authored-by: blurb-it[bot] <43283697+blurb-it[bot]@users.noreply.github.com> Co-authored-by: Irit Katriel <[email protected]>
1 parent 38ddc8b commit ee03bad

File tree

3 files changed

+20
-1
lines changed

3 files changed

+20
-1
lines changed

Lib/pdb.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,11 @@ def code(self):
169169

170170
class ModuleTarget(str):
171171
def check(self):
172-
pass
172+
try:
173+
self._details
174+
except Exception:
175+
traceback.print_exc()
176+
sys.exit(1)
173177

174178
@functools.cached_property
175179
def _details(self):

Lib/test/test_pdb.py

+14
Original file line numberDiff line numberDiff line change
@@ -1729,6 +1729,20 @@ def test_module_without_a_main(self):
17291729
self.assertIn("ImportError: No module named t_main.__main__",
17301730
stdout.splitlines())
17311731

1732+
def test_package_without_a_main(self):
1733+
pkg_name = 't_pkg'
1734+
module_name = 't_main'
1735+
os_helper.rmtree(pkg_name)
1736+
modpath = pkg_name + '/' + module_name
1737+
os.makedirs(modpath)
1738+
with open(modpath + '/__init__.py', 'w') as f:
1739+
pass
1740+
self.addCleanup(os_helper.rmtree, pkg_name)
1741+
stdout, stderr = self._run_pdb(['-m', modpath.replace('/', '.')], "")
1742+
self.assertIn(
1743+
"'t_pkg.t_main' is a package and cannot be directly executed",
1744+
stdout)
1745+
17321746
def test_blocks_at_first_code_line(self):
17331747
script = """
17341748
#This is a comment, on line 2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix bug with :mod:`pdb`'s handling of import error due to a package which does not have a ``__main__`` module

0 commit comments

Comments
 (0)