diff --git a/Cheetah/ImportManager.py b/Cheetah/ImportManager.py index 17ab4808..2922678a 100644 --- a/Cheetah/ImportManager.py +++ b/Cheetah/ImportManager.py @@ -433,7 +433,7 @@ def importHook(self, name, globals=None, locals=None, # first see if we could be importing a relative name _sys_modules_get = sys.modules.get contexts = [None] - if globals: + if (PY2 or not name or level > 0) and globals: importernm = globals.get('__name__', '') if importernm: if hasattr(_sys_modules_get(importernm), '__path__'): @@ -477,7 +477,8 @@ def importHook(self, name, globals=None, locals=None, if i < len(nmparts): if ctx and hasattr(sys.modules[ctx], nmparts[i]): return sys.modules[nmparts[0]] - del sys.modules[fqname] + if fqname in sys.modules: + del sys.modules[fqname] raise ModuleNotFoundError("No module named %s" % fqname) if fromlist is None: if context: diff --git a/docs/news.rst b/docs/news.rst index d821b913..6277d0b9 100644 --- a/docs/news.rst +++ b/docs/news.rst @@ -9,6 +9,8 @@ Bug fixes: - Fixed ``ImportHooks``: it must raise ``ModuleNotFoundError`` instead of ``ImportError``. + - Fixed absolute import in ``ImportHooks`` under Python 3. + - Fixed ``Template.webInput``: Use ``urllib.parse.parse_qs`` instead of ``cgi.FieldStorage``; Python 3.13 dropped ``cgi``.