MCPcopy Index your code
hub / github.com/python/mypy / write_cache

Function write_cache

mypyc/codegen/emitmodule.py:417–467  ·  view source on GitHub ↗

Write out the cache information for modules. Each module has the following cache information written (which is in addition to the cache information written by mypy itself): * A serialized version of its mypyc IR, minus the bodies of functions. This allows code that depends on

(
    modules: ModuleIRs,
    result: BuildResult,
    group_map: dict[str, str | None],
    ctext: dict[str | None, list[tuple[str, str]]],
)

Source from the content-addressed store, hash-verified

415
416
417def write_cache(
418 modules: ModuleIRs,
419 result: BuildResult,
420 group_map: dict[str, str | None],
421 ctext: dict[str | None, list[tuple[str, str]]],
422) -> None:
423 """Write out the cache information for modules.
424
425 Each module has the following cache information written (which is
426 in addition to the cache information written by mypy itself):
427 * A serialized version of its mypyc IR, minus the bodies of
428 functions. This allows code that depends on it to use
429 these serialized data structures when compiling against it
430 instead of needing to recompile it. (Compiling against a
431 module requires access to both its mypy and mypyc data
432 structures.)
433 * The hash of the mypy metadata cache file for the module.
434 This is used to ensure that the mypyc cache and the mypy
435 cache are in sync and refer to the same version of the code.
436 This is particularly important if mypyc crashes/errors/is
437 stopped after mypy has written its cache but before mypyc has.
438 * The hashes of all the source file outputs for the group
439 the module is in. This is so that the module will be
440 recompiled if the source outputs are missing.
441 """
442
443 hashes = {}
444 for name, files in ctext.items():
445 hashes[name] = {file: compute_hash(data) for file, data in files}
446
447 # Write out cache data
448 for id, module in modules.items():
449 st = result.graph[id]
450
451 meta_path, _, _ = get_cache_names(id, st.xpath, result.manager.options)
452 # If the metadata isn't there, skip writing the cache.
453 try:
454 meta_data = result.manager.metastore.read(meta_path)
455 except OSError:
456 continue
457
458 newpath = get_state_ir_cache_name(st)
459 ir_data = {
460 "ir": module.serialize(),
461 "meta_hash": hash_digest(meta_data),
462 "src_hashes": hashes[group_map[id]],
463 }
464
465 result.manager.metastore.write(newpath, json_dumps(ir_data))
466
467 result.manager.metastore.commit()
468
469
470def load_scc_from_cache(

Callers 1

compile_modules_to_cFunction · 0.70

Calls 10

compute_hashFunction · 0.90
get_cache_namesFunction · 0.90
hash_digestFunction · 0.90
json_dumpsFunction · 0.90
get_state_ir_cache_nameFunction · 0.85
itemsMethod · 0.45
readMethod · 0.45
serializeMethod · 0.45
writeMethod · 0.45
commitMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…