4 # Write a file containing frozen code for the modules in the dictionary.
11 } frozen_modules[] = {
14 {0, 0, 0} /* sentinel */
18 def makefreeze(outfp
, dict):
27 sys
.stderr
.write("%s: %s\n" % (modfn
, str(msg
)))
30 done
.append(mod
, len(str))
31 writecode(outfp
, mod
, str)
33 for mod
, size
in done
:
34 outfp
.write('\t{"%s", M_%s, %d},\n' % (mod
, mod
, size
))
38 # Return code string for a given module -- either a .py or a .pyc
39 # file. Return either a string or None (if it's not Python code).
42 def makecode(filename
):
43 if filename
[-3:] == '.py':
44 f
= open(filename
, 'r')
47 code
= compile(text
, filename
, 'exec')
50 return marshal
.dumps(code
)
51 if filename
[-4:] == '.pyc':
52 f
= open(filename
, 'rb')
59 # Can't generate code for this extension
63 # Write a C initializer for a module containing the frozen python code.
64 # The array is called M_<mod>.
66 def writecode(outfp
, mod
, str):
67 outfp
.write('static unsigned char M_%s[] = {' % mod
)
68 for i
in range(0, len(str), 16):
71 outfp
.write('%d,' % ord(c
))
75 # Test for the above functions.
81 print 'usage: python freezepython.py file.py(c) ...'
84 for arg
in sys
.argv
[1:]:
85 base
= os
.path
.basename(arg
)
86 mod
, ext
= os
.path
.splitext(base
)
88 makefreeze(sys
.stdout
, dict)
90 if __name__
== '__main__':