def __init__(self, target_cwd): report_time("Before MainBackend") MainBackend.__init__(self) report_time("After MainBackend") global _backend _backend = self self._ini = None self._object_info_tweakers = [] self._import_handlers = {} self._input_queue = queue.Queue() self._source_preprocessors = [] self._ast_postprocessors = [] self._main_dir = os.path.dirname(sys.modules["thonny"].__file__) self._heap = { } # WeakValueDictionary would be better, but can't store reference to None self._source_info_by_frame = {} site.sethelper() # otherwise help function is not available self._install_fake_streams() self._install_repl_helper() self._current_executor = None self._io_level = 0 self._tty_mode = True self._tcl = None update_system_path(os.environ, get_augmented_system_path(get_exe_dirs())) # clean __main__ global scope for key in list(__main__.__dict__.keys()): if not key.startswith("__") or key in {"__file__", "__cached__"}: del __main__.__dict__[key] # unset __doc__, then exec dares to write doc of the script there __main__.__doc__ = None logger.info("Loading plugins") report_time("Before loading plugins") execute_with_frontend_sys_path(self._load_plugins) report_time("After loading plugins") # preceding code was run in the directory containing thonny module, now switch to provided try: os.chdir(os.path.expanduser(target_cwd)) except OSError: try: os.chdir(os.path.expanduser("~")) except OSError: os.chdir("/") # yes, this works also in Windows # ... and replace current-dir path item # start in shell mode (may be later switched to script mode) # required in shell mode and also required to overwrite thonny location dir sys.path[0] = "" sys.argv[:] = [""] # empty "script name" if os.name == "nt": self._install_signal_handler()
def get_environment_with_overrides(overrides): env = os.environ.copy() for key in overrides: if overrides[key] is None and key in env: del env[key] else: assert isinstance(overrides[key], str) if key.upper() == "PATH": update_system_path(env, overrides[key]) else: env[key] = overrides[key] return env