blob: ed54f47a7051ca3488f6267f07aebb5b65ade6e2 [file] [log] [blame]
Guido van Rossum3bb54481994-08-29 10:52:58 +00001# Routine to "compile" a .py file to a .pyc file.
2# This has intimate knowledge of how Python/import.c does it.
3# By Sjoerd Mullender (I forced him to write it :-).
4
5MAGIC = 0x999903
6
7def wr_long(f, x):
8 f.write(chr( x & 0xff))
9 f.write(chr((x >> 8) & 0xff))
10 f.write(chr((x >> 16) & 0xff))
11 f.write(chr((x >> 24) & 0xff))
12
13def compile(file, cfile = None):
14 import os, marshal, __builtin__
15 f = open(file)
16 codestring = f.read()
17 timestamp = os.fstat(f.fileno())[8]
18 f.close()
19 codeobject = __builtin__.compile(codestring, file, 'exec')
20 if not cfile:
21 cfile = file + 'c'
22 fc = open(cfile, 'w')
23 wr_long(fc, MAGIC)
24 wr_long(fc, timestamp)
25 marshal.dump(codeobject, fc)