##// END OF EJS Templates
Allow passing a custom CachingCompiler to the interactive shell...
martinRenou -
Show More
@@ -112,25 +112,53 b' class CachingCompiler(codeop.Compile):'
112 """
112 """
113 return self.flags
113 return self.flags
114
114
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):
116 """Make a name for a block of code, and cache the code.
135 """Make a name for a block of code, and cache the code.
117
136
118 Parameters
137 Parameters
119 ----------
138 ----------
120 code : str
139 transformed_code : str
121 The Python source code to cache.
140 The executable Python source code to cache and compile.
122 number : int
141 number : int
123 A number which forms part of the code's name. Used for the execution
142 A number which forms part of the code's name. Used for the execution
124 counter.
143 counter.
144 raw_code : str
145 The raw code before transformation, if None, set to `transformed_code`.
125
146
126 Returns
147 Returns
127 -------
148 -------
128 The name of the cached code (as a string). Pass this as the filename
149 The name of the cached code (as a string). Pass this as the filename
129 argument to compilation, so that tracebacks are correctly hooked up.
150 argument to compilation, so that tracebacks are correctly hooked up.
130 """
151 """
131 name = code_name(code, number)
152 if raw_code is None:
132 entry = (len(code), time.time(),
153 raw_code = transformed_code
133 [line+'\n' for line in code.splitlines()], name)
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 )
134 linecache.cache[name] = entry
162 linecache.cache[name] = entry
135 linecache._ipython_cache[name] = entry
163 linecache._ipython_cache[name] = entry
136 return name
164 return name
@@ -443,6 +443,7 b' class InteractiveShell(SingletonConfigurable):'
443 display_formatter = Instance(DisplayFormatter, allow_none=True)
443 display_formatter = Instance(DisplayFormatter, allow_none=True)
444 displayhook_class = Type(DisplayHook)
444 displayhook_class = Type(DisplayHook)
445 display_pub_class = Type(DisplayPublisher)
445 display_pub_class = Type(DisplayPublisher)
446 compiler_class = Type(CachingCompiler)
446
447
447 sphinxify_docstring = Bool(False, help=
448 sphinxify_docstring = Bool(False, help=
448 """
449 """
@@ -748,7 +749,7 b' class InteractiveShell(SingletonConfigurable):'
748 self.more = False
749 self.more = False
749
750
750 # command compiler
751 # command compiler
751 self.compile = CachingCompiler()
752 self.compile = self.compiler_class()
752
753
753 # Make an empty namespace, which extension writers can rely on both
754 # Make an empty namespace, which extension writers can rely on both
754 # existing and NEVER being used by ipython itself. This gives them a
755 # existing and NEVER being used by ipython itself. This gives them a
@@ -1775,8 +1776,14 b' class InteractiveShell(SingletonConfigurable):'
1775 if meth == 'pdoc':
1776 if meth == 'pdoc':
1776 pmethod(info.obj, oname, formatter)
1777 pmethod(info.obj, oname, formatter)
1777 elif meth == 'pinfo':
1778 elif meth == 'pinfo':
1778 pmethod(info.obj, oname, formatter, info,
1779 pmethod(
1779 enable_html_pager=self.enable_html_pager, **kw)
1780 info.obj,
1781 oname,
1782 formatter,
1783 info,
1784 enable_html_pager=self.enable_html_pager,
1785 **kw
1786 )
1780 else:
1787 else:
1781 pmethod(info.obj, oname)
1788 pmethod(info.obj, oname)
1782 else:
1789 else:
@@ -2299,8 +2306,9 b' class InteractiveShell(SingletonConfigurable):'
2299 # Defined here so that it's included in the documentation
2306 # Defined here so that it's included in the documentation
2300 @functools.wraps(magic.MagicsManager.register_function)
2307 @functools.wraps(magic.MagicsManager.register_function)
2301 def register_magic_function(self, func, magic_kind='line', magic_name=None):
2308 def register_magic_function(self, func, magic_kind='line', magic_name=None):
2302 self.magics_manager.register_function(func,
2309 self.magics_manager.register_function(
2303 magic_kind=magic_kind, magic_name=magic_name)
2310 func, magic_kind=magic_kind, magic_name=magic_name
2311 )
2304
2312
2305 def run_line_magic(self, magic_name, line, _stack_depth=1):
2313 def run_line_magic(self, magic_name, line, _stack_depth=1):
2306 """Execute the given line magic.
2314 """Execute the given line magic.
@@ -3096,12 +3104,14 b' class InteractiveShell(SingletonConfigurable):'
3096 # Our own compiler remembers the __future__ environment. If we want to
3104 # Our own compiler remembers the __future__ environment. If we want to
3097 # run code with a separate __future__ environment, use the default
3105 # run code with a separate __future__ environment, use the default
3098 # compiler
3106 # compiler
3099 compiler = self.compile if shell_futures else CachingCompiler()
3107 compiler = self.compile if shell_futures else self.compiler_class()
3100
3108
3101 _run_async = False
3109 _run_async = False
3102
3110
3103 with self.builtin_trap:
3111 with self.builtin_trap:
3104 cell_name = self.compile.cache(cell, self.execution_count)
3112 cell_name = self.compile.cache(
3113 cell, self.execution_count, raw_code=raw_cell
3114 )
3105
3115
3106 with self.display_trap:
3116 with self.display_trap:
3107 # Compile to bytecode
3117 # Compile to bytecode
General Comments 0
You need to be logged in to leave comments. Login now