Skip to content

Refactor module/script handling to share an interface. #16

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 1 commit 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: 0 additions & 1 deletion Lib/bdb.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ def __init__(self, skip=None):
self.breaks = {}
self.fncache = {}
self.frame_returning = None
self.botframe = None

self._load_breaks()

Expand Down
96 changes: 64 additions & 32 deletions Lib/pdb.py
Original file line number Diff line number Diff line change
Expand Up @@ -1534,11 +1534,16 @@ def lookupmodule(self, filename):
return fullname
return None

def _run(self, target):
if isinstance(target, ModuleTarget):
return self._runmodule(target)
elif isinstance(target, ScriptTarget):
return self._runscript(target)

def _runmodule(self, module_name):
self._wait_for_mainpyfile = True
self._user_requested_quit = False
import runpy
mod_name, mod_spec, code = runpy._get_module_details(module_name)
mod_name, mod_spec, code = module_name.module_details
self.mainpyfile = self.canonic(code.co_filename)
import __main__
__main__.__dict__.clear()
Expand Down Expand Up @@ -1665,6 +1670,51 @@ def help():
To let the script run up to a given line X in the debugged file, use
"-c 'until X'"."""


class RunTarget(str):
@staticmethod
def choose(args, opts):
module_indicated = any(opt in ['-m'] for opt, optarg in opts)
cls = ModuleTarget if module_indicated else ScriptTarget
return cls(args[0])

def check(self):
"""
Check that the target is suitable for execution.
"""


class ScriptTarget(RunTarget):
def __new__(cls, val):
res = super().__new__(cls, os.path.realpath(val))
res.orig = val
return res

def check(self):
if not os.path.exists(self):
print('Error:', self.orig, 'does not exist')
sys.exit(1)

# Replace pdb's dir with script's dir in front of module search path.
sys.path[0] = os.path.dirname(self)


class ModuleTarget(RunTarget):
def check(self):
try:
self.module_details
except ImportError:
traceback.print_exc()
sys.exit(1)
Copy link
Owner

Choose a reason for hiding this comment

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

Is this what we currently do if import fails?

Copy link
Author

Choose a reason for hiding this comment

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

This change is comparable to what ScriptTarget does when the file doesn't exist. To retain the current behavior, the body of this check would be pass. By adding this check early, it allows the main routine to detect if a module is invalid for running, allowing main to bail early for an unrunnable target.

Copy link
Author

Choose a reason for hiding this comment

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

This change has the other advantage of not dropping into interactive mode when the indicated module is unrunnable, comparable to a non-existent file.

Copy link
Owner

Choose a reason for hiding this comment

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

Right, my point is this is not a refactor, it's new behaviour.

Copy link
Author

Choose a reason for hiding this comment

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

Oh, good point. I could perform the refactor as a separate commit. Let me know if that's something you'd like to see.

Copy link
Owner

Choose a reason for hiding this comment

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

Possibly, though I would be careful with it - pdb invokes code in several different way (from command line, from the pdb prompt, from Pdb subclasses that various debuggers implement). I'd need to spend more time before I'm convinced that moving error detection around is safe (and the tests don't tell the whole story).

pdb has not been very well maintained in recent years. Xavier de Gaye and Daniel Hahler used to work on it and left some abandoned PRs. I tried to reach out to them to continue the work but they are not responding anymore. I think some their PRs should be updated and merged, they both knew what they were doing.

I've also been trying to go through the backlog of known bugs, and got some fixed, but it's always a problem to find someone to review. For instance this one is quite simple and it's open: python#26656

Copy link
Author

Choose a reason for hiding this comment

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

Good to know that pdb is in need of some love. If you wish to loop me in, I'll see what I can do to help. I don't have any spare bandwidth, but I expect to be able to review them eventually. I took a look at the one you mentioned and found it's already been addressed. Thanks for all your help.


@property
def module_details(self):
if not hasattr(self, '_details'):
import runpy
self._details = runpy._get_module_details(self)
return self._details


def main():
import getopt

Expand All @@ -1674,28 +1724,16 @@ def main():
print(_usage)
sys.exit(2)

commands = []
run_as_module = False
for opt, optarg in opts:
if opt in ['-h', '--help']:
print(_usage)
sys.exit()
elif opt in ['-c', '--command']:
commands.append(optarg)
elif opt in ['-m']:
run_as_module = True

mainpyfile = args[0] # Get script filename
if not run_as_module and not os.path.exists(mainpyfile):
print('Error:', mainpyfile, 'does not exist')
sys.exit(1)
if any(opt in ['-h', '--help'] for opt, optarg in opts):
print(_usage)
sys.exit()

sys.argv[:] = args # Hide "pdb.py" and pdb options from argument list
commands = [optarg for opt, optarg in opts if opt in ['-c', '--command']]

if not run_as_module:
mainpyfile = os.path.realpath(mainpyfile)
# Replace pdb's dir with script's dir in front of module search path.
sys.path[0] = os.path.dirname(mainpyfile)
target = RunTarget.choose(args, opts)
target.check()

sys.argv[:] = args # Hide "pdb.py" and pdb options from argument list

# Note on saving/restoring sys.argv: it's a good idea when sys.argv was
# modified by the script being debugged. It's a bad idea when it was
Expand All @@ -1705,15 +1743,12 @@ def main():
pdb.rcLines.extend(commands)
while True:
try:
if run_as_module:
pdb._runmodule(mainpyfile)
else:
pdb._runscript(mainpyfile)
pdb._run(target)
if pdb._user_requested_quit:
break
print("The program finished and will be restarted")
except Restart:
print("Restarting", mainpyfile, "with arguments:")
print("Restarting", target, "with arguments:")
print("\t" + " ".join(sys.argv[1:]))
except SystemExit:
# In most cases SystemExit does not warrant a post-mortem session.
Expand All @@ -1728,11 +1763,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
else:
print("Post mortem debugger finished. The " + mainpyfile +
" will be restarted")
print("Post mortem debugger finished. The " + target +
" will be restarted")


# When invoked as main program, invoke the debugger on a script
Expand Down