Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit4e2caf2

Browse files
gh-118500: Add pdb support for zipapp (#118501)
1 parente54b0c8 commit4e2caf2

File tree

5 files changed

+77
-4
lines changed

5 files changed

+77
-4
lines changed

‎Doc/whatsnew/3.13.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -705,6 +705,9 @@ pdb
705705
command line option or:envvar:`PYTHONSAFEPATH` environment variable).
706706
(Contributed by Tian Gao and Christian Walther in:gh:`111762`.)
707707

708+
*:mod:`zipapp` is supported as a debugging target.
709+
(Contributed by Tian Gao in:gh:`118501`.)
710+
708711
queue
709712
-----
710713

‎Lib/pdb.py

Lines changed: 47 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,10 @@ def find_function(funcname, filename):
120120
try:
121121
fp=tokenize.open(filename)
122122
exceptOSError:
123-
returnNone
123+
lines=linecache.getlines(filename)
124+
ifnotlines:
125+
returnNone
126+
fp=io.StringIO(''.join(lines))
124127
funcdef=""
125128
funcstart=None
126129
# consumer of this info expects the first line to be 1
@@ -237,6 +240,44 @@ def namespace(self):
237240
)
238241

239242

243+
class_ZipTarget(_ExecutableTarget):
244+
def__init__(self,target):
245+
importrunpy
246+
247+
self._target=os.path.realpath(target)
248+
sys.path.insert(0,self._target)
249+
try:
250+
_,self._spec,self._code=runpy._get_main_module_details()
251+
exceptImportErrorase:
252+
print(f"ImportError:{e}")
253+
sys.exit(1)
254+
exceptException:
255+
traceback.print_exc()
256+
sys.exit(1)
257+
258+
def__repr__(self):
259+
returnself._target
260+
261+
@property
262+
deffilename(self):
263+
returnself._code.co_filename
264+
265+
@property
266+
defcode(self):
267+
returnself._code
268+
269+
@property
270+
defnamespace(self):
271+
returndict(
272+
__name__='__main__',
273+
__file__=os.path.normcase(os.path.abspath(self.filename)),
274+
__package__=self._spec.parent,
275+
__loader__=self._spec.loader,
276+
__spec__=self._spec,
277+
__builtins__=__builtins__,
278+
)
279+
280+
240281
class_PdbInteractiveConsole(code.InteractiveConsole):
241282
def__init__(self,ns,message):
242283
self._message=message
@@ -1076,7 +1117,7 @@ def lineinfo(self, identifier):
10761117
iff:
10771118
fname=f
10781119
item=parts[1]
1079-
answer=find_function(item,fname)
1120+
answer=find_function(item,self.canonic(fname))
10801121
returnanswerorfailed
10811122

10821123
defcheckline(self,filename,lineno):
@@ -2282,7 +2323,10 @@ def main():
22822323
ifnotopts.args:
22832324
parser.error("no module or script to run")
22842325
file=opts.args.pop(0)
2285-
target=_ScriptTarget(file)
2326+
iffile.endswith('.pyz'):
2327+
target=_ZipTarget(file)
2328+
else:
2329+
target=_ScriptTarget(file)
22862330

22872331
sys.argv[:]= [file]+opts.args# Hide "pdb.py" and pdb options from argument list
22882332

‎Lib/test/test_pdb.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
importsubprocess
1111
importtextwrap
1212
importlinecache
13+
importzipapp
1314

1415
fromcontextlibimportExitStack,redirect_stdout
1516
fromioimportStringIO
@@ -3532,6 +3533,30 @@ def test_non_utf8_encoding(self):
35323533
iffilename.endswith(".py"):
35333534
self._run_pdb([os.path.join(script_dir,filename)],'q')
35343535

3536+
deftest_zipapp(self):
3537+
withos_helper.temp_dir()astemp_dir:
3538+
os.mkdir(os.path.join(temp_dir,'source'))
3539+
script=textwrap.dedent(
3540+
"""
3541+
def f(x):
3542+
return x + 1
3543+
f(21 + 21)
3544+
"""
3545+
)
3546+
withopen(os.path.join(temp_dir,'source','__main__.py'),'w')asf:
3547+
f.write(script)
3548+
zipapp.create_archive(os.path.join(temp_dir,'source'),
3549+
os.path.join(temp_dir,'zipapp.pyz'))
3550+
stdout,_=self._run_pdb([os.path.join(temp_dir,'zipapp.pyz')],'\n'.join([
3551+
'b f',
3552+
'c',
3553+
'p x',
3554+
'q'
3555+
]))
3556+
self.assertIn('42',stdout)
3557+
self.assertIn('return x + 1',stdout)
3558+
3559+
35353560
classChecklineTests(unittest.TestCase):
35363561
defsetUp(self):
35373562
linecache.clearcache()# Pdb.checkline() uses linecache.getline()

‎Lib/test/test_pyclbr.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ def test_others(self):
226226
cm(
227227
'pdb',
228228
# pyclbr does not handle elegantly `typing` or properties
229-
ignore=('Union','_ModuleTarget','_ScriptTarget'),
229+
ignore=('Union','_ModuleTarget','_ScriptTarget','_ZipTarget'),
230230
)
231231
cm('pydoc',ignore=('input','output',))# properties
232232

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Add:mod:`pdb` support for zipapps

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp