Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 3bb5448

Browse files
committed
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)
1 parent 7b1e974 commit 3bb5448

5 files changed

Lines changed: 96 additions & 2 deletions

File tree

Lib/compileall.py

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
# Routines to force "compilation" of all .py files in a directory
2+
# tree or on sys.path. By default recursion is pruned at a depth of
3+
# 10 and the current directory, if it occurs in sys.path, is skipped.
4+
# When called as a script, compiles argument directories, or sys.path
5+
# if no arguments.
6+
# After a similar module by Sjoerd Mullender.
7+
8+
import os
9+
import sys
10+
import py_compile
11+
12+
def compile_dir(dir, maxlevels = 10):
13+
print 'Listing', dir, '...'
14+
try:
15+
names = os.listdir(dir)
16+
except os.error:
17+
print "Can't list", dir
18+
names = []
19+
names.sort()
20+
for name in names:
21+
fullname = os.path.join(dir, name)
22+
if os.path.isfile(fullname):
23+
head, tail = name[:-3], name[-3:]
24+
if tail == '.py':
25+
print 'Compiling', fullname, '...'
26+
try:
27+
py_compile.compile(fullname)
28+
except KeyboardInterrupt:
29+
del names[:]
30+
print '\n[interrupt]'
31+
break
32+
except:
33+
print 'Sorry:', sys.exc_type + ':',
34+
print sys.exc_value
35+
elif maxlevels > 0 and \
36+
name != os.curdir and name != os.pardir and \
37+
os.path.isdir(fullname) and \
38+
not os.path.islink(fullname):
39+
compile_dir(fullname, maxlevels - 1)
40+
41+
def compile_path(skip_curdir = 1):
42+
for dir in sys.path:
43+
if dir == os.curdir and skip_curdir:
44+
print 'Skipping current directory'
45+
else:
46+
compile_dir(dir, 0)
47+
48+
def main():
49+
import getopt
50+
try:
51+
opts, args = getopt.getopt(sys.argv[1:], 'l')
52+
except getopt.error, msg:
53+
print msg
54+
print "usage: compileall [-l] [directory ...]"
55+
print "-l: don't recurse down"
56+
print "if no arguments, -l sys.path is assumed"
57+
maxlevels = 10
58+
for o, a in opts:
59+
if o == '-l': maxlevels = 0
60+
if args:
61+
for dir in sys.argv[1:]:
62+
compile_dir(dir, maxlevels)
63+
else:
64+
compile_path()
65+
66+
if __name__ == '__main__':
67+
main()

Lib/importall.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# THIS IS OBSOLETE -- USE MODULE 'compileall' INSTEAD!
2+
13
# Utility module to import all modules in the path, in the hope
24
# that this will update their ".pyc" files.
35

Lib/py_compile.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# 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+
5+
MAGIC = 0x999903
6+
7+
def 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+
13+
def 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)

Lib/urllib.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -452,7 +452,7 @@ def unquote(s):
452452
i = j+3
453453
return res
454454

455-
always_safe = string.letters + string.digits + '_,.+-'
455+
always_safe = string.letters + string.digits + '_,.-'
456456
def quote(s, safe = '/'):
457457
safe = always_safe + safe
458458
res = ''

Lib/whrandom.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ def __init__(self, x = None, y = None, z = None):
3939
if x is None:
4040
# Initialize from current time
4141
import time
42-
t = int(time.time())
42+
t = int(time.time() % 0x80000000)
4343
t, x = divmod(t, 256)
4444
t, y = divmod(t, 256)
4545
t, z = divmod(t, 256)

0 commit comments

Comments
 (0)