Skip to content


Subversion checkout URL

You can clone with
Download ZIP


Fix BytecodeCache error on Python 3 #240

wants to merge 2 commits into from

4 participants


This adds a test that reproduces an error with BytecodeCache on Python 3:

marca@marca-mac:~/dev/git-repos/jinja2$ .tox/py33/bin/python -v jinja2.testsuite.bytecode_cache.ByteCodeCacheTestCase
test_simple (jinja2.testsuite.bytecode_cache.ByteCodeCacheTestCase) ... ERROR

ERROR: test_simple (jinja2.testsuite.bytecode_cache.ByteCodeCacheTestCase)
Traceback (most recent call last):
  File "/Users/marca/dev/git-repos/jinja2/jinja2/testsuite/", line 29, in test_simple
    tmpl = env.get_template('test.html')
  File "/Users/marca/dev/git-repos/jinja2/jinja2/", line 791, in get_template
    return self._load_template(name, self.make_globals(globals))
  File "/Users/marca/dev/git-repos/jinja2/jinja2/", line 765, in _load_template
    template = self.loader.load(self, name, globals)
  File "/Users/marca/dev/git-repos/jinja2/jinja2/", line 119, in load
    bucket = bcc.get_bucket(environment, name, filename, source)
  File "/Users/marca/dev/git-repos/jinja2/jinja2/", line 176, in get_bucket
    key = self.get_cache_key(name, filename)
  File "/Users/marca/dev/git-repos/jinja2/jinja2/", line 163, in get_cache_key
    if isinstance(filename, unicode):
NameError: global name 'unicode' is not defined

Ran 1 test in 0.001s

FAILED (errors=1)

and then fixes it by replacing unicode with jinja2._compat.text_type.

msabramo added some commits
@msabramo msabramo Add test (jinja2.testsuite.bytecode_cache.ByteCodeCacheTestCase) for
BytecodeCache that fails on Python 3 because it uses `unicode`
@msabramo msabramo Fix Python 3 compatibility of BytecodeCache by checking for `if
isinstance(filename, text_type)` instead of `if isinstance(filename,

Oops. I just noticed that this is almost identical to PR #228.

This also fixes issue #227, which I don't think should've been closed.


@mitsuhiko Could you please merge this PR? This bug breaks Jinja2 on Python 3.


+1, this breaks pyramid_jinja2


Thanks, fixed in 2.7-maintenance and master.

@mitsuhiko mitsuhiko closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Jun 6, 2013
  1. @msabramo

    Add test (jinja2.testsuite.bytecode_cache.ByteCodeCacheTestCase) for

    msabramo committed
    BytecodeCache that fails on Python 3 because it uses `unicode`
  2. @msabramo

    Fix Python 3 compatibility of BytecodeCache by checking for `if

    msabramo committed
    isinstance(filename, text_type)` instead of `if isinstance(filename,
This page is out of date. Refresh to see the latest.
4 jinja2/
@@ -21,7 +21,7 @@
import fnmatch
from hashlib import sha1
from jinja2.utils import open_if_exists
-from jinja2._compat import BytesIO, pickle, PY2
+from jinja2._compat import BytesIO, pickle, PY2, text_type
# marshal works better on 3.x, one hack less required
@@ -160,7 +160,7 @@ def get_cache_key(self, name, filename=None):
hash = sha1(name.encode('utf-8'))
if filename is not None:
filename = '|' + filename
- if isinstance(filename, unicode):
+ if isinstance(filename, text_type):
filename = filename.encode('utf-8')
return hash.hexdigest()
3  jinja2/testsuite/
@@ -123,7 +123,7 @@ def loadTestsFromName(self, name, module=None):
def suite():
from jinja2.testsuite import ext, filters, tests, core_tags, \
loader, inheritance, imports, lexnparse, security, api, \
- regression, debug, utils, doctests
+ regression, debug, utils, bytecode_cache, doctests
suite = unittest.TestSuite()
@@ -138,6 +138,7 @@ def suite():
+ suite.addTest(bytecode_cache.suite())
# doctests will not run on python 3 currently. Too many issues
# with that, do not test that on that platform.
37 jinja2/testsuite/
@@ -0,0 +1,37 @@
+# -*- coding: utf-8 -*-
+ jinja2.testsuite.bytecode_cache
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ Test bytecode caching
+ :copyright: (c) 2010 by the Jinja Team.
+ :license: BSD, see LICENSE for more details.
+import unittest
+from jinja2.testsuite import JinjaTestCase, package_loader
+from jinja2 import Environment
+from jinja2.bccache import FileSystemBytecodeCache
+from jinja2.exceptions import TemplateNotFound
+bytecode_cache = FileSystemBytecodeCache()
+env = Environment(
+ loader=package_loader,
+ bytecode_cache=bytecode_cache,
+class ByteCodeCacheTestCase(JinjaTestCase):
+ def test_simple(self):
+ tmpl = env.get_template('test.html')
+ assert tmpl.render().strip() == 'BAR'
+ self.assert_raises(TemplateNotFound, env.get_template, 'missing.html')
+def suite():
+ suite = unittest.TestSuite()
+ suite.addTest(unittest.makeSuite(ByteCodeCacheTestCase))
+ return suite
Something went wrong with that request. Please try again.