From 72e73d4a9c4e055affa53314ab95534087f256c2 Mon Sep 17 00:00:00 2001 From: James Socol Date: Mon, 21 Sep 2015 13:41:10 -0400 Subject: [PATCH] Python 3.3 (shakes fist) --- jingo/__init__.py | 30 ++++++++++++++++++++++-------- 1 file changed, 22 insertions(+), 8 deletions(-) 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: