Loads bytecode from a file or file like object.
(self, f: t.BinaryIO)
| 63 | self.code: t.Optional[CodeType] = None |
| 64 | |
| 65 | def load_bytecode(self, f: t.BinaryIO) -> None: |
| 66 | """Loads bytecode from a file or file like object.""" |
| 67 | # make sure the magic header is correct |
| 68 | magic = f.read(len(bc_magic)) |
| 69 | if magic != bc_magic: |
| 70 | self.reset() |
| 71 | return |
| 72 | # the source code of the file changed, we need to reload |
| 73 | checksum = pickle.load(f) |
| 74 | if self.checksum != checksum: |
| 75 | self.reset() |
| 76 | return |
| 77 | # if marshal_load fails then we need to reload |
| 78 | try: |
| 79 | self.code = marshal.load(f) |
| 80 | except (EOFError, ValueError, TypeError): |
| 81 | self.reset() |
| 82 | return |
| 83 | |
| 84 | def write_bytecode(self, f: t.IO[bytes]) -> None: |
| 85 | """Dump the bytecode into the file or file like object passed.""" |
no test coverage detected