this repo has no description
at trunk 35 lines 1.1 kB view raw
1# Copyright (c) Facebook, Inc. and its affiliates. (http://www.facebook.com) 2"""Load benchmark module file so bytecode is compiled and cached.""" 3import os 4import py_compile 5import runpy 6import sys 7 8 9def pyc_file(filename): 10 """Put the interpreter-tagged .pyc next to the .py so that __file__ 11 directives in benchmarks work.""" 12 basename = os.path.basename(filename) 13 stem, _ = os.path.splitext(basename) 14 containing_dir = os.path.dirname(filename) 15 return os.path.join(containing_dir, f"{stem}.{sys.implementation.cache_tag}.pyc") 16 17 18def main(): 19 main_file = sys.argv[1] 20 runpy.run_path(main_file) 21 pyc = pyc_file(main_file) 22 try: 23 # There's no good cache invalidation mechanism that takes into account 24 # both the interpreter running and the benchmark running. Remove the 25 # cached bytecode just in case. 26 os.remove(pyc) 27 except FileNotFoundError: 28 pass 29 # The tool that runs _compile_tool uses the stdout of this process to 30 # determine where the result .pyc is located. 31 print(py_compile.compile(main_file, pyc)) 32 33 34if __name__ == "__main__": 35 main()