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

Skip to content

Commit b3edf5e

Browse files
authored
Reduce types in splitinput.py (#15057)
As far as I can tell, those are always strings, so should remove some overhead. Also deprecated and ruff-format the file.
2 parents ce9a3a4 + 01bc61a commit b3edf5e

2 files changed

Lines changed: 47 additions & 37 deletions

File tree

IPython/core/prefilter.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -498,7 +498,7 @@ def check(self, line_info):
498498
if not self.shell.autocall:
499499
return None
500500

501-
oinfo = line_info.ofind(self.shell) # This can mutate state via getattr
501+
oinfo = self.shell._ofind(line_info.ifun) # This can mutate state via getattr
502502
if not oinfo.found:
503503
return None
504504

@@ -610,7 +610,7 @@ def handle(self, line_info):
610610
the_rest = line_info.the_rest
611611
esc = line_info.esc
612612
continue_prompt = line_info.continue_prompt
613-
obj = line_info.ofind(self.shell).obj
613+
obj = self.shell._ofind(line_info.ifun).obj
614614

615615
# This should only be active for single-line input!
616616
if continue_prompt:

IPython/core/splitinput.py

Lines changed: 45 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,21 @@
1-
# encoding: utf-8
21
"""
32
Simple utility for splitting user input. This is used by both inputsplitter and
43
prefilter.
5-
6-
Authors:
7-
8-
* Brian Granger
9-
* Fernando Perez
104
"""
115

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-
#-----------------------------------------------------------------------------
18-
19-
#-----------------------------------------------------------------------------
6+
# -----------------------------------------------------------------------------
207
# Imports
21-
#-----------------------------------------------------------------------------
8+
# -----------------------------------------------------------------------------
229

2310
import re
2411
import sys
12+
import warnings
2513

26-
from IPython.utils import py3compat
27-
from IPython.utils.encoding import get_stream_enc
2814
from IPython.core.oinspect import OInfo
2915

30-
#-----------------------------------------------------------------------------
16+
# -----------------------------------------------------------------------------
3117
# Main function
32-
#-----------------------------------------------------------------------------
18+
# -----------------------------------------------------------------------------
3319

3420
# RegExp for splitting line contents into pre-char//first word-method//rest.
3521
# For clarity, each group in on one line.
@@ -42,34 +28,37 @@
4228
# ! and !! trigger if they are first char(s) *or* follow an indent
4329
# ? triggers as first or last char.
4430

45-
line_split = re.compile(r"""
31+
line_split = re.compile(
32+
r"""
4633
^(\s*) # any leading space
4734
([,;/%]|!!?|\?\??)? # escape character or characters
4835
\s*(%{0,2}[\w\.\*]*) # function/method, possibly with leading %
4936
# to correctly treat things like '?%magic'
5037
(.*?$|$) # rest of line
51-
""", re.VERBOSE)
38+
""",
39+
re.VERBOSE,
40+
)
5241

5342

54-
def split_user_input(line, pattern=None):
43+
def split_user_input(
44+
line: str, pattern: re.Pattern[str] | None = None
45+
) -> tuple[str, str, str, str]:
5546
"""Split user input into initial whitespace, escape character, function part
5647
and the rest.
5748
"""
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)
49+
assert isinstance(line, str)
6150

6251
if pattern is None:
6352
pattern = line_split
6453
match = pattern.match(line)
6554
if not match:
6655
# print("match failed for line '%s'" % line)
6756
try:
68-
ifun, the_rest = line.split(None,1)
57+
ifun, the_rest = line.split(None, 1)
6958
except ValueError:
7059
# print("split failed for line '%s'" % line)
71-
ifun, the_rest = line, u''
72-
pre = re.match(r'^(\s*)(.*)',line).groups()[0]
60+
ifun, the_rest = line, ""
61+
pre = re.match(r"^(\s*)(.*)", line).groups()[0]
7362
esc = ""
7463
else:
7564
pre, esc, ifun, the_rest = match.groups()
@@ -111,15 +100,27 @@ class LineInfo:
111100
raw_the_rest
112101
the_rest without whitespace stripped.
113102
"""
114-
def __init__(self, line, continue_prompt=False):
115-
self.line = line
103+
104+
line: str
105+
continue_prompt: bool
106+
pre: str
107+
esc: str
108+
ifun: str
109+
raw_the_rest: str
110+
the_rest: str
111+
pre_char: str
112+
pre_whitespace: str
113+
114+
def __init__(self, line: str, continue_prompt: bool = False) -> None:
115+
assert isinstance(line, str)
116+
self.line = line
116117
self.continue_prompt = continue_prompt
117118
self.pre, self.esc, self.ifun, self.raw_the_rest = split_user_input(line)
118119
self.the_rest = self.raw_the_rest.lstrip()
119120

120-
self.pre_char = self.pre.strip()
121+
self.pre_char = self.pre.strip()
121122
if self.pre_char:
122-
self.pre_whitespace = '' # No whitespace allowed before esc chars
123+
self.pre_whitespace = "" # No whitespace allowed before esc chars
123124
else:
124125
self.pre_whitespace = self.pre
125126

@@ -135,11 +136,20 @@ def ofind(self, ip) -> OInfo:
135136
136137
Does cache the results of the call, so can be called multiple times
137138
without worrying about *further* damaging state.
139+
140+
.. deprecated:: 9.8
141+
Use ``shell._ofind(line_info.ifun)`` directly instead.
138142
"""
143+
warnings.warn(
144+
"LineInfo.ofind() is deprecated since IPython 9.9. "
145+
"Use shell._ofind(line_info.ifun) directly instead.",
146+
DeprecationWarning,
147+
stacklevel=2,
148+
)
139149
return ip._ofind(self.ifun)
140150

141-
def __str__(self):
142-
return "LineInfo [%s|%s|%s|%s]" %(self.pre, self.esc, self.ifun, self.the_rest)
151+
def __str__(self) -> str:
152+
return "LineInfo [%s|%s|%s|%s]" % (self.pre, self.esc, self.ifun, self.the_rest)
143153

144-
def __repr__(self):
154+
def __repr__(self) -> str:
145155
return "<" + str(self) + ">"

0 commit comments

Comments
 (0)