annotate Lib/py_compile.py @ 4566:6403c94de50d legacy-trunk

Added safeguard against failure in __del__.
author Guido van Rossum <guido@python.org>
date Wed, 27 Nov 1996 19:39:58 +0000
parents 1fec88b22e87
children 5cf83ad3251e
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1828
b464e1d0b2fb New way of generating .pyc files, thanks to Sjoerd.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1 # Routine to "compile" a .py file to a .pyc file.
b464e1d0b2fb New way of generating .pyc files, thanks to Sjoerd.
Guido van Rossum <guido@python.org>
parents:
diff changeset
2 # This has intimate knowledge of how Python/import.c does it.
b464e1d0b2fb New way of generating .pyc files, thanks to Sjoerd.
Guido van Rossum <guido@python.org>
parents:
diff changeset
3 # By Sjoerd Mullender (I forced him to write it :-).
b464e1d0b2fb New way of generating .pyc files, thanks to Sjoerd.
Guido van Rossum <guido@python.org>
parents:
diff changeset
4
2853
d42941d3a95f Get magic number from interpreter (using module imp).
Sjoerd Mullender <sjoerd@acm.org>
parents: 2665
diff changeset
5 import imp
d42941d3a95f Get magic number from interpreter (using module imp).
Sjoerd Mullender <sjoerd@acm.org>
parents: 2665
diff changeset
6 MAGIC = imp.get_magic()
1828
b464e1d0b2fb New way of generating .pyc files, thanks to Sjoerd.
Guido van Rossum <guido@python.org>
parents:
diff changeset
7
b464e1d0b2fb New way of generating .pyc files, thanks to Sjoerd.
Guido van Rossum <guido@python.org>
parents:
diff changeset
8 def wr_long(f, x):
b464e1d0b2fb New way of generating .pyc files, thanks to Sjoerd.
Guido van Rossum <guido@python.org>
parents:
diff changeset
9 f.write(chr( x & 0xff))
b464e1d0b2fb New way of generating .pyc files, thanks to Sjoerd.
Guido van Rossum <guido@python.org>
parents:
diff changeset
10 f.write(chr((x >> 8) & 0xff))
b464e1d0b2fb New way of generating .pyc files, thanks to Sjoerd.
Guido van Rossum <guido@python.org>
parents:
diff changeset
11 f.write(chr((x >> 16) & 0xff))
b464e1d0b2fb New way of generating .pyc files, thanks to Sjoerd.
Guido van Rossum <guido@python.org>
parents:
diff changeset
12 f.write(chr((x >> 24) & 0xff))
b464e1d0b2fb New way of generating .pyc files, thanks to Sjoerd.
Guido van Rossum <guido@python.org>
parents:
diff changeset
13
b464e1d0b2fb New way of generating .pyc files, thanks to Sjoerd.
Guido van Rossum <guido@python.org>
parents:
diff changeset
14 def compile(file, cfile = None):
b464e1d0b2fb New way of generating .pyc files, thanks to Sjoerd.
Guido van Rossum <guido@python.org>
parents:
diff changeset
15 import os, marshal, __builtin__
b464e1d0b2fb New way of generating .pyc files, thanks to Sjoerd.
Guido van Rossum <guido@python.org>
parents:
diff changeset
16 f = open(file)
b464e1d0b2fb New way of generating .pyc files, thanks to Sjoerd.
Guido van Rossum <guido@python.org>
parents:
diff changeset
17 codestring = f.read()
b464e1d0b2fb New way of generating .pyc files, thanks to Sjoerd.
Guido van Rossum <guido@python.org>
parents:
diff changeset
18 f.close()
4483
1fec88b22e87 Cast mtime gotten from stat() to long int -- else it won't work
Guido van Rossum <guido@python.org>
parents: 3635
diff changeset
19 timestamp = long(os.stat(file)[8])
1828
b464e1d0b2fb New way of generating .pyc files, thanks to Sjoerd.
Guido van Rossum <guido@python.org>
parents:
diff changeset
20 codeobject = __builtin__.compile(codestring, file, 'exec')
b464e1d0b2fb New way of generating .pyc files, thanks to Sjoerd.
Guido van Rossum <guido@python.org>
parents:
diff changeset
21 if not cfile:
b464e1d0b2fb New way of generating .pyc files, thanks to Sjoerd.
Guido van Rossum <guido@python.org>
parents:
diff changeset
22 cfile = file + 'c'
2260
a81b6db6d33c changes for the Mac
Guido van Rossum <guido@python.org>
parents: 1828
diff changeset
23 fc = open(cfile, 'wb')
2853
d42941d3a95f Get magic number from interpreter (using module imp).
Sjoerd Mullender <sjoerd@acm.org>
parents: 2665
diff changeset
24 fc.write(MAGIC)
1828
b464e1d0b2fb New way of generating .pyc files, thanks to Sjoerd.
Guido van Rossum <guido@python.org>
parents:
diff changeset
25 wr_long(fc, timestamp)
b464e1d0b2fb New way of generating .pyc files, thanks to Sjoerd.
Guido van Rossum <guido@python.org>
parents:
diff changeset
26 marshal.dump(codeobject, fc)
2260
a81b6db6d33c changes for the Mac
Guido van Rossum <guido@python.org>
parents: 1828
diff changeset
27 fc.close()
a81b6db6d33c changes for the Mac
Guido van Rossum <guido@python.org>
parents: 1828
diff changeset
28 if os.name == 'mac':
2665
2f8e8b6129f7 Mac-specific code for setting filetype was outdated.
Jack Jansen <jack.jansen@cwi.nl>
parents: 2260
diff changeset
29 import macfs
3635
89417895d963 change Mac creator/type
Guido van Rossum <guido@python.org>
parents: 2853
diff changeset
30 macfs.FSSpec(cfile).SetCreatorType('Pyth', 'PYC ')
89417895d963 change Mac creator/type
Guido van Rossum <guido@python.org>
parents: 2853
diff changeset
31 macfs.FSSpec(file).SetCreatorType('Pyth', 'TEXT')