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