@@ -1785,13 +1785,30 @@ def _run_pdb(self, pdb_args, commands, expected_returncode=0):
1785
1785
)
1786
1786
return stdout , stderr
1787
1787
1788
- def run_pdb_script (self , script , commands , expected_returncode = 0 ):
1788
+ def run_pdb_script (self , script , commands ,
1789
+ expected_returncode = 0 ,
1790
+ pdbrc = None ,
1791
+ remove_home = False ):
1789
1792
"""Run 'script' lines with pdb and the pdb 'commands'."""
1790
1793
filename = 'main.py'
1791
1794
with open (filename , 'w' ) as f :
1792
1795
f .write (textwrap .dedent (script ))
1796
+
1797
+ if pdbrc is not None :
1798
+ with open ('.pdbrc' , 'w' ) as f :
1799
+ f .write (textwrap .dedent (pdbrc ))
1800
+ self .addCleanup (os_helper .unlink , '.pdbrc' )
1793
1801
self .addCleanup (os_helper .unlink , filename )
1794
- return self ._run_pdb ([filename ], commands , expected_returncode )
1802
+
1803
+ homesave = None
1804
+ if remove_home :
1805
+ homesave = os .environ .pop ('HOME' , None )
1806
+ try :
1807
+ stdout , stderr = self ._run_pdb ([filename ], commands , expected_returncode )
1808
+ finally :
1809
+ if homesave is not None :
1810
+ os .environ ['HOME' ] = homesave
1811
+ return stdout , stderr
1795
1812
1796
1813
def run_pdb_module (self , script , commands ):
1797
1814
"""Runs the script code as part of a module"""
@@ -2031,37 +2048,80 @@ def test_issue26053(self):
2031
2048
self .assertRegex (res , "Restarting .* with arguments:\n a b c" )
2032
2049
self .assertRegex (res , "Restarting .* with arguments:\n d e f" )
2033
2050
2034
- def test_readrc_kwarg (self ):
2051
+ def test_pdbrc_basic (self ):
2035
2052
script = textwrap .dedent ("""
2036
- import pdb; pdb.Pdb(readrc=False).set_trace()
2053
+ a = 1
2054
+ b = 2
2055
+ """ )
2037
2056
2038
- print('hello')
2057
+ pdbrc = textwrap .dedent ("""
2058
+ # Comments should be fine
2059
+ n
2060
+ p f"{a+8=}"
2039
2061
""" )
2040
2062
2041
- save_home = os .environ .pop ('HOME' , None )
2042
- try :
2043
- with os_helper .temp_cwd ():
2044
- with open ('.pdbrc' , 'w' ) as f :
2045
- f .write ("invalid\n " )
2046
-
2047
- with open ('main.py' , 'w' ) as f :
2048
- f .write (script )
2049
-
2050
- cmd = [sys .executable , 'main.py' ]
2051
- proc = subprocess .Popen (
2052
- cmd ,
2053
- stdout = subprocess .PIPE ,
2054
- stdin = subprocess .PIPE ,
2055
- stderr = subprocess .PIPE ,
2056
- )
2057
- with proc :
2058
- stdout , stderr = proc .communicate (b'q\n ' )
2059
- self .assertNotIn (b"NameError: name 'invalid' is not defined" ,
2060
- stdout )
2063
+ stdout , stderr = self .run_pdb_script (script , 'q\n ' , pdbrc = pdbrc , remove_home = True )
2064
+ self .assertIn ("a+8=9" , stdout )
2061
2065
2062
- finally :
2063
- if save_home is not None :
2064
- os .environ ['HOME' ] = save_home
2066
+ def test_pdbrc_alias (self ):
2067
+ script = textwrap .dedent ("""
2068
+ class A:
2069
+ def __init__(self):
2070
+ self.attr = 1
2071
+ a = A()
2072
+ b = 2
2073
+ """ )
2074
+
2075
+ pdbrc = textwrap .dedent ("""
2076
+ alias pi for k in %1.__dict__.keys(): print(f"%1.{k} = {%1.__dict__[k]}")
2077
+ until 6
2078
+ pi a
2079
+ """ )
2080
+
2081
+ stdout , stderr = self .run_pdb_script (script , 'q\n ' , pdbrc = pdbrc , remove_home = True )
2082
+ self .assertIn ("a.attr = 1" , stdout )
2083
+
2084
+ def test_pdbrc_semicolon (self ):
2085
+ script = textwrap .dedent ("""
2086
+ class A:
2087
+ def __init__(self):
2088
+ self.attr = 1
2089
+ a = A()
2090
+ b = 2
2091
+ """ )
2092
+
2093
+ pdbrc = textwrap .dedent ("""
2094
+ b 5;;c;;n
2095
+ """ )
2096
+
2097
+ stdout , stderr = self .run_pdb_script (script , 'q\n ' , pdbrc = pdbrc , remove_home = True )
2098
+ self .assertIn ("-> b = 2" , stdout )
2099
+
2100
+ def test_pdbrc_commands (self ):
2101
+ script = textwrap .dedent ("""
2102
+ class A:
2103
+ def __init__(self):
2104
+ self.attr = 1
2105
+ a = A()
2106
+ b = 2
2107
+ """ )
2108
+
2109
+ pdbrc = textwrap .dedent ("""
2110
+ b 6
2111
+ commands 1 ;; p a;; end
2112
+ c
2113
+ """ )
2114
+
2115
+ stdout , stderr = self .run_pdb_script (script , 'q\n ' , pdbrc = pdbrc , remove_home = True )
2116
+ self .assertIn ("<__main__.A object at" , stdout )
2117
+
2118
+ def test_readrc_kwarg (self ):
2119
+ script = textwrap .dedent ("""
2120
+ print('hello')
2121
+ """ )
2122
+
2123
+ stdout , stderr = self .run_pdb_script (script , 'q\n ' , pdbrc = 'invalid' , remove_home = True )
2124
+ self .assertIn ("NameError: name 'invalid' is not defined" , stdout )
2065
2125
2066
2126
def test_readrc_homedir (self ):
2067
2127
save_home = os .environ .pop ("HOME" , None )
@@ -2076,40 +2136,6 @@ def test_readrc_homedir(self):
2076
2136
if save_home is not None :
2077
2137
os .environ ["HOME" ] = save_home
2078
2138
2079
- def test_read_pdbrc_with_ascii_encoding (self ):
2080
- script = textwrap .dedent ("""
2081
- import pdb; pdb.Pdb().set_trace()
2082
- print('hello')
2083
- """ )
2084
- save_home = os .environ .pop ('HOME' , None )
2085
- try :
2086
- with os_helper .temp_cwd ():
2087
- with open ('.pdbrc' , 'w' , encoding = 'utf-8' ) as f :
2088
- f .write ("Fran\u00E7 ais" )
2089
-
2090
- with open ('main.py' , 'w' , encoding = 'utf-8' ) as f :
2091
- f .write (script )
2092
-
2093
- cmd = [sys .executable , 'main.py' ]
2094
- env = {'PYTHONIOENCODING' : 'ascii' }
2095
- if sys .platform == 'win32' :
2096
- env ['PYTHONLEGACYWINDOWSSTDIO' ] = 'non-empty-string'
2097
- proc = subprocess .Popen (
2098
- cmd ,
2099
- stdout = subprocess .PIPE ,
2100
- stdin = subprocess .PIPE ,
2101
- stderr = subprocess .PIPE ,
2102
- env = {** os .environ , ** env }
2103
- )
2104
- with proc :
2105
- stdout , stderr = proc .communicate (b'c\n ' )
2106
- self .assertIn (b"UnicodeEncodeError: \' ascii\' codec can\' t encode character "
2107
- b"\' \\ xe7\' in position 21: ordinal not in range(128)" , stderr )
2108
-
2109
- finally :
2110
- if save_home is not None :
2111
- os .environ ['HOME' ] = save_home
2112
-
2113
2139
def test_header (self ):
2114
2140
stdout = StringIO ()
2115
2141
header = 'Nobody expects... blah, blah, blah'
0 commit comments