Skip to content

Commit

Permalink
Fix(ImportManager): Use cache_from_source to find byte-code files
Browse files Browse the repository at this point in the history
  • Loading branch information
phdru committed Nov 16, 2024
1 parent 917752e commit b6499ba
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 3 deletions.
12 changes: 9 additions & 3 deletions Cheetah/ImportManager.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand All @@ -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:
Expand Down
12 changes: 12 additions & 0 deletions Cheetah/compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -59,3 +69,5 @@ def get_suffixes():
]

return extensions + source + bytecode

from importlib.util import cache_from_source # noqa: F401 unused
3 changes: 3 additions & 0 deletions docs/news.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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``.

Expand Down

0 comments on commit b6499ba

Please sign in to comment.