| Tor Norbye | 3a2425a | 2013-11-04 10:16:08 -0800 | [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 | """ |
| 5 | |
| 6 | import _py_compile |
| 7 | import os |
| 8 | import sys |
| 9 | import traceback |
| 10 | |
| 11 | __all__ = ["compile", "main", "PyCompileError"] |
| 12 | |
| 13 | |
| 14 | class PyCompileError(Exception): |
| 15 | """Exception raised when an error occurs while attempting to |
| 16 | compile the file. |
| 17 | |
| 18 | To raise this exception, use |
| 19 | |
| 20 | raise PyCompileError(exc_type,exc_value,file[,msg]) |
| 21 | |
| 22 | where |
| 23 | |
| 24 | exc_type: exception type to be used in error message |
| 25 | type name can be accesses as class variable |
| 26 | 'exc_type_name' |
| 27 | |
| 28 | exc_value: exception value to be used in error message |
| 29 | can be accesses as class variable 'exc_value' |
| 30 | |
| 31 | file: name of file being compiled to be used in error message |
| 32 | can be accesses as class variable 'file' |
| 33 | |
| 34 | msg: string message to be written as error message |
| 35 | If no value is given, a default exception message will be given, |
| 36 | consistent with 'standard' py_compile output. |
| 37 | message (or default) can be accesses as class variable 'msg' |
| 38 | |
| 39 | """ |
| 40 | |
| 41 | def __init__(self, exc_type, exc_value, file, msg=''): |
| 42 | exc_type_name = exc_type.__name__ |
| 43 | if exc_type is SyntaxError: |
| 44 | tbtext = ''.join(traceback.format_exception_only(exc_type, exc_value)) |
| 45 | errmsg = tbtext.replace('File "<string>"', 'File "%s"' % file) |
| 46 | else: |
| 47 | errmsg = "Sorry: %s: %s" % (exc_type_name,exc_value) |
| 48 | |
| 49 | Exception.__init__(self,msg or errmsg,exc_type_name,exc_value,file) |
| 50 | |
| 51 | self.exc_type_name = exc_type_name |
| 52 | self.exc_value = exc_value |
| 53 | self.file = file |
| 54 | self.msg = msg or errmsg |
| 55 | |
| 56 | def __str__(self): |
| 57 | return self.msg |
| 58 | |
| 59 | |
| 60 | def compile(file, cfile=None, dfile=None, doraise=False): |
| 61 | """Byte-compile one Python source file to Python bytecode. |
| 62 | |
| 63 | Arguments: |
| 64 | |
| 65 | file: source filename |
| 66 | cfile: target filename; defaults to source with 'c' or 'o' appended |
| 67 | ('c' normally, 'o' in optimizing mode, giving .pyc or .pyo) |
| 68 | dfile: purported filename; defaults to source (this is the filename |
| 69 | that will show up in error messages) |
| 70 | doraise: flag indicating whether or not an exception should be |
| 71 | raised when a compile error is found. If an exception |
| 72 | occurs and this flag is set to False, a string |
| 73 | indicating the nature of the exception will be printed, |
| 74 | and the function will return to the caller. If an |
| 75 | exception occurs and this flag is set to True, a |
| 76 | PyCompileError exception will be raised. |
| 77 | |
| 78 | Note that it isn't necessary to byte-compile Python modules for |
| 79 | execution efficiency -- Python itself byte-compiles a module when |
| 80 | it is loaded, and if it can, writes out the bytecode to the |
| 81 | corresponding .pyc (or .pyo) file. |
| 82 | |
| 83 | However, if a Python installation is shared between users, it is a |
| 84 | good idea to byte-compile all modules upon installation, since |
| 85 | other users may not be able to write in the source directories, |
| 86 | and thus they won't be able to write the .pyc/.pyo file, and then |
| 87 | they would be byte-compiling every module each time it is loaded. |
| 88 | This can slow down program start-up considerably. |
| 89 | |
| 90 | See compileall.py for a script/module that uses this module to |
| 91 | byte-compile all installed files (or all files in selected |
| 92 | directories). |
| 93 | |
| 94 | """ |
| 95 | try: |
| 96 | _py_compile.compile(file, cfile, dfile) |
| 97 | except Exception,err: |
| 98 | py_exc = PyCompileError(err.__class__,err.args,dfile or file) |
| 99 | if doraise: |
| 100 | raise py_exc |
| 101 | else: |
| 102 | sys.stderr.write(py_exc.msg + '\n') |
| 103 | return |
| 104 | |
| 105 | def main(args=None): |
| 106 | """Compile several source files. |
| 107 | |
| 108 | The files named in 'args' (or on the command line, if 'args' is |
| 109 | not specified) are compiled and the resulting bytecode is cached |
| 110 | in the normal manner. This function does not search a directory |
| 111 | structure to locate source files; it only compiles files named |
| 112 | explicitly. |
| 113 | |
| 114 | """ |
| 115 | if args is None: |
| 116 | args = sys.argv[1:] |
| 117 | for filename in args: |
| 118 | try: |
| 119 | compile(filename, doraise=True) |
| 120 | except PyCompileError,err: |
| 121 | sys.stderr.write(err.msg) |
| 122 | |
| 123 | if __name__ == "__main__": |
| 124 | main() |