inducer / pudb

Full-screen console debugger for Python
https://documen.tician.de/pudb/
Other
3k stars 230 forks source link

Add -m switch to run as module #390

Closed eliasdorneles closed 4 years ago

eliasdorneles commented 4 years ago

Hello,

Here is an implementation for the -m module switch (see https://github.com/inducer/pudb/issues/389).

Does this look good?

asmeurer commented 4 years ago

If you try to run a module that doesn't exist, you get an error like

Traceback (most recent call last):
  File "/Users/aaronmeurer/Documents/pudb/pudb/__init__.py", line 167, in runscript
    dbg._runmodule(mainpyfile)
  File "/Users/aaronmeurer/Documents/pudb/pudb/debugger.py", line 491, in _runmodule
    mod_name, mod_spec, code = runpy._get_module_details(module_name)
  File "/Users/aaronmeurer/anaconda3/lib/python3.7/runpy.py", line 136, in _get_module_details
    raise error("No module named %s" % mod_name)
ImportError: No module named fdjkslf

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Users/aaronmeurer/anaconda3/lib/python3.7/runpy.py", line 193, in _run_module_as_main
    "__main__", mod_spec)
  File "/Users/aaronmeurer/anaconda3/lib/python3.7/runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "/Users/aaronmeurer/Documents/pudb/pudb/__main__.py", line 31, in <module>
    main()
  File "/Users/aaronmeurer/Documents/pudb/pudb/run.py", line 25, in main
    steal_output=options.steal_output)
  File "/Users/aaronmeurer/Documents/pudb/pudb/__init__.py", line 125, in runmodule
    runscript(*args, **kwargs)
  File "/Users/aaronmeurer/Documents/pudb/pudb/__init__.py", line 176, in runscript
    dbg.interaction(None, sys.exc_info())
  File "/Users/aaronmeurer/Documents/pudb/pudb/debugger.py", line 389, in interaction
    self.stack, index = self.get_shortened_stack(frame, tb)
  File "/Users/aaronmeurer/Documents/pudb/pudb/debugger.py", line 350, in get_shortened_stack
    stack, index = self.get_stack(frame, tb)
  File "/Users/aaronmeurer/anaconda3/lib/python3.7/bdb.py", line 520, in get_stack
    if f is self.botframe:
AttributeError: 'Debugger' object has no attribute 'botframe'

Only the top-level ImportError message should be shown. I think somewhere it needs to catch the ImportError and give up.

asmeurer commented 4 years ago

It's a little disappointing to see that pudb is still using optparse instead of argparse, but I guess that can be fixed in a separate issue.

I noticed that it allows more than one script argument, even though only the first one is processed. This is an issue also in master, so maybe it doesn't need to be fixed here, but when we do we should not allow any script arguments if -m is used (unless we instead want to support debugging one script after the other).

asmeurer commented 4 years ago

Actually, scrap that. It is passing the "second" script as the argv, like it should.

asmeurer commented 4 years ago

For the ImportError, we may want to just print the error message instead showing the full traceback, like how python -m does.

eliasdorneles commented 4 years ago

For the ImportError, we may want to just print the error message instead showing the full traceback, like how python -m does.

Makes sense, will do!

asmeurer commented 4 years ago

Makes sense, will do!

Be sure to test both cases: a module that doesn't exist, and a package that does exist but doesn't have __main__.py.

eliasdorneles commented 4 years ago

Be sure to test both cases: a module that doesn't exist, and a package that does exist but doesn't have __main__.py.

Just did, in both cases it works the same as python -m.

asmeurer commented 4 years ago

I've tested the new code. It seems to work. I noticed that if you pass it -m but give it a file instead of a module, it just executes the file without running the debugger. I'm not sure if this is intentional, although it does seem to match python -m and pdb -m behavior.

eliasdorneles commented 4 years ago

I've tested the new code. It seems to work. I noticed that if you pass it -m but give it a file instead of a module, it just executes the file without running the debugger. I'm not sure if this is intentional, although it does seem to match python -m and pdb -m behavior.

Oh, I hadn't tested that case. Well, it seems okay to leave it like that?

inducer commented 4 years ago

Cool. Thanks @asmeurer for taking another look. In it goes.

inducer commented 4 years ago

And thanks @eliasdorneles for your contribution!

eliasdorneles commented 4 years ago

Awesome, thank you both!

asmeurer commented 4 years ago

Thanks for taking the time to work through the issues here. And I'm also happy to see the command line code now use argparse instead of optparse.

delbao11 commented 3 years ago

do we have the latest release version with this PR merged?

inducer commented 3 years ago

Thanks for the reminder. 2020.1, out now.