8000 Refactor module/script handling to share an interface (check method). · python/cpython@59b5906 · GitHub
[go: up one dir, main page]

Skip to content

Commit 59b5906

Browse files
committed
Refactor module/script handling to share an interface (check method).
1 parent 2560c61 commit 59b5906

File tree

1 file changed

+51
-30
lines changed

1 file changed

+51
-30
lines changed

Lib/pdb.py

Lines changed: 51 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1534,11 +1534,16 @@ def lookupmodule(self, filename):
15341534
return fullname
15351535
return None
15361536

1537-
def _runmodule(self, module_name):
1537+
def _run(self, target):
1538+
if isinstance(target, ModuleTarget):
1539+
return self._runmodule(target)
1540+
elif isinstance(target, ScriptTarget):
1541+
return self._runscript(target)
1542+
1543+
def _runmodule(self, target: 'ModuleTarget'):
15381544
self._wait_for_mainpyfile = True
15391545
self._user_requested_quit = False
1540-
import runpy
1541-
mod_name, mod_spec, code = runpy._get_module_details(module_name)
1546+
mod_name, mod_spec, code = target.module_details
15421547
self.mainpyfile = self.canonic(code.co_filename)
15431548
import __main__
15441549
__main__.__dict__.clear()
@@ -1552,7 +1557,7 @@ def _runmodule(self, module_name):
15521557
})
15531558
self.run(code)
15541559

1555-
def _runscript(self, filename):
1560+
def _runscript(self, filename: 'ScriptTarget'):
15561561
# The script has to run in __main__ namespace (or imports from
15571562
# __main__ will break).
15581563
#
@@ -1665,6 +1670,34 @@ def help():
16651670
To let the script run up to a given line X in the debugged file, use
16661671
"-c 'until X'"."""
16671672

1673+
1674+
class ScriptTarget(str):
1675+
def __new__(cls, val):
1676+
res = super().__new__(cls, os.path.realpath(val))
1677+
res.orig = val
1678+
return res
1679+
1680+
def check(self):
1681+
if not os.path.exists(self):
1682+
print('Error:', self.orig, 'does not exist')
1683+
sys.exit(1)
1684+
1685+
# Replace pdb's dir with script's dir in front of module search path.
1686+
sys.path[0] = os.path.dirname(self)
1687+
1688+
1689+
class ModuleTarget(str):
1690+
def check(self):
1691+
pass
1692+
1693+
@property
1694+
def module_details(self):
1695+
if not hasattr(self, '_details'):
1696+
import runpy
1697+
self._details = runpy._get_module_details(self)
1698+
return self._details
1699+
1700+
16681701
def main():
16691702
import getopt
16701703

@@ -1674,28 +1707,19 @@ def main():
16741707
print(_usage)
16751708
sys.exit(2)
16761709

1677-
commands = []
1678-
run_as_module = False
1679-
for opt, optarg in opts:
1680-
if opt in ['-h', '--help']:
1681-
print(_usage)
1682-
sys.exit()
1683-
elif opt in ['-c', '--command']:
1684-
commands.append(optarg)
1685-
elif opt in ['-m']:
1686-
run_as_module = True
1687-
1688-
mainpyfile = args[0] # Get script filename
1689-
if not run_as_module and not os.path.exists(mainpyfile):
1690-
print('Error:', mainpyfile, 'does not exist')
1691-
sys.exit(1)
1710+
if any(opt in ['-h', '--help'] for opt, optarg in opts):
1711+
print(_usage)
1712+
sys.exit()
16921713

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

1695-
if not run_as_module:
1696-
mainpyfile = os.path.realpath(mainpyfile)
1697-
# Replace pdb's dir with script's dir in front of module search path.
1698-
sys.path[0] = os.path.dirname(mainpyfile)
1716+
module_indicated = any(opt in ['-m'] for opt, optarg in opts)
1717+
cls = ModuleTarget if module_indicated else ScriptTarget
1718+
target = cls(args[0])
1719+
1720+
target.check()
1721+
1722+
sys.argv[:] = args # Hide "pdb.py" and pdb options from argument list
16991723

17001724
# Note on saving/restoring sys.argv: it's a good idea when sys.argv was
17011725
# modified by the script being debugged. It's a bad idea when it was
@@ -1705,15 +1729,12 @@ def main():
17051729
pdb.rcLines.extend(commands)
17061730
while True:
17071731
try:
1708-
if run_as_module:
1709-
pdb._runmodule(mainpyfile)
1710-
else:
1711-
pdb._runscript(mainpyfile)
1732+
pdb._run(target)
17121733
if pdb._user_requested_quit:
17131734
break
17141735
print("The program finished and will be restarted")
17151736
except Restart:
1716-
print("Restarting", mainpyfile, "with arguments:")
1737+
print("Restarting", target, "with arguments:")
17171738
print("\t" + " ".join(sys.argv[1:]))
17181739
except SystemExit:
17191740
# In most cases SystemExit does not warrant a post-mortem session.
@@ -1728,7 +1749,7 @@ def main():
17281749
print("Running 'cont' or 'step' will restart the program")
17291750
t = sys.exc_info()[2]
17301751
pdb.interaction(None, t)
1731-
print("Post mortem debugger finished. The " + mainpyfile +
1752+
print("Post mortem debugger finished. The " + target +
17321753
" will be restarted")
17331754

17341755

0 commit comments

Comments
 (0)
0