@@ -73,25 +73,6 @@ class CachingCompiler(codeop.Compile):
7373 def __init__ (self ):
7474 codeop .Compile .__init__ (self )
7575
76- # This is ugly, but it must be done this way to allow multiple
77- # simultaneous ipython instances to coexist. Since Python itself
78- # directly accesses the data structures in the linecache module, and
79- # the cache therein is global, we must work with that data structure.
80- # We must hold a reference to the original checkcache routine and call
81- # that in our own check_cache() below, but the special IPython cache
82- # must also be shared by all IPython instances. If we were to hold
83- # separate caches (one in each CachingCompiler instance), any call made
84- # by Python itself to linecache.checkcache() would obliterate the
85- # cached data from the other IPython instances.
86- if not hasattr (linecache , '_ipython_cache' ):
87- linecache ._ipython_cache = {}
88- if not hasattr (linecache , '_checkcache_ori' ):
89- linecache ._checkcache_ori = linecache .checkcache
90- # Now, we must monkeypatch the linecache directly so that parts of the
91- # stdlib that call it outside our control go through our codepath
92- # (otherwise we'd lose our tracebacks).
93- linecache .checkcache = check_linecache_ipython
94-
9576 # Caching a dictionary { filename: execution_count } for nicely
9677 # rendered tracebacks. The filename corresponds to the filename
9778 # argument used for the builtins.compile function.
@@ -161,14 +142,24 @@ def cache(self, transformed_code, number=0, raw_code=None):
161142 # Save the execution count
162143 self ._filename_map [name ] = number
163144
145+ # Since Python 2.5, setting mtime to `None` means the lines will
146+ # never be removed by `linecache.checkcache`. This means all the
147+ # monkeypatching has *never* been necessary, since this code was
148+ # only added in 2010, at which point IPython had already stopped
149+ # supporting Python 2.4.
150+ #
151+ # Note that `linecache.clearcache` and `linecache.updatecache` may
152+ # still remove our code from the cache, but those show explicit
153+ # intent, and we should not try to interfere. Normally the former
154+ # is never called except when out of memory, and the latter is only
155+ # called for lines *not* in the cache.
164156 entry = (
165157 len (transformed_code ),
166- time . time () ,
158+ None ,
167159 [line + "\n " for line in transformed_code .splitlines ()],
168160 name ,
169161 )
170162 linecache .cache [name ] = entry
171- linecache ._ipython_cache [name ] = entry
172163 return name
173164
174165 @contextmanager
@@ -187,10 +178,22 @@ def extra_flags(self, flags):
187178
188179
189180def check_linecache_ipython (* args ):
190- """Call linecache.checkcache() safely protecting our cached values.
181+ """Deprecated since IPython 8.6. Call linecache.checkcache() directly.
182+
183+ It was already not necessary to call this function directly. If no
184+ CachingCompiler had been created, this function would fail badly. If
185+ an instance had been created, this function would've been monkeypatched
186+ into place.
187+
188+ As of IPython 8.6, the monkeypatching has gone away entirely. But there
189+ were still internal callers of this function, so maybe external callers
190+ also existed?
191191 """
192- # First call the original checkcache as intended
193- linecache ._checkcache_ori (* args )
194- # Then, update back the cache with our data, so that tracebacks related
195- # to our compiled codes can be produced.
196- linecache .cache .update (linecache ._ipython_cache )
192+ import warnings
193+
194+ warnings .warn (
195+ "Deprecated Since IPython 8.6, Just call linecache.checkcache() directly." ,
196+ DeprecationWarning ,
197+ stacklevel = 2 ,
198+ )
199+ linecache .checkcache ()
0 commit comments