Byte-compile one Python source file to Python bytecode. :param file: The source file name. :param cfile: The target byte compiled file name. When not given, this defaults to the PEP 3147/PEP 488 location. :param dfile: Purported file name, i.e. the file name that shows up in
(file, cfile=None, dfile=None, doraise=False, optimize=-1,
invalidation_mode=None, quiet=0)
| 77 | |
| 78 | |
| 79 | def compile(file, cfile=None, dfile=None, doraise=False, optimize=-1, |
| 80 | invalidation_mode=None, quiet=0): |
| 81 | """Byte-compile one Python source file to Python bytecode. |
| 82 | |
| 83 | :param file: The source file name. |
| 84 | :param cfile: The target byte compiled file name. When not given, this |
| 85 | defaults to the PEP 3147/PEP 488 location. |
| 86 | :param dfile: Purported file name, i.e. the file name that shows up in |
| 87 | error messages. Defaults to the source file name. |
| 88 | :param doraise: Flag indicating whether or not an exception should be |
| 89 | raised when a compile error is found. If an exception occurs and this |
| 90 | flag is set to False, a string indicating the nature of the exception |
| 91 | will be printed, and the function will return to the caller. If an |
| 92 | exception occurs and this flag is set to True, a PyCompileError |
| 93 | exception will be raised. |
| 94 | :param optimize: The optimization level for the compiler. Valid values |
| 95 | are -1, 0, 1 and 2. A value of -1 means to use the optimization |
| 96 | level of the current interpreter, as given by -O command line options. |
| 97 | :param invalidation_mode: |
| 98 | :param quiet: Return full output with False or 0, errors only with 1, |
| 99 | and no output with 2. |
| 100 | |
| 101 | :return: Path to the resulting byte compiled file. |
| 102 | |
| 103 | Note that it isn't necessary to byte-compile Python modules for |
| 104 | execution efficiency -- Python itself byte-compiles a module when |
| 105 | it is loaded, and if it can, writes out the bytecode to the |
| 106 | corresponding .pyc file. |
| 107 | |
| 108 | However, if a Python installation is shared between users, it is a |
| 109 | good idea to byte-compile all modules upon installation, since |
| 110 | other users may not be able to write in the source directories, |
| 111 | and thus they won't be able to write the .pyc file, and then |
| 112 | they would be byte-compiling every module each time it is loaded. |
| 113 | This can slow down program start-up considerably. |
| 114 | |
| 115 | See compileall.py for a script/module that uses this module to |
| 116 | byte-compile all installed files (or all files in selected |
| 117 | directories). |
| 118 | |
| 119 | Do note that FileExistsError is raised if cfile ends up pointing at a |
| 120 | non-regular file or symlink. Because the compilation uses a file renaming, |
| 121 | the resulting file would be regular and thus not the same type of file as |
| 122 | it was previously. |
| 123 | """ |
| 124 | if invalidation_mode is None: |
| 125 | invalidation_mode = _get_default_invalidation_mode() |
| 126 | if cfile is None: |
| 127 | if optimize >= 0: |
| 128 | optimization = optimize if optimize >= 1 else '' |
| 129 | cfile = importlib.util.cache_from_source(file, |
| 130 | optimization=optimization) |
| 131 | else: |
| 132 | cfile = importlib.util.cache_from_source(file) |
| 133 | if os.path.islink(cfile): |
| 134 | msg = ('{} is a symlink and will be changed into a regular file if ' |
| 135 | 'import writes a byte-compiled file to it') |
| 136 | raise FileExistsError(msg.format(cfile)) |
searching dependent graphs…