The following way is the recommended method to run PYTHONSTARTUP in a console, the type of which inherits from code.InteractiveConsole:
ifpath:=os.getenv('PYTHONSTARTUP'):
withtokenize.open(path) asfile:
sys.audit('cpython.run_startup', path)
exec(compile(file.read(), path, 'exec'), console.locals)but this loads the platform and sys modules into the locals dictionary (readline on non-windows as well), polluting the namespace and making for a strange user experience (e.g. when one prints the contents of globals during debugging or when forgetting to import modules works anyways)
From pythonrc.py:
importplatformimportsysifsys.platform!="win32":
importreadlineoriginal_ps1=">>> "is_wsl="microsoft-standard-WSL"inplatform.release()
classREPLHooks:
def__init__(self):
self.global_exit=Noneself.failure_flag=Falseself.original_excepthook=sys.excepthookself.original_displayhook=sys.displayhooksys.excepthook=self.my_excepthooksys.displayhook=self.my_displayhookdefmy_displayhook(self, value):
ifvalueisNone:
self.failure_flag=Falseself.original_displayhook(value)
defmy_excepthook(self, type_, value, traceback):
self.global_exit=valueself.failure_flag=Trueself.original_excepthook(type_, value, traceback)
defget_last_command():
# Get the last history itemlast_command=""ifsys.platform!="win32":
last_command=readline.get_history_item(readline.get_current_history_length())
returnlast_commandclassPS1:
hooks=REPLHooks()
sys.excepthook=hooks.my_excepthooksys.displayhook=hooks.my_displayhook# str will get called for every prompt with exit code to show success/failuredef__str__(self):
exit_code=int(bool(self.hooks.failure_flag))
self.hooks.failure_flag=False# Guide following official VS Code doc for shell integration sequence:result=""# For non-windows allow recent_command history.ifsys.platform!="win32":
result="{soh}{command_executed}{command_line}{command_finished}{prompt_started}{stx}{prompt}{soh}{command_start}{stx}".format(
soh="\001",
stx="\002",
command_executed="\x1b]633;C\x07",
command_line="\x1b]633;E;"+str(get_last_command()) +"\x07",
command_finished="\x1b]633;D;"+str(exit_code) +"\x07",
prompt_started="\x1b]633;A\x07",
prompt=original_ps1,
command_start="\x1b]633;B\x07",
)
else:
result="{command_finished}{prompt_started}{prompt}{command_start}{command_executed}".format(
command_finished="\x1b]633;D;"+str(exit_code) +"\x07",
prompt_started="\x1b]633;A\x07",
prompt=original_ps1,
command_start="\x1b]633;B\x07",
command_executed="\x1b]633;C\x07",
)
# result = f"{chr(27)}]633;D;{exit_code}{chr(7)}{chr(27)}]633;A{chr(7)}{original_ps1}{chr(27)}]633;B{chr(7)}{chr(27)}]633;C{chr(7)}"returnresultdef__repr__(self):
return"<Custom PS1 for VS Code Python Shell Integration>"ifsys.platform!="win32"and (notis_wsl):
sys.ps1=PS1()
ifsys.platform=="darwin":
print("Cmd click to launch VS Code Native REPL")
else:
print("Ctrl click to launch VS Code Native REPL")I propose to add the following at the end of that file:
delplatform, sysifsys.platform!='win32': delreadline
The following way is the recommended method to run PYTHONSTARTUP in a console, the type of which inherits from
code.InteractiveConsole:but this loads the
platformandsysmodules into the locals dictionary (readlineon non-windows as well), polluting the namespace and making for a strange user experience (e.g. when one prints the contents of globals during debugging or when forgetting to import modules works anyways)From
pythonrc.py:I propose to add the following at the end of that file: