| Guido van Rossum | 63566e2 | 1998-01-19 04:01:26 +0000 | [diff] [blame] | 1 | """Routine to "compile" a .py file to a .pyc (or .pyo) file. | 
 | 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 |  | 
| Georg Brandl | 1a3284e | 2007-12-02 09:40:06 +0000 | [diff] [blame] | 6 | import builtins | 
| Sjoerd Mullender | 2e5168c | 1995-07-19 11:21:47 +0000 | [diff] [blame] | 7 | import imp | 
| Fred Drake | a96f1a3 | 2002-08-21 20:23:22 +0000 | [diff] [blame] | 8 | import marshal | 
 | 9 | import os | 
 | 10 | import sys | 
| Benjamin Peterson | 0088893 | 2010-03-18 22:37:38 +0000 | [diff] [blame^] | 11 | import tokenize | 
| Fred Drake | a96f1a3 | 2002-08-21 20:23:22 +0000 | [diff] [blame] | 12 | import traceback | 
 | 13 |  | 
| Sjoerd Mullender | 2e5168c | 1995-07-19 11:21:47 +0000 | [diff] [blame] | 14 | MAGIC = imp.get_magic() | 
| Guido van Rossum | 3bb5448 | 1994-08-29 10:52:58 +0000 | [diff] [blame] | 15 |  | 
| Martin v. Löwis | 0c6774d | 2003-01-15 11:51:06 +0000 | [diff] [blame] | 16 | __all__ = ["compile", "main", "PyCompileError"] | 
 | 17 |  | 
 | 18 |  | 
 | 19 | class PyCompileError(Exception): | 
 | 20 |     """Exception raised when an error occurs while attempting to | 
 | 21 |     compile the file. | 
 | 22 |  | 
 | 23 |     To raise this exception, use | 
 | 24 |  | 
 | 25 |         raise PyCompileError(exc_type,exc_value,file[,msg]) | 
 | 26 |  | 
 | 27 |     where | 
| Tim Peters | 2c60f7a | 2003-01-29 03:49:43 +0000 | [diff] [blame] | 28 |  | 
| Martin v. Löwis | 0c6774d | 2003-01-15 11:51:06 +0000 | [diff] [blame] | 29 |         exc_type:   exception type to be used in error message | 
 | 30 |                     type name can be accesses as class variable | 
 | 31 |                     'exc_type_name' | 
| Tim Peters | 2c60f7a | 2003-01-29 03:49:43 +0000 | [diff] [blame] | 32 |  | 
| Martin v. Löwis | 0c6774d | 2003-01-15 11:51:06 +0000 | [diff] [blame] | 33 |         exc_value:  exception value to be used in error message | 
 | 34 |                     can be accesses as class variable 'exc_value' | 
| Tim Peters | 2c60f7a | 2003-01-29 03:49:43 +0000 | [diff] [blame] | 35 |  | 
| Martin v. Löwis | 0c6774d | 2003-01-15 11:51:06 +0000 | [diff] [blame] | 36 |         file:       name of file being compiled to be used in error message | 
 | 37 |                     can be accesses as class variable 'file' | 
| Tim Peters | 2c60f7a | 2003-01-29 03:49:43 +0000 | [diff] [blame] | 38 |  | 
| Martin v. Löwis | 0c6774d | 2003-01-15 11:51:06 +0000 | [diff] [blame] | 39 |         msg:        string message to be written as error message | 
 | 40 |                     If no value is given, a default exception message will be given, | 
 | 41 |                     consistent with 'standard' py_compile output. | 
 | 42 |                     message (or default) can be accesses as class variable '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: | 
 | 49 |             tbtext = ''.join(traceback.format_exception_only(exc_type, exc_value)) | 
 | 50 |             errmsg = tbtext.replace('File "<string>"', 'File "%s"' % file) | 
 | 51 |         else: | 
 | 52 |             errmsg = "Sorry: %s: %s" % (exc_type_name,exc_value) | 
| Tim Peters | 2c60f7a | 2003-01-29 03:49:43 +0000 | [diff] [blame] | 53 |  | 
| Martin v. Löwis | 0c6774d | 2003-01-15 11:51:06 +0000 | [diff] [blame] | 54 |         Exception.__init__(self,msg or errmsg,exc_type_name,exc_value,file) | 
 | 55 |  | 
 | 56 |         self.exc_type_name = exc_type_name | 
 | 57 |         self.exc_value = exc_value | 
 | 58 |         self.file = file | 
 | 59 |         self.msg = msg or errmsg | 
 | 60 |  | 
 | 61 |     def __str__(self): | 
 | 62 |         return self.msg | 
 | 63 |  | 
| Skip Montanaro | c62c81e | 2001-02-12 02:00:42 +0000 | [diff] [blame] | 64 |  | 
| Guido van Rossum | 3bb5448 | 1994-08-29 10:52:58 +0000 | [diff] [blame] | 65 | def wr_long(f, x): | 
| Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 66 |     """Internal; write a 32-bit int to a file in little-endian order.""" | 
| Guido van Rossum | d6ca546 | 2007-05-22 01:29:33 +0000 | [diff] [blame] | 67 |     f.write(bytes([x        & 0xff, | 
 | 68 |                    (x >> 8)  & 0xff, | 
 | 69 |                    (x >> 16) & 0xff, | 
 | 70 |                    (x >> 24) & 0xff])) | 
| Guido van Rossum | 3bb5448 | 1994-08-29 10:52:58 +0000 | [diff] [blame] | 71 |  | 
| Martin v. Löwis | 0c6774d | 2003-01-15 11:51:06 +0000 | [diff] [blame] | 72 | def compile(file, cfile=None, dfile=None, doraise=False): | 
| Guido van Rossum | 63566e2 | 1998-01-19 04:01:26 +0000 | [diff] [blame] | 73 |     """Byte-compile one Python source file to Python bytecode. | 
 | 74 |  | 
 | 75 |     Arguments: | 
 | 76 |  | 
| Martin v. Löwis | 0c6774d | 2003-01-15 11:51:06 +0000 | [diff] [blame] | 77 |     file:    source filename | 
 | 78 |     cfile:   target filename; defaults to source with 'c' or 'o' appended | 
 | 79 |              ('c' normally, 'o' in optimizing mode, giving .pyc or .pyo) | 
 | 80 |     dfile:   purported filename; defaults to source (this is the filename | 
 | 81 |              that will show up in error messages) | 
 | 82 |     doraise: flag indicating whether or not an exception should be | 
 | 83 |              raised when a compile error is found. If an exception | 
 | 84 |              occurs and this flag is set to False, a string | 
 | 85 |              indicating the nature of the exception will be printed, | 
 | 86 |              and the function will return to the caller. If an | 
 | 87 |              exception occurs and this flag is set to True, a | 
 | 88 |              PyCompileError exception will be raised. | 
| Tim Peters | 2c60f7a | 2003-01-29 03:49:43 +0000 | [diff] [blame] | 89 |  | 
| Guido van Rossum | 63566e2 | 1998-01-19 04:01:26 +0000 | [diff] [blame] | 90 |     Note that it isn't necessary to byte-compile Python modules for | 
 | 91 |     execution efficiency -- Python itself byte-compiles a module when | 
 | 92 |     it is loaded, and if it can, writes out the bytecode to the | 
 | 93 |     corresponding .pyc (or .pyo) file. | 
 | 94 |  | 
 | 95 |     However, if a Python installation is shared between users, it is a | 
 | 96 |     good idea to byte-compile all modules upon installation, since | 
 | 97 |     other users may not be able to write in the source directories, | 
 | 98 |     and thus they won't be able to write the .pyc/.pyo file, and then | 
 | 99 |     they would be byte-compiling every module each time it is loaded. | 
 | 100 |     This can slow down program start-up considerably. | 
 | 101 |  | 
 | 102 |     See compileall.py for a script/module that uses this module to | 
 | 103 |     byte-compile all installed files (or all files in selected | 
 | 104 |     directories). | 
 | 105 |  | 
 | 106 |     """ | 
| Benjamin Peterson | 0088893 | 2010-03-18 22:37:38 +0000 | [diff] [blame^] | 107 |     with open(file, "rb") as f: | 
 | 108 |         encoding = tokenize.detect_encoding(f.readline)[0] | 
| Benjamin Peterson | 32ca454 | 2010-03-18 21:58:43 +0000 | [diff] [blame] | 109 |     with open(file, encoding=encoding) as f: | 
 | 110 |         try: | 
 | 111 |             timestamp = int(os.fstat(f.fileno()).st_mtime) | 
 | 112 |         except AttributeError: | 
 | 113 |             timestamp = int(os.stat(file).st_mtime) | 
 | 114 |         codestring = f.read() | 
| Guido van Rossum | 63566e2 | 1998-01-19 04:01:26 +0000 | [diff] [blame] | 115 |     if codestring and codestring[-1] != '\n': | 
| Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 116 |         codestring = codestring + '\n' | 
| Guido van Rossum | f984a65 | 1998-09-29 15:57:42 +0000 | [diff] [blame] | 117 |     try: | 
| Georg Brandl | 1a3284e | 2007-12-02 09:40:06 +0000 | [diff] [blame] | 118 |         codeobject = builtins.compile(codestring, dfile or file,'exec') | 
| Guido van Rossum | b940e11 | 2007-01-10 16:19:56 +0000 | [diff] [blame] | 119 |     except Exception as err: | 
| Guido van Rossum | bd4a63e | 2007-08-10 17:36:34 +0000 | [diff] [blame] | 120 |         py_exc = PyCompileError(err.__class__, err, dfile or file) | 
| Martin v. Löwis | 0c6774d | 2003-01-15 11:51:06 +0000 | [diff] [blame] | 121 |         if doraise: | 
 | 122 |             raise py_exc | 
 | 123 |         else: | 
| Georg Brandl | e537d6e | 2005-06-10 17:15:18 +0000 | [diff] [blame] | 124 |             sys.stderr.write(py_exc.msg + '\n') | 
| Martin v. Löwis | 0c6774d | 2003-01-15 11:51:06 +0000 | [diff] [blame] | 125 |             return | 
| Raymond Hettinger | 16e3c42 | 2002-06-01 16:07:16 +0000 | [diff] [blame] | 126 |     if cfile is None: | 
| Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 127 |         cfile = file + (__debug__ and 'c' or 'o') | 
| Benjamin Peterson | 32ca454 | 2010-03-18 21:58:43 +0000 | [diff] [blame] | 128 |     with open(cfile, 'wb') as fc: | 
 | 129 |         fc.write(b'\0\0\0\0') | 
 | 130 |         wr_long(fc, timestamp) | 
 | 131 |         marshal.dump(codeobject, fc) | 
 | 132 |         fc.flush() | 
 | 133 |         fc.seek(0, 0) | 
 | 134 |         fc.write(MAGIC) | 
| Fred Drake | 61cf440 | 2002-08-21 20:56:21 +0000 | [diff] [blame] | 135 |  | 
 | 136 | def main(args=None): | 
 | 137 |     """Compile several source files. | 
 | 138 |  | 
 | 139 |     The files named in 'args' (or on the command line, if 'args' is | 
 | 140 |     not specified) are compiled and the resulting bytecode is cached | 
 | 141 |     in the normal manner.  This function does not search a directory | 
 | 142 |     structure to locate source files; it only compiles files named | 
 | 143 |     explicitly. | 
 | 144 |  | 
 | 145 |     """ | 
 | 146 |     if args is None: | 
 | 147 |         args = sys.argv[1:] | 
| Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 148 |     rv = 0 | 
| Fred Drake | 61cf440 | 2002-08-21 20:56:21 +0000 | [diff] [blame] | 149 |     for filename in args: | 
| Martin v. Löwis | 0c6774d | 2003-01-15 11:51:06 +0000 | [diff] [blame] | 150 |         try: | 
 | 151 |             compile(filename, doraise=True) | 
| Guido van Rossum | b940e11 | 2007-01-10 16:19:56 +0000 | [diff] [blame] | 152 |         except PyCompileError as err: | 
| Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 153 |             # return value to indicate at least one failure | 
 | 154 |             rv = 1 | 
| Martin v. Löwis | 0c6774d | 2003-01-15 11:51:06 +0000 | [diff] [blame] | 155 |             sys.stderr.write(err.msg) | 
| Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 156 |     return rv | 
| Tim Peters | 2c60f7a | 2003-01-29 03:49:43 +0000 | [diff] [blame] | 157 |  | 
| Fred Drake | 61cf440 | 2002-08-21 20:56:21 +0000 | [diff] [blame] | 158 | if __name__ == "__main__": | 
| Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 159 |     sys.exit(main()) |