Skip to content

bpo-44461: Fix bug with pdb's handling of import error due to a packa… #26937

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
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
1 change: 1 addition & 0 deletions Lib/bdb.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ def __init__(self, skip=None):
self.breaks = {}
self.fncache = {}
self.frame_returning = None
self.botframe = None

self._load_breaks()

Expand Down
2 changes: 2 additions & 0 deletions Lib/pdb.py
Original file line number Diff line number Diff line change
Expand Up @@ -1728,6 +1728,8 @@ def main():
print("Running 'cont' or 'step' will restart the program")
t = sys.exc_info()[2]
pdb.interaction(None, t)
if pdb._user_requested_quit:
break
Comment on lines +1731 to +1732
Copy link
Member

Choose a reason for hiding this comment

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

After experimenting, I understand the motivation for this change here, but I also worry that it might affect other workflows we haven't considered. Could there be other use-cases where a different Exception occurred and pdb._user_requested_quit is True, but we would want that to fall through (not break)?

Copy link
Member Author

Choose a reason for hiding this comment

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

Good question. self._user_requested_quit will be True only when we get quit()/exit() or EOF. ISTM that it's safe to exit in both cases.

I'm pretty sure I've had to kill pdb processes in the past, when they stopped responding to quit(), so I would guess it's more likely that there are other code paths that would benefit from this change.

print("Post mortem debugger finished. The " + mainpyfile +
" will be restarted")

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 @@ -1695,6 +1695,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)
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