@@ -1936,13 +1936,30 @@ def _run_pdb(self, pdb_args, commands, expected_returncode=0):
1936
1936
)
1937
1937
return stdout , stderr
1938
1938
1939
- def run_pdb_script (self , script , commands , expected_returncode = 0 ):
1939
+ def run_pdb_script (self , script , commands ,
1940
+ expected_returncode = 0 ,
1941
+ pdbrc = None ,
1942
+ remove_home = False ):
1940
1943
"""Run 'script' lines with pdb and the pdb 'commands'."""
1941
1944
filename = 'main.py'
1942
1945
with open (filename , 'w' ) as f :
1943
1946
f .write (textwrap .dedent (script ))
1947
+
1948
+ if pdbrc is not None :
1949
+ with open ('.pdbrc' , 'w' ) as f :
1950
+ f .write (textwrap .dedent (pdbrc ))
1951
+ self .addCleanup (os_helper .unlink , '.pdbrc' )
1944
1952
self .addCleanup (os_helper .unlink , filename )
1945
- return self ._run_pdb ([filename ], commands , expected_returncode )
1953
+
1954
+ homesave = None
1955
+ if remove_home :
1956
+ homesave = os .environ .pop ('HOME' , None )
1957
+ try :
1958
+ stdout , stderr = self ._run_pdb ([filename ], commands , expected_returncode )
1959
+ finally :
1960
+ if homesave is not None :
1961
+ os .environ ['HOME' ] = homesave
1962
+ return stdout , stderr
1946
1963
1947
1964
def run_pdb_module (self , script , commands ):
1948
1965
"""Runs the script code as part of a module"""
@@ -2182,37 +2199,80 @@ def test_issue26053(self):
2182
2199
self .assertRegex (res , "Restarting .* with arguments:\n a b c" )
2183
2200
self .assertRegex (res , "Restarting .* with arguments:\n d e f" )
2184
2201
2185
- def test_readrc_kwarg (self ):
2202
+ def test_pdbrc_basic (self ):
2186
2203
script = textwrap .dedent ("""
2187
- import pdb; pdb.Pdb(readrc=False).set_trace()
2204
+ a = 1
2205
+ b = 2
2206
+ """ )
2188
2207
2189
- print('hello')
2208
+ pdbrc = textwrap .dedent ("""
2209
+ # Comments should be fine
2210
+ n
2211
+ p f"{a+8=}"
2190
2212
""" )
2191
2213
2192
- save_home = os .environ .pop ('HOME' , None )
2193
- try :
2194
- with os_helper .temp_cwd ():
2195
- with open ('.pdbrc' , 'w' ) as f :
2196
- f .write ("invalid\n " )
2197
-
2198
- with open ('main.py' , 'w' ) as f :
2199
- f .write (script )
2200
-
2201
- cmd = [sys .executable , 'main.py' ]
2202
- proc = subprocess .Popen (
2203
- cmd ,
2204
- stdout = subprocess .PIPE ,
2205
- stdin = subprocess .PIPE ,
2206
- stderr = subprocess .PIPE ,
2207
- )
2208
- with proc :
2209
- stdout , stderr = proc .communicate (b'q\n ' )
2210
- self .assertNotIn (b"NameError: name 'invalid' is not defined" ,
2211
- stdout )
2214
+ stdout , stderr = self .run_pdb_script (script , 'q\n ' , pdbrc = pdbrc , remove_home = True )
2215
+ self .assertIn ("a+8=9" , stdout )
2212
2216
2213
- finally :
2214
- if save_home is not None :
2215
- os .environ ['HOME' ] = save_home
2217
+ def test_pdbrc_alias (self ):
2218
+ script = textwrap .dedent ("""
2219
+ class A:
2220
+ def __init__(self):
2221
+ self.attr = 1
2222
+ a = A()
2223
+ b = 2
2224
+ """ )
2225
+
2226
+ pdbrc = textwrap .dedent ("""
2227
+ alias pi for k in %1.__dict__.keys(): print(f"%1.{k} = {%1.__dict__[k]}")
2228
+ until 6
2229
+ pi a
2230
+ """ )
2231
+
2232
+ stdout , stderr = self .run_pdb_script (script , 'q\n ' , pdbrc = pdbrc , remove_home = True )
2233
+ self .assertIn ("a.attr = 1" , stdout )
2234
+
2235
+ def test_pdbrc_semicolon (self ):
2236
+ script = textwrap .dedent ("""
2237
+ class A:
2238
+ def __init__(self):
2239
+ self.attr = 1
2240
+ a = A()
2241
+ b = 2
2242
+ """ )
2243
+
2244
+ pdbrc = textwrap .dedent ("""
2245
+ b 5;;c;;n
2246
+ """ )
2247
+
2248
+ stdout , stderr = self .run_pdb_script (script , 'q\n ' , pdbrc = pdbrc , remove_home = True )
2249
+ self .assertIn ("-> b = 2" , stdout )
2250
+
2251
+ def test_pdbrc_commands (self ):
2252
+ script = textwrap .dedent ("""
2253
+ class A:
2254
+ def __init__(self):
2255
+ self.attr = 1
2256
+ a = A()
2257
+ b = 2
2258
+ """ )
2259
+
2260
+ pdbrc = textwrap .dedent ("""
2261
+ b 6
2262
+ commands 1 ;; p a;; end
2263
+ c
2264
+ """ )
2265
+
2266
+ stdout , stderr = self .run_pdb_script (script , 'q\n ' , pdbrc = pdbrc , remove_home = True )
2267
+ self .assertIn ("<__main__.A object at" , stdout )
2268
+
2269
+ def test_readrc_kwarg (self ):
2270
+ script = textwrap .dedent ("""
2271
+ print('hello')
2272
+ """ )
2273
+
2274
+ stdout , stderr = self .run_pdb_script (script , 'q\n ' , pdbrc = 'invalid' , remove_home = True )
2275
+ self .assertIn ("NameError: name 'invalid' is not defined" , stdout )
2216
2276
2217
2277
def test_readrc_homedir (self ):
2218
2278
save_home = os .environ .pop ("HOME" , None )
@@ -2227,40 +2287,6 @@ def test_readrc_homedir(self):
2227
2287
if save_home is not None :
2228
2288
os .environ ["HOME" ] = save_home
2229
2289
2230
- def test_read_pdbrc_with_ascii_encoding (self ):
2231
- script = textwrap .dedent ("""
2232
- import pdb; pdb.Pdb().set_trace()
2233
- print('hello')
2234
- """ )
2235
- save_home = os .environ .pop ('HOME' , None )
2236
- try :
2237
- with os_helper .temp_cwd ():
2238
- with open ('.pdbrc' , 'w' , encoding = 'utf-8' ) as f :
2239
- f .write ("Fran\u00E7 ais" )
2240
-
2241
- with open ('main.py' , 'w' , encoding = 'utf-8' ) as f :
2242
- f .write (script )
2243
-
2244
- cmd = [sys .executable , 'main.py' ]
2245
- env = {'PYTHONIOENCODING' : 'ascii' }
2246
- if sys .platform == 'win32' :
2247
- env ['PYTHONLEGACYWINDOWSSTDIO' ] = 'non-empty-string'
2248
- proc = subprocess .Popen (
2249
- cmd ,
2250
- stdout = subprocess .PIPE ,
2251
- stdin = subprocess .PIPE ,
2252
- stderr = subprocess .PIPE ,
2253
- env = {** os .environ , ** env }
2254
- )
2255
- with proc :
2256
- stdout , stderr = proc .communicate (b'c\n ' )
2257
- self .assertIn (b"UnicodeEncodeError: \' ascii\' codec can\' t encode character "
2258
- b"\' \\ xe7\' in position 21: ordinal not in range(128)" , stderr )
2259
-
2260
- finally :
2261
- if save_home is not None :
2262
- os .environ ['HOME' ] = save_home
2263
-
2264
2290
def test_header (self ):
2265
2291
stdout = StringIO ()
2266
2292
header = 'Nobody expects... blah, blah, blah'
0 commit comments