Brett Cannon | f299abd | 2015-04-13 14:21:02 -0400 | [diff] [blame] | 1 | """Routine to "compile" a .py file to a .pyc file. |
Guido van Rossum | 63566e2 | 1998-01-19 04:01:26 +0000 | [diff] [blame] | 2 | |
| 3 | This module has intimate knowledge of the format of .pyc files. |
| 4 | """ |
Guido van Rossum | 3bb5448 | 1994-08-29 10:52:58 +0000 | [diff] [blame] | 5 | |
Benjamin Peterson | 42aa93b | 2017-12-09 10:26:52 -0800 | [diff] [blame] | 6 | import enum |
Eric Snow | 32439d6 | 2015-05-02 19:15:18 -0600 | [diff] [blame] | 7 | import importlib._bootstrap_external |
Brett Cannon | 14581d5 | 2013-01-26 08:48:36 -0500 | [diff] [blame] | 8 | import importlib.machinery |
Brett Cannon | df96068 | 2013-06-15 14:07:21 -0400 | [diff] [blame] | 9 | import importlib.util |
Fred Drake | a96f1a3 | 2002-08-21 20:23:22 +0000 | [diff] [blame] | 10 | import os |
Brett Cannon | 33915eb | 2013-06-14 18:33:00 -0400 | [diff] [blame] | 11 | import os.path |
Fred Drake | a96f1a3 | 2002-08-21 20:23:22 +0000 | [diff] [blame] | 12 | import sys |
| 13 | import traceback |
| 14 | |
Benjamin Peterson | 42aa93b | 2017-12-09 10:26:52 -0800 | [diff] [blame] | 15 | __all__ = ["compile", "main", "PyCompileError", "PycInvalidationMode"] |
Martin v. Löwis | 0c6774d | 2003-01-15 11:51:06 +0000 | [diff] [blame] | 16 | |
| 17 | |
| 18 | class PyCompileError(Exception): |
| 19 | """Exception raised when an error occurs while attempting to |
| 20 | compile the file. |
| 21 | |
| 22 | To raise this exception, use |
| 23 | |
| 24 | raise PyCompileError(exc_type,exc_value,file[,msg]) |
| 25 | |
| 26 | where |
Tim Peters | 2c60f7a | 2003-01-29 03:49:43 +0000 | [diff] [blame] | 27 | |
Martin v. Löwis | 0c6774d | 2003-01-15 11:51:06 +0000 | [diff] [blame] | 28 | exc_type: exception type to be used in error message |
| 29 | type name can be accesses as class variable |
| 30 | 'exc_type_name' |
Tim Peters | 2c60f7a | 2003-01-29 03:49:43 +0000 | [diff] [blame] | 31 | |
Martin v. Löwis | 0c6774d | 2003-01-15 11:51:06 +0000 | [diff] [blame] | 32 | exc_value: exception value to be used in error message |
| 33 | can be accesses as class variable 'exc_value' |
Tim Peters | 2c60f7a | 2003-01-29 03:49:43 +0000 | [diff] [blame] | 34 | |
Martin v. Löwis | 0c6774d | 2003-01-15 11:51:06 +0000 | [diff] [blame] | 35 | file: name of file being compiled to be used in error message |
| 36 | can be accesses as class variable 'file' |
Tim Peters | 2c60f7a | 2003-01-29 03:49:43 +0000 | [diff] [blame] | 37 | |
Martin v. Löwis | 0c6774d | 2003-01-15 11:51:06 +0000 | [diff] [blame] | 38 | msg: string message to be written as error message |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 39 | If no value is given, a default exception message will be |
| 40 | given, consistent with 'standard' py_compile output. |
| 41 | message (or default) can be accesses as class variable |
| 42 | 'msg' |
Tim Peters | 2c60f7a | 2003-01-29 03:49:43 +0000 | [diff] [blame] | 43 | |
Martin v. Löwis | 0c6774d | 2003-01-15 11:51:06 +0000 | [diff] [blame] | 44 | """ |
Tim Peters | 2c60f7a | 2003-01-29 03:49:43 +0000 | [diff] [blame] | 45 | |
Martin v. Löwis | 0c6774d | 2003-01-15 11:51:06 +0000 | [diff] [blame] | 46 | def __init__(self, exc_type, exc_value, file, msg=''): |
| 47 | exc_type_name = exc_type.__name__ |
| 48 | if exc_type is SyntaxError: |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 49 | tbtext = ''.join(traceback.format_exception_only( |
| 50 | exc_type, exc_value)) |
Martin v. Löwis | 0c6774d | 2003-01-15 11:51:06 +0000 | [diff] [blame] | 51 | errmsg = tbtext.replace('File "<string>"', 'File "%s"' % file) |
| 52 | else: |
| 53 | errmsg = "Sorry: %s: %s" % (exc_type_name,exc_value) |
Tim Peters | 2c60f7a | 2003-01-29 03:49:43 +0000 | [diff] [blame] | 54 | |
Martin v. Löwis | 0c6774d | 2003-01-15 11:51:06 +0000 | [diff] [blame] | 55 | Exception.__init__(self,msg or errmsg,exc_type_name,exc_value,file) |
| 56 | |
| 57 | self.exc_type_name = exc_type_name |
| 58 | self.exc_value = exc_value |
| 59 | self.file = file |
| 60 | self.msg = msg or errmsg |
| 61 | |
| 62 | def __str__(self): |
| 63 | return self.msg |
| 64 | |
Skip Montanaro | c62c81e | 2001-02-12 02:00:42 +0000 | [diff] [blame] | 65 | |
Benjamin Peterson | 42aa93b | 2017-12-09 10:26:52 -0800 | [diff] [blame] | 66 | class PycInvalidationMode(enum.Enum): |
| 67 | TIMESTAMP = 1 |
| 68 | CHECKED_HASH = 2 |
| 69 | UNCHECKED_HASH = 3 |
| 70 | |
| 71 | |
Elvis Pranskevichus | a6b3ec5 | 2018-10-10 12:43:14 -0400 | [diff] [blame] | 72 | def _get_default_invalidation_mode(): |
| 73 | if os.environ.get('SOURCE_DATE_EPOCH'): |
| 74 | return PycInvalidationMode.CHECKED_HASH |
| 75 | else: |
| 76 | return PycInvalidationMode.TIMESTAMP |
| 77 | |
| 78 | |
Benjamin Peterson | 42aa93b | 2017-12-09 10:26:52 -0800 | [diff] [blame] | 79 | def compile(file, cfile=None, dfile=None, doraise=False, optimize=-1, |
Joannah Nanjekye | 2e33ecd | 2019-05-28 13:29:04 -0300 | [diff] [blame] | 80 | invalidation_mode=None, quiet=0): |
Guido van Rossum | 63566e2 | 1998-01-19 04:01:26 +0000 | [diff] [blame] | 81 | """Byte-compile one Python source file to Python bytecode. |
| 82 | |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 83 | :param file: The source file name. |
| 84 | :param cfile: The target byte compiled file name. When not given, this |
Brett Cannon | f299abd | 2015-04-13 14:21:02 -0400 | [diff] [blame] | 85 | defaults to the PEP 3147/PEP 488 location. |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 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. |
Georg Brandl | 8334fd9 | 2010-12-04 10:26:46 +0000 | [diff] [blame] | 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. |
Benjamin Peterson | 42aa93b | 2017-12-09 10:26:52 -0800 | [diff] [blame] | 97 | :param invalidation_mode: |
Joannah Nanjekye | 2e33ecd | 2019-05-28 13:29:04 -0300 | [diff] [blame] | 98 | :param quiet: Return full output with False or 0, errors only with 1, |
| 99 | and no output with 2. |
Georg Brandl | 8334fd9 | 2010-12-04 10:26:46 +0000 | [diff] [blame] | 100 | |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 101 | :return: Path to the resulting byte compiled file. |
Tim Peters | 2c60f7a | 2003-01-29 03:49:43 +0000 | [diff] [blame] | 102 | |
Guido van Rossum | 63566e2 | 1998-01-19 04:01:26 +0000 | [diff] [blame] | 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 |
Brett Cannon | f299abd | 2015-04-13 14:21:02 -0400 | [diff] [blame] | 106 | corresponding .pyc file. |
Guido van Rossum | 63566e2 | 1998-01-19 04:01:26 +0000 | [diff] [blame] | 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, |
Brett Cannon | f299abd | 2015-04-13 14:21:02 -0400 | [diff] [blame] | 111 | and thus they won't be able to write the .pyc file, and then |
Guido van Rossum | 63566e2 | 1998-01-19 04:01:26 +0000 | [diff] [blame] | 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). |
Brett Cannon | 33915eb | 2013-06-14 18:33:00 -0400 | [diff] [blame] | 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. |
Guido van Rossum | 63566e2 | 1998-01-19 04:01:26 +0000 | [diff] [blame] | 123 | """ |
Elvis Pranskevichus | a6b3ec5 | 2018-10-10 12:43:14 -0400 | [diff] [blame] | 124 | if invalidation_mode is None: |
| 125 | invalidation_mode = _get_default_invalidation_mode() |
Brett Cannon | 14581d5 | 2013-01-26 08:48:36 -0500 | [diff] [blame] | 126 | if cfile is None: |
| 127 | if optimize >= 0: |
Brett Cannon | f299abd | 2015-04-13 14:21:02 -0400 | [diff] [blame] | 128 | optimization = optimize if optimize >= 1 else '' |
Brett Cannon | df96068 | 2013-06-15 14:07:21 -0400 | [diff] [blame] | 129 | cfile = importlib.util.cache_from_source(file, |
Brett Cannon | f299abd | 2015-04-13 14:21:02 -0400 | [diff] [blame] | 130 | optimization=optimization) |
Brett Cannon | 14581d5 | 2013-01-26 08:48:36 -0500 | [diff] [blame] | 131 | else: |
Brett Cannon | df96068 | 2013-06-15 14:07:21 -0400 | [diff] [blame] | 132 | cfile = importlib.util.cache_from_source(file) |
Brett Cannon | 33915eb | 2013-06-14 18:33:00 -0400 | [diff] [blame] | 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') |
Brett Cannon | 9674bd0 | 2013-06-17 17:48:30 -0400 | [diff] [blame] | 136 | raise FileExistsError(msg.format(cfile)) |
Brett Cannon | 33915eb | 2013-06-14 18:33:00 -0400 | [diff] [blame] | 137 | elif os.path.exists(cfile) and not os.path.isfile(cfile): |
| 138 | msg = ('{} is a non-regular file and will be changed into a regular ' |
| 139 | 'one if import writes a byte-compiled file to it') |
Brett Cannon | 9674bd0 | 2013-06-17 17:48:30 -0400 | [diff] [blame] | 140 | raise FileExistsError(msg.format(cfile)) |
Brett Cannon | 14581d5 | 2013-01-26 08:48:36 -0500 | [diff] [blame] | 141 | loader = importlib.machinery.SourceFileLoader('<py_compile>', file) |
| 142 | source_bytes = loader.get_data(file) |
Guido van Rossum | f984a65 | 1998-09-29 15:57:42 +0000 | [diff] [blame] | 143 | try: |
Brett Cannon | 14581d5 | 2013-01-26 08:48:36 -0500 | [diff] [blame] | 144 | code = loader.source_to_code(source_bytes, dfile or file, |
Brett Cannon | edfd6ae | 2013-04-14 12:48:15 -0400 | [diff] [blame] | 145 | _optimize=optimize) |
Guido van Rossum | b940e11 | 2007-01-10 16:19:56 +0000 | [diff] [blame] | 146 | except Exception as err: |
Guido van Rossum | bd4a63e | 2007-08-10 17:36:34 +0000 | [diff] [blame] | 147 | py_exc = PyCompileError(err.__class__, err, dfile or file) |
Joannah Nanjekye | 2e33ecd | 2019-05-28 13:29:04 -0300 | [diff] [blame] | 148 | if quiet < 2: |
| 149 | if doraise: |
| 150 | raise py_exc |
| 151 | else: |
| 152 | sys.stderr.write(py_exc.msg + '\n') |
| 153 | return |
Benjamin Peterson | 25216ba | 2010-05-08 19:52:21 +0000 | [diff] [blame] | 154 | try: |
Meador Inge | 22b9b37 | 2011-11-28 09:27:32 -0600 | [diff] [blame] | 155 | dirname = os.path.dirname(cfile) |
| 156 | if dirname: |
| 157 | os.makedirs(dirname) |
Brett Cannon | 14581d5 | 2013-01-26 08:48:36 -0500 | [diff] [blame] | 158 | except FileExistsError: |
| 159 | pass |
Benjamin Peterson | 42aa93b | 2017-12-09 10:26:52 -0800 | [diff] [blame] | 160 | if invalidation_mode == PycInvalidationMode.TIMESTAMP: |
| 161 | source_stats = loader.path_stats(file) |
| 162 | bytecode = importlib._bootstrap_external._code_to_timestamp_pyc( |
Brett Cannon | edfd6ae | 2013-04-14 12:48:15 -0400 | [diff] [blame] | 163 | code, source_stats['mtime'], source_stats['size']) |
Benjamin Peterson | 42aa93b | 2017-12-09 10:26:52 -0800 | [diff] [blame] | 164 | else: |
| 165 | source_hash = importlib.util.source_hash(source_bytes) |
| 166 | bytecode = importlib._bootstrap_external._code_to_hash_pyc( |
| 167 | code, |
| 168 | source_hash, |
| 169 | (invalidation_mode == PycInvalidationMode.CHECKED_HASH), |
| 170 | ) |
Eric Snow | 32439d6 | 2015-05-02 19:15:18 -0600 | [diff] [blame] | 171 | mode = importlib._bootstrap_external._calc_mode(file) |
| 172 | importlib._bootstrap_external._write_atomic(cfile, bytecode, mode) |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 173 | return cfile |
Fred Drake | 61cf440 | 2002-08-21 20:56:21 +0000 | [diff] [blame] | 174 | |
Brett Cannon | edfd6ae | 2013-04-14 12:48:15 -0400 | [diff] [blame] | 175 | |
Fred Drake | 61cf440 | 2002-08-21 20:56:21 +0000 | [diff] [blame] | 176 | def main(args=None): |
| 177 | """Compile several source files. |
| 178 | |
| 179 | The files named in 'args' (or on the command line, if 'args' is |
| 180 | not specified) are compiled and the resulting bytecode is cached |
| 181 | in the normal manner. This function does not search a directory |
| 182 | structure to locate source files; it only compiles files named |
Barry Warsaw | d5f9bf5 | 2010-03-31 21:36:22 +0000 | [diff] [blame] | 183 | explicitly. If '-' is the only parameter in args, the list of |
| 184 | files is taken from standard input. |
Fred Drake | 61cf440 | 2002-08-21 20:56:21 +0000 | [diff] [blame] | 185 | |
| 186 | """ |
| 187 | if args is None: |
| 188 | args = sys.argv[1:] |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 189 | rv = 0 |
Barry Warsaw | d5f9bf5 | 2010-03-31 21:36:22 +0000 | [diff] [blame] | 190 | if args == ['-']: |
| 191 | while True: |
| 192 | filename = sys.stdin.readline() |
| 193 | if not filename: |
| 194 | break |
| 195 | filename = filename.rstrip('\n') |
| 196 | try: |
| 197 | compile(filename, doraise=True) |
| 198 | except PyCompileError as error: |
| 199 | rv = 1 |
Joannah Nanjekye | 2e33ecd | 2019-05-28 13:29:04 -0300 | [diff] [blame] | 200 | if quiet < 2: |
| 201 | sys.stderr.write("%s\n" % error.msg) |
Andrew Svetlov | f7a17b4 | 2012-12-25 16:47:37 +0200 | [diff] [blame] | 202 | except OSError as error: |
Barry Warsaw | d5f9bf5 | 2010-03-31 21:36:22 +0000 | [diff] [blame] | 203 | rv = 1 |
Joannah Nanjekye | 2e33ecd | 2019-05-28 13:29:04 -0300 | [diff] [blame] | 204 | if quiet < 2: |
| 205 | sys.stderr.write("%s\n" % error) |
Barry Warsaw | d5f9bf5 | 2010-03-31 21:36:22 +0000 | [diff] [blame] | 206 | else: |
| 207 | for filename in args: |
| 208 | try: |
| 209 | compile(filename, doraise=True) |
Matthias Klose | 1c99473 | 2010-04-20 19:48:04 +0000 | [diff] [blame] | 210 | except PyCompileError as error: |
Barry Warsaw | d5f9bf5 | 2010-03-31 21:36:22 +0000 | [diff] [blame] | 211 | # return value to indicate at least one failure |
| 212 | rv = 1 |
Joannah Nanjekye | 2e33ecd | 2019-05-28 13:29:04 -0300 | [diff] [blame] | 213 | if quiet < 2: |
| 214 | sys.stderr.write("%s\n" % error.msg) |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 215 | return rv |
Tim Peters | 2c60f7a | 2003-01-29 03:49:43 +0000 | [diff] [blame] | 216 | |
Fred Drake | 61cf440 | 2002-08-21 20:56:21 +0000 | [diff] [blame] | 217 | if __name__ == "__main__": |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 218 | sys.exit(main()) |