Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/IPython/core/splitinput.py: 34%
35 statements
« prev ^ index » next coverage.py v7.4.4, created at 2024-04-20 06:09 +0000
« prev ^ index » next coverage.py v7.4.4, created at 2024-04-20 06:09 +0000
1# encoding: utf-8
2"""
3Simple utility for splitting user input. This is used by both inputsplitter and
4prefilter.
6Authors:
8* Brian Granger
9* Fernando Perez
10"""
12#-----------------------------------------------------------------------------
13# Copyright (C) 2008-2011 The IPython Development Team
14#
15# Distributed under the terms of the BSD License. The full license is in
16# the file COPYING, distributed as part of this software.
17#-----------------------------------------------------------------------------
19#-----------------------------------------------------------------------------
20# Imports
21#-----------------------------------------------------------------------------
23import re
24import sys
26from IPython.utils import py3compat
27from IPython.utils.encoding import get_stream_enc
28from IPython.core.oinspect import OInfo
30#-----------------------------------------------------------------------------
31# Main function
32#-----------------------------------------------------------------------------
34# RegExp for splitting line contents into pre-char//first word-method//rest.
35# For clarity, each group in on one line.
37# WARNING: update the regexp if the escapes in interactiveshell are changed, as
38# they are hardwired in.
40# Although it's not solely driven by the regex, note that:
41# ,;/% only trigger if they are the first character on the line
42# ! and !! trigger if they are first char(s) *or* follow an indent
43# ? triggers as first or last char.
45line_split = re.compile(r"""
46 ^(\s*) # any leading space
47 ([,;/%]|!!?|\?\??)? # escape character or characters
48 \s*(%{0,2}[\w\.\*]*) # function/method, possibly with leading %
49 # to correctly treat things like '?%magic'
50 (.*?$|$) # rest of line
51 """, re.VERBOSE)
54def split_user_input(line, pattern=None):
55 """Split user input into initial whitespace, escape character, function part
56 and the rest.
57 """
58 # We need to ensure that the rest of this routine deals only with unicode
59 encoding = get_stream_enc(sys.stdin, 'utf-8')
60 line = py3compat.cast_unicode(line, encoding)
62 if pattern is None:
63 pattern = line_split
64 match = pattern.match(line)
65 if not match:
66 # print "match failed for line '%s'" % line
67 try:
68 ifun, the_rest = line.split(None,1)
69 except ValueError:
70 # print "split failed for line '%s'" % line
71 ifun, the_rest = line, u''
72 pre = re.match(r'^(\s*)(.*)',line).groups()[0]
73 esc = ""
74 else:
75 pre, esc, ifun, the_rest = match.groups()
77 #print 'line:<%s>' % line # dbg
78 #print 'pre <%s> ifun <%s> rest <%s>' % (pre,ifun.strip(),the_rest) # dbg
79 return pre, esc or '', ifun.strip(), the_rest.lstrip()
82class LineInfo(object):
83 """A single line of input and associated info.
85 Includes the following as properties:
87 line
88 The original, raw line
90 continue_prompt
91 Is this line a continuation in a sequence of multiline input?
93 pre
94 Any leading whitespace.
96 esc
97 The escape character(s) in pre or the empty string if there isn't one.
98 Note that '!!' and '??' are possible values for esc. Otherwise it will
99 always be a single character.
101 ifun
102 The 'function part', which is basically the maximal initial sequence
103 of valid python identifiers and the '.' character. This is what is
104 checked for alias and magic transformations, used for auto-calling,
105 etc. In contrast to Python identifiers, it may start with "%" and contain
106 "*".
108 the_rest
109 Everything else on the line.
110 """
111 def __init__(self, line, continue_prompt=False):
112 self.line = line
113 self.continue_prompt = continue_prompt
114 self.pre, self.esc, self.ifun, self.the_rest = split_user_input(line)
116 self.pre_char = self.pre.strip()
117 if self.pre_char:
118 self.pre_whitespace = '' # No whitespace allowed before esc chars
119 else:
120 self.pre_whitespace = self.pre
122 def ofind(self, ip) -> OInfo:
123 """Do a full, attribute-walking lookup of the ifun in the various
124 namespaces for the given IPython InteractiveShell instance.
126 Return a dict with keys: {found, obj, ospace, ismagic}
128 Note: can cause state changes because of calling getattr, but should
129 only be run if autocall is on and if the line hasn't matched any
130 other, less dangerous handlers.
132 Does cache the results of the call, so can be called multiple times
133 without worrying about *further* damaging state.
134 """
135 return ip._ofind(self.ifun)
137 def __str__(self):
138 return "LineInfo [%s|%s|%s|%s]" %(self.pre, self.esc, self.ifun, self.the_rest)