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

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Lib/pdb.py
Original file line number Diff line number Diff line change
Expand Up @@ -1532,7 +1532,7 @@ def _runmodule(self, module_name):
__main__.__dict__.update({
"__name__": "__main__",
"__file__": self.mainpyfile,
"__package__": module_name,
"__package__": mod_spec.parent,
Copy link
Contributor

Choose a reason for hiding this comment

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

Huh, I initially thought this was wrong, but I'd forgotten that it's runpy._get_module_details that handles the recursive search for pkg.__main__, so mod_spec.parent == module_name in those cases.

"__loader__": mod_spec.loader,
"__spec__": mod_spec,
"__builtins__": __builtins__,
Expand Down
6 changes: 3 additions & 3 deletions Lib/test/test_pdb.py
Original file line number Diff line number Diff line change
Expand Up @@ -1417,7 +1417,7 @@ def test_blocks_at_first_code_line(self):
def test_relative_imports(self):
Copy link
Contributor

Choose a reason for hiding this comment

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

I think we do want to explicitly test both paths through runpy._get_module_details here, as otherwise we're exposed to an internal refactoring in runpy breaking the above assumption about how executable packages are handled.

Alternatively, you could add some unit tests for runpy._get_module_details directly to Lib/test/test_runpy.py that ensure that this behaviour is retained (that approach would also be a good foundation for potentially converting this to a public API in 3.8 that not only returns the current module details, but also the values to use to update the module namespace prior to execution)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I've duplicated the test (added one for the plain modules situation).
If you find it appropriate I can add a helper function to create those "scripts" as it is quite duplicated and change the existing code to use it.

Additionally, if you find interesting the idea of making _get_module_details public I can file an issue and draft something out, as it will be useful for the other PRs on running as module

self.module_name = 't_main'
support.rmtree(self.module_name)
main_file = self.module_name + '/__main__.py'
main_file = self.module_name + '/runme.py'
init_file = self.module_name + '/__init__.py'
module_file = self.module_name + '/module.py'
self.addCleanup(support.rmtree, self.module_name)
Expand Down Expand Up @@ -1446,8 +1446,8 @@ def test_relative_imports(self):
p module.var2
quit
"""
stdout, _ = self._run_pdb(['-m', self.module_name], commands)
self.assertTrue(any("VAR from module" in l for l in stdout.splitlines()))
stdout, _ = self._run_pdb(['-m', self.module_name + '.runme'], commands)
self.assertTrue(any("VAR from module" in l for l in stdout.splitlines()), stdout)
self.assertTrue(any("VAR from top" in l for l in stdout.splitlines()))
self.assertTrue(any("second var" in l for l in stdout.splitlines()))

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Use mod_spec.parent when running modules with pdb