@@ -2509,6 +2509,26 @@ def complete_shell(self, text, line, begidx, endidx):
25092509 index_dict = {1 : self .shell_cmd_complete }
25102510 return self .index_based_complete (text , line , begidx , endidx , index_dict , self .path_complete )
25112511
2512+ @staticmethod
2513+ def _reset_sys () -> None :
2514+ """
2515+ Resets the dynamic objects in the sys modules that the py and ipy consoles fight over
2516+ This makes it so the py console doesn't end up looking like the ipy console in terms of
2517+ prompt and exception text. That happens if a user runs py after being in an IPython console.
2518+
2519+ """
2520+ # Delete any prompts set by the interactive consoles
2521+ attributes = ['ps1' , 'ps2' , 'ps3' ]
2522+ for cur_attr in attributes :
2523+ try :
2524+ del sys .__dict__ [cur_attr ]
2525+ except KeyError :
2526+ pass
2527+
2528+ # Reset functions
2529+ sys .displayhook = sys .__displayhook__
2530+ sys .excepthook = sys .__excepthook__
2531+
25122532 def do_py (self , arg ):
25132533 """
25142534 Invoke python command, shell, or script
@@ -2620,10 +2640,13 @@ def quit():
26202640 interp .runcode ("import readline" )
26212641 interp .runcode ("readline.set_completer(Completer(locals()).complete)" )
26222642
2623- cprt = 'Type "help", "copyright", "credits" or "license" for more information.'
2643+ # Set up sys for the console
2644+ self ._reset_sys ()
26242645 keepstate = Statekeeper (sys , ('stdin' , 'stdout' ))
26252646 sys .stdout = self .stdout
26262647 sys .stdin = self .stdin
2648+
2649+ cprt = 'Type "help", "copyright", "credits" or "license" for more information.'
26272650 docstr = self .do_py .__doc__ .replace ('pyscript_name' , self .pyscript_name )
26282651
26292652 try :
@@ -2715,6 +2738,7 @@ def do_ipy(self, arg):
27152738 End with ``Ctrl-D`` (Unix) / ``Ctrl-Z`` (Windows), ``quit()``, '`exit()``.
27162739 """
27172740 from .pyscript_bridge import PyscriptBridge
2741+ self ._reset_sys ()
27182742 bridge = PyscriptBridge (self )
27192743
27202744 if self .locals_in_py :
0 commit comments