From cd58b7eac1fcddddc4a63e49f98430c16fb23b4f Mon Sep 17 00:00:00 2001 From: Alex Willmer Date: Tue, 13 Jan 2026 12:17:57 +0000 Subject: [PATCH] mitogen: Avoid import of linecache on Python >= 2.5 It's only needed for a workaround on Python 2.4. --- mitogen/core.py | 33 +++++++++++++++------------------ 1 file changed, 15 insertions(+), 18 deletions(-) diff --git a/mitogen/core.py b/mitogen/core.py index ab5804c9..458d7af5 100644 --- a/mitogen/core.py +++ b/mitogen/core.py @@ -68,7 +68,6 @@ import collections import errno import fcntl import itertools -import linecache import logging import os import pickle as py_pickle @@ -105,6 +104,19 @@ except ImportError: # Python < 3.4, PEP 302 Import Hooks import imp +if sys.version_info >= (2, 5): + def _update_linecache(path, data): pass +else: + import linecache + def _update_linecache(path, data): + """ + Directly populate the linecache cache for modules loaded by Mitogen. + In Python 2.4 the linecache module, does not support PEP-302. + """ + if 'mitogen' not in path: + return + linecache.cache[path] = (len(data), 0.0, data.splitlines(True), path) + # Absolute imports for <2.5. select = __import__('select') @@ -1356,7 +1368,7 @@ class Importer(object): self._callbacks = {} self._cache = {} if core_src: - self._update_linecache('x/mitogen/core.py', core_src) + _update_linecache('x/mitogen/core.py', core_src) self._cache['mitogen.core'] = ( 'mitogen.core', None, @@ -1366,21 +1378,6 @@ class Importer(object): ) self._install_handler(router) - def _update_linecache(self, path, data): - """ - The Python 2.4 linecache module, used to fetch source code for - tracebacks and :func:`inspect.getsource`, does not support PEP-302, - meaning it needs extra help to for Mitogen-loaded modules. Directly - populate its cache if a loaded module belongs to the Mitogen package. - """ - if PY24 and 'mitogen' in path: - linecache.cache[path] = ( - len(data), - 0.0, - [line+'\n' for line in data.splitlines()], - path, - ) - def _install_handler(self, router): router.add_handler( fn=self._on_load_module, @@ -1584,7 +1581,7 @@ class Importer(object): try: self._cache[fullname] = tup if tup[2] is not None and PY24: - self._update_linecache( + _update_linecache( path='master:' + tup[2], data=zlib.decompress(tup[3]) )