blob: d10c5cb9e6b43a89cd1c4ec4412664ae29d53cd0 [file] [log] [blame]
Guido van Rossum63566e21998-01-19 04:01:26 +00001"""Routine to "compile" a .py file to a .pyc (or .pyo) file.
2
3This module has intimate knowledge of the format of .pyc files.
4"""
Guido van Rossum3bb54481994-08-29 10:52:58 +00005
Sjoerd Mullender2e5168c1995-07-19 11:21:47 +00006import imp
7MAGIC = imp.get_magic()
Guido van Rossum3bb54481994-08-29 10:52:58 +00008
Skip Montanaroc62c81e2001-02-12 02:00:42 +00009__all__ = ["compile"]
10
Guido van Rossum3bb54481994-08-29 10:52:58 +000011def wr_long(f, x):
Guido van Rossum54f22ed2000-02-04 15:10:34 +000012 """Internal; write a 32-bit int to a file in little-endian order."""
Guido van Rossum63566e21998-01-19 04:01:26 +000013 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 Rossum3bb54481994-08-29 10:52:58 +000017
Guido van Rossum63566e21998-01-19 04:01:26 +000018def 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 Rossum45e2fbc1998-03-26 21:13:24 +000025 ('c' normally, 'o' in optimizing mode, giving .pyc or .pyo)
Guido van Rossum63566e21998-01-19 04:01:26 +000026 dfile: purported filename; defaults to source (this is the filename
Guido van Rossum45e2fbc1998-03-26 21:13:24 +000027 that will show up in error messages)
Guido van Rossum63566e21998-01-19 04:01:26 +000028
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 Jansen7b8c7542002-04-14 20:12:41 +000047 f = open(file, 'U')
Guido van Rossum63566e21998-01-19 04:01:26 +000048 try:
Raymond Hettinger32200ae2002-06-01 19:51:15 +000049 timestamp = long(os.fstat(f.fileno()).st_mtime)
Guido van Rossum63566e21998-01-19 04:01:26 +000050 except AttributeError:
Raymond Hettinger32200ae2002-06-01 19:51:15 +000051 timestamp = long(os.stat(file).st_mtime)
Guido van Rossum63566e21998-01-19 04:01:26 +000052 codestring = f.read()
Martin v. Löwisff1ce0f2000-09-15 06:57:26 +000053 # 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 Rossum63566e21998-01-19 04:01:26 +000058 f.close()
59 if codestring and codestring[-1] != '\n':
Guido van Rossum45e2fbc1998-03-26 21:13:24 +000060 codestring = codestring + '\n'
Guido van Rossumf984a651998-09-29 15:57:42 +000061 try:
62 codeobject = __builtin__.compile(codestring, dfile or file, 'exec')
63 except SyntaxError, detail:
Eric S. Raymond6b71e742001-02-09 08:56:30 +000064 import traceback, sys
Guido van Rossumf984a651998-09-29 15:57:42 +000065 lines = traceback.format_exception_only(SyntaxError, detail)
66 for line in lines:
Eric S. Raymond6b71e742001-02-09 08:56:30 +000067 sys.stderr.write(line.replace('File "<string>"',
Guido van Rossumf984a651998-09-29 15:57:42 +000068 'File "%s"' % (dfile or file)))
69 return
Raymond Hettinger16e3c422002-06-01 16:07:16 +000070 if cfile is None:
Guido van Rossum45e2fbc1998-03-26 21:13:24 +000071 cfile = file + (__debug__ and 'c' or 'o')
Guido van Rossum63566e21998-01-19 04:01:26 +000072 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 Rossum45e2fbc1998-03-26 21:13:24 +000081 import macfs
82 macfs.FSSpec(cfile).SetCreatorType('Pyth', 'PYC ')