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 | |
Sjoerd Mullender | 2e5168c | 1995-07-19 11:21:47 +0000 | [diff] [blame] | 6 | import imp |
| 7 | MAGIC = imp.get_magic() |
Guido van Rossum | 3bb5448 | 1994-08-29 10:52:58 +0000 | [diff] [blame] | 8 | |
Skip Montanaro | c62c81e | 2001-02-12 02:00:42 +0000 | [diff] [blame] | 9 | __all__ = ["compile"] |
| 10 | |
Guido van Rossum | 3bb5448 | 1994-08-29 10:52:58 +0000 | [diff] [blame] | 11 | def wr_long(f, x): |
Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 12 | """Internal; write a 32-bit int to a file in little-endian order.""" |
Guido van Rossum | 63566e2 | 1998-01-19 04:01:26 +0000 | [diff] [blame] | 13 | f.write(chr( x & 0xff)) |
| 14 | f.write(chr((x >> 8) & 0xff)) |
| 15 | f.write(chr((x >> 16) & 0xff)) |
| 16 | f.write(chr((x >> 24) & 0xff)) |
Guido van Rossum | 3bb5448 | 1994-08-29 10:52:58 +0000 | [diff] [blame] | 17 | |
Guido van Rossum | 63566e2 | 1998-01-19 04:01:26 +0000 | [diff] [blame] | 18 | def compile(file, cfile=None, dfile=None): |
| 19 | """Byte-compile one Python source file to Python bytecode. |
| 20 | |
| 21 | Arguments: |
| 22 | |
| 23 | file: source filename |
| 24 | cfile: target filename; defaults to source with 'c' or 'o' appended |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 25 | ('c' normally, 'o' in optimizing mode, giving .pyc or .pyo) |
Guido van Rossum | 63566e2 | 1998-01-19 04:01:26 +0000 | [diff] [blame] | 26 | dfile: purported filename; defaults to source (this is the filename |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 27 | that will show up in error messages) |
Guido van Rossum | 63566e2 | 1998-01-19 04:01:26 +0000 | [diff] [blame] | 28 | |
| 29 | Note that it isn't necessary to byte-compile Python modules for |
| 30 | execution efficiency -- Python itself byte-compiles a module when |
| 31 | it is loaded, and if it can, writes out the bytecode to the |
| 32 | corresponding .pyc (or .pyo) file. |
| 33 | |
| 34 | However, if a Python installation is shared between users, it is a |
| 35 | good idea to byte-compile all modules upon installation, since |
| 36 | other users may not be able to write in the source directories, |
| 37 | and thus they won't be able to write the .pyc/.pyo file, and then |
| 38 | they would be byte-compiling every module each time it is loaded. |
| 39 | This can slow down program start-up considerably. |
| 40 | |
| 41 | See compileall.py for a script/module that uses this module to |
| 42 | byte-compile all installed files (or all files in selected |
| 43 | directories). |
| 44 | |
| 45 | """ |
| 46 | import os, marshal, __builtin__ |
Jack Jansen | 7b8c754 | 2002-04-14 20:12:41 +0000 | [diff] [blame] | 47 | f = open(file, 'U') |
Guido van Rossum | 63566e2 | 1998-01-19 04:01:26 +0000 | [diff] [blame] | 48 | try: |
Raymond Hettinger | 32200ae | 2002-06-01 19:51:15 +0000 | [diff] [blame] | 49 | timestamp = long(os.fstat(f.fileno()).st_mtime) |
Guido van Rossum | 63566e2 | 1998-01-19 04:01:26 +0000 | [diff] [blame] | 50 | except AttributeError: |
Raymond Hettinger | 32200ae | 2002-06-01 19:51:15 +0000 | [diff] [blame] | 51 | timestamp = long(os.stat(file).st_mtime) |
Guido van Rossum | 63566e2 | 1998-01-19 04:01:26 +0000 | [diff] [blame] | 52 | codestring = f.read() |
Martin v. Löwis | ff1ce0f | 2000-09-15 06:57:26 +0000 | [diff] [blame] | 53 | # If parsing from a string, line breaks are \n (see parsetok.c:tok_nextc) |
| 54 | # Replace will return original string if pattern is not found, so |
| 55 | # we don't need to check whether it is found first. |
| 56 | codestring = codestring.replace("\r\n","\n") |
| 57 | codestring = codestring.replace("\r","\n") |
Guido van Rossum | 63566e2 | 1998-01-19 04:01:26 +0000 | [diff] [blame] | 58 | f.close() |
| 59 | if codestring and codestring[-1] != '\n': |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 60 | codestring = codestring + '\n' |
Guido van Rossum | f984a65 | 1998-09-29 15:57:42 +0000 | [diff] [blame] | 61 | try: |
| 62 | codeobject = __builtin__.compile(codestring, dfile or file, 'exec') |
| 63 | except SyntaxError, detail: |
Eric S. Raymond | 6b71e74 | 2001-02-09 08:56:30 +0000 | [diff] [blame] | 64 | import traceback, sys |
Guido van Rossum | f984a65 | 1998-09-29 15:57:42 +0000 | [diff] [blame] | 65 | lines = traceback.format_exception_only(SyntaxError, detail) |
| 66 | for line in lines: |
Eric S. Raymond | 6b71e74 | 2001-02-09 08:56:30 +0000 | [diff] [blame] | 67 | sys.stderr.write(line.replace('File "<string>"', |
Guido van Rossum | f984a65 | 1998-09-29 15:57:42 +0000 | [diff] [blame] | 68 | 'File "%s"' % (dfile or file))) |
| 69 | return |
Raymond Hettinger | 16e3c42 | 2002-06-01 16:07:16 +0000 | [diff] [blame] | 70 | if cfile is None: |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 71 | cfile = file + (__debug__ and 'c' or 'o') |
Guido van Rossum | 63566e2 | 1998-01-19 04:01:26 +0000 | [diff] [blame] | 72 | fc = open(cfile, 'wb') |
| 73 | fc.write('\0\0\0\0') |
| 74 | wr_long(fc, timestamp) |
| 75 | marshal.dump(codeobject, fc) |
| 76 | fc.flush() |
| 77 | fc.seek(0, 0) |
| 78 | fc.write(MAGIC) |
| 79 | fc.close() |
| 80 | if os.name == 'mac': |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 81 | import macfs |
| 82 | macfs.FSSpec(cfile).SetCreatorType('Pyth', 'PYC ') |