Return (arcname, bytecode) for the path. Given a module name path, return the bytecode and archive name. For example, given /python/lib/string, return ('string', b' ').
(self, pathname, basename)
| 2273 | self.writestr(arcname, bytecode) |
| 2274 | |
| 2275 | def _get_code(self, pathname, basename): |
| 2276 | """Return (arcname, bytecode) for the path. |
| 2277 | |
| 2278 | Given a module name path, return the bytecode and archive |
| 2279 | name. For example, given /python/lib/string, return |
| 2280 | ('string', b'<bytecode of string>'). |
| 2281 | """ |
| 2282 | import importlib._bootstrap_external |
| 2283 | import importlib.machinery |
| 2284 | |
| 2285 | file_py = pathname + ".py" |
| 2286 | file_pyc = pathname + ".pyc" |
| 2287 | archivename = os.path.split(file_pyc)[1] |
| 2288 | |
| 2289 | loader = importlib.machinery.SourceFileLoader('<py_compile>', file_py) |
| 2290 | source_bytes = loader.get_data(file_py) |
| 2291 | try: |
| 2292 | if self.debug: |
| 2293 | print("Compiling", file_py) |
| 2294 | code = loader.source_to_code(source_bytes, archivename) |
| 2295 | except Exception as err: |
| 2296 | # Historically, this function prints messages here rather than raising |
| 2297 | # (see test_zipfile.test_write_filtered_python_package) |
| 2298 | from py_compile import PyCompileError |
| 2299 | print(PyCompileError(type(err), err, file_py).msg) |
| 2300 | |
| 2301 | archivename = os.path.split(file_py)[1] |
| 2302 | bytecode = source_bytes |
| 2303 | else: |
| 2304 | # Historically this function has used timestamp comparisons, so we |
| 2305 | # keep using it until someone makes that specific improvement. |
| 2306 | source_stats = loader.path_stats(file_py) |
| 2307 | bytecode = importlib._bootstrap_external._code_to_timestamp_pyc( |
| 2308 | code, source_stats['mtime'], source_stats['size']) |
| 2309 | |
| 2310 | if basename: |
| 2311 | archivename = "%s/%s" % (basename, archivename) |
| 2312 | |
| 2313 | return archivename, bytecode |
| 2314 | |
| 2315 | |
| 2316 | def main(args=None): |
no test coverage detected