Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 17afbbe

Browse files
committed
Improvements in the code that breaks up user input.
1 parent d60f657 commit 17afbbe

5 files changed

Lines changed: 38 additions & 41 deletions

File tree

IPython/core/alias.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
#-----------------------------------------------------------------------------
3939

4040
# This is used as the pattern for calls to split_user_input.
41-
shell_line_split = re.compile(r'^(\s*)(\S*\s*)(.*$)')
41+
shell_line_split = re.compile(r'^(\s*)()(\S+)(.*$)')
4242

4343
def default_aliases():
4444
"""Return list of shell aliases to auto-define.
@@ -222,7 +222,7 @@ def expand_alias(self, line):
222222
<16> 'q:/opt/np/notepad++.exe myfile.txt'
223223
"""
224224

225-
pre,fn,rest = split_user_input(line)
225+
pre,_,fn,rest = split_user_input(line)
226226
res = pre + self.expand_aliases(fn, rest)
227227
return res
228228

@@ -242,7 +242,7 @@ def expand_aliases(self, fn, rest):
242242

243243
done = set()
244244
while 1:
245-
pre,fn,rest = split_user_input(line, shell_line_split)
245+
pre,_,fn,rest = split_user_input(line, shell_line_split)
246246
if fn in self.alias_table:
247247
if fn in done:
248248
warn("Cyclic alias definition, repeated '%s'" % fn)

IPython/core/oinspect.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,16 @@
2424
import sys
2525
import types
2626
from collections import namedtuple
27-
from itertools import izip_longest
27+
try:
28+
from itertools import izip_longest
29+
except ImportError:
30+
from itertools import zip_longest as izip_longest
2831

2932
# IPython's own
3033
from IPython.core import page
3134
from IPython.utils import PyColorize
3235
from IPython.utils import io
36+
from IPython.utils import py3compat
3337
from IPython.utils.text import indent
3438
from IPython.utils.wildcard import list_namespace
3539
from IPython.utils.coloransi import *
@@ -249,7 +253,7 @@ def _getdef(self,obj,oname=''):
249253
try:
250254
# We need a plain string here, NOT unicode!
251255
hdef = oname + inspect.formatargspec(*getargspec(obj))
252-
return hdef.encode('ascii')
256+
return py3compat.unicode_to_str(hdef.encode('ascii'))
253257
except:
254258
return None
255259

@@ -362,7 +366,7 @@ def psource(self,obj,oname=''):
362366
except:
363367
self.noinfo('source',oname)
364368
else:
365-
page.page(self.format(src))
369+
page.page(self.format(py3compat.unicode_to_str(src)))
366370

367371
def pfile(self,obj,oname=''):
368372
"""Show the whole file where an object was defined."""

IPython/core/prefilter.py

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ class LineInfo(object):
131131
def __init__(self, line, continue_prompt):
132132
self.line = line
133133
self.continue_prompt = continue_prompt
134-
self.pre, self.ifun, self.the_rest = split_user_input(line)
134+
self.pre, self.esc, self.ifun, self.the_rest = split_user_input(line)
135135

136136
self.pre_char = self.pre.strip()
137137
if self.pre_char:
@@ -630,7 +630,7 @@ def check(self, line_info):
630630
# both ! and !!.
631631
if line_info.continue_prompt \
632632
and self.prefilter_manager.multi_line_specials:
633-
if line_info.ifun.startswith(ESC_MAGIC):
633+
if line_info.esc == ESC_MAGIC:
634634
return self.prefilter_manager.get_handler_by_name('magic')
635635
else:
636636
return None
@@ -644,14 +644,16 @@ def check(self, line_info):
644644
"""Check for escape character and return either a handler to handle it,
645645
or None if there is no escape char."""
646646
if line_info.line[-1] == ESC_HELP \
647-
and line_info.pre_char != ESC_SHELL \
648-
and line_info.pre_char != ESC_SH_CAP:
647+
and line_info.esc != ESC_SHELL \
648+
and line_info.esc != ESC_SH_CAP:
649649
# the ? can be at the end, but *not* for either kind of shell escape,
650650
# because a ? can be a vaild final char in a shell cmd
651651
return self.prefilter_manager.get_handler_by_name('help')
652652
else:
653+
if line_info.pre:
654+
return None
653655
# This returns None like it should if no handler exists
654-
return self.prefilter_manager.get_handler_by_esc(line_info.pre_char)
656+
return self.prefilter_manager.get_handler_by_esc(line_info.esc)
655657

656658

657659
class AssignmentChecker(PrefilterChecker):
@@ -872,6 +874,7 @@ def handle(self, line_info):
872874
ifun = line_info.ifun
873875
the_rest = line_info.the_rest
874876
pre = line_info.pre
877+
esc = line_info.esc
875878
continue_prompt = line_info.continue_prompt
876879
obj = line_info.ofind(self)['obj']
877880
#print 'pre <%s> ifun <%s> rest <%s>' % (pre,ifun,the_rest) # dbg
@@ -883,13 +886,13 @@ def handle(self, line_info):
883886
force_auto = isinstance(obj, IPyAutocall)
884887
auto_rewrite = getattr(obj, 'rewrite', True)
885888

886-
if pre == ESC_QUOTE:
889+
if esc == ESC_QUOTE:
887890
# Auto-quote splitting on whitespace
888891
newcmd = '%s("%s")' % (ifun,'", "'.join(the_rest.split()) )
889-
elif pre == ESC_QUOTE2:
892+
elif esc == ESC_QUOTE2:
890893
# Auto-quote whole string
891894
newcmd = '%s("%s")' % (ifun,the_rest)
892-
elif pre == ESC_PAREN:
895+
elif esc == ESC_PAREN:
893896
newcmd = '%s(%s)' % (ifun,",".join(the_rest.split()))
894897
else:
895898
# Auto-paren.
@@ -946,7 +949,7 @@ def handle(self, line_info):
946949
line = line[:-1]
947950
if line:
948951
#print 'line:<%r>' % line # dbg
949-
self.shell.magic_pinfo(line)
952+
self.shell.magic_pinfo(line_info.ifun)
950953
else:
951954
self.shell.show_usage()
952955
return '' # Empty string is needed here!

IPython/core/splitinput.py

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,15 @@
4040
# ! and !! trigger if they are first char(s) *or* follow an indent
4141
# ? triggers as first or last char.
4242

43-
# The three parts of the regex are:
44-
# 1) pre: pre_char *or* initial whitespace
45-
# 2) ifun: first word/method (mix of \w and '.')
46-
# 3) the_rest: rest of line (separated from ifun by space if non-empty)
47-
line_split = re.compile(r'^([,;/%?]|!!?|\s*)'
43+
# The four parts of the regex are:
44+
# 1) pre: initial whitespace
45+
# 2) esc: escape character
46+
# 3) ifun: first word/method (mix of \w and '.')
47+
# 4) the_rest: rest of line (separated from ifun by space if non-empty)
48+
line_split = re.compile(r'^(\s*)'
49+
r'([,;/%?]|!!?)?'
4850
r'\s*([\w\.]+)'
49-
r'(\s+.*$|$)')
51+
r'(.*$|$)')
5052

5153
# r'[\w\.]+'
5254
# r'\s*=\s*%.*'
@@ -71,19 +73,14 @@ def split_user_input(line, pattern=None):
7173
# print "split failed for line '%s'" % line
7274
ifun, the_rest = line, u''
7375
pre = re.match('^(\s*)(.*)',line).groups()[0]
76+
esc = ""
7477
else:
75-
pre,ifun,the_rest = match.groups()
78+
pre, esc, ifun, the_rest = match.groups()
7679

77-
if not py3compat.PY3:
78-
# ifun has to be a valid python identifier, so it better encode into
79-
# ascii. We do still make it a unicode string so that we consistently
80-
# return unicode, but it will be one that is guaranteed to be pure ascii
81-
try:
82-
ifun = unicode(ifun.encode('ascii'))
83-
except UnicodeEncodeError:
84-
the_rest = ifun + u' ' + the_rest
85-
ifun = u''
80+
if not py3compat.isidentifier(ifun, dotted=True):
81+
the_rest = ifun + u' ' + the_rest
82+
ifun = u''
8683

8784
#print 'line:<%s>' % line # dbg
8885
#print 'pre <%s> ifun <%s> rest <%s>' % (pre,ifun.strip(),the_rest) # dbg
89-
return pre, ifun.strip(), the_rest.lstrip()
86+
return pre, esc, ifun.strip(), the_rest.lstrip()

IPython/core/tests/test_handlers.py

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
# our own packages
1111
from IPython.core import autocall
1212
from IPython.testing import decorators as dec
13+
from IPython.testing import tools as tt
1314
from IPython.testing.globalipapp import get_ipython
1415

1516
#-----------------------------------------------------------------------------
@@ -40,15 +41,7 @@ def run(tests):
4041
"""Loop through a list of (pre, post) inputs, where pre is the string
4142
handed to ipython, and post is how that string looks after it's been
4243
transformed (i.e. ipython's notion of _i)"""
43-
for pre, post in tests:
44-
global num_tests
45-
num_tests += 1
46-
actual = ip.prefilter_manager.prefilter_lines(pre)
47-
if actual != None:
48-
actual = actual.rstrip('\n')
49-
if actual != post:
50-
failures.append('Expected %r to become %r, found %r' % (
51-
pre, post, actual))
44+
tt.check_pairs(ip.prefilter_manager.prefilter_lines, tests)
5245

5346

5447
def test_handlers():

0 commit comments

Comments
 (0)