Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork32.4k
gh-118500: Add pdb support for zipapp#118501
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Uh oh!
There was an error while loading.Please reload this page.
Changes fromall commits
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -687,6 +687,9 @@ pdb | ||
command line option or :envvar:`PYTHONSAFEPATH` environment variable). | ||
(Contributed by Tian Gao and Christian Walther in :gh:`111762`.) | ||
* :mod:`zipapp` is supported as a debugging target. | ||
iritkatriel marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
(Contributed by Tian Gao in :gh:`118501`.) | ||
queue | ||
----- | ||
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -120,7 +120,10 @@ def find_function(funcname, filename): | ||
try: | ||
fp = tokenize.open(filename) | ||
except OSError: | ||
lines = linecache.getlines(filename) | ||
if not lines: | ||
return None | ||
fp = io.StringIO(''.join(lines)) | ||
funcdef = "" | ||
funcstart = None | ||
# consumer of this info expects the first line to be 1 | ||
@@ -237,6 +240,44 @@ def namespace(self): | ||
) | ||
class _ZipTarget(_ExecutableTarget): | ||
def __init__(self, target): | ||
import runpy | ||
self._target = os.path.realpath(target) | ||
sys.path.insert(0, self._target) | ||
iritkatriel marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
try: | ||
_, self._spec, self._code = runpy._get_main_module_details() | ||
except ImportError as e: | ||
print(f"ImportError: {e}") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. Why do you need to special-case ImportError here? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. That's from#108791. This is a very common case but the exception traceback would be very large because it's generated rather deeply and it distracts users. It would be a better experience to simply show that there's an import error when the user passes in an invalid module. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. Ok. there is also traceback.format_exception_only(). There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. Yeah I think the orignal thought here was just to suppress the common import error, and leave the others be. In that case we need a special case for | ||
sys.exit(1) | ||
except Exception: | ||
traceback.print_exc() | ||
sys.exit(1) | ||
def __repr__(self): | ||
return self._target | ||
@property | ||
def filename(self): | ||
return self._code.co_filename | ||
@property | ||
def code(self): | ||
return self._code | ||
@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__, | ||
) | ||
class _PdbInteractiveConsole(code.InteractiveConsole): | ||
def __init__(self, ns, message): | ||
self._message = message | ||
@@ -1076,7 +1117,7 @@ def lineinfo(self, identifier): | ||
if f: | ||
fname = f | ||
item = parts[1] | ||
answer = find_function(item,self.canonic(fname)) | ||
return answer or failed | ||
def checkline(self, filename, lineno): | ||
@@ -2276,7 +2317,10 @@ def main(): | ||
if not opts.args: | ||
parser.error("no module or script to run") | ||
file = opts.args.pop(0) | ||
if file.endswith('.pyz'): | ||
target = _ZipTarget(file) | ||
else: | ||
target = _ScriptTarget(file) | ||
sys.argv[:] = [file] + opts.args # Hide "pdb.py" and pdb options from argument list | ||
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Add :mod:`pdb` support for zipapps | ||
iritkatriel marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. |