@@ -1534,11 +1534,16 @@ def lookupmodule(self, filename):
1534
1534
return fullname
1535
1535
return None
1536
1536
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' ):
1538
1544
self ._wait_for_mainpyfile = True
1539
1545
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
1542
1547
self .mainpyfile = self .canonic (code .co_filename )
1543
1548
import __main__
1544
1549
__main__ .__dict__ .clear ()
@@ -1552,7 +1557,7 @@ def _runmodule(self, module_name):
1552
1557
})
1553
1558
self .run (code )
1554
1559
1555
- def _runscript (self , filename ):
1560
+ def _runscript (self , filename : 'ScriptTarget' ):
1556
1561
# The script has to run in __main__ namespace (or imports from
1557
1562
# __main__ will break).
1558
1563
#
@@ -1665,6 +1670,34 @@ def help():
1665
1670
To let the script run up to a given line X in the debugged file, use
1666
1671
"-c 'until X'"."""
1667
1672
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
10000
span>.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
+
1668
1701
def main ():
1669
1702
import getopt
1670
1703
@@ -1674,28 +1707,19 @@ def main():
1674
1707
print (_usage )
1675
1708
sys .exit (2 )
1676
1709
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 ()
1692
1713
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' ]]
1694
1715
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
1699
1723
1700
1724
# Note on saving/restoring sys.argv: it's a good idea when sys.argv was
1701
1725
# modified by the script being debugged. It's a bad idea when it was
@@ -1705,15 +1729,12 @@ def main():
1705
1729
pdb .rcLines .extend (commands )
1706
1730
while True :
1707
1731
try :
1708
- if run_as_module :
1709
- pdb ._runmodule (mainpyfile )
1710
- else :
1711
- pdb ._runscript (mainpyfile )
1732
+ pdb ._run (target )
1712
1733
if pdb ._user_requested_quit :
1713
1734
break
1714
1735
print ("The program finished and will be restarted" )
1715
1736
except Restart :
1716
- print ("Restarting" , mainpyfile , "with arguments:" )
1737
+ print ("Restarting" , target , "with arguments:" )
1717
1738
print ("\t " + " " .join (sys .argv [1 :]))
1718
1739
except SystemExit :
1719
1740
# In most cases SystemExit does not warrant a post-mortem session.
@@ -1728,7 +1749,7 @@ def main():
1728
1749
print ("Running 'cont' or 'step' will restart the program" )
1729
1750
t = sys .exc_info ()[2 ]
1730
1751
pdb .interaction (None , t )
1731
- print ("Post mortem debugger finished. The " + mainpyfile +
1752
+ print ("Post mortem debugger finished. The " + target +
1732
1753
" will be restarted" )
1733
1754
1734
1755
0 commit comments