Various tweaks to support bccache on Python 3. This fixes #34
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Armin Ronacher · committed by Armin Ronacher · parent d01d34b · Browse files · 1 file·+26 added−14 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
1 file+26 added−14 removed
- other:+26 added−14 removed
Provenance
- Commit
086174c- Tree
9cb5448- Author
Armin Ronacher <armin.ronacher@active-4.com>- Committer
Armin Ronacher <armin.ronacher@active-4.com>- Parent
d01d34b
1 file changed+26 added−14 removed
modified:jinja2/bccache.py+26 added-14 removed
18 unmodified lines19 import marshal20 import tempfile21 import cPickle as pickle22 import fnmatch23-from cStringIO import StringIO23 try:24 from hashlib import sha125 except ImportError:26 from sha import new as sha127 from jinja2.utils import open_if_exists28 29 30+# marshal works better on 3.x, one hack less required31+if sys.version_info > (3, 0):32+ from io import BytesIO33+ marshal_dump = marshal.dump34+ marshal_load = marshal.load35+else:36+ from cStringIO import StringIO as BytesIO37+38+ def marshal_dump(code, f):39+ if isinstance(f, file):40+ marshal.dump(code, f)41+ else:42+ f.write(marshal.dumps(code))43+44+ def marshal_load(f):45+ if isinstance(f, file):46+ return marshal.load(f)47+ return marshal.loads(f.read())48+49+50 bc_version = 251 52 # magic version used to only change with new jinja versions. With 2.653 # we change this to also take Python version changes into account. The35 unmodified lines89 checksum = pickle.load(f)90 if self.checksum != checksum:91 self.reset()92 return74- # now load the code. Because marshal is not able to load75- # from arbitrary streams we have to work around that76- if isinstance(f, file):77- self.code = marshal.load(f)93+ self.code = marshal_load(f)78- else:79- self.code = marshal.loads(f.read())94 95 def write_bytecode(self, f):96 """Dump the bytecode into the file or file like object passed."""97 if self.code is None:98 raise TypeError('can\'t write empty bucket')99 f.write(bc_magic)100 pickle.dump(self.checksum, f, 2)87- if isinstance(f, file):88- marshal.dump(self.code, f)101+ marshal_dump(code, f)89- else:90- f.write(marshal.dumps(self.code))102 103 def bytecode_from_string(self, string):104 """Load bytecode from a string."""94- self.load_bytecode(StringIO(string))105+ self.load_bytecode(BytesIO(string))106 107 def bytecode_to_string(self):108 """Return the bytecode as string."""98- out = StringIO()109+ out = BytesIO()110 self.write_bytecode(out)111 return out.getvalue()112 113 49 unmodified lines163 def get_cache_key(self, name, filename=None):164 """Returns the unique hash key for this template name."""165 hash = sha1(name.encode('utf-8'))166 if filename is not None:167+ filename = '|' + filename168 if isinstance(filename, unicode):169 filename = filename.encode('utf-8')158- hash.update('|' + filename)170+ hash.update(filename)171 return hash.hexdigest()172 173 def get_source_checksum(self, source):174 """Returns a checksum for the source."""127 unmodified lines