8000 bpo-44554: refactor pdb targets (and internal tweaks) by jaraco · Pull Request #26992 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

bpo-44554: refactor pdb tar 8000 gets (and internal tweaks) #26992

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 14 commits into from
Jul 19, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Replace singledispatch _run with a unified implementation selecting o…
…n properties of the Target object.
  • Loading branch information
jaraco committed Jul 18, 2021
commit fe8aea5ae98c57cb98573b3dd0a04cb4fc3d12b0
98 changes: 59 additions & 39 deletions Lib/pdb.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@
import traceback
import linecache

from typing import Union


class Restart(Exception):
"""Causes a debugger to be restarted for the debugged python program."""
Expand Down Expand Up @@ -147,6 +149,23 @@ def check(self):
# Replace pdb's dir with script's dir in front of module search path.
sys.path[0] = os.path.dirname(self)

@property
def filename(self):
return self

@property
def namespace(self):
return dict(
__name__='__main__',
__file__=self,
__builtins__=__builtins__,
)

@property
def code(self):
with io.open(self) as fp:
return f"exec(compile({fp.read()!r}, {self!r}, 'exec'))"


class ModuleTarget(str):
def check(self):
Expand All @@ -157,6 +176,31 @@ def details(self):
import runpy
return runpy._get_module_details(self)

@property
def filename(self):
return self.code.co_filename

@property
def code(self):
name, spec, code = self.details
return code

@property
def spec(self):
name, spec, code = self.details
return spec

@property
def namespace(self):
return dict(
__name__='__main__',
__file__=os.path.normcase(os.path.abspath(self.filename)),
__package__=self.spec.parent,
__loader__=self.spec.loader,
__spec__=self.spec,
__builtins__=__builtins__,
)


# Interaction prompt line will separate file and call info from code
# text using value of line_prefix string. A newline and arrow may
Expand Down Expand Up @@ -1564,50 +1608,26 @@ def lookupmodule(self, filename):
return fullname
return None

@functools.singledispatchmethod
def _run(self, target: 'ModuleTarget'):
def _run(self, target: Union[ModuleTarget, ScriptTarget]):
# When bdb sets tracing, a number of call and line events happen
# BEFORE debugger even reaches user's code (and the exact sequence of
# events depends on python version). Take special measures to
# avoid stopping before reaching the main script (see user_line and
# user_call for details).
self._wait_for_mainpyfile = True
self._user_requested_quit = False
mod_name, mod_spec, code = target.details
self.mainpyfile = self.canonic(code.co_filename)
import __main__
__main__.__dict__.clear()
__main__.__dict__.update({
"__name__": "__main__",
"__file__": self.mainpyfile,
"__package__": mod_spec.parent,
"__loader__": mod_spec.loader,
"__spec__": mod_spec,
"__builtins__": __builtins__,
})
self.run(code)

@_run.register
def _(self, filename: 'ScriptTarget'):
# The script has to run in __main__ namespace (or imports from
# __main__ will break).
#
# So we clear up the __main__ and set several special variables
# (this gets rid of pdb's globals and cleans old variables on restarts).

self.mainpyfile = self.canonic(target.filename)

# The target has to run in __main__ namespace (or imports from
# __main__ will break). Clear __main__ and replace with
# the target namespace.
import __main__
__main__.__dict__.clear()
__main__.__dict__.update({"__name__" : "__main__",
"__file__" : filename,
"__builtins__": __builtins__,
})
__main__.__dict__.update(target.namespace)

self.run(target.code)

# When bdb sets tracing, a number of call and line events happens
# BEFORE debugger even reaches user's code (and the exact sequence of
# events depends on python version). So we take special measures to
# avoid stopping before we reach the main script (see user_line and
# user_call for details).
self._wait_for_mainpyfile = True
self.mainpyfile = self.canonic(filename)
self._user_requested_quit = False
with io.open_code(filename) as fp:
statement = "exec(compile(%r, %r, 'exec'))" % \
(fp.read(), self.mainpyfile)
self.run(statement)

# Collect all command help into docstring, if not run with -OO

Expand Down
2 changes: 1 addition & 1 deletion Lib/test/test_pdb.py
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,7 @@ def test_pdb_breakpoints_preserved_across_interactive_sessions():
1 breakpoint keep yes at ...test_pdb.py:...
2 breakpoint keep yes at ...test_pdb.py:...
(Pdb) break pdb.find_function
Breakpoint 3 at ...pdb.py:95
Breakpoint 3 at ...pdb.py:97
(Pdb) break
Num Type Disp Enb Where
1 breakpoint keep yes at ...test_pdb.py:...
Expand Down
0