-
-
Notifications
You must be signed in to change notification settings - Fork 31.9k
bpo-32206: Pdb can now run modules #4752
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
Conversation
Since PEP 338 we can run python modules as a script via `python -m module_name` but there is no way to run pdb on those. The proposal is to add a new argument "-m" to the pdb module to allow users to run `python -m pdb -m my_module_name`.
The test is validating where pdb starts therefore the part that is failing (checking modulename "/" file) is not relevant on that test
5acd330
to
7a1494a
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The basic approach looks reasonable to me (I'd missed that we could cheat and use private APIs to get around the fact that the public runpy
API can't actually handle the pdb
use case properly).
Just a few minor comments inline, plus a potentially larger request related to the structure of the test cases.
It would also be good to add a What's New entry (in the standard library updates section - existing entries should give a reasonable sense of the expected style).
Doc/library/pdb.rst
Outdated
@@ -61,6 +61,11 @@ useful than quitting the debugger upon program's exit. | |||
:file:`pdb.py` now accepts a ``-c`` option that executes commands as if given | |||
in a :file:`.pdbrc` file, see :ref:`debugger-commands`. | |||
|
|||
.. versionadded:: 3.7 | |||
:file:`pdb.py` now accepts a ``-m`` option that execute modules similar to how | |||
``python3 -m`` does. The debugger will stop in the first line like with a script. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Slight rephrasing of the second sentence here: As with a script, the debugger will pause execution just before the first line of the module.
Doc/library/pdb.rst
Outdated
@@ -61,6 +61,11 @@ useful than quitting the debugger upon program's exit. | |||
:file:`pdb.py` now accepts a ``-c`` option that executes commands as if given | |||
in a :file:`.pdbrc` file, see :ref:`debugger-commands`. | |||
|
|||
.. versionadded:: 3.7 | |||
:file:`pdb.py` now accepts a ``-m`` option that execute modules similar to how |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Replace "how" with "the way"
Lib/pdb.py
Outdated
__main__.__dict__.update({ | ||
"__name__": "__main__", | ||
"__file__": self.mainpyfile, | ||
# "__package__": module_name, # Not needed, will rely on __spec__.parent |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Even though it's not always needed these days, runpy
still promises to set __package__
, so this should set it too.
Lib/test/test_pdb.py
Outdated
@@ -1123,9 +1130,136 @@ def tearDown(self): | |||
support.unlink(support.TESTFN) | |||
|
|||
|
|||
class PdbModuleTestCase(PdbBaseTestCase): | |||
"""Re-runs all tests used for a script but using a module""" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Our preferred structure for this kind of testing is to put the actual test methods in a base class that isn't a unittest.TestCase
subclass (so we only write them once and structurally ensure that they're consistent across test scenarios), and then add unittest.TestCase
as a mixin in the subclasses.
Having two copies of the test methods is significantly less desirable, as that makes it easier for the test cases to diverge over time.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There is actually not that much being shared between the two test cases other than the "run module".
I guess you were referring to something like this
When I was going through the work I realized there is not that many tests in that test case that made sense running with both module/script. Most "basic" unit tests are covered in the doctests. If you want I can try to rework the whole suite (moving the doctests?) and the do the mixing thing. otherwise I've added a commit that just adds the new helper function to run a module and left the tests in the same suite.
A Python core developer has requested some changes be made to your pull request before we can consider merging it. If you could please address their requests along with any other requests in other reviews from core developers that would be appreciated. Once you have made the requested changes, please leave a comment on this pull request containing the phrase |
I have made the requested changes; please review again |
Thanks for making the requested changes! @ncoghlan: please review the changes made to this pull request. |
Thanks a lot for the review. There is still a quesion on how to structure the tests, I cannot see a clear solution other than the one I did in d77dc87 as there is not that much shareable atm. |
Flattening the test cases back into a single class works for me - it was the three-class structure without any shared test cases that looked odd. Thanks for the patch! |
This change allows pdb to run modules and not only script.
This is quite useful when working on "runnable" modules as there is no other way to run them at the moment.
Example running unittest as a module
There is an alternative implementation using runpy._run_module_as_main by applying:
mariocj89@e00c7b2 (It still has some issues to fix)
https://bugs.python.org/issue32206