Skip to content

Commit 8a7e5d4

Browse files
committed
More compiled regular expressions
Signed-off-by: Sebastian Ramacher <sebastian+dev@ramacher.at>
1 parent 9d4308d commit 8a7e5d4

File tree

2 files changed

+18
-8
lines changed

2 files changed

+18
-8
lines changed

bpython/curtsiesfrontend/preprocess.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
11
"""Tools for preparing code to be run in the REPL (removing blank lines, etc)"""
2-
import re
2+
3+
from bpython.lazyre import LazyReCompile
34

45
#TODO specifically catch IndentationErrors instead of any syntax errors
56

7+
8+
indent_empty_lines_re = LazyReCompile(r'\s*')
9+
10+
611
def indent_empty_lines(s, compiler):
712
"""Indents blank lines that would otherwise cause early compilation
813
@@ -16,8 +21,8 @@ def indent_empty_lines(s, compiler):
1621

1722
for p_line, line, n_line in zip([''] + lines[:-1], lines, lines[1:] + ['']):
1823
if len(line) == 0:
19-
p_indent = re.match(r'\s*', p_line).group()
20-
n_indent = re.match(r'\s*', n_line).group()
24+
p_indent = indent_empty_lines_re.match(p_line).group()
25+
n_indent = indent_empty_lines_re.match(n_line).group()
2126
result_lines.append(min([p_indent, n_indent], key=len) + line)
2227
else:
2328
result_lines.append(line)

bpython/inspection.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,12 @@
2626
import inspect
2727
import keyword
2828
import pydoc
29-
import re
3029
import types
3130

3231
from pygments.token import Token
3332

3433
from bpython._py3compat import PythonLexer, py3
34+
from bpython.lazyre import LazyReCompile
3535

3636
try:
3737
collections.Callable
@@ -45,7 +45,7 @@
4545
has_instance_type = False
4646

4747
if not py3:
48-
_name = re.compile(r'[a-zA-Z_]\w*$')
48+
_name = LazyReCompile(r'[a-zA-Z_]\w*$')
4949

5050

5151
class AttrCleaner(object):
@@ -175,14 +175,16 @@ def fixlongargs(f, argspec):
175175
argspec[3] = values
176176

177177

178+
getpydocspec_re = LazyReCompile(r'([a-zA-Z_][a-zA-Z0-9_]*?)\((.*?)\)')
179+
180+
178181
def getpydocspec(f, func):
179182
try:
180183
argspec = pydoc.getdoc(f)
181184
except NameError:
182185
return None
183186

184-
rx = re.compile(r'([a-zA-Z_][a-zA-Z0-9_]*?)\((.*?)\)')
185-
s = rx.search(argspec)
187+
s = getpydocspec_re.search(argspec)
186188
if s is None:
187189
return None
188190

@@ -272,9 +274,12 @@ def is_callable(obj):
272274
return callable(obj)
273275

274276

277+
get_encoding_re = LazyReCompile(r'coding[:=]\s*([-\w.]+)')
278+
279+
275280
def get_encoding(obj):
276281
for line in inspect.findsource(obj)[0][:2]:
277-
m = re.search(r'coding[:=]\s*([-\w.]+)', line)
282+
m = get_encoding_re.search(line)
278283
if m:
279284
return m.group(1)
280285
return 'ascii'

0 commit comments

Comments
 (0)