Skip to content
Myr

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

1 file+26 added−14 removed

modified:jinja2/bccache.py+26 added-14 removed
18 unmodified lines
19import marshal20import tempfile21import cPickle as pickle22import fnmatch23from cStringIO import StringIO23try:24 from hashlib import sha125except ImportError:26 from sha import new as sha127from jinja2.utils import open_if_exists282930# marshal works better on 3.x, one hack less required31if sys.version_info > (3, 0):32 from io import BytesIO33 marshal_dump = marshal.dump34 marshal_load = marshal.load35else:36 from cStringIO import StringIO as BytesIO3738 def marshal_dump(code, f):39 if isinstance(f, file):40 marshal.dump(code, f)41 else:42 f.write(marshal.dumps(code))4344 def marshal_load(f):45 if isinstance(f, file):46 return marshal.load(f)47 return marshal.loads(f.read())484950bc_version = 25152# magic version used to only change with new jinja versions. With 2.653# we change this to also take Python version changes into account. The
35 unmodified lines
89 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())9495 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))102103 def bytecode_from_string(self, string):104 """Load bytecode from a string."""94 self.load_bytecode(StringIO(string))105 self.load_bytecode(BytesIO(string))106107 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()112113
49 unmodified lines
163 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()172173 def get_source_checksum(self, source):174 """Returns a checksum for the source."""
127 unmodified lines