MCPcopy Create free account
hub / github.com/nodejs/node / Bucket

Class Bucket

deps/v8/third_party/jinja2/bccache.py:44–98  ·  view source on GitHub ↗

Buckets are used to store the bytecode for one template. It's created and initialized by the bytecode cache and passed to the loading functions. The buckets get an internal checksum from the cache assigned and use this to automatically reject outdated cache material. Individual byteco

Source from the content-addressed store, hash-verified

42
43
44class Bucket:
45 """Buckets are used to store the bytecode for one template. It's created
46 and initialized by the bytecode cache and passed to the loading functions.
47
48 The buckets get an internal checksum from the cache assigned and use this
49 to automatically reject outdated cache material. Individual bytecode
50 cache subclasses don't have to care about cache invalidation.
51 """
52
53 def __init__(self, environment: "Environment", key: str, checksum: str) -> None:
54 self.environment = environment
55 self.key = key
56 self.checksum = checksum
57 self.reset()
58
59 def reset(self) -> None:
60 """Resets the bucket (unloads the bytecode)."""
61 self.code: t.Optional[CodeType] = None
62
63 def load_bytecode(self, f: t.BinaryIO) -> None:
64 """Loads bytecode from a file or file like object."""
65 # make sure the magic header is correct
66 magic = f.read(len(bc_magic))
67 if magic != bc_magic:
68 self.reset()
69 return
70 # the source code of the file changed, we need to reload
71 checksum = pickle.load(f)
72 if self.checksum != checksum:
73 self.reset()
74 return
75 # if marshal_load fails then we need to reload
76 try:
77 self.code = marshal.load(f)
78 except (EOFError, ValueError, TypeError):
79 self.reset()
80 return
81
82 def write_bytecode(self, f: t.IO[bytes]) -> None:
83 """Dump the bytecode into the file or file like object passed."""
84 if self.code is None:
85 raise TypeError("can't write empty bucket")
86 f.write(bc_magic)
87 pickle.dump(self.checksum, f, 2)
88 marshal.dump(self.code, f)
89
90 def bytecode_from_string(self, string: bytes) -> None:
91 """Load bytecode from bytes."""
92 self.load_bytecode(BytesIO(string))
93
94 def bytecode_to_string(self) -> bytes:
95 """Return the bytecode as bytes."""
96 out = BytesIO()
97 self.write_bytecode(out)
98 return out.getvalue()
99
100
101class BytecodeCache:

Callers 1

get_bucketMethod · 0.70

Calls

no outgoing calls

Tested by

no test coverage detected