Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
[3.9] bpo-44461: Check early that a pdb target is valid for execution. (
GH-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 <jaraco@jaraco.com>

* If running as a module, first check that it can run as a module. Alternate fix for bpo-44461.

Co-authored-by: Irit Katriel <iritkatriel@yahoo.com>
Co-authored-by: blurb-it[bot] <43283697+blurb-it[bot]@users.noreply.github.com>
Co-authored-by: Irit Katriel <1055913+iritkatriel@users.noreply.github.com>.
(cherry picked from commit ee03bad)

Co-authored-by: Jason R. Coombs <jaraco@jaraco.com>
  • Loading branch information
jaraco committed Jul 28, 2021
commit e1b1784b96e40d03583f7cb16310ca7e738a1eb0
8 changes: 8 additions & 0 deletions Lib/pdb.py
Original file line number Diff line number Diff line change
Expand Up @@ -1694,6 +1694,14 @@ def main():
print('Error:', mainpyfile, 'does not exist')
sys.exit(1)

if run_as_module:
import runpy
try:
runpy._get_module_details(mainpyfile)
except Exception:
traceback.print_exc()
sys.exit(1)

sys.argv[:] = args # Hide "pdb.py" and pdb options from argument list

if not run_as_module:
Expand Down
14 changes: 14 additions & 0 deletions Lib/test/test_pdb.py
Original file line number Diff line number Diff line change
Expand Up @@ -1632,6 +1632,20 @@ def test_module_without_a_main(self):
self.assertIn("ImportError: No module named t_main.__main__",
stdout.splitlines())

def test_package_without_a_main(self):
pkg_name = 't_pkg'
module_name = 't_main'
os_helper.rmtree(pkg_name)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like you're missing an from test.support import os_helper import for this line to work.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking at the header, support was already imported, so I re-used that import.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ugh. Bad idea. Okay, going with your suggestion ;)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggestion doesn't work either. I remember now, between 3.9 and 3.10, test.support was refactored.

modpath = pkg_name + '/' + module_name
os.makedirs(modpath)
with open(modpath + '/__init__.py', 'w') as f:
pass
self.addCleanup(os_helper.rmtree, pkg_name)
stdout, stderr = self._run_pdb(['-m', modpath.replace('/', '.')], "")
self.assertIn(
"'t_pkg.t_main' is a package and cannot be directly executed",
stdout)

def test_blocks_at_first_code_line(self):
script = """
#This is a comment, on line 2
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix bug with :mod:`pdb`'s handling of import error due to a package which does not have a ``__main__`` module