Skip to content

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

Merged
merged 12 commits into from
Jan 6, 2018
Merged

Conversation

mariocj89
Copy link
Contributor

@mariocj89 mariocj89 commented Dec 7, 2017

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

$ ./python -m pdb -m unittest
> /mnt/d/linux/cpython/Lib/unittest/__main__.py(3)<module>()
-> import sys
(Pdb) c
----------------------------------------------------------------------
Ran 0 tests in 0.000s
OK
The program exited via sys.exit(). Exit status: False
> /mnt/d/linux/cpython/Lib/unittest/__main__.py(3)<module>()
-> import sys
(Pdb)  

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

mariocj89 and others added 6 commits December 3, 2017 13:31
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`.
mariocj89 and others added 3 commits December 13, 2017 09:35
The test is validating where pdb starts therefore the part that is
failing (checking modulename "/" file) is not relevant on that test
Copy link
Contributor

@ncoghlan ncoghlan left a 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).

@@ -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.
Copy link
Contributor

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.

@@ -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
Copy link
Contributor

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
Copy link
Contributor

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.

@@ -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"""
Copy link
Contributor

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.

Copy link
Contributor Author

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.

@bedevere-bot
Copy link

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. I will then notify any core developers who have left a review that you're ready for them to take another look at this pull request.

@mariocj89
Copy link
Contributor Author

I have made the requested changes; please review again

@bedevere-bot
Copy link

Thanks for making the requested changes!

@ncoghlan: please review the changes made to this pull request.

@mariocj89
Copy link
Contributor Author

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.

@ncoghlan ncoghlan merged commit 9f1e5f1 into python:master Jan 6, 2018
@ncoghlan
Copy link
Contributor

ncoghlan commented Jan 6, 2018

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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants