diff options
author | Guido van Rossum <guido@python.org> | 1994-08-29 10:52:58 +0000 |
---|---|---|
committer | Guido van Rossum <guido@python.org> | 1994-08-29 10:52:58 +0000 |
commit | 92ea6f84dcb5dd336e554e49f6c917e067a2033e (patch) | |
tree | 64107fd12f55c7dda531d2de8aeb9d2e9b9f01a6 | |
parent | 038d9b75fa0ffe5cbd9b284684d488c5bc7f4f2c (diff) | |
download | cpython-92ea6f84dcb5dd336e554e49f6c917e067a2033e.tar.gz |
New way of generating .pyc files, thanks to Sjoerd.
urllib.py: '+' is not always safe (even though the RFC says so :-( )
whrandom.py: throw away top bits of time to avoid overflow on Mac
(where times can be negative)
-rw-r--r-- | Lib/compileall.py | 67 | ||||
-rwxr-xr-x | Lib/importall.py | 2 | ||||
-rw-r--r-- | Lib/py_compile.py | 25 | ||||
-rw-r--r-- | Lib/urllib.py | 2 | ||||
-rw-r--r-- | Lib/whrandom.py | 2 |
5 files changed, 96 insertions, 2 deletions
diff --git a/Lib/compileall.py b/Lib/compileall.py new file mode 100644 index 0000000000..cd02851326 --- /dev/null +++ b/Lib/compileall.py @@ -0,0 +1,67 @@ +# Routines to force "compilation" of all .py files in a directory +# tree or on sys.path. By default recursion is pruned at a depth of +# 10 and the current directory, if it occurs in sys.path, is skipped. +# When called as a script, compiles argument directories, or sys.path +# if no arguments. +# After a similar module by Sjoerd Mullender. + +import os +import sys +import py_compile + +def compile_dir(dir, maxlevels = 10): + print 'Listing', dir, '...' + try: + names = os.listdir(dir) + except os.error: + print "Can't list", dir + names = [] + names.sort() + for name in names: + fullname = os.path.join(dir, name) + if os.path.isfile(fullname): + head, tail = name[:-3], name[-3:] + if tail == '.py': + print 'Compiling', fullname, '...' + try: + py_compile.compile(fullname) + except KeyboardInterrupt: + del names[:] + print '\n[interrupt]' + break + except: + print 'Sorry:', sys.exc_type + ':', + print sys.exc_value + elif maxlevels > 0 and \ + name != os.curdir and name != os.pardir and \ + os.path.isdir(fullname) and \ + not os.path.islink(fullname): + compile_dir(fullname, maxlevels - 1) + +def compile_path(skip_curdir = 1): + for dir in sys.path: + if dir == os.curdir and skip_curdir: + print 'Skipping current directory' + else: + compile_dir(dir, 0) + +def main(): + import getopt + try: + opts, args = getopt.getopt(sys.argv[1:], 'l') + except getopt.error, msg: + print msg + print "usage: compileall [-l] [directory ...]" + print "-l: don't recurse down" + print "if no arguments, -l sys.path is assumed" + maxlevels = 10 + for o, a in opts: + if o == '-l': maxlevels = 0 + if args: + for dir in sys.argv[1:]: + compile_dir(dir, maxlevels) + else: + compile_path() + +if __name__ == '__main__': + main() diff --git a/Lib/importall.py b/Lib/importall.py index 1d20377105..1383e80790 100755 --- a/Lib/importall.py +++ b/Lib/importall.py @@ -1,3 +1,5 @@ +# THIS IS OBSOLETE -- USE MODULE 'compileall' INSTEAD! + # Utility module to import all modules in the path, in the hope # that this will update their ".pyc" files. diff --git a/Lib/py_compile.py b/Lib/py_compile.py new file mode 100644 index 0000000000..ed54f47a70 --- /dev/null +++ b/Lib/py_compile.py @@ -0,0 +1,25 @@ +# Routine to "compile" a .py file to a .pyc file. +# This has intimate knowledge of how Python/import.c does it. +# By Sjoerd Mullender (I forced him to write it :-). + +MAGIC = 0x999903 + +def wr_long(f, x): + f.write(chr( x & 0xff)) + f.write(chr((x >> 8) & 0xff)) + f.write(chr((x >> 16) & 0xff)) + f.write(chr((x >> 24) & 0xff)) + +def compile(file, cfile = None): + import os, marshal, __builtin__ + f = open(file) + codestring = f.read() + timestamp = os.fstat(f.fileno())[8] + f.close() + codeobject = __builtin__.compile(codestring, file, 'exec') + if not cfile: + cfile = file + 'c' + fc = open(cfile, 'w') + wr_long(fc, MAGIC) + wr_long(fc, timestamp) + marshal.dump(codeobject, fc) diff --git a/Lib/urllib.py b/Lib/urllib.py index 476570bf94..a4a891fc4d 100644 --- a/Lib/urllib.py +++ b/Lib/urllib.py @@ -452,7 +452,7 @@ def unquote(s): i = j+3 return res -always_safe = string.letters + string.digits + '_,.+-' +always_safe = string.letters + string.digits + '_,.-' def quote(s, safe = '/'): safe = always_safe + safe res = '' diff --git a/Lib/whrandom.py b/Lib/whrandom.py index 670ca7a911..7317b443f8 100644 --- a/Lib/whrandom.py +++ b/Lib/whrandom.py @@ -39,7 +39,7 @@ class whrandom: if x is None: # Initialize from current time import time - t = int(time.time()) + t = int(time.time() % 0x80000000) t, x = divmod(t, 256) t, y = divmod(t, 256) t, z = divmod(t, 256) |