diff --git a/jingo/__init__.py b/jingo/__init__.py index dbed164..e2b0db6 100644 --- a/jingo/__init__.py +++ b/jingo/__init__.py @@ -6,12 +6,32 @@ import functools import logging import re +from django.apps import apps +from django.conf import settings +from django.template.base import Origin, TemplateDoesNotExist +from django.template.loader import BaseLoader +from django.utils.importlib import import_module + try: import importlib.util - def has_helpers(path): - return importlib.util.find_spec('helpers', path) is not None + if hasattr(importlib.util, 'find_spec'): # Py3>=3.4 + def has_helpers(path): + return importlib.util.find_spec('helpers', path) is not None + else: # Py3<3.4 + def has_helpers(path): + # For Python 3.3, just try to import the module. Unfortunately, + # this changes the contract slightly for Python 3.3: if there is an + # module but this raises a legitimate ImportError, jingo will act + # as if the module doesn't exist. The intent is that we raise + # legitimate ImportErrors but ignore missing modules. + try: + import_module('helpers', path) + return True + except ImportError: + return False except ImportError: import imp + def has_helpers(path): try: imp.find_module('helpers', path) @@ -19,12 +39,6 @@ except ImportError: except ImportError: return False -from django.apps import apps -from django.conf import settings -from django.template.base import Origin, TemplateDoesNotExist -from django.template.loader import BaseLoader -from django.utils.importlib import import_module - import jinja2 try: