55import  subprocess 
66import  sys 
77import  unittest 
8+ from  functools  import  partial 
89from  textwrap  import  dedent 
910from  test  import  support 
1011from  test .support  import  (
2728    raise  unittest .SkipTest ("test module requires subprocess" )
2829
2930
30- def  spawn_repl (* args , stdout = subprocess .PIPE , stderr = subprocess .STDOUT , ** kw ):
31+ def  spawn_repl (* args , stdout = subprocess .PIPE , stderr = subprocess .STDOUT , custom = False ,  ** kw ):
3132    """Run the Python REPL with the given arguments. 
3233
3334    kw is extra keyword args to pass to subprocess.Popen. Returns a Popen 
@@ -41,7 +42,11 @@ def spawn_repl(*args, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, **kw):
4142    # path may be used by Py_GetPath() to build the default module search 
4243    # path. 
4344    stdin_fname  =  os .path .join (os .path .dirname (sys .executable ), "<stdin>" )
44-     cmd_line  =  [stdin_fname , '-I' , '-i' ]
45+     cmd_line  =  [stdin_fname , '-I' ]
46+     # Don't re-run the built-in REPL from interactive mode 
47+     # if we're testing a custom REPL (such as the asyncio REPL). 
48+     if  not  custom :
49+         cmd_line .append ('-i' )
4550    cmd_line .extend (args )
4651
4752    # Set TERM=vt100, for the rationale see the comments in spawn_python() of 
@@ -55,6 +60,10 @@ def spawn_repl(*args, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, **kw):
5560                            stdout = stdout , stderr = stderr ,
5661                            ** kw )
5762
63+ 
64+ spawn_asyncio_repl  =  partial (spawn_repl , "-m" , "asyncio" , custom = True )
65+ 
66+ 
5867def  run_on_interactive_mode (source ):
5968    """Spawn a new Python interpreter, pass the given 
6069    input source code from the stdin and return the 
@@ -359,7 +368,7 @@ def f():
359368class  TestAsyncioREPL (unittest .TestCase ):
360369    def  test_multiple_statements_fail_early (self ):
361370        user_input  =  "1 / 0; print(f'afterwards: {1+1}')" 
362-         p  =  spawn_repl ( "-m" ,  "asyncio" )
371+         p  =  spawn_asyncio_repl ( )
363372        p .stdin .write (user_input )
364373        output  =  kill_python (p )
365374        self .assertIn ("ZeroDivisionError" , output )
@@ -371,7 +380,7 @@ def test_toplevel_contextvars_sync(self):
371380        var = ContextVar("var", default="failed") 
372381        var.set("ok") 
373382        """ )
374-         p  =  spawn_repl ( "-m" ,  "asyncio" )
383+         p  =  spawn_asyncio_repl ( )
375384        p .stdin .write (user_input )
376385        user_input2  =  dedent (""" 
377386        print(f"toplevel contextvar test: {var.get()}") 
@@ -387,7 +396,7 @@ def test_toplevel_contextvars_async(self):
387396         from contextvars import ContextVar
388397        var = ContextVar('var', default='failed') 
389398        """ )
390-         p  =  spawn_repl ( "-m" ,  "asyncio" )
399+         p  =  spawn_asyncio_repl ( )
391400        p .stdin .write (user_input )
392401        user_input2  =  "async def set_var(): var.set('ok')\n " 
393402        p .stdin .write (user_input2 )
0 commit comments