annotate Tools/scripts/checkpyc.py @ 87690:66c1c9f32567 default tip

Add use_weakref option to functools.lru_cache The use_weakref option makes lru_cache create weak references to the function arguments when creating the cache key, which prevents them from staying in the memory forever even after all other references to them are gone. This is a proof of concept for http://bugs.python.org/issue19859
author Radomir Dopieralski <devel@sheep.art.pl>
date Tue, 03 Dec 2013 16:02:48 +0100
parents ca3bdac1f88a
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
59522
fcfe324663a6 convert shebang lines: python -> python3
Benjamin Peterson <benjamin@python.org>
parents: 47100
diff changeset
1 #! /usr/bin/env python3
500
7f3faafd83ed Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2 # Check that all ".pyc" files exist and are up-to-date
758
bfc655074523 posix -> os
Guido van Rossum <guido@python.org>
parents: 559
diff changeset
3 # Uses module 'os'
500
7f3faafd83ed Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
4
7f3faafd83ed Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
5 import sys
758
bfc655074523 posix -> os
Guido van Rossum <guido@python.org>
parents: 559
diff changeset
6 import os
500
7f3faafd83ed Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
7 from stat import ST_MTIME
84158
ca3bdac1f88a Issue #17177: update checkpyc to stop using imp
Brett Cannon <brett@python.org>
parents: 81017
diff changeset
8 import importlib.util
500
7f3faafd83ed Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
9
63873
5c0d5de56fdf Fix ``Tools/scripts/checkpyc.py`` after PEP 3147.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59522
diff changeset
10 # PEP 3147 compatibility (PYC Repository Directories)
84158
ca3bdac1f88a Issue #17177: update checkpyc to stop using imp
Brett Cannon <brett@python.org>
parents: 81017
diff changeset
11 cache_from_source = (importlib.util.cache_from_source if sys.implementation.cache_tag
ca3bdac1f88a Issue #17177: update checkpyc to stop using imp
Brett Cannon <brett@python.org>
parents: 81017
diff changeset
12 else lambda path: path + 'c')
63873
5c0d5de56fdf Fix ``Tools/scripts/checkpyc.py`` after PEP 3147.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59522
diff changeset
13
5c0d5de56fdf Fix ``Tools/scripts/checkpyc.py`` after PEP 3147.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59522
diff changeset
14
500
7f3faafd83ed Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
15 def main():
63873
5c0d5de56fdf Fix ``Tools/scripts/checkpyc.py`` after PEP 3147.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59522
diff changeset
16 if len(sys.argv) > 1:
5c0d5de56fdf Fix ``Tools/scripts/checkpyc.py`` after PEP 3147.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59522
diff changeset
17 verbose = (sys.argv[1] == '-v')
5c0d5de56fdf Fix ``Tools/scripts/checkpyc.py`` after PEP 3147.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59522
diff changeset
18 silent = (sys.argv[1] == '-s')
5c0d5de56fdf Fix ``Tools/scripts/checkpyc.py`` after PEP 3147.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59522
diff changeset
19 else:
5c0d5de56fdf Fix ``Tools/scripts/checkpyc.py`` after PEP 3147.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59522
diff changeset
20 verbose = silent = False
84158
ca3bdac1f88a Issue #17177: update checkpyc to stop using imp
Brett Cannon <brett@python.org>
parents: 81017
diff changeset
21 MAGIC = importlib.util.MAGIC_NUMBER
15930
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 9294
diff changeset
22 if not silent:
42427
46625e6122e9 Convert print statements to function calls in Tools/.
Collin Winter <collinw@gmail.com>
parents: 33074
diff changeset
23 print('Using MAGIC word', repr(MAGIC))
15930
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 9294
diff changeset
24 for dirname in sys.path:
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 9294
diff changeset
25 try:
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 9294
diff changeset
26 names = os.listdir(dirname)
81017
6cfe2982de42 Use OESeeror instead of os.error (#16720)
Andrew Svetlov <andrew.svetlov@gmail.com>
parents: 63873
diff changeset
27 except OSError:
42427
46625e6122e9 Convert print statements to function calls in Tools/.
Collin Winter <collinw@gmail.com>
parents: 33074
diff changeset
28 print('Cannot list directory', repr(dirname))
15930
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 9294
diff changeset
29 continue
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 9294
diff changeset
30 if not silent:
42427
46625e6122e9 Convert print statements to function calls in Tools/.
Collin Winter <collinw@gmail.com>
parents: 33074
diff changeset
31 print('Checking ', repr(dirname), '...')
63873
5c0d5de56fdf Fix ``Tools/scripts/checkpyc.py`` after PEP 3147.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59522
diff changeset
32 for name in sorted(names):
5c0d5de56fdf Fix ``Tools/scripts/checkpyc.py`` after PEP 3147.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59522
diff changeset
33 if name.endswith('.py'):
15930
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 9294
diff changeset
34 name = os.path.join(dirname, name)
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 9294
diff changeset
35 try:
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 9294
diff changeset
36 st = os.stat(name)
81017
6cfe2982de42 Use OESeeror instead of os.error (#16720)
Andrew Svetlov <andrew.svetlov@gmail.com>
parents: 63873
diff changeset
37 except OSError:
42427
46625e6122e9 Convert print statements to function calls in Tools/.
Collin Winter <collinw@gmail.com>
parents: 33074
diff changeset
38 print('Cannot stat', repr(name))
15930
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 9294
diff changeset
39 continue
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 9294
diff changeset
40 if verbose:
42427
46625e6122e9 Convert print statements to function calls in Tools/.
Collin Winter <collinw@gmail.com>
parents: 33074
diff changeset
41 print('Check', repr(name), '...')
63873
5c0d5de56fdf Fix ``Tools/scripts/checkpyc.py`` after PEP 3147.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59522
diff changeset
42 name_c = cache_from_source(name)
15930
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 9294
diff changeset
43 try:
63873
5c0d5de56fdf Fix ``Tools/scripts/checkpyc.py`` after PEP 3147.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59522
diff changeset
44 with open(name_c, 'rb') as f:
5c0d5de56fdf Fix ``Tools/scripts/checkpyc.py`` after PEP 3147.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59522
diff changeset
45 magic_str = f.read(4)
5c0d5de56fdf Fix ``Tools/scripts/checkpyc.py`` after PEP 3147.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59522
diff changeset
46 mtime_str = f.read(4)
15930
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 9294
diff changeset
47 except IOError:
42427
46625e6122e9 Convert print statements to function calls in Tools/.
Collin Winter <collinw@gmail.com>
parents: 33074
diff changeset
48 print('Cannot open', repr(name_c))
15930
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 9294
diff changeset
49 continue
47100
893a4dc17a91 Ran 2to3 over scripts directory.
Georg Brandl <georg@python.org>
parents: 42427
diff changeset
50 if magic_str != MAGIC:
42427
46625e6122e9 Convert print statements to function calls in Tools/.
Collin Winter <collinw@gmail.com>
parents: 33074
diff changeset
51 print('Bad MAGIC word in ".pyc" file', end=' ')
46625e6122e9 Convert print statements to function calls in Tools/.
Collin Winter <collinw@gmail.com>
parents: 33074
diff changeset
52 print(repr(name_c))
15930
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 9294
diff changeset
53 continue
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 9294
diff changeset
54 mtime = get_long(mtime_str)
63873
5c0d5de56fdf Fix ``Tools/scripts/checkpyc.py`` after PEP 3147.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59522
diff changeset
55 if mtime in {0, -1}:
42427
46625e6122e9 Convert print statements to function calls in Tools/.
Collin Winter <collinw@gmail.com>
parents: 33074
diff changeset
56 print('Bad ".pyc" file', repr(name_c))
47100
893a4dc17a91 Ran 2to3 over scripts directory.
Georg Brandl <georg@python.org>
parents: 42427
diff changeset
57 elif mtime != st[ST_MTIME]:
42427
46625e6122e9 Convert print statements to function calls in Tools/.
Collin Winter <collinw@gmail.com>
parents: 33074
diff changeset
58 print('Out-of-date ".pyc" file', end=' ')
46625e6122e9 Convert print statements to function calls in Tools/.
Collin Winter <collinw@gmail.com>
parents: 33074
diff changeset
59 print(repr(name_c))
500
7f3faafd83ed Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
60
63873
5c0d5de56fdf Fix ``Tools/scripts/checkpyc.py`` after PEP 3147.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59522
diff changeset
61
500
7f3faafd83ed Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
62 def get_long(s):
47100
893a4dc17a91 Ran 2to3 over scripts directory.
Georg Brandl <georg@python.org>
parents: 42427
diff changeset
63 if len(s) != 4:
15930
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 9294
diff changeset
64 return -1
63873
5c0d5de56fdf Fix ``Tools/scripts/checkpyc.py`` after PEP 3147.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59522
diff changeset
65 return s[0] + (s[1] << 8) + (s[2] << 16) + (s[3] << 24)
5c0d5de56fdf Fix ``Tools/scripts/checkpyc.py`` after PEP 3147.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59522
diff changeset
66
500
7f3faafd83ed Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
67
33074
981fff7363b9 [Patch #1005491 ] use __name__ == '__main__' in scripts
Andrew M. Kuchling <amk@amk.ca>
parents: 31515
diff changeset
68 if __name__ == '__main__':
981fff7363b9 [Patch #1005491 ] use __name__ == '__main__' in scripts
Andrew M. Kuchling <amk@amk.ca>
parents: 31515
diff changeset
69 main()