diff options
Diffstat (limited to 'Cheetah/ImportHooks.py')
-rw-r--r-- | Cheetah/ImportHooks.py | 35 |
1 files changed, 25 insertions, 10 deletions
diff --git a/Cheetah/ImportHooks.py b/Cheetah/ImportHooks.py index e2a26e9..2441fa2 100644 --- a/Cheetah/ImportHooks.py +++ b/Cheetah/ImportHooks.py @@ -7,8 +7,13 @@ To use these: Cheetah.ImportHooks.install() """ +try: + from importlib import invalidate_caches +except ImportError: + invalidate_caches = None import sys import os.path +import py_compile import types try: import builtins as builtin @@ -44,6 +49,7 @@ class CheetahDirOwner(DirOwner): _releaseLock = _lock.release templateFileExtensions = ('.tmpl',) + debuglevel = 0 def getmod(self, name): self._acquireLock() @@ -71,22 +77,31 @@ class CheetahDirOwner(DirOwner): self._releaseLock() def _compile(self, name, tmplPath): + if invalidate_caches: + invalidate_caches() + # @@ consider adding an ImportError raiser here code = str(Compiler(file=tmplPath, moduleName=name, mainClassName=name)) if _cacheDir: - __file__ = os.path.join(_cacheDir[0], - convertTmplPathToModuleName(tmplPath)) \ - + '.py' - try: - open(__file__, 'w').write(code) - except OSError: - # @@ TR: need to add some error code here - traceback.print_exc(file=sys.stderr) - __file__ = tmplPath + __file__ = os.path.join( + _cacheDir[0], convertTmplPathToModuleName(tmplPath)) + '.py' else: + __file__ = os.path.splitext(tmplPath)[0] + '.py' + try: + with open(__file__, 'w') as _py_file: + _py_file.write(code) + except (IOError, OSError): + # @@ TR: need to add some error code here + if self.debuglevel > 0: + traceback.print_exc(file=sys.stderr) __file__ = tmplPath - co = compile(code+'\n', __file__, 'exec') + else: + try: + py_compile.compile(__file__) + except IOError: + pass + co = compile(code + '\n', __file__, 'exec') mod = types.ModuleType(name) mod.__file__ = co.co_filename |