diff --git a/Cheetah/ImportManager.py b/Cheetah/ImportManager.py index 676a0e63..6337f764 100644 --- a/Cheetah/ImportManager.py +++ b/Cheetah/ImportManager.py @@ -21,7 +21,8 @@ import py_compile import sys from .compat import PY2, string_type, new_module, get_suffixes, \ - load_module_from_file, ModuleNotFoundError, RecursionError + load_module_from_file, cache_from_source, \ + ModuleNotFoundError, RecursionError if PY2: import imp else: @@ -192,7 +193,10 @@ def getmod(self, nm, getsuffixes=get_suffixes, py = pyc = None for pth, ispkg, pkgpth in possibles: for ext, mode, typ in getsuffixes(): - attempt = pth + ext + if typ == 2: # imp.PY_COMPILED + attempt = cache_from_source(pth + '.py') + else: + attempt = pth + ext try: st = _os_stat(attempt) except Exception: @@ -202,8 +206,10 @@ def getmod(self, nm, getsuffixes=get_suffixes, return load_module_from_file(nm, nm, attempt) elif typ == 1: # imp.PY_SOURCE py = (attempt, st) - else: + elif typ == 2: # imp.PY_COMPILED pyc = (attempt, st) + else: + raise ValueError("Unknown module type %d" % typ) if py or pyc: break if py is None and pyc is None: diff --git a/Cheetah/compat.py b/Cheetah/compat.py index 55bc552b..78cfcddd 100644 --- a/Cheetah/compat.py +++ b/Cheetah/compat.py @@ -37,6 +37,16 @@ def load_module_from_file(base_name, module_name, filename): new_module = imp.new_module get_suffixes = imp.get_suffixes + def cache_from_source(path, debug_override=None): + assert path.endswith('.py'), path + if debug_override is None: + debug_override = __debug__ + if debug_override: + suffix = 'c' + else: + suffix = 'o' + return path + suffix + else: import importlib.machinery import importlib.util @@ -59,3 +69,5 @@ def get_suffixes(): ] return extensions + source + bytecode + + from importlib.util import cache_from_source # noqa: F401 unused diff --git a/docs/news.rst b/docs/news.rst index aca4e879..b325b33e 100644 --- a/docs/news.rst +++ b/docs/news.rst @@ -11,6 +11,9 @@ Bug fixes: - Fixed absolute import in ``ImportHooks`` under Python 3. + - Use ``cache_from_source`` in ``ImportManager`` to find out + ``.pyc``/``.pyo`` byte-code files. + - Fixed ``Template.webInput``: Use ``urllib.parse.parse_qs`` instead of ``cgi.FieldStorage``; Python 3.13 dropped ``cgi``.