Mercurial > cpython-lru_cache-weakref
annotate Tools/scripts/finddiv.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 | 6cfe2982de42 |
| children |
| rev | line source |
|---|---|
|
59522
fcfe324663a6
convert shebang lines: python -> python3
Benjamin Peterson <benjamin@python.org>
parents:
45049
diff
changeset
|
1 #! /usr/bin/env python3 |
|
19652
de0f035205ef
A grep-like tool that looks for division operators.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2 |
|
de0f035205ef
A grep-like tool that looks for division operators.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3 """finddiv - a grep-like tool that looks for division operators. |
|
de0f035205ef
A grep-like tool that looks for division operators.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
4 |
|
de0f035205ef
A grep-like tool that looks for division operators.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5 Usage: finddiv [-l] file_or_directory ... |
|
de0f035205ef
A grep-like tool that looks for division operators.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6 |
|
de0f035205ef
A grep-like tool that looks for division operators.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7 For directory arguments, all files in the directory whose name ends in |
|
de0f035205ef
A grep-like tool that looks for division operators.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8 .py are processed, and subdirectories are processed recursively. |
|
de0f035205ef
A grep-like tool that looks for division operators.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
9 |
|
de0f035205ef
A grep-like tool that looks for division operators.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
10 This actually tokenizes the files to avoid false hits in comments or |
|
de0f035205ef
A grep-like tool that looks for division operators.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
11 strings literals. |
|
de0f035205ef
A grep-like tool that looks for division operators.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
12 |
|
de0f035205ef
A grep-like tool that looks for division operators.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
13 By default, this prints all lines containing a / or /= operator, in |
|
de0f035205ef
A grep-like tool that looks for division operators.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
14 grep -n style. With the -l option specified, it prints the filename |
|
de0f035205ef
A grep-like tool that looks for division operators.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
15 of files that contain at least one / or /= operator. |
|
de0f035205ef
A grep-like tool that looks for division operators.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
16 """ |
|
de0f035205ef
A grep-like tool that looks for division operators.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
17 |
|
de0f035205ef
A grep-like tool that looks for division operators.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
18 import os |
|
de0f035205ef
A grep-like tool that looks for division operators.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
19 import sys |
|
de0f035205ef
A grep-like tool that looks for division operators.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
20 import getopt |
|
de0f035205ef
A grep-like tool that looks for division operators.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
21 import tokenize |
|
de0f035205ef
A grep-like tool that looks for division operators.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
22 |
|
de0f035205ef
A grep-like tool that looks for division operators.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
23 def main(): |
|
de0f035205ef
A grep-like tool that looks for division operators.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
24 try: |
|
de0f035205ef
A grep-like tool that looks for division operators.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
25 opts, args = getopt.getopt(sys.argv[1:], "lh") |
|
40587
6b18a095c2e7
SF patch 1631942 by Collin Winter:
Guido van Rossum <guido@python.org>
parents:
29107
diff
changeset
|
26 except getopt.error as msg: |
|
19652
de0f035205ef
A grep-like tool that looks for division operators.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
27 usage(msg) |
|
de0f035205ef
A grep-like tool that looks for division operators.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
28 return 2 |
|
de0f035205ef
A grep-like tool that looks for division operators.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
29 if not args: |
|
de0f035205ef
A grep-like tool that looks for division operators.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
30 usage("at least one file argument is required") |
|
de0f035205ef
A grep-like tool that looks for division operators.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
31 return 2 |
|
de0f035205ef
A grep-like tool that looks for division operators.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
32 listnames = 0 |
|
de0f035205ef
A grep-like tool that looks for division operators.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
33 for o, a in opts: |
|
de0f035205ef
A grep-like tool that looks for division operators.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
34 if o == "-h": |
|
42427
46625e6122e9
Convert print statements to function calls in Tools/.
Collin Winter <collinw@gmail.com>
parents:
40587
diff
changeset
|
35 print(__doc__) |
|
19652
de0f035205ef
A grep-like tool that looks for division operators.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
36 return |
|
de0f035205ef
A grep-like tool that looks for division operators.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
37 if o == "-l": |
|
de0f035205ef
A grep-like tool that looks for division operators.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
38 listnames = 1 |
|
de0f035205ef
A grep-like tool that looks for division operators.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
39 exit = None |
|
29107
93c92f20b6bf
Fix use of 'file' as a variable name.
Andrew M. Kuchling <amk@amk.ca>
parents:
19652
diff
changeset
|
40 for filename in args: |
|
93c92f20b6bf
Fix use of 'file' as a variable name.
Andrew M. Kuchling <amk@amk.ca>
parents:
19652
diff
changeset
|
41 x = process(filename, listnames) |
|
19652
de0f035205ef
A grep-like tool that looks for division operators.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
42 exit = exit or x |
|
de0f035205ef
A grep-like tool that looks for division operators.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
43 return exit |
|
de0f035205ef
A grep-like tool that looks for division operators.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
44 |
|
de0f035205ef
A grep-like tool that looks for division operators.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
45 def usage(msg): |
|
de0f035205ef
A grep-like tool that looks for division operators.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
46 sys.stderr.write("%s: %s\n" % (sys.argv[0], msg)) |
|
de0f035205ef
A grep-like tool that looks for division operators.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
47 sys.stderr.write("Usage: %s [-l] file ...\n" % sys.argv[0]) |
|
de0f035205ef
A grep-like tool that looks for division operators.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
48 sys.stderr.write("Try `%s -h' for more information.\n" % sys.argv[0]) |
|
de0f035205ef
A grep-like tool that looks for division operators.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
49 |
|
29107
93c92f20b6bf
Fix use of 'file' as a variable name.
Andrew M. Kuchling <amk@amk.ca>
parents:
19652
diff
changeset
|
50 def process(filename, listnames): |
|
93c92f20b6bf
Fix use of 'file' as a variable name.
Andrew M. Kuchling <amk@amk.ca>
parents:
19652
diff
changeset
|
51 if os.path.isdir(filename): |
|
93c92f20b6bf
Fix use of 'file' as a variable name.
Andrew M. Kuchling <amk@amk.ca>
parents:
19652
diff
changeset
|
52 return processdir(filename, listnames) |
|
19652
de0f035205ef
A grep-like tool that looks for division operators.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
53 try: |
|
29107
93c92f20b6bf
Fix use of 'file' as a variable name.
Andrew M. Kuchling <amk@amk.ca>
parents:
19652
diff
changeset
|
54 fp = open(filename) |
|
40587
6b18a095c2e7
SF patch 1631942 by Collin Winter:
Guido van Rossum <guido@python.org>
parents:
29107
diff
changeset
|
55 except IOError as msg: |
|
19652
de0f035205ef
A grep-like tool that looks for division operators.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
56 sys.stderr.write("Can't open: %s\n" % msg) |
|
de0f035205ef
A grep-like tool that looks for division operators.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
57 return 1 |
|
de0f035205ef
A grep-like tool that looks for division operators.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
58 g = tokenize.generate_tokens(fp.readline) |
|
de0f035205ef
A grep-like tool that looks for division operators.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
59 lastrow = None |
|
de0f035205ef
A grep-like tool that looks for division operators.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
60 for type, token, (row, col), end, line in g: |
|
de0f035205ef
A grep-like tool that looks for division operators.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
61 if token in ("/", "/="): |
|
de0f035205ef
A grep-like tool that looks for division operators.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
62 if listnames: |
|
42427
46625e6122e9
Convert print statements to function calls in Tools/.
Collin Winter <collinw@gmail.com>
parents:
40587
diff
changeset
|
63 print(filename) |
|
19652
de0f035205ef
A grep-like tool that looks for division operators.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
64 break |
|
de0f035205ef
A grep-like tool that looks for division operators.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
65 if row != lastrow: |
|
de0f035205ef
A grep-like tool that looks for division operators.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
66 lastrow = row |
|
42427
46625e6122e9
Convert print statements to function calls in Tools/.
Collin Winter <collinw@gmail.com>
parents:
40587
diff
changeset
|
67 print("%s:%d:%s" % (filename, row, line), end=' ') |
|
19652
de0f035205ef
A grep-like tool that looks for division operators.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
68 fp.close() |
|
de0f035205ef
A grep-like tool that looks for division operators.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
69 |
|
de0f035205ef
A grep-like tool that looks for division operators.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
70 def processdir(dir, listnames): |
|
de0f035205ef
A grep-like tool that looks for division operators.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
71 try: |
|
de0f035205ef
A grep-like tool that looks for division operators.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
72 names = os.listdir(dir) |
|
81017
6cfe2982de42
Use OESeeror instead of os.error (#16720)
Andrew Svetlov <andrew.svetlov@gmail.com>
parents:
59522
diff
changeset
|
73 except OSError as msg: |
|
19652
de0f035205ef
A grep-like tool that looks for division operators.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
74 sys.stderr.write("Can't list directory: %s\n" % dir) |
|
de0f035205ef
A grep-like tool that looks for division operators.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
75 return 1 |
|
de0f035205ef
A grep-like tool that looks for division operators.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
76 files = [] |
|
de0f035205ef
A grep-like tool that looks for division operators.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
77 for name in names: |
|
de0f035205ef
A grep-like tool that looks for division operators.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
78 fn = os.path.join(dir, name) |
|
de0f035205ef
A grep-like tool that looks for division operators.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
79 if os.path.normcase(fn).endswith(".py") or os.path.isdir(fn): |
|
de0f035205ef
A grep-like tool that looks for division operators.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
80 files.append(fn) |
|
45049
90e86ee3684b
Convert some custom sort comparison functions to equivalent key functions.
Raymond Hettinger <python@rcn.com>
parents:
42427
diff
changeset
|
81 files.sort(key=os.path.normcase) |
|
19652
de0f035205ef
A grep-like tool that looks for division operators.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
82 exit = None |
|
de0f035205ef
A grep-like tool that looks for division operators.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
83 for fn in files: |
|
de0f035205ef
A grep-like tool that looks for division operators.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
84 x = process(fn, listnames) |
|
de0f035205ef
A grep-like tool that looks for division operators.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
85 exit = exit or x |
|
de0f035205ef
A grep-like tool that looks for division operators.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
86 return exit |
|
de0f035205ef
A grep-like tool that looks for division operators.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
87 |
|
de0f035205ef
A grep-like tool that looks for division operators.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
88 if __name__ == "__main__": |
|
de0f035205ef
A grep-like tool that looks for division operators.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
89 sys.exit(main()) |
