Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
from xonsh.execer import Execer
from xonsh.proc import HiddenCommandPipeline
from xonsh.jobs import ignore_sigtstp
from xonsh.tools import setup_win_unicode_console, print_color, to_bool_or_int
from xonsh.platform import HAS_PYGMENTS, ON_WINDOWS
from xonsh.codecache import run_script_with_cache, run_code_with_cache
from xonsh.xonfig import print_welcome_screen
from xonsh.xontribs import xontribs_load
from xonsh.lazyimps import pygments, pyghooks
from xonsh.imphooks import install_import_hooks
from xonsh.events import events
from xonsh.environ import xonshrc_context, make_args_env
from xonsh.built_ins import XonshSession, load_builtins
events.transmogrify("on_post_init", "LoadEvent")
events.doc(
"on_post_init",
"""
on_post_init() -> None
Fired after all initialization is finished and we're ready to do work.
NOTE: This is fired before the wizard is automatically started.
""",
)
events.transmogrify("on_exit", "LoadEvent")
events.doc(
"on_exit",
"""
on_exit() -> None
execer = Execer(
xonsh_ctx=ctx,
debug_level=debug,
scriptcache=shell_kwargs.get("scriptcache", True),
cacheall=shell_kwargs.get("cacheall", False),
)
events.on_timingprobe.fire(name="post_execer_init")
# load rc files
login = shell_kwargs.get("login", True)
env = builtins.__xonsh__.env
rc = shell_kwargs.get("rc", None)
rc = env.get("XONSHRC") if rc is None else rc
if args.mode != XonshMode.interactive and not args.force_interactive:
# Don't load xonshrc if not interactive shell
rc = None
events.on_pre_rc.fire()
xonshrc_context(rcfiles=rc, execer=execer, ctx=ctx, env=env, login=login)
events.on_post_rc.fire()
# create shell
builtins.__xonsh__.shell = Shell(execer=execer, **shell_kwargs)
ctx["__name__"] = "__main__"
return env
def default(self, line):
"""Implements code execution."""
line = line if line.endswith("\n") else line + "\n"
src, code = self.push(line)
if code is None:
return
events.on_precommand.fire(cmd=src)
env = builtins.__xonsh_env__
hist = builtins.__xonsh_history__ # pylint: disable=no-member
ts1 = None
enc = env.get("XONSH_ENCODING")
err = env.get("XONSH_ENCODING_ERRORS")
tee = Tee(encoding=enc, errors=err)
try:
ts0 = time.time()
run_compiled_code(code, self.ctx, None, "single")
ts1 = time.time()
if hist is not None and hist.last_cmd_rtn is None:
hist.last_cmd_rtn = 0 # returncode for success
except XonshError as e:
print(e.args[0], file=sys.stderr)
if hist is not None and hist.last_cmd_rtn is None:
debug_level=debug,
scriptcache=shell_kwargs.get("scriptcache", True),
cacheall=shell_kwargs.get("cacheall", False),
)
events.on_timingprobe.fire(name="post_execer_init")
# load rc files
login = shell_kwargs.get("login", True)
env = builtins.__xonsh__.env
rc = shell_kwargs.get("rc", None)
rc = env.get("XONSHRC") if rc is None else rc
if args.mode != XonshMode.interactive and not args.force_interactive:
# Don't load xonshrc if not interactive shell
rc = None
events.on_pre_rc.fire()
xonshrc_context(rcfiles=rc, execer=execer, ctx=ctx, env=env, login=login)
events.on_post_rc.fire()
# create shell
builtins.__xonsh__.shell = Shell(execer=execer, **shell_kwargs)
ctx["__name__"] = "__main__"
return env
def exec_module(self, module):
"""Executes the module in its own namespace."""
events.on_import_pre_exec_module.fire(module=module)
rtn = self.loader.exec_module(module)
events.on_import_post_exec_module.fire(module=module)
return rtn
pass
else:
# OK PWD is really gone.
msg = "{UNDERLINE_INTENSE_WHITE}{BACKGROUND_INTENSE_BLACK}"
msg += "xonsh: working directory does not exist: " + pwd
msg += "{NO_COLOR}"
self.print_color(msg, file=sys.stderr)
elif "PWD" not in env:
# $PWD is missing from env, recreate it
env["PWD"] = cwd
elif os.path.realpath(cwd) != os.path.realpath(env["PWD"]):
# The working directory has changed without updating $PWD, fix this
old = env["PWD"]
env["PWD"] = cwd
env["OLDPWD"] = old
events.on_chdir.fire(olddir=old, newdir=cwd)
def _post_run_event_fire(self, name, proc):
event_name = "on_post_spec_run_" + name
if events.exists(event_name):
event = getattr(events, event_name)
event.fire(spec=self, proc=proc)
NOTE: All the caveats of the ``atexit`` module also apply to this event.
""",
)
events.transmogrify("on_pre_rc", "LoadEvent")
events.doc(
"on_pre_rc",
"""
on_pre_rc() -> None
Fired just before rc files are loaded, if they are.
""",
)
events.transmogrify("on_post_rc", "LoadEvent")
events.doc(
"on_post_rc",
"""
on_post_rc() -> None
Fired just after rc files are loaded, if they are.
""",
)
def get_setproctitle():
"""Proxy function for loading process title"""
try:
from setproctitle import setproctitle as spt
except ImportError:
return
}
if builtins.__xonsh__.env.get("COLOR_INPUT"):
style = style_from_pygments_dict(DEFAULT_STYLE_DICT)
prompt_args["style"] = style
style_overrides_env = env.get("PTK_STYLE_OVERRIDES")
if style_overrides_env:
try:
style_overrides = Style.from_dict(style_overrides_env)
prompt_args["style"] = merge_styles([style, style_overrides])
except (AttributeError, TypeError, ValueError):
print_exception()
line = self.prompter.prompt(**prompt_args)
events.on_post_prompt.fire()
return line
""",
)
events.transmogrify("on_post_cmdloop", "LoadEvent")
events.doc(
"on_post_cmdloop",
"""
on_post_cmdloop() -> None
Fired just after the command loop finishes, if it is.
NOTE: All the caveats of the ``atexit`` module also apply to this event.
""",
)
events.transmogrify("on_pre_rc", "LoadEvent")
events.doc(
"on_pre_rc",
"""
on_pre_rc() -> None
Fired just before rc files are loaded, if they are.
""",
)
events.transmogrify("on_post_rc", "LoadEvent")
events.doc(
"on_post_rc",
"""
on_post_rc() -> None
Fired just after rc files are loaded, if they are.