Skip to content

Commit 743e661

Browse files
committed
get_theme() is relatively expensive, fetch it early and cache it
1 parent 3a6bcd3 commit 743e661

File tree

1 file changed

+5
-2
lines changed

1 file changed

+5
-2
lines changed

Lib/_pyrepl/fancycompleter.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@ def __init__(
3333

3434
if self.use_colors:
3535
readline.parse_and_bind('set dont-escape-ctrl-chars on')
36+
self.theme = get_theme()
37+
else:
38+
self.theme = None
39+
3640
if self.consider_getitems:
3741
delims = readline.get_completer_delims()
3842
delims = delims.replace('[', '')
@@ -152,13 +156,12 @@ def color_for_obj(self, i, name, value):
152156
return f"{N}{color}{name}{ANSIColors.RESET}"
153157

154158
def color_by_type(self, t):
155-
theme = get_theme()
156159
typename = t.__name__
157160
# this is needed e.g. to turn method-wrapper into method_wrapper,
158161
# because if we want _colorize.FancyCompleter to be "dataclassable"
159162
# our keys need to be valid identifiers.
160163
typename = typename.replace('-', '_').replace('.', '_')
161-
return getattr(theme.fancycompleter, typename, ANSIColors.RESET)
164+
return getattr(self.theme.fancycompleter, typename, ANSIColors.RESET)
162165

163166

164167
def commonprefix(names, base=''):

0 commit comments

Comments
 (0)