From dec91944ee5e5e7bc5b02ab9756aaa4a9af08cfb Mon Sep 17 00:00:00 2001 From: Armin Ronacher Date: Mon, 20 May 2013 12:11:02 +0100 Subject: Disable py_compile for Python 3 and pypy --- CHANGES | 1 + jinja2/_compat.py | 1 + jinja2/environment.py | 23 +++++++++++++++-------- jinja2/testsuite/loader.py | 28 +++++++++++++--------------- 4 files changed, 30 insertions(+), 23 deletions(-) diff --git a/CHANGES b/CHANGES index 7dac016..2932d6a 100644 --- 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 ----------- diff --git a/jinja2/_compat.py b/jinja2/_compat.py index 97f6354..8fa8a49 100644 --- a/jinja2/_compat.py +++ b/jinja2/_compat.py @@ -13,6 +13,7 @@ import sys PY2 = sys.version_info[0] == 2 +PYPY = hasattr(sys, 'pypy_translation_info') _identity = lambda x: x diff --git a/jinja2/environment.py b/jinja2/environment.py index ca46019..fad5e23 100644 --- a/jinja2/environment.py +++ b/jinja2/environment.py @@ -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: diff --git a/jinja2/testsuite/loader.py b/jinja2/testsuite/loader.py index 3412d15..a7350aa 100644 --- a/jinja2/testsuite/loader.py +++ b/jinja2/testsuite/loader.py @@ -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 -- cgit v1.2.1