mirror of
https://github.com/xonsh/xonsh.git
synced 2025-03-06 17:30:59 +01:00
prelime enter_macro()
This commit is contained in:
parent
80c72a8348
commit
61879bab5f
1 changed files with 44 additions and 26 deletions
|
@ -842,32 +842,6 @@ def call_macro(f, raw_args, glbs, locs):
|
||||||
return rtn
|
return rtn
|
||||||
|
|
||||||
|
|
||||||
def enter_macro(obj, raw_block, glbs, locs):
|
|
||||||
"""Prepares to enter a context manager macro by attaching the contents
|
|
||||||
of the macro block, globals, and locals to the object. These modifications
|
|
||||||
are made in-place and the original object is returned.
|
|
||||||
|
|
||||||
|
|
||||||
Parameters
|
|
||||||
----------
|
|
||||||
obj : context manager
|
|
||||||
The object that is about to be entered via a with-statement.
|
|
||||||
raw_block : str
|
|
||||||
The str of the block that is the context body.
|
|
||||||
This string will be parsed, compiled, evaled, or left as
|
|
||||||
a string dependending on the return annotation of obj.__enter__.
|
|
||||||
glbs : Mapping
|
|
||||||
The globals from the context site.
|
|
||||||
locs : Mapping or None
|
|
||||||
The locals from the context site.
|
|
||||||
|
|
||||||
Returns
|
|
||||||
-------
|
|
||||||
obj : context manager
|
|
||||||
The same context manager but with the new macro information applied.
|
|
||||||
"""
|
|
||||||
return obj
|
|
||||||
|
|
||||||
@lazyobject
|
@lazyobject
|
||||||
def KWARG_RE():
|
def KWARG_RE():
|
||||||
return re.compile('([A-Za-z_]\w*=|\*\*)')
|
return re.compile('([A-Za-z_]\w*=|\*\*)')
|
||||||
|
@ -900,6 +874,50 @@ def _eval_regular_args(raw_args, glbs, locs):
|
||||||
return args, kwargs
|
return args, kwargs
|
||||||
|
|
||||||
|
|
||||||
|
def enter_macro(obj, raw_block, glbs, locs):
|
||||||
|
"""Prepares to enter a context manager macro by attaching the contents
|
||||||
|
of the macro block, globals, and locals to the object. These modifications
|
||||||
|
are made in-place and the original object is returned.
|
||||||
|
|
||||||
|
|
||||||
|
Parameters
|
||||||
|
----------
|
||||||
|
obj : context manager
|
||||||
|
The object that is about to be entered via a with-statement.
|
||||||
|
raw_block : str
|
||||||
|
The str of the block that is the context body.
|
||||||
|
This string will be parsed, compiled, evaled, or left as
|
||||||
|
a string dependending on the return annotation of obj.__enter__.
|
||||||
|
glbs : Mapping
|
||||||
|
The globals from the context site.
|
||||||
|
locs : Mapping or None
|
||||||
|
The locals from the context site.
|
||||||
|
|
||||||
|
Returns
|
||||||
|
-------
|
||||||
|
obj : context manager
|
||||||
|
The same context manager but with the new macro information applied.
|
||||||
|
"""
|
||||||
|
# find kind from return annotation, default to str
|
||||||
|
enter = getattr(obj, '__enter__', None)
|
||||||
|
if callable(enter):
|
||||||
|
sig = inspect.signature(enter)
|
||||||
|
kind = sig.return_annotation
|
||||||
|
if kind is inspect.Parameter.empty or kind is None:
|
||||||
|
kind = str
|
||||||
|
else:
|
||||||
|
kind = str
|
||||||
|
# convert block as needed
|
||||||
|
macroname = getattr(obj, '__name__', '<context>')
|
||||||
|
block = convert_macro_arg(raw_block, kind, glbs, locs, name='<with!>',
|
||||||
|
macroname=macroname)
|
||||||
|
# attach attrs
|
||||||
|
obj.macro_globals = glbs
|
||||||
|
obj.macro_locals = locs
|
||||||
|
obj.macro_block = block
|
||||||
|
return obj
|
||||||
|
|
||||||
|
|
||||||
def load_builtins(execer=None, config=None, login=False, ctx=None):
|
def load_builtins(execer=None, config=None, login=False, ctx=None):
|
||||||
"""Loads the xonsh builtins into the Python builtins. Sets the
|
"""Loads the xonsh builtins into the Python builtins. Sets the
|
||||||
BUILTINS_LOADED variable to True.
|
BUILTINS_LOADED variable to True.
|
||||||
|
|
Loading…
Add table
Reference in a new issue