Show More
@@ -15,7 +15,7 b' from IPython.core.formatters import _safe_get_formatter_method' | |||
|
15 | 15 | from IPython.config.configurable import Configurable |
|
16 | 16 | from IPython.utils import io |
|
17 | 17 | from IPython.utils.py3compat import builtin_mod |
|
18 | from IPython.utils.traitlets import Instance | |
|
18 | from IPython.utils.traitlets import Instance, Float | |
|
19 | 19 | from IPython.utils.warn import warn |
|
20 | 20 | |
|
21 | 21 | # TODO: Move the various attributes (cache_size, [others now moved]). Some |
@@ -30,10 +30,10 b' class DisplayHook(Configurable):' | |||
|
30 | 30 | """ |
|
31 | 31 | |
|
32 | 32 | shell = Instance('IPython.core.interactiveshell.InteractiveShellABC') |
|
33 | cull_fraction = Float(0.2) | |
|
33 | 34 | |
|
34 | 35 | def __init__(self, shell=None, cache_size=1000, **kwargs): |
|
35 | 36 | super(DisplayHook, self).__init__(shell=shell, **kwargs) |
|
36 | ||
|
37 | 37 | cache_size_min = 3 |
|
38 | 38 | if cache_size <= 0: |
|
39 | 39 | self.do_full_cache = 0 |
@@ -178,13 +178,7 b' class DisplayHook(Configurable):' | |||
|
178 | 178 | # Avoid recursive reference when displaying _oh/Out |
|
179 | 179 | if result is not self.shell.user_ns['_oh']: |
|
180 | 180 | if len(self.shell.user_ns['_oh']) >= self.cache_size and self.do_full_cache: |
|
181 | warn('Output cache limit (currently '+ | |
|
182 | repr(self.cache_size)+' entries) hit.\n' | |
|
183 | 'Flushing cache and resetting history counter...\n' | |
|
184 | 'The only history variables available will be _,__,___ and _1\n' | |
|
185 | 'with the current result.') | |
|
186 | ||
|
187 | self.flush() | |
|
181 | self.cull_cache() | |
|
188 | 182 | # Don't overwrite '_' and friends if '_' is in __builtin__ (otherwise |
|
189 | 183 | # we cause buggy behavior for things like gettext). |
|
190 | 184 | |
@@ -243,6 +237,21 b' class DisplayHook(Configurable):' | |||
|
243 | 237 | self.log_output(format_dict) |
|
244 | 238 | self.finish_displayhook() |
|
245 | 239 | |
|
240 | def cull_cache(self): | |
|
241 | """Output cache is full, cull the oldest entries""" | |
|
242 | oh = self.shell.user_ns.get('_oh', {}) | |
|
243 | sz = len(oh) | |
|
244 | cull_count = max(int(sz * self.cull_fraction), 2) | |
|
245 | warn('Output cache limit (currently {sz} entries) hit.\n' | |
|
246 | 'Flushing oldest {cull_count} entries.'.format(sz=sz, cull_count=cull_count)) | |
|
247 | ||
|
248 | for i, n in enumerate(sorted(oh)): | |
|
249 | if i >= cull_count: | |
|
250 | break | |
|
251 | self.shell.user_ns.pop('_%i' % n, None) | |
|
252 | oh.pop(n, None) | |
|
253 | ||
|
254 | ||
|
246 | 255 | def flush(self): |
|
247 | 256 | if not self.do_full_cache: |
|
248 | 257 | raise ValueError("You shouldn't have reached the cache flush " |
General Comments 0
You need to be logged in to leave comments.
Login now