Skip to content

Commit 3d73b3e

Browse files
SylvainCorlaymeeseeksmachine
authored andcommitted
Backport PR #12809: Allow passing a custom CachingCompiler to the interactive shell
1 parent 9c40b71 commit 3d73b3e

File tree

2 files changed

+112
-74
lines changed

2 files changed

+112
-74
lines changed

IPython/core/compilerop.py

Lines changed: 36 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ def ast_parse(self, source, filename='<unknown>', symbol='exec'):
9999
Arguments are exactly the same as ast.parse (in the standard library),
100100
and are passed to the built-in compile function."""
101101
return compile(source, filename, symbol, self.flags | PyCF_ONLY_AST, 1)
102-
102+
103103
def reset_compiler_flags(self):
104104
"""Reset compiler flags to default state."""
105105
# This value is copied from codeop.Compile.__init__, so if that ever
@@ -112,25 +112,53 @@ def compiler_flags(self):
112112
"""
113113
return self.flags
114114

115-
def cache(self, code, number=0):
115+
def get_code_name(self, raw_code, transformed_code, number):
116+
"""Compute filename given the code, and the cell number.
117+
118+
Parameters
119+
----------
120+
raw_code : str
121+
The raw cell code.
122+
transformed_code : str
123+
The executable Python source code to cache and compile.
124+
number : int
125+
A number which forms part of the code's name. Used for the execution
126+
counter.
127+
128+
Returns
129+
-------
130+
The computed filename.
131+
"""
132+
return code_name(transformed_code, number)
133+
134+
def cache(self, transformed_code, number=0, raw_code=None):
116135
"""Make a name for a block of code, and cache the code.
117136
118137
Parameters
119138
----------
120-
code : str
121-
The Python source code to cache.
139+
transformed_code : str
140+
The executable Python source code to cache and compile.
122141
number : int
123142
A number which forms part of the code's name. Used for the execution
124143
counter.
144+
raw_code : str
145+
The raw code before transformation, if None, set to `transformed_code`.
125146
126147
Returns
127148
-------
128149
The name of the cached code (as a string). Pass this as the filename
129150
argument to compilation, so that tracebacks are correctly hooked up.
130151
"""
131-
name = code_name(code, number)
132-
entry = (len(code), time.time(),
133-
[line+'\n' for line in code.splitlines()], name)
152+
if raw_code is None:
153+
raw_code = transformed_code
154+
155+
name = self.get_code_name(raw_code, transformed_code, number)
156+
entry = (
157+
len(transformed_code),
158+
time.time(),
159+
[line + "\n" for line in transformed_code.splitlines()],
160+
name,
161+
)
134162
linecache.cache[name] = entry
135163
linecache._ipython_cache[name] = entry
136164
return name
@@ -146,7 +174,7 @@ def extra_flags(self, flags):
146174
yield
147175
finally:
148176
# turn off only the bits we turned on so that something like
149-
# __future__ that set flags stays.
177+
# __future__ that set flags stays.
150178
self.flags &= ~turn_on_bits
151179

152180

0 commit comments

Comments
 (0)