]> git.ipfire.org Git - thirdparty/jinja.git/commitdiff
Disable py_compile for Python 3 and pypy
authorArmin Ronacher <armin.ronacher@active-4.com>
Mon, 20 May 2013 11:11:02 +0000 (12:11 +0100)
committerArmin Ronacher <armin.ronacher@active-4.com>
Mon, 20 May 2013 11:11:02 +0000 (12:11 +0100)
CHANGES
jinja2/_compat.py
jinja2/environment.py
jinja2/testsuite/loader.py

diff --git a/CHANGES b/CHANGES
index 7dac016565a4971f92c3677674a0057e2d743cf5..2932d6a012f186810a857444deeaefd82e313edf 100644 (file)
--- a/CHANGES
+++ b/CHANGES
@@ -30,6 +30,7 @@ Version 2.7
   filters.
 - Added support for `loop.depth` to figure out how deep inside a recursive
   loop the code is.
+- Disabled py_compile for pypy and python 3.
 
 Version 2.6
 -----------
index 97f6354be5a09a766a8988bd24b882ae4eff38d1..8fa8a49a0a22c22d3b58e23daacca4a163c365c8 100644 (file)
@@ -13,6 +13,7 @@
 import sys
 
 PY2 = sys.version_info[0] == 2
+PYPY = hasattr(sys, 'pypy_translation_info')
 _identity = lambda x: x
 
 
index ca4601974697fc7a3d2a45b5a3ea71edcb186587..fad5e237733bfd14223835cdafa3d9699f7df08a 100644 (file)
@@ -29,7 +29,7 @@ from jinja2.utils import import_string, LRUCache, Markup, missing, \
      concat, consume, internalcode
 from jinja2._compat import imap, ifilter, string_types, iteritems, \
      text_type, reraise, implements_iterator, implements_to_string, \
-     get_next, encode_filename
+     get_next, encode_filename, PY2, PYPY
 from functools import reduce
 
 
@@ -617,7 +617,9 @@ class Environment(object):
         to `False` and you will get an exception on syntax errors.
 
         If `py_compile` is set to `True` .pyc files will be written to the
-        target instead of standard .py files.
+        target instead of standard .py files.  This flag does not do anything
+        on pypy and Python 3 where pyc files are not picked up by itself and
+        don't give much benefit.
 
         .. versionadded:: 2.4
         """
@@ -627,13 +629,18 @@ class Environment(object):
             log_function = lambda x: None
 
         if py_compile:
-            import imp, marshal
-            py_header = imp.get_magic() + \
-                u'\xff\xff\xff\xff'.encode('iso-8859-15')
+            if not PY2 or PYPY:
+                from warnings import warn
+                warn(Warning('py_compile has no effect on pypy or Python 3'))
+                py_compile = False
+            else:
+                import imp, marshal
+                py_header = imp.get_magic() + \
+                    u'\xff\xff\xff\xff'.encode('iso-8859-15')
 
-            # Python 3.3 added a source filesize to the header
-            if sys.version_info >= (3, 3):
-                py_header += u'\x00\x00\x00\x00'.encode('iso-8859-15')
+                # Python 3.3 added a source filesize to the header
+                if sys.version_info >= (3, 3):
+                    py_header += u'\x00\x00\x00\x00'.encode('iso-8859-15')
 
         def write_file(filename, data, mode):
             if zip:
index 3412d154eca62f5855bab6f025753b4f727378f9..a7350aab9fbe4b33c1c922ee21fbf46c019c545f 100644 (file)
@@ -19,6 +19,7 @@ from jinja2.testsuite import JinjaTestCase, dict_loader, \
      choice_loader, prefix_loader
 
 from jinja2 import Environment, loaders
+from jinja2._compat import PYPY, PY2
 from jinja2.loaders import split_template_path
 from jinja2.exceptions import TemplateNotFound
 
@@ -181,17 +182,18 @@ class ModuleLoaderTestCase(JinjaTestCase):
 
         assert name not in sys.modules
 
-    def test_byte_compilation(self):
-        log = self.compile_down(py_compile=True)
-        assert 'Byte-compiled "a/test.html"' in log
-        tmpl1 = self.mod_env.get_template('a/test.html')
-        mod = self.mod_env.loader.module. \
-            tmpl_3c4ddf650c1a73df961a6d3d2ce2752f1b8fd490
-        assert mod.__file__.endswith('.pyc')
+    # This test only makes sense on non-pypy python 2
+    if PY2 and not PYPY:
+        def test_byte_compilation(self):
+            log = self.compile_down(py_compile=True)
+            assert 'Byte-compiled "a/test.html"' in log
+            tmpl1 = self.mod_env.get_template('a/test.html')
+            mod = self.mod_env.loader.module. \
+                tmpl_3c4ddf650c1a73df961a6d3d2ce2752f1b8fd490
+            assert mod.__file__.endswith('.pyc')
 
     def test_choice_loader(self):
-        log = self.compile_down(py_compile=True)
-        assert 'Byte-compiled "a/test.html"' in log
+        log = self.compile_down()
 
         self.mod_env.loader = loaders.ChoiceLoader([
             self.mod_env.loader,
@@ -204,8 +206,7 @@ class ModuleLoaderTestCase(JinjaTestCase):
         self.assert_equal(tmpl2.render(), 'DICT_TEMPLATE')
 
     def test_prefix_loader(self):
-        log = self.compile_down(py_compile=True)
-        assert 'Byte-compiled "a/test.html"' in log
+        log = self.compile_down()
 
         self.mod_env.loader = loaders.PrefixLoader({
             'MOD':      self.mod_env.loader,
@@ -221,8 +222,5 @@ class ModuleLoaderTestCase(JinjaTestCase):
 def suite():
     suite = unittest.TestSuite()
     suite.addTest(unittest.makeSuite(LoaderTestCase))
-    # pypy currently does not support compiled jinja modules because
-    # of changes in the load system.
-    if not hasattr(sys, 'pypy_version_info'):
-        suite.addTest(unittest.makeSuite(ModuleLoaderTestCase))
+    suite.addTest(unittest.makeSuite(ModuleLoaderTestCase))
     return suite