##// END OF EJS Templates
Merge pull request #13213 from Kojoley/fix-bunch-of-doctests...
Merge pull request #13213 from Kojoley/fix-bunch-of-doctests Fix bunch of doctests

File last commit:

r25595:d9c0e690
r26940:32497c8d merge
Show More
inputtransformer.py
536 lines | 17.9 KiB | text/x-python | PythonLexer
/ IPython / core / inputtransformer.py
Thomas Kluyver
Mark inputsplitter & inputtransformer as deprecated
r24177 """DEPRECATED: Input transformer classes to support IPython special syntax.
This module was deprecated in IPython 7.0, in favour of inputtransformer2.
Thomas Kluyver
Standardise some module docstring first lines
r13888
This includes the machinery to recognise and transform ``%magic`` commands,
``!system`` commands, ``help?`` querying, prompt stripping, and so forth.
"""
Thomas Kluyver
First go at new input transformation system
r10090 import abc
Thomas Kluyver
Reorganise InputTransformer decorator architecture.
r10102 import functools
Thomas Kluyver
First go at new input transformation system
r10090 import re
Thomas Kluyver
Drop bundled, outdated copy of the tokenize module
r24179 import tokenize
from tokenize import generate_tokens, untokenize, TokenError
Srinivas Reddy Thatiparthy
remove PY3
r23113 from io import StringIO
Thomas Kluyver
Prototype transformer to assemble logical lines
r10105
Thomas Kluyver
Remove unused imports from IPython.core
r11124 from IPython.core.splitinput import LineInfo
Thomas Kluyver
Update inputsplitter to use new input transformers
r10093
#-----------------------------------------------------------------------------
# Globals
#-----------------------------------------------------------------------------
# The escape sequences that define the syntax transformations IPython will
# apply to user input. These can NOT be just changed here: many regular
# expressions and other parts of the code may use their hardcoded values, and
# for all intents and purposes they constitute the 'IPython syntax', so they
# should be considered fixed.
ESC_SHELL = '!' # Send line to underlying system shell
ESC_SH_CAP = '!!' # Send line to system shell and capture output
ESC_HELP = '?' # Find information about object
ESC_HELP2 = '??' # Find extra-detailed information about object
ESC_MAGIC = '%' # Call magic function
ESC_MAGIC2 = '%%' # Call cell-magic function
ESC_QUOTE = ',' # Split args on whitespace, quote each as string and call
ESC_QUOTE2 = ';' # Quote all args as a single string, call
ESC_PAREN = '/' # Call first argument with rest of line as arguments
ESC_SEQUENCES = [ESC_SHELL, ESC_SH_CAP, ESC_HELP ,\
ESC_HELP2, ESC_MAGIC, ESC_MAGIC2,\
ESC_QUOTE, ESC_QUOTE2, ESC_PAREN ]
Thomas Kluyver
Remove uses of with_metaclass compatibility hack
r23071 class InputTransformer(metaclass=abc.ABCMeta):
Thomas Kluyver
Add docstrings in IPython.core.inputtransformer
r10101 """Abstract base class for line-based input transformers."""
Thomas Kluyver
First go at new input transformation system
r10090
@abc.abstractmethod
def push(self, line):
Thomas Kluyver
Add docstrings in IPython.core.inputtransformer
r10101 """Send a line of input to the transformer, returning the transformed
input or None if the transformer is waiting for more input.
Must be overridden by subclasses.
Volker Braun
Also catch SyntaxErrors from InputTransformers in run_cell()...
r13525
Implementations may raise ``SyntaxError`` if the input is invalid. No
other exceptions may be raised.
Thomas Kluyver
Add docstrings in IPython.core.inputtransformer
r10101 """
Thomas Kluyver
First go at new input transformation system
r10090 pass
@abc.abstractmethod
def reset(self):
Thomas Kluyver
Add docstrings in IPython.core.inputtransformer
r10101 """Return, transformed any lines that the transformer has accumulated,
and reset its internal state.
Must be overridden by subclasses.
"""
Thomas Kluyver
First go at new input transformation system
r10090 pass
Thomas Kluyver
More input transformers
r10091
Thomas Kluyver
Reorganise InputTransformer decorator architecture.
r10102 @classmethod
def wrap(cls, func):
"""Can be used by subclasses as a decorator, to return a factory that
will allow instantiation with the decorated object.
"""
@functools.wraps(func)
Thomas Kluyver
Allow IPythonInputSplitter to accept cell magics containing blank lines
r10252 def transformer_factory(**kwargs):
return cls(func, **kwargs)
Thomas Kluyver
Fix tests in IPython.core
r10097
Thomas Kluyver
Reorganise InputTransformer decorator architecture.
r10102 return transformer_factory
class StatelessInputTransformer(InputTransformer):
"""Wrapper for a stateless input transformer implemented as a function."""
def __init__(self, func):
self.func = func
def __repr__(self):
Jason Grout
format positions are required in Python 2.6.x
r11163 return "StatelessInputTransformer(func={0!r})".format(self.func)
Thomas Kluyver
First go at new input transformation system
r10090
Thomas Kluyver
Reorganise InputTransformer decorator architecture.
r10102 def push(self, line):
"""Send a line of input to the transformer, returning the
transformed input."""
return self.func(line)
def reset(self):
"""No-op - exists for compatibility."""
pass
class CoroutineInputTransformer(InputTransformer):
"""Wrapper for an input transformer implemented as a coroutine."""
Thomas Kluyver
Allow IPythonInputSplitter to accept cell magics containing blank lines
r10252 def __init__(self, coro, **kwargs):
Thomas Kluyver
Reorganise InputTransformer decorator architecture.
r10102 # Prime it
Thomas Kluyver
Allow IPythonInputSplitter to accept cell magics containing blank lines
r10252 self.coro = coro(**kwargs)
Thomas Kluyver
Reorganise InputTransformer decorator architecture.
r10102 next(self.coro)
def __repr__(self):
Jason Grout
format positions are required in Python 2.6.x
r11163 return "CoroutineInputTransformer(coro={0!r})".format(self.coro)
Thomas Kluyver
Reorganise InputTransformer decorator architecture.
r10102
def push(self, line):
"""Send a line of input to the transformer, returning the
transformed input or None if the transformer is waiting for more
input.
"""
return self.coro.send(line)
def reset(self):
"""Return, transformed any lines that the transformer has
accumulated, and reset its internal state.
"""
return self.coro.send(None)
Thomas Kluyver
First go at new input transformation system
r10090
Thomas Kluyver
Add TokenInputTransformer
r10103 class TokenInputTransformer(InputTransformer):
"""Wrapper for a token-based input transformer.
func should accept a list of tokens (5-tuples, see tokenize docs), and
return an iterable which can be passed to tokenize.untokenize().
"""
def __init__(self, func):
self.func = func
Thomas Kluyver
Give input lines to tokenize one at a time...
r23184 self.buf = []
Thomas Kluyver
Prototype transformer to assemble logical lines
r10105 self.reset_tokenizer()
Thomas Kluyver
Give input lines to tokenize one at a time...
r23184
Thomas Kluyver
Prototype transformer to assemble logical lines
r10105 def reset_tokenizer(self):
Thomas Kluyver
Give input lines to tokenize one at a time...
r23184 it = iter(self.buf)
self.tokenizer = generate_tokens(it.__next__)
Thomas Kluyver
Add TokenInputTransformer
r10103 def push(self, line):
Thomas Kluyver
Give input lines to tokenize one at a time...
r23184 self.buf.append(line + '\n')
if all(l.isspace() for l in self.buf):
Thomas Kluyver
Revised input transformation framework.
r10106 return self.reset()
Thomas Kluyver
Give input lines to tokenize one at a time...
r23184
Thomas Kluyver
Add TokenInputTransformer
r10103 tokens = []
Thomas Kluyver
Revised input transformation framework.
r10106 stop_at_NL = False
Thomas Kluyver
Add TokenInputTransformer
r10103 try:
for intok in self.tokenizer:
tokens.append(intok)
Thomas Kluyver
Revised input transformation framework.
r10106 t = intok[0]
Thomas Kluyver
Drop bundled, outdated copy of the tokenize module
r24179 if t == tokenize.NEWLINE or (stop_at_NL and t == tokenize.NL):
Thomas Kluyver
Add TokenInputTransformer
r10103 # Stop before we try to pull a line we don't have yet
break
Thomas Kluyver
Drop bundled, outdated copy of the tokenize module
r24179 elif t == tokenize.ERRORTOKEN:
Thomas Kluyver
Revised input transformation framework.
r10106 stop_at_NL = True
Thomas Kluyver
Now include patched copies of tokenize for Python 2 and 3.
r10110 except TokenError:
Thomas Kluyver
Add TokenInputTransformer
r10103 # Multi-line statement - stop and try again with the next line
Thomas Kluyver
Prototype transformer to assemble logical lines
r10105 self.reset_tokenizer()
Thomas Kluyver
Add TokenInputTransformer
r10103 return None
Thomas Kluyver
Revised input transformation framework.
r10106 return self.output(tokens)
def output(self, tokens):
Thomas Kluyver
Give input lines to tokenize one at a time...
r23184 self.buf.clear()
Thomas Kluyver
Prototype transformer to assemble logical lines
r10105 self.reset_tokenizer()
return untokenize(self.func(tokens)).rstrip('\n')
Thomas Kluyver
Add TokenInputTransformer
r10103
def reset(self):
Thomas Kluyver
Give input lines to tokenize one at a time...
r23184 l = ''.join(self.buf)
self.buf.clear()
Thomas Kluyver
Revised input transformation framework.
r10106 self.reset_tokenizer()
Thomas Kluyver
Add TokenInputTransformer
r10103 if l:
return l.rstrip('\n')
Thomas Kluyver
Revised input transformation framework.
r10106 class assemble_python_lines(TokenInputTransformer):
def __init__(self):
super(assemble_python_lines, self).__init__(None)
def output(self, tokens):
return self.reset()
@CoroutineInputTransformer.wrap
def assemble_logical_lines():
Matthias Bussonnier
Fix more escape sequences
r24452 r"""Join lines following explicit line continuations (\)"""
Thomas Kluyver
Revised input transformation framework.
r10106 line = ''
while True:
line = (yield line)
if not line or line.isspace():
continue
parts = []
while line is not None:
Thomas Kluyver
Fix for \ at end of comment, and add tests
r10112 if line.endswith('\\') and (not has_comment(line)):
parts.append(line[:-1])
line = (yield None) # Get another line
else:
parts.append(line)
Thomas Kluyver
Revised input transformation framework.
r10106 break
# Output
Thomas Kluyver
Fix for \ at end of comment, and add tests
r10112 line = ''.join(parts)
Thomas Kluyver
Update inputsplitter to use new input transformers
r10093
# Utilities
def _make_help_call(target, esc, lspace, next_input=None):
"""Prepares a pinfo(2)/psearch call from a target name and the escape
(i.e. ? or ??)"""
method = 'pinfo2' if esc == '??' \
else 'psearch' if '*' in target \
else 'pinfo'
arg = " ".join([method, target])
adityausathe
fix for #10327 : get_ipython().magic() replaced with get_ipython().run_line_magic()
r23751 #Prepare arguments for get_ipython().run_line_magic(magic_name, magic_args)
t_magic_name, _, t_magic_arg_s = arg.partition(' ')
t_magic_name = t_magic_name.lstrip(ESC_MAGIC)
Thomas Kluyver
Update inputsplitter to use new input transformers
r10093 if next_input is None:
adityausathe
fix for #10327 : get_ipython().magic() replaced with get_ipython().run_line_magic()
r23751 return '%sget_ipython().run_line_magic(%r, %r)' % (lspace, t_magic_name, t_magic_arg_s)
Thomas Kluyver
Update inputsplitter to use new input transformers
r10093 else:
adityausathe
fix for #10327 : get_ipython().magic() replaced with get_ipython().run_line_magic()
r23751 return '%sget_ipython().set_next_input(%r);get_ipython().run_line_magic(%r, %r)' % \
(lspace, next_input, t_magic_name, t_magic_arg_s)
Thomas Kluyver
Add docstrings in IPython.core.inputtransformer
r10101
Thomas Kluyver
Simplify input transformers...
r10107 # These define the transformations for the different escape characters.
def _tr_system(line_info):
"Translate lines escaped with: !"
cmd = line_info.line.lstrip().lstrip(ESC_SHELL)
return '%sget_ipython().system(%r)' % (line_info.pre, cmd)
def _tr_system2(line_info):
"Translate lines escaped with: !!"
cmd = line_info.line.lstrip()[2:]
return '%sget_ipython().getoutput(%r)' % (line_info.pre, cmd)
def _tr_help(line_info):
"Translate lines escaped with: ?/??"
# A naked help line should just fire the intro help screen
if not line_info.line[1:]:
return 'get_ipython().show_usage()'
return _make_help_call(line_info.ifun, line_info.esc, line_info.pre)
def _tr_magic(line_info):
"Translate lines escaped with: %"
adityausathe
fix for #10327 : get_ipython.magic() replaced with get_ipython.run_line_magic()
r23750 tpl = '%sget_ipython().run_line_magic(%r, %r)'
MinRK
show UsageError for cell magics mid-cell
r11465 if line_info.line.startswith(ESC_MAGIC2):
return line_info.line
Thomas Kluyver
Simplify input transformers...
r10107 cmd = ' '.join([line_info.ifun, line_info.the_rest]).strip()
adityausathe
fix for #10327 : get_ipython.magic() replaced with get_ipython.run_line_magic()
r23750 #Prepare arguments for get_ipython().run_line_magic(magic_name, magic_args)
t_magic_name, _, t_magic_arg_s = cmd.partition(' ')
t_magic_name = t_magic_name.lstrip(ESC_MAGIC)
return tpl % (line_info.pre, t_magic_name, t_magic_arg_s)
Thomas Kluyver
Simplify input transformers...
r10107
def _tr_quote(line_info):
"Translate lines escaped with: ,"
return '%s%s("%s")' % (line_info.pre, line_info.ifun,
'", "'.join(line_info.the_rest.split()) )
def _tr_quote2(line_info):
"Translate lines escaped with: ;"
return '%s%s("%s")' % (line_info.pre, line_info.ifun,
line_info.the_rest)
def _tr_paren(line_info):
"Translate lines escaped with: /"
return '%s%s(%s)' % (line_info.pre, line_info.ifun,
", ".join(line_info.the_rest.split()))
tr = { ESC_SHELL : _tr_system,
ESC_SH_CAP : _tr_system2,
ESC_HELP : _tr_help,
ESC_HELP2 : _tr_help,
ESC_MAGIC : _tr_magic,
ESC_QUOTE : _tr_quote,
ESC_QUOTE2 : _tr_quote2,
ESC_PAREN : _tr_paren }
@StatelessInputTransformer.wrap
def escaped_commands(line):
"""Transform escaped commands - %magic, !system, ?help + various autocalls.
Thomas Kluyver
Add docstrings in IPython.core.inputtransformer
r10101 """
Thomas Kluyver
Simplify input transformers...
r10107 if not line or line.isspace():
return line
lineinf = LineInfo(line)
if lineinf.esc not in tr:
return line
Thomas Kluyver
Update inputsplitter to use new input transformers
r10093
Thomas Kluyver
Simplify input transformers...
r10107 return tr[lineinf.esc](lineinf)
Thomas Kluyver
First go at new input transformation system
r10090
_initial_space_re = re.compile(r'\s*')
_help_end_re = re.compile(r"""(%{0,2}
Markus Wageringel
support for unicode identifiers...
r25595 (?!\d)[\w*]+ # Variable name
(\.(?!\d)[\w*]+)* # .etc.etc
Thomas Kluyver
First go at new input transformation system
r10090 )
MinRK
don't close string from inside re.VERBOSE comment...
r11461 (\?\??)$ # ? or ??
""",
Thomas Kluyver
First go at new input transformation system
r10090 re.VERBOSE)
Thomas Kluyver
help_end transformer shouldn't pick up ? in multiline string...
r12352 # Extra pseudotokens for multiline strings and data structures
_MULTILINE_STRING = object()
_MULTILINE_STRUCTURE = object()
def _line_tokens(line):
"""Helper for has_comment and ends_in_comment_or_string."""
readline = StringIO(line).readline
toktypes = set()
try:
for t in generate_tokens(readline):
toktypes.add(t[0])
except TokenError as e:
# There are only two cases where a TokenError is raised.
if 'multi-line string' in e.args[0]:
toktypes.add(_MULTILINE_STRING)
else:
toktypes.add(_MULTILINE_STRUCTURE)
return toktypes
Thomas Kluyver
Update inputsplitter to use new input transformers
r10093 def has_comment(src):
"""Indicate whether an input line has (i.e. ends in, or is) a comment.
This uses tokenize, so it can distinguish comments from # inside strings.
Parameters
----------
src : string
A single line input string.
Returns
-------
Thomas Kluyver
Use numpy docstring format correctly
r10109 comment : bool
True if source has a comment.
Thomas Kluyver
Update inputsplitter to use new input transformers
r10093 """
Thomas Kluyver
Drop bundled, outdated copy of the tokenize module
r24179 return (tokenize.COMMENT in _line_tokens(src))
Thomas Kluyver
Update inputsplitter to use new input transformers
r10093
Thomas Kluyver
help_end transformer shouldn't pick up ? in multiline string...
r12352 def ends_in_comment_or_string(src):
"""Indicates whether or not an input line ends in a comment or within
a multiline string.
Parameters
----------
src : string
A single line input string.
Returns
-------
comment : bool
True if source ends in a comment or multiline string.
"""
toktypes = _line_tokens(src)
Thomas Kluyver
Drop bundled, outdated copy of the tokenize module
r24179 return (tokenize.COMMENT in toktypes) or (_MULTILINE_STRING in toktypes)
Thomas Kluyver
help_end transformer shouldn't pick up ? in multiline string...
r12352
Thomas Kluyver
More whitespace between top level functions
r10100
Thomas Kluyver
Reorganise InputTransformer decorator architecture.
r10102 @StatelessInputTransformer.wrap
Thomas Kluyver
Update inputsplitter to use new input transformers
r10093 def help_end(line):
Thomas Kluyver
First go at new input transformation system
r10090 """Translate lines with ?/?? at the end"""
m = _help_end_re.search(line)
Thomas Kluyver
help_end transformer shouldn't pick up ? in multiline string...
r12352 if m is None or ends_in_comment_or_string(line):
Thomas Kluyver
First go at new input transformation system
r10090 return line
target = m.group(1)
esc = m.group(3)
lspace = _initial_space_re.match(line).group(0)
# If we're mid-command, put it back on the next prompt for the user.
next_input = line.rstrip('?') if line.strip() != m.group(0) else None
return _make_help_call(target, esc, lspace, next_input)
Thomas Kluyver
More whitespace between top level functions
r10100
Thomas Kluyver
Reorganise InputTransformer decorator architecture.
r10102 @CoroutineInputTransformer.wrap
Thomas Kluyver
Allow IPythonInputSplitter to accept cell magics containing blank lines
r10252 def cellmagic(end_on_blank_line=False):
Thomas Kluyver
Add docstrings in IPython.core.inputtransformer
r10101 """Captures & transforms cell magics.
After a cell magic is started, this stores up any lines it gets until it is
reset (sent None).
"""
Thomas Kluyver
First go at new input transformation system
r10090 tpl = 'get_ipython().run_cell_magic(%r, %r, %r)'
Matthias Bussonnier
Fix more escape sequences
r24452 cellmagic_help_re = re.compile(r'%%\w+\?')
Thomas Kluyver
First go at new input transformation system
r10090 line = ''
while True:
line = (yield line)
MinRK
don't allow cell magics mid-cell
r11462 # consume leading empty lines
while not line:
line = (yield line)
if not line.startswith(ESC_MAGIC2):
# This isn't a cell magic, idle waiting for reset then start over
while line is not None:
line = (yield line)
Thomas Kluyver
First go at new input transformation system
r10090 continue
Thomas Kluyver
Fix tests in IPython.core
r10097 if cellmagic_help_re.match(line):
# This case will be handled by help_end
continue
Thomas Kluyver
First go at new input transformation system
r10090 first = line
body = []
line = (yield None)
Thomas Kluyver
Allow IPythonInputSplitter to accept cell magics containing blank lines
r10252 while (line is not None) and \
((line.strip() != '') or not end_on_blank_line):
Thomas Kluyver
First go at new input transformation system
r10090 body.append(line)
line = (yield None)
# Output
magic_name, _, first = first.partition(' ')
magic_name = magic_name.lstrip(ESC_MAGIC2)
Thomas Kluyver
Fix tests in IPython.core
r10097 line = tpl % (magic_name, first, u'\n'.join(body))
Thomas Kluyver
More input transformers
r10091
Thomas Kluyver
More whitespace between top level functions
r10100
Thomas Kluyver
Deactivate prompt transformers if the first line matches certain patterns...
r21954 def _strip_prompts(prompt_re, initial_re=None, turnoff_re=None):
MinRK
only strip continuation prompts if regular prompts seen first...
r12231 """Remove matching input prompts from a block of input.
MinRK
adjust strip_prompts logic a bit further...
r12309 Parameters
----------
prompt_re : regular expression
A regular expression matching any input prompt (including continuation)
initial_re : regular expression, optional
A regular expression matching only the initial prompt, but not continuation.
If no initial expression is given, prompt_re will be used everywhere.
Used mainly for plain Python prompts, where the continuation prompt
``...`` is a valid Python expression in Python 3, so shouldn't be stripped.
MinRK
only strip continuation prompts if regular prompts seen first...
r12231
MinRK
adjust strip_prompts logic a bit further...
r12309 If initial_re and prompt_re differ,
only initial_re will be tested against the first line.
If any prompt is found on the first two lines,
prompts will be stripped from the rest of the block.
MinRK
only strip continuation prompts if regular prompts seen first...
r12231 """
MinRK
adjust strip_prompts logic a bit further...
r12309 if initial_re is None:
initial_re = prompt_re
Thomas Kluyver
More input transformers
r10091 line = ''
while True:
line = (yield line)
Thomas Kluyver
Strip prompts even if the prompt isn't present on the first line....
r10652 # First line of cell
Thomas Kluyver
More input transformers
r10091 if line is None:
continue
MinRK
adjust strip_prompts logic a bit further...
r12309 out, n1 = initial_re.subn('', line, count=1)
Thomas Kluyver
Deactivate prompt transformers if the first line matches certain patterns...
r21954 if turnoff_re and not n1:
if turnoff_re.match(line):
# We're in e.g. a cell magic; disable this transformer for
# the rest of the cell.
Thomas Kluyver
No yield from for us...
r21956 while line is not None:
line = (yield line)
Thomas Kluyver
Deactivate prompt transformers if the first line matches certain patterns...
r21954 continue
Thomas Kluyver
Strip prompts even if the prompt isn't present on the first line....
r10652 line = (yield out)
if line is None:
continue
MinRK
adjust strip_prompts logic a bit further...
r12309 # check for any prompt on the second line of the cell,
# because people often copy from just after the first prompt,
# so we might not see it in the first line.
out, n2 = prompt_re.subn('', line, count=1)
Thomas Kluyver
Strip prompts even if the prompt isn't present on the first line....
r10652 line = (yield out)
if n1 or n2:
MinRK
adjust strip_prompts logic a bit further...
r12309 # Found a prompt in the first two lines - check for it in
Thomas Kluyver
Strip prompts even if the prompt isn't present on the first line....
r10652 # the rest of the cell as well.
while line is not None:
MinRK
adjust strip_prompts logic a bit further...
r12309 line = (yield prompt_re.sub('', line, count=1))
Thomas Kluyver
More input transformers
r10091
else:
# Prompts not in input - wait for reset
Thomas Kluyver
No yield from for us...
r21956 while line is not None:
line = (yield line)
Thomas Kluyver
More input transformers
r10091
Thomas Kluyver
Reorganise InputTransformer decorator architecture.
r10102 @CoroutineInputTransformer.wrap
Thomas Kluyver
More input transformers
r10091 def classic_prompt():
Thomas Kluyver
Add docstrings in IPython.core.inputtransformer
r10101 """Strip the >>>/... prompts of the Python interactive shell."""
Eric O. LEBIGOT (EOL)
Removed unnecessary regexp part. Added comments about whether using a capturing group is necessary.
r11078 # FIXME: non-capturing version (?:...) usable?
Thomas Kluyver
Only allow '>>>' prompt without space afterwards if line is blank...
r17038 prompt_re = re.compile(r'^(>>>|\.\.\.)( |$)')
initial_re = re.compile(r'^>>>( |$)')
Thomas Kluyver
Deactivate prompt transformers if the first line matches certain patterns...
r21954 # Any %magic/!system is IPython syntax, so we needn't look for >>> prompts
turnoff_re = re.compile(r'^[%!]')
return _strip_prompts(prompt_re, initial_re, turnoff_re)
Thomas Kluyver
More input transformers
r10091
Thomas Kluyver
Reorganise InputTransformer decorator architecture.
r10102 @CoroutineInputTransformer.wrap
Thomas Kluyver
More input transformers
r10091 def ipy_prompt():
Thomas Kluyver
Add docstrings in IPython.core.inputtransformer
r10101 """Strip IPython's In [1]:/...: prompts."""
Eric O. LEBIGOT (EOL)
Removed unnecessary regexp part. Added comments about whether using a capturing group is necessary.
r11078 # FIXME: non-capturing version (?:...) usable?
Pascal Bugnion
Fixed whitespace bug when pasting IPython continuation prompt....
r19037 prompt_re = re.compile(r'^(In \[\d+\]: |\s*\.{3,}: ?)')
Thomas Kluyver
Deactivate prompt transformers if the first line matches certain patterns...
r21954 # Disable prompt stripping inside cell magics
turnoff_re = re.compile(r'^%%')
return _strip_prompts(prompt_re, turnoff_re=turnoff_re)
Thomas Kluyver
More input transformers
r10091
Thomas Kluyver
More whitespace between top level functions
r10100
Thomas Kluyver
Reorganise InputTransformer decorator architecture.
r10102 @CoroutineInputTransformer.wrap
Thomas Kluyver
More input transformers
r10091 def leading_indent():
Thomas Kluyver
Add docstrings in IPython.core.inputtransformer
r10101 """Remove leading indentation.
If the first line starts with a spaces or tabs, the same whitespace will be
removed from each following line until it is reset.
"""
Thomas Kluyver
More input transformers
r10091 space_re = re.compile(r'^[ \t]+')
line = ''
while True:
line = (yield line)
if line is None:
continue
m = space_re.match(line)
if m:
space = m.group(0)
while line is not None:
if line.startswith(space):
line = line[len(space):]
line = (yield line)
else:
# No leading spaces - wait for reset
while line is not None:
line = (yield line)
Thomas Kluyver
More whitespace between top level functions
r10100
Thomas Kluyver
Improve assignment regex to match more tuple unpacking syntax...
r15786 _assign_pat = \
r'''(?P<lhs>(\s*)
([\w\.]+) # Initial identifier
(\s*,\s*
\*?[\w\.]+)* # Further identifiers for unpacking
\s*?,? # Trailing comma
)
\s*=\s*
'''
assign_system_re = re.compile(r'{}!\s*(?P<cmd>.*)'.format(_assign_pat), re.VERBOSE)
Thomas Kluyver
Simplify input transformers...
r10107 assign_system_template = '%s = get_ipython().getoutput(%r)'
@StatelessInputTransformer.wrap
def assign_from_system(line):
Thomas Kluyver
Add docstrings in IPython.core.inputtransformer
r10101 """Transform assignment from system commands (e.g. files = !ls)"""
Thomas Kluyver
Simplify input transformers...
r10107 m = assign_system_re.match(line)
if m is None:
return line
return assign_system_template % m.group('lhs', 'cmd')
Thomas Kluyver
Transformers for assignment from %magic and \!system calls
r10092
Thomas Kluyver
Improve assignment regex to match more tuple unpacking syntax...
r15786 assign_magic_re = re.compile(r'{}%\s*(?P<cmd>.*)'.format(_assign_pat), re.VERBOSE)
adityausathe
fix for #10327 : get_ipython.magic() replaced with get_ipython.run_line_magic()
r23750 assign_magic_template = '%s = get_ipython().run_line_magic(%r, %r)'
Thomas Kluyver
Simplify input transformers...
r10107 @StatelessInputTransformer.wrap
def assign_from_magic(line):
Thomas Kluyver
Add docstrings in IPython.core.inputtransformer
r10101 """Transform assignment from magic commands (e.g. a = %who_ls)"""
Thomas Kluyver
Simplify input transformers...
r10107 m = assign_magic_re.match(line)
if m is None:
return line
adityausathe
fix for #10327 : get_ipython.magic() replaced with get_ipython.run_line_magic()
r23750 #Prepare arguments for get_ipython().run_line_magic(magic_name, magic_args)
m_lhs, m_cmd = m.group('lhs', 'cmd')
adityausathe
fix for #10327 : get_ipython().magic() replaced with get_ipython().run_line_magic()
r23751 t_magic_name, _, t_magic_arg_s = m_cmd.partition(' ')
adityausathe
fix for #10327 : get_ipython.magic() replaced with get_ipython.run_line_magic()
r23750 t_magic_name = t_magic_name.lstrip(ESC_MAGIC)
return assign_magic_template % (m_lhs, t_magic_name, t_magic_arg_s)