Skip to content

Commit c7eaede

Browse files
committed
Fix Tools/scripts/checkpyc.py after PEP 3147.
1 parent e4a3380 commit c7eaede

File tree

2 files changed

+22
-17
lines changed

2 files changed

+22
-17
lines changed

Misc/NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,8 @@ Library
150150
Tools/Demos
151151
-----------
152152

153+
- Fix ``Tools/scripts/checkpyc.py`` after PEP 3147.
154+
153155
- Issue #8867: Fix ``Tools/scripts/serve.py`` to work with files containing
154156
non-ASCII content.
155157

Tools/scripts/checkpyc.py

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,17 @@
77
from stat import ST_MTIME
88
import imp
99

10+
# PEP 3147 compatibility (PYC Repository Directories)
11+
cache_from_source = (imp.cache_from_source if hasattr(imp, 'get_tag') else
12+
lambda path: path + 'c')
13+
14+
1015
def main():
11-
silent = 0
12-
verbose = 0
13-
if sys.argv[1:]:
14-
if sys.argv[1] == '-v':
15-
verbose = 1
16-
elif sys.argv[1] == '-s':
17-
silent = 1
16+
if len(sys.argv) > 1:
17+
verbose = (sys.argv[1] == '-v')
18+
silent = (sys.argv[1] == '-s')
19+
else:
20+
verbose = silent = False
1821
MAGIC = imp.get_magic()
1922
if not silent:
2023
print('Using MAGIC word', repr(MAGIC))
@@ -26,9 +29,8 @@ def main():
2629
continue
2730
if not silent:
2831
print('Checking ', repr(dirname), '...')
29-
names.sort()
30-
for name in names:
31-
if name[-3:] == '.py':
32+
for name in sorted(names):
33+
if name.endswith('.py'):
3234
name = os.path.join(dirname, name)
3335
try:
3436
st = os.stat(name)
@@ -37,30 +39,31 @@ def main():
3739
continue
3840
if verbose:
3941
print('Check', repr(name), '...')
40-
name_c = name + 'c'
42+
name_c = cache_from_source(name)
4143
try:
42-
f = open(name_c, 'r')
44+
with open(name_c, 'rb') as f:
45+
magic_str = f.read(4)
46+
mtime_str = f.read(4)
4347
except IOError:
4448
print('Cannot open', repr(name_c))
4549
continue
46-
magic_str = f.read(4)
47-
mtime_str = f.read(4)
48-
f.close()
4950
if magic_str != MAGIC:
5051
print('Bad MAGIC word in ".pyc" file', end=' ')
5152
print(repr(name_c))
5253
continue
5354
mtime = get_long(mtime_str)
54-
if mtime == 0 or mtime == -1:
55+
if mtime in {0, -1}:
5556
print('Bad ".pyc" file', repr(name_c))
5657
elif mtime != st[ST_MTIME]:
5758
print('Out-of-date ".pyc" file', end=' ')
5859
print(repr(name_c))
5960

61+
6062
def get_long(s):
6163
if len(s) != 4:
6264
return -1
63-
return ord(s[0]) + (ord(s[1])<<8) + (ord(s[2])<<16) + (ord(s[3])<<24)
65+
return s[0] + (s[1] << 8) + (s[2] << 16) + (s[3] << 24)
66+
6467

6568
if __name__ == '__main__':
6669
main()

0 commit comments

Comments
 (0)