Mercurial > cpython-lru_cache-weakref
annotate Tools/scripts/fixdiv.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 | fcfe324663a6 |
| children |
| rev | line source |
|---|---|
|
59522
fcfe324663a6
convert shebang lines: python -> python3
Benjamin Peterson <benjamin@python.org>
parents:
47100
diff
changeset
|
1 #! /usr/bin/env python3 |
|
19639
60f290a7eae8
The beginnings of a script to help finding / operators that may need
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2 |
|
60f290a7eae8
The beginnings of a script to help finding / operators that may need
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3 """fixdiv - tool to fix division operators. |
|
60f290a7eae8
The beginnings of a script to help finding / operators that may need
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
4 |
|
19673
b3271297b984
PEP 238 documented -Qwarn as warning only for classic int or long
Guido van Rossum <guido@python.org>
parents:
19672
diff
changeset
|
5 To use this tool, first run `python -Qwarnall yourscript.py 2>warnings'. |
|
19639
60f290a7eae8
The beginnings of a script to help finding / operators that may need
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6 This runs the script `yourscript.py' while writing warning messages |
|
60f290a7eae8
The beginnings of a script to help finding / operators that may need
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7 about all uses of the classic division operator to the file |
|
19653
79e2327e51d6
Added more text to the docstring, updated the way the exit status is
Guido van Rossum <guido@python.org>
parents:
19639
diff
changeset
|
8 `warnings'. The warnings look like this: |
|
79e2327e51d6
Added more text to the docstring, updated the way the exit status is
Guido van Rossum <guido@python.org>
parents:
19639
diff
changeset
|
9 |
|
79e2327e51d6
Added more text to the docstring, updated the way the exit status is
Guido van Rossum <guido@python.org>
parents:
19639
diff
changeset
|
10 <file>:<line>: DeprecationWarning: classic <type> division |
|
79e2327e51d6
Added more text to the docstring, updated the way the exit status is
Guido van Rossum <guido@python.org>
parents:
19639
diff
changeset
|
11 |
|
79e2327e51d6
Added more text to the docstring, updated the way the exit status is
Guido van Rossum <guido@python.org>
parents:
19639
diff
changeset
|
12 The warnings are written to stderr, so you must use `2>' for the I/O |
|
79e2327e51d6
Added more text to the docstring, updated the way the exit status is
Guido van Rossum <guido@python.org>
parents:
19639
diff
changeset
|
13 redirect. I know of no way to redirect stderr on Windows in a DOS |
|
79e2327e51d6
Added more text to the docstring, updated the way the exit status is
Guido van Rossum <guido@python.org>
parents:
19639
diff
changeset
|
14 box, so you will have to modify the script to set sys.stderr to some |
|
79e2327e51d6
Added more text to the docstring, updated the way the exit status is
Guido van Rossum <guido@python.org>
parents:
19639
diff
changeset
|
15 kind of log file if you want to do this on Windows. |
|
79e2327e51d6
Added more text to the docstring, updated the way the exit status is
Guido van Rossum <guido@python.org>
parents:
19639
diff
changeset
|
16 |
|
79e2327e51d6
Added more text to the docstring, updated the way the exit status is
Guido van Rossum <guido@python.org>
parents:
19639
diff
changeset
|
17 The warnings are not limited to the script; modules imported by the |
|
79e2327e51d6
Added more text to the docstring, updated the way the exit status is
Guido van Rossum <guido@python.org>
parents:
19639
diff
changeset
|
18 script may also trigger warnings. In fact a useful technique is to |
|
79e2327e51d6
Added more text to the docstring, updated the way the exit status is
Guido van Rossum <guido@python.org>
parents:
19639
diff
changeset
|
19 write a test script specifically intended to exercise all code in a |
|
79e2327e51d6
Added more text to the docstring, updated the way the exit status is
Guido van Rossum <guido@python.org>
parents:
19639
diff
changeset
|
20 particular module or set of modules. |
|
19639
60f290a7eae8
The beginnings of a script to help finding / operators that may need
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
21 |
|
60f290a7eae8
The beginnings of a script to help finding / operators that may need
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
22 Then run `python fixdiv.py warnings'. This first reads the warnings, |
|
60f290a7eae8
The beginnings of a script to help finding / operators that may need
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
23 looking for classic division warnings, and sorts them by file name and |
|
60f290a7eae8
The beginnings of a script to help finding / operators that may need
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
24 line number. Then, for each file that received at least one warning, |
|
60f290a7eae8
The beginnings of a script to help finding / operators that may need
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
25 it parses the file and tries to match the warnings up to the division |
|
19686
749e49c947c0
- Reverse the meaning of the -m option: warnings about multiple /
Guido van Rossum <guido@python.org>
parents:
19679
diff
changeset
|
26 operators found in the source code. If it is successful, it writes |
|
749e49c947c0
- Reverse the meaning of the -m option: warnings about multiple /
Guido van Rossum <guido@python.org>
parents:
19679
diff
changeset
|
27 its findings to stdout, preceded by a line of dashes and a line of the |
|
749e49c947c0
- Reverse the meaning of the -m option: warnings about multiple /
Guido van Rossum <guido@python.org>
parents:
19679
diff
changeset
|
28 form: |
|
749e49c947c0
- Reverse the meaning of the -m option: warnings about multiple /
Guido van Rossum <guido@python.org>
parents:
19679
diff
changeset
|
29 |
|
749e49c947c0
- Reverse the meaning of the -m option: warnings about multiple /
Guido van Rossum <guido@python.org>
parents:
19679
diff
changeset
|
30 Index: <file> |
|
749e49c947c0
- Reverse the meaning of the -m option: warnings about multiple /
Guido van Rossum <guido@python.org>
parents:
19679
diff
changeset
|
31 |
|
749e49c947c0
- Reverse the meaning of the -m option: warnings about multiple /
Guido van Rossum <guido@python.org>
parents:
19679
diff
changeset
|
32 If the only findings found are suggestions to change a / operator into |
|
749e49c947c0
- Reverse the meaning of the -m option: warnings about multiple /
Guido van Rossum <guido@python.org>
parents:
19679
diff
changeset
|
33 a // operator, the output is acceptable input for the Unix 'patch' |
|
749e49c947c0
- Reverse the meaning of the -m option: warnings about multiple /
Guido van Rossum <guido@python.org>
parents:
19679
diff
changeset
|
34 program. |
|
19653
79e2327e51d6
Added more text to the docstring, updated the way the exit status is
Guido van Rossum <guido@python.org>
parents:
19639
diff
changeset
|
35 |
|
19686
749e49c947c0
- Reverse the meaning of the -m option: warnings about multiple /
Guido van Rossum <guido@python.org>
parents:
19679
diff
changeset
|
36 Here are the possible messages on stdout (N stands for a line number): |
|
749e49c947c0
- Reverse the meaning of the -m option: warnings about multiple /
Guido van Rossum <guido@python.org>
parents:
19679
diff
changeset
|
37 |
|
749e49c947c0
- Reverse the meaning of the -m option: warnings about multiple /
Guido van Rossum <guido@python.org>
parents:
19679
diff
changeset
|
38 - A plain-diff-style change ('NcN', a line marked by '<', a line |
|
749e49c947c0
- Reverse the meaning of the -m option: warnings about multiple /
Guido van Rossum <guido@python.org>
parents:
19679
diff
changeset
|
39 containing '---', and a line marked by '>'): |
|
19653
79e2327e51d6
Added more text to the docstring, updated the way the exit status is
Guido van Rossum <guido@python.org>
parents:
19639
diff
changeset
|
40 |
|
19686
749e49c947c0
- Reverse the meaning of the -m option: warnings about multiple /
Guido van Rossum <guido@python.org>
parents:
19679
diff
changeset
|
41 A / operator was found that should be changed to //. This is the |
|
749e49c947c0
- Reverse the meaning of the -m option: warnings about multiple /
Guido van Rossum <guido@python.org>
parents:
19679
diff
changeset
|
42 recommendation when only int and/or long arguments were seen. |
|
749e49c947c0
- Reverse the meaning of the -m option: warnings about multiple /
Guido van Rossum <guido@python.org>
parents:
19679
diff
changeset
|
43 |
|
749e49c947c0
- Reverse the meaning of the -m option: warnings about multiple /
Guido van Rossum <guido@python.org>
parents:
19679
diff
changeset
|
44 - 'True division / operator at line N' and a line marked by '=': |
|
749e49c947c0
- Reverse the meaning of the -m option: warnings about multiple /
Guido van Rossum <guido@python.org>
parents:
19679
diff
changeset
|
45 |
|
749e49c947c0
- Reverse the meaning of the -m option: warnings about multiple /
Guido van Rossum <guido@python.org>
parents:
19679
diff
changeset
|
46 A / operator was found that can remain unchanged. This is the |
|
19653
79e2327e51d6
Added more text to the docstring, updated the way the exit status is
Guido van Rossum <guido@python.org>
parents:
19639
diff
changeset
|
47 recommendation when only float and/or complex arguments were seen. |
|
79e2327e51d6
Added more text to the docstring, updated the way the exit status is
Guido van Rossum <guido@python.org>
parents:
19639
diff
changeset
|
48 |
|
19686
749e49c947c0
- Reverse the meaning of the -m option: warnings about multiple /
Guido van Rossum <guido@python.org>
parents:
19679
diff
changeset
|
49 - 'Ambiguous / operator (..., ...) at line N', line marked by '?': |
|
19653
79e2327e51d6
Added more text to the docstring, updated the way the exit status is
Guido van Rossum <guido@python.org>
parents:
19639
diff
changeset
|
50 |
|
19686
749e49c947c0
- Reverse the meaning of the -m option: warnings about multiple /
Guido van Rossum <guido@python.org>
parents:
19679
diff
changeset
|
51 A / operator was found for which int or long as well as float or |
|
19653
79e2327e51d6
Added more text to the docstring, updated the way the exit status is
Guido van Rossum <guido@python.org>
parents:
19639
diff
changeset
|
52 complex arguments were seen. This is highly unlikely; if it occurs, |
|
79e2327e51d6
Added more text to the docstring, updated the way the exit status is
Guido van Rossum <guido@python.org>
parents:
19639
diff
changeset
|
53 you may have to restructure the code to keep the classic semantics, |
|
79e2327e51d6
Added more text to the docstring, updated the way the exit status is
Guido van Rossum <guido@python.org>
parents:
19639
diff
changeset
|
54 or maybe you don't care about the classic semantics. |
|
79e2327e51d6
Added more text to the docstring, updated the way the exit status is
Guido van Rossum <guido@python.org>
parents:
19639
diff
changeset
|
55 |
|
19686
749e49c947c0
- Reverse the meaning of the -m option: warnings about multiple /
Guido van Rossum <guido@python.org>
parents:
19679
diff
changeset
|
56 - 'No conclusive evidence on line N', line marked by '*': |
|
749e49c947c0
- Reverse the meaning of the -m option: warnings about multiple /
Guido van Rossum <guido@python.org>
parents:
19679
diff
changeset
|
57 |
|
749e49c947c0
- Reverse the meaning of the -m option: warnings about multiple /
Guido van Rossum <guido@python.org>
parents:
19679
diff
changeset
|
58 A / operator was found for which no warnings were seen. This could |
| 30723 | 59 be code that was never executed, or code that was only executed |
|
19653
79e2327e51d6
Added more text to the docstring, updated the way the exit status is
Guido van Rossum <guido@python.org>
parents:
19639
diff
changeset
|
60 with user-defined objects as arguments. You will have to |
|
79e2327e51d6
Added more text to the docstring, updated the way the exit status is
Guido van Rossum <guido@python.org>
parents:
19639
diff
changeset
|
61 investigate further. Note that // can be overloaded separately from |
|
79e2327e51d6
Added more text to the docstring, updated the way the exit status is
Guido van Rossum <guido@python.org>
parents:
19639
diff
changeset
|
62 /, using __floordiv__. True division can also be separately |
|
79e2327e51d6
Added more text to the docstring, updated the way the exit status is
Guido van Rossum <guido@python.org>
parents:
19639
diff
changeset
|
63 overloaded, using __truediv__. Classic division should be the same |
|
79e2327e51d6
Added more text to the docstring, updated the way the exit status is
Guido van Rossum <guido@python.org>
parents:
19639
diff
changeset
|
64 as either of those. (XXX should I add a warning for division on |
|
79e2327e51d6
Added more text to the docstring, updated the way the exit status is
Guido van Rossum <guido@python.org>
parents:
19639
diff
changeset
|
65 user-defined objects, to disambiguate this case from code that was |
|
79e2327e51d6
Added more text to the docstring, updated the way the exit status is
Guido van Rossum <guido@python.org>
parents:
19639
diff
changeset
|
66 never executed?) |
|
79e2327e51d6
Added more text to the docstring, updated the way the exit status is
Guido van Rossum <guido@python.org>
parents:
19639
diff
changeset
|
67 |
|
19686
749e49c947c0
- Reverse the meaning of the -m option: warnings about multiple /
Guido van Rossum <guido@python.org>
parents:
19679
diff
changeset
|
68 - 'Phantom ... warnings for line N', line marked by '*': |
|
749e49c947c0
- Reverse the meaning of the -m option: warnings about multiple /
Guido van Rossum <guido@python.org>
parents:
19679
diff
changeset
|
69 |
|
749e49c947c0
- Reverse the meaning of the -m option: warnings about multiple /
Guido van Rossum <guido@python.org>
parents:
19679
diff
changeset
|
70 A warning was seen for a line not containing a / operator. The most |
|
749e49c947c0
- Reverse the meaning of the -m option: warnings about multiple /
Guido van Rossum <guido@python.org>
parents:
19679
diff
changeset
|
71 likely cause is a warning about code executed by 'exec' or eval() |
|
749e49c947c0
- Reverse the meaning of the -m option: warnings about multiple /
Guido van Rossum <guido@python.org>
parents:
19679
diff
changeset
|
72 (see note below), or an indirect invocation of the / operator, for |
|
749e49c947c0
- Reverse the meaning of the -m option: warnings about multiple /
Guido van Rossum <guido@python.org>
parents:
19679
diff
changeset
|
73 example via the div() function in the operator module. It could |
|
749e49c947c0
- Reverse the meaning of the -m option: warnings about multiple /
Guido van Rossum <guido@python.org>
parents:
19679
diff
changeset
|
74 also be caused by a change to the file between the time the test |
|
749e49c947c0
- Reverse the meaning of the -m option: warnings about multiple /
Guido van Rossum <guido@python.org>
parents:
19679
diff
changeset
|
75 script was run to collect warnings and the time fixdiv was run. |
|
749e49c947c0
- Reverse the meaning of the -m option: warnings about multiple /
Guido van Rossum <guido@python.org>
parents:
19679
diff
changeset
|
76 |
|
749e49c947c0
- Reverse the meaning of the -m option: warnings about multiple /
Guido van Rossum <guido@python.org>
parents:
19679
diff
changeset
|
77 - 'More than one / operator in line N'; or |
|
749e49c947c0
- Reverse the meaning of the -m option: warnings about multiple /
Guido van Rossum <guido@python.org>
parents:
19679
diff
changeset
|
78 'More than one / operator per statement in lines N-N': |
|
19653
79e2327e51d6
Added more text to the docstring, updated the way the exit status is
Guido van Rossum <guido@python.org>
parents:
19639
diff
changeset
|
79 |
|
19686
749e49c947c0
- Reverse the meaning of the -m option: warnings about multiple /
Guido van Rossum <guido@python.org>
parents:
19679
diff
changeset
|
80 The scanner found more than one / operator on a single line, or in a |
|
749e49c947c0
- Reverse the meaning of the -m option: warnings about multiple /
Guido van Rossum <guido@python.org>
parents:
19679
diff
changeset
|
81 statement split across multiple lines. Because the warnings |
|
749e49c947c0
- Reverse the meaning of the -m option: warnings about multiple /
Guido van Rossum <guido@python.org>
parents:
19679
diff
changeset
|
82 framework doesn't (and can't) show the offset within the line, and |
|
749e49c947c0
- Reverse the meaning of the -m option: warnings about multiple /
Guido van Rossum <guido@python.org>
parents:
19679
diff
changeset
|
83 the code generator doesn't always give the correct line number for |
|
749e49c947c0
- Reverse the meaning of the -m option: warnings about multiple /
Guido van Rossum <guido@python.org>
parents:
19679
diff
changeset
|
84 operations in a multi-line statement, we can't be sure whether all |
|
749e49c947c0
- Reverse the meaning of the -m option: warnings about multiple /
Guido van Rossum <guido@python.org>
parents:
19679
diff
changeset
|
85 operators in the statement were executed. To be on the safe side, |
|
749e49c947c0
- Reverse the meaning of the -m option: warnings about multiple /
Guido van Rossum <guido@python.org>
parents:
19679
diff
changeset
|
86 by default a warning is issued about this case. In practice, these |
|
749e49c947c0
- Reverse the meaning of the -m option: warnings about multiple /
Guido van Rossum <guido@python.org>
parents:
19679
diff
changeset
|
87 cases are usually safe, and the -m option suppresses these warning. |
|
749e49c947c0
- Reverse the meaning of the -m option: warnings about multiple /
Guido van Rossum <guido@python.org>
parents:
19679
diff
changeset
|
88 |
|
749e49c947c0
- Reverse the meaning of the -m option: warnings about multiple /
Guido van Rossum <guido@python.org>
parents:
19679
diff
changeset
|
89 - 'Can't find the / operator in line N', line marked by '*': |
|
749e49c947c0
- Reverse the meaning of the -m option: warnings about multiple /
Guido van Rossum <guido@python.org>
parents:
19679
diff
changeset
|
90 |
|
749e49c947c0
- Reverse the meaning of the -m option: warnings about multiple /
Guido van Rossum <guido@python.org>
parents:
19679
diff
changeset
|
91 This really shouldn't happen. It means that the tokenize module |
|
749e49c947c0
- Reverse the meaning of the -m option: warnings about multiple /
Guido van Rossum <guido@python.org>
parents:
19679
diff
changeset
|
92 reported a '/' operator but the line it returns didn't contain a '/' |
|
749e49c947c0
- Reverse the meaning of the -m option: warnings about multiple /
Guido van Rossum <guido@python.org>
parents:
19679
diff
changeset
|
93 character at the indicated position. |
|
749e49c947c0
- Reverse the meaning of the -m option: warnings about multiple /
Guido van Rossum <guido@python.org>
parents:
19679
diff
changeset
|
94 |
|
749e49c947c0
- Reverse the meaning of the -m option: warnings about multiple /
Guido van Rossum <guido@python.org>
parents:
19679
diff
changeset
|
95 - 'Bad warning for line N: XYZ', line marked by '*': |
|
749e49c947c0
- Reverse the meaning of the -m option: warnings about multiple /
Guido van Rossum <guido@python.org>
parents:
19679
diff
changeset
|
96 |
|
749e49c947c0
- Reverse the meaning of the -m option: warnings about multiple /
Guido van Rossum <guido@python.org>
parents:
19679
diff
changeset
|
97 This really shouldn't happen. It means that a 'classic XYZ |
|
749e49c947c0
- Reverse the meaning of the -m option: warnings about multiple /
Guido van Rossum <guido@python.org>
parents:
19679
diff
changeset
|
98 division' warning was read with XYZ being something other than |
|
749e49c947c0
- Reverse the meaning of the -m option: warnings about multiple /
Guido van Rossum <guido@python.org>
parents:
19679
diff
changeset
|
99 'int', 'long', 'float', or 'complex'. |
|
19653
79e2327e51d6
Added more text to the docstring, updated the way the exit status is
Guido van Rossum <guido@python.org>
parents:
19639
diff
changeset
|
100 |
|
79e2327e51d6
Added more text to the docstring, updated the way the exit status is
Guido van Rossum <guido@python.org>
parents:
19639
diff
changeset
|
101 Notes: |
|
79e2327e51d6
Added more text to the docstring, updated the way the exit status is
Guido van Rossum <guido@python.org>
parents:
19639
diff
changeset
|
102 |
|
79e2327e51d6
Added more text to the docstring, updated the way the exit status is
Guido van Rossum <guido@python.org>
parents:
19639
diff
changeset
|
103 - The augmented assignment operator /= is handled the same way as the |
|
79e2327e51d6
Added more text to the docstring, updated the way the exit status is
Guido van Rossum <guido@python.org>
parents:
19639
diff
changeset
|
104 / operator. |
|
79e2327e51d6
Added more text to the docstring, updated the way the exit status is
Guido van Rossum <guido@python.org>
parents:
19639
diff
changeset
|
105 |
|
79e2327e51d6
Added more text to the docstring, updated the way the exit status is
Guido van Rossum <guido@python.org>
parents:
19639
diff
changeset
|
106 - This tool never looks at the // operator; no warnings are ever |
|
79e2327e51d6
Added more text to the docstring, updated the way the exit status is
Guido van Rossum <guido@python.org>
parents:
19639
diff
changeset
|
107 generated for use of this operator. |
|
79e2327e51d6
Added more text to the docstring, updated the way the exit status is
Guido van Rossum <guido@python.org>
parents:
19639
diff
changeset
|
108 |
|
79e2327e51d6
Added more text to the docstring, updated the way the exit status is
Guido van Rossum <guido@python.org>
parents:
19639
diff
changeset
|
109 - This tool never looks at the / operator when a future division |
|
79e2327e51d6
Added more text to the docstring, updated the way the exit status is
Guido van Rossum <guido@python.org>
parents:
19639
diff
changeset
|
110 statement is in effect; no warnings are generated in this case, and |
|
79e2327e51d6
Added more text to the docstring, updated the way the exit status is
Guido van Rossum <guido@python.org>
parents:
19639
diff
changeset
|
111 because the tool only looks at files for which at least one classic |
|
79e2327e51d6
Added more text to the docstring, updated the way the exit status is
Guido van Rossum <guido@python.org>
parents:
19639
diff
changeset
|
112 division warning was seen, it will never look at files containing a |
|
79e2327e51d6
Added more text to the docstring, updated the way the exit status is
Guido van Rossum <guido@python.org>
parents:
19639
diff
changeset
|
113 future division statement. |
|
79e2327e51d6
Added more text to the docstring, updated the way the exit status is
Guido van Rossum <guido@python.org>
parents:
19639
diff
changeset
|
114 |
|
79e2327e51d6
Added more text to the docstring, updated the way the exit status is
Guido van Rossum <guido@python.org>
parents:
19639
diff
changeset
|
115 - Warnings may be issued for code not read from a file, but executed |
|
39782
c94f37054f19
Patch #1550800: make exec a function.
Georg Brandl <georg@python.org>
parents:
30723
diff
changeset
|
116 using the exec() or eval() functions. These may have |
|
19686
749e49c947c0
- Reverse the meaning of the -m option: warnings about multiple /
Guido van Rossum <guido@python.org>
parents:
19679
diff
changeset
|
117 <string> in the filename position, in which case the fixdiv script |
|
749e49c947c0
- Reverse the meaning of the -m option: warnings about multiple /
Guido van Rossum <guido@python.org>
parents:
19679
diff
changeset
|
118 will attempt and fail to open a file named '<string>' and issue a |
|
749e49c947c0
- Reverse the meaning of the -m option: warnings about multiple /
Guido van Rossum <guido@python.org>
parents:
19679
diff
changeset
|
119 warning about this failure; or these may be reported as 'Phantom' |
|
749e49c947c0
- Reverse the meaning of the -m option: warnings about multiple /
Guido van Rossum <guido@python.org>
parents:
19679
diff
changeset
|
120 warnings (see above). You're on your own to deal with these. You |
|
749e49c947c0
- Reverse the meaning of the -m option: warnings about multiple /
Guido van Rossum <guido@python.org>
parents:
19679
diff
changeset
|
121 could make all recommended changes and add a future division |
|
749e49c947c0
- Reverse the meaning of the -m option: warnings about multiple /
Guido van Rossum <guido@python.org>
parents:
19679
diff
changeset
|
122 statement to all affected files, and then re-run the test script; it |
|
749e49c947c0
- Reverse the meaning of the -m option: warnings about multiple /
Guido van Rossum <guido@python.org>
parents:
19679
diff
changeset
|
123 should not issue any warnings. If there are any, and you have a |
|
749e49c947c0
- Reverse the meaning of the -m option: warnings about multiple /
Guido van Rossum <guido@python.org>
parents:
19679
diff
changeset
|
124 hard time tracking down where they are generated, you can use the |
|
749e49c947c0
- Reverse the meaning of the -m option: warnings about multiple /
Guido van Rossum <guido@python.org>
parents:
19679
diff
changeset
|
125 -Werror option to force an error instead of a first warning, |
|
749e49c947c0
- Reverse the meaning of the -m option: warnings about multiple /
Guido van Rossum <guido@python.org>
parents:
19679
diff
changeset
|
126 generating a traceback. |
|
19653
79e2327e51d6
Added more text to the docstring, updated the way the exit status is
Guido van Rossum <guido@python.org>
parents:
19639
diff
changeset
|
127 |
|
79e2327e51d6
Added more text to the docstring, updated the way the exit status is
Guido van Rossum <guido@python.org>
parents:
19639
diff
changeset
|
128 - The tool should be run from the same directory as that from which |
|
79e2327e51d6
Added more text to the docstring, updated the way the exit status is
Guido van Rossum <guido@python.org>
parents:
19639
diff
changeset
|
129 the original script was run, otherwise it won't be able to open |
|
79e2327e51d6
Added more text to the docstring, updated the way the exit status is
Guido van Rossum <guido@python.org>
parents:
19639
diff
changeset
|
130 files given by relative pathnames. |
|
19639
60f290a7eae8
The beginnings of a script to help finding / operators that may need
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
131 """ |
|
60f290a7eae8
The beginnings of a script to help finding / operators that may need
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
132 |
|
60f290a7eae8
The beginnings of a script to help finding / operators that may need
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
133 import sys |
|
60f290a7eae8
The beginnings of a script to help finding / operators that may need
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
134 import getopt |
|
60f290a7eae8
The beginnings of a script to help finding / operators that may need
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
135 import re |
|
60f290a7eae8
The beginnings of a script to help finding / operators that may need
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
136 import tokenize |
|
60f290a7eae8
The beginnings of a script to help finding / operators that may need
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
137 |
|
19686
749e49c947c0
- Reverse the meaning of the -m option: warnings about multiple /
Guido van Rossum <guido@python.org>
parents:
19679
diff
changeset
|
138 multi_ok = 0 |
|
19660
b4edce9d6e99
Implement what the docstring said: multiple slashes per line are
Guido van Rossum <guido@python.org>
parents:
19653
diff
changeset
|
139 |
|
19639
60f290a7eae8
The beginnings of a script to help finding / operators that may need
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
140 def main(): |
|
60f290a7eae8
The beginnings of a script to help finding / operators that may need
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
141 try: |
|
19660
b4edce9d6e99
Implement what the docstring said: multiple slashes per line are
Guido van Rossum <guido@python.org>
parents:
19653
diff
changeset
|
142 opts, args = getopt.getopt(sys.argv[1:], "hm") |
|
40587
6b18a095c2e7
SF patch 1631942 by Collin Winter:
Guido van Rossum <guido@python.org>
parents:
40484
diff
changeset
|
143 except getopt.error as msg: |
|
19653
79e2327e51d6
Added more text to the docstring, updated the way the exit status is
Guido van Rossum <guido@python.org>
parents:
19639
diff
changeset
|
144 usage(msg) |
|
79e2327e51d6
Added more text to the docstring, updated the way the exit status is
Guido van Rossum <guido@python.org>
parents:
19639
diff
changeset
|
145 return 2 |
|
19639
60f290a7eae8
The beginnings of a script to help finding / operators that may need
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
146 for o, a in opts: |
|
60f290a7eae8
The beginnings of a script to help finding / operators that may need
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
147 if o == "-h": |
|
42427
46625e6122e9
Convert print statements to function calls in Tools/.
Collin Winter <collinw@gmail.com>
parents:
40587
diff
changeset
|
148 print(__doc__) |
|
19653
79e2327e51d6
Added more text to the docstring, updated the way the exit status is
Guido van Rossum <guido@python.org>
parents:
19639
diff
changeset
|
149 return |
|
19660
b4edce9d6e99
Implement what the docstring said: multiple slashes per line are
Guido van Rossum <guido@python.org>
parents:
19653
diff
changeset
|
150 if o == "-m": |
|
b4edce9d6e99
Implement what the docstring said: multiple slashes per line are
Guido van Rossum <guido@python.org>
parents:
19653
diff
changeset
|
151 global multi_ok |
|
19686
749e49c947c0
- Reverse the meaning of the -m option: warnings about multiple /
Guido van Rossum <guido@python.org>
parents:
19679
diff
changeset
|
152 multi_ok = 1 |
|
19639
60f290a7eae8
The beginnings of a script to help finding / operators that may need
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
153 if not args: |
|
19653
79e2327e51d6
Added more text to the docstring, updated the way the exit status is
Guido van Rossum <guido@python.org>
parents:
19639
diff
changeset
|
154 usage("at least one file argument is required") |
|
79e2327e51d6
Added more text to the docstring, updated the way the exit status is
Guido van Rossum <guido@python.org>
parents:
19639
diff
changeset
|
155 return 2 |
|
19639
60f290a7eae8
The beginnings of a script to help finding / operators that may need
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
156 if args[1:]: |
|
60f290a7eae8
The beginnings of a script to help finding / operators that may need
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
157 sys.stderr.write("%s: extra file arguments ignored\n", sys.argv[0]) |
|
19653
79e2327e51d6
Added more text to the docstring, updated the way the exit status is
Guido van Rossum <guido@python.org>
parents:
19639
diff
changeset
|
158 warnings = readwarnings(args[0]) |
|
79e2327e51d6
Added more text to the docstring, updated the way the exit status is
Guido van Rossum <guido@python.org>
parents:
19639
diff
changeset
|
159 if warnings is None: |
|
79e2327e51d6
Added more text to the docstring, updated the way the exit status is
Guido van Rossum <guido@python.org>
parents:
19639
diff
changeset
|
160 return 1 |
|
47100
893a4dc17a91
Ran 2to3 over scripts directory.
Georg Brandl <georg@python.org>
parents:
42427
diff
changeset
|
161 files = list(warnings.keys()) |
|
19653
79e2327e51d6
Added more text to the docstring, updated the way the exit status is
Guido van Rossum <guido@python.org>
parents:
19639
diff
changeset
|
162 if not files: |
|
42427
46625e6122e9
Convert print statements to function calls in Tools/.
Collin Winter <collinw@gmail.com>
parents:
40587
diff
changeset
|
163 print("No classic division warnings read from", args[0]) |
|
19653
79e2327e51d6
Added more text to the docstring, updated the way the exit status is
Guido van Rossum <guido@python.org>
parents:
19639
diff
changeset
|
164 return |
|
79e2327e51d6
Added more text to the docstring, updated the way the exit status is
Guido van Rossum <guido@python.org>
parents:
19639
diff
changeset
|
165 files.sort() |
|
79e2327e51d6
Added more text to the docstring, updated the way the exit status is
Guido van Rossum <guido@python.org>
parents:
19639
diff
changeset
|
166 exit = None |
|
29107
93c92f20b6bf
Fix use of 'file' as a variable name.
Andrew M. Kuchling <amk@amk.ca>
parents:
19686
diff
changeset
|
167 for filename in files: |
|
93c92f20b6bf
Fix use of 'file' as a variable name.
Andrew M. Kuchling <amk@amk.ca>
parents:
19686
diff
changeset
|
168 x = process(filename, warnings[filename]) |
|
19653
79e2327e51d6
Added more text to the docstring, updated the way the exit status is
Guido van Rossum <guido@python.org>
parents:
19639
diff
changeset
|
169 exit = exit or x |
|
79e2327e51d6
Added more text to the docstring, updated the way the exit status is
Guido van Rossum <guido@python.org>
parents:
19639
diff
changeset
|
170 return exit |
|
19639
60f290a7eae8
The beginnings of a script to help finding / operators that may need
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
171 |
|
19653
79e2327e51d6
Added more text to the docstring, updated the way the exit status is
Guido van Rossum <guido@python.org>
parents:
19639
diff
changeset
|
172 def usage(msg): |
|
79e2327e51d6
Added more text to the docstring, updated the way the exit status is
Guido van Rossum <guido@python.org>
parents:
19639
diff
changeset
|
173 sys.stderr.write("%s: %s\n" % (sys.argv[0], msg)) |
|
19660
b4edce9d6e99
Implement what the docstring said: multiple slashes per line are
Guido van Rossum <guido@python.org>
parents:
19653
diff
changeset
|
174 sys.stderr.write("Usage: %s [-m] warnings\n" % sys.argv[0]) |
|
19639
60f290a7eae8
The beginnings of a script to help finding / operators that may need
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
175 sys.stderr.write("Try `%s -h' for more information.\n" % sys.argv[0]) |
|
60f290a7eae8
The beginnings of a script to help finding / operators that may need
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
176 |
|
19653
79e2327e51d6
Added more text to the docstring, updated the way the exit status is
Guido van Rossum <guido@python.org>
parents:
19639
diff
changeset
|
177 PATTERN = ("^(.+?):(\d+): DeprecationWarning: " |
|
79e2327e51d6
Added more text to the docstring, updated the way the exit status is
Guido van Rossum <guido@python.org>
parents:
19639
diff
changeset
|
178 "classic (int|long|float|complex) division$") |
|
19639
60f290a7eae8
The beginnings of a script to help finding / operators that may need
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
179 |
|
60f290a7eae8
The beginnings of a script to help finding / operators that may need
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
180 def readwarnings(warningsfile): |
|
19653
79e2327e51d6
Added more text to the docstring, updated the way the exit status is
Guido van Rossum <guido@python.org>
parents:
19639
diff
changeset
|
181 prog = re.compile(PATTERN) |
|
19639
60f290a7eae8
The beginnings of a script to help finding / operators that may need
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
182 try: |
|
60f290a7eae8
The beginnings of a script to help finding / operators that may need
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
183 f = open(warningsfile) |
|
40587
6b18a095c2e7
SF patch 1631942 by Collin Winter:
Guido van Rossum <guido@python.org>
parents:
40484
diff
changeset
|
184 except IOError as msg: |
|
19639
60f290a7eae8
The beginnings of a script to help finding / operators that may need
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
185 sys.stderr.write("can't open: %s\n" % msg) |
|
60f290a7eae8
The beginnings of a script to help finding / operators that may need
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
186 return |
|
60f290a7eae8
The beginnings of a script to help finding / operators that may need
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
187 warnings = {} |
|
60f290a7eae8
The beginnings of a script to help finding / operators that may need
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
188 while 1: |
|
60f290a7eae8
The beginnings of a script to help finding / operators that may need
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
189 line = f.readline() |
|
60f290a7eae8
The beginnings of a script to help finding / operators that may need
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
190 if not line: |
|
60f290a7eae8
The beginnings of a script to help finding / operators that may need
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
191 break |
|
19653
79e2327e51d6
Added more text to the docstring, updated the way the exit status is
Guido van Rossum <guido@python.org>
parents:
19639
diff
changeset
|
192 m = prog.match(line) |
|
19639
60f290a7eae8
The beginnings of a script to help finding / operators that may need
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
193 if not m: |
|
60f290a7eae8
The beginnings of a script to help finding / operators that may need
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
194 if line.find("division") >= 0: |
|
60f290a7eae8
The beginnings of a script to help finding / operators that may need
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
195 sys.stderr.write("Warning: ignored input " + line) |
|
60f290a7eae8
The beginnings of a script to help finding / operators that may need
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
196 continue |
|
29107
93c92f20b6bf
Fix use of 'file' as a variable name.
Andrew M. Kuchling <amk@amk.ca>
parents:
19686
diff
changeset
|
197 filename, lineno, what = m.groups() |
|
93c92f20b6bf
Fix use of 'file' as a variable name.
Andrew M. Kuchling <amk@amk.ca>
parents:
19686
diff
changeset
|
198 list = warnings.get(filename) |
|
19639
60f290a7eae8
The beginnings of a script to help finding / operators that may need
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
199 if list is None: |
|
29107
93c92f20b6bf
Fix use of 'file' as a variable name.
Andrew M. Kuchling <amk@amk.ca>
parents:
19686
diff
changeset
|
200 warnings[filename] = list = [] |
|
40484
8565e3fea9b6
Patch #1601678: move intern() to sys.intern().
Georg Brandl <georg@python.org>
parents:
39782
diff
changeset
|
201 list.append((int(lineno), sys.intern(what))) |
|
19639
60f290a7eae8
The beginnings of a script to help finding / operators that may need
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
202 f.close() |
|
19653
79e2327e51d6
Added more text to the docstring, updated the way the exit status is
Guido van Rossum <guido@python.org>
parents:
19639
diff
changeset
|
203 return warnings |
|
19639
60f290a7eae8
The beginnings of a script to help finding / operators that may need
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
204 |
|
29107
93c92f20b6bf
Fix use of 'file' as a variable name.
Andrew M. Kuchling <amk@amk.ca>
parents:
19686
diff
changeset
|
205 def process(filename, list): |
|
42427
46625e6122e9
Convert print statements to function calls in Tools/.
Collin Winter <collinw@gmail.com>
parents:
40587
diff
changeset
|
206 print("-"*70) |
|
19653
79e2327e51d6
Added more text to the docstring, updated the way the exit status is
Guido van Rossum <guido@python.org>
parents:
19639
diff
changeset
|
207 assert list # if this fails, readwarnings() is broken |
|
19639
60f290a7eae8
The beginnings of a script to help finding / operators that may need
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
208 try: |
|
29107
93c92f20b6bf
Fix use of 'file' as a variable name.
Andrew M. Kuchling <amk@amk.ca>
parents:
19686
diff
changeset
|
209 fp = open(filename) |
|
40587
6b18a095c2e7
SF patch 1631942 by Collin Winter:
Guido van Rossum <guido@python.org>
parents:
40484
diff
changeset
|
210 except IOError as msg: |
|
19639
60f290a7eae8
The beginnings of a script to help finding / operators that may need
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
211 sys.stderr.write("can't open: %s\n" % msg) |
|
19653
79e2327e51d6
Added more text to the docstring, updated the way the exit status is
Guido van Rossum <guido@python.org>
parents:
19639
diff
changeset
|
212 return 1 |
|
42427
46625e6122e9
Convert print statements to function calls in Tools/.
Collin Winter <collinw@gmail.com>
parents:
40587
diff
changeset
|
213 print("Index:", filename) |
|
19639
60f290a7eae8
The beginnings of a script to help finding / operators that may need
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
214 f = FileContext(fp) |
|
60f290a7eae8
The beginnings of a script to help finding / operators that may need
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
215 list.sort() |
|
60f290a7eae8
The beginnings of a script to help finding / operators that may need
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
216 index = 0 # list[:index] has been processed, list[index:] is still to do |
|
60f290a7eae8
The beginnings of a script to help finding / operators that may need
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
217 g = tokenize.generate_tokens(f.readline) |
|
60f290a7eae8
The beginnings of a script to help finding / operators that may need
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
218 while 1: |
|
60f290a7eae8
The beginnings of a script to help finding / operators that may need
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
219 startlineno, endlineno, slashes = lineinfo = scanline(g) |
|
60f290a7eae8
The beginnings of a script to help finding / operators that may need
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
220 if startlineno is None: |
|
60f290a7eae8
The beginnings of a script to help finding / operators that may need
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
221 break |
|
60f290a7eae8
The beginnings of a script to help finding / operators that may need
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
222 assert startlineno <= endlineno is not None |
|
19653
79e2327e51d6
Added more text to the docstring, updated the way the exit status is
Guido van Rossum <guido@python.org>
parents:
19639
diff
changeset
|
223 orphans = [] |
|
19639
60f290a7eae8
The beginnings of a script to help finding / operators that may need
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
224 while index < len(list) and list[index][0] < startlineno: |
|
60f290a7eae8
The beginnings of a script to help finding / operators that may need
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
225 orphans.append(list[index]) |
|
60f290a7eae8
The beginnings of a script to help finding / operators that may need
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
226 index += 1 |
|
19653
79e2327e51d6
Added more text to the docstring, updated the way the exit status is
Guido van Rossum <guido@python.org>
parents:
19639
diff
changeset
|
227 if orphans: |
|
79e2327e51d6
Added more text to the docstring, updated the way the exit status is
Guido van Rossum <guido@python.org>
parents:
19639
diff
changeset
|
228 reportphantomwarnings(orphans, f) |
|
19639
60f290a7eae8
The beginnings of a script to help finding / operators that may need
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
229 warnings = [] |
|
60f290a7eae8
The beginnings of a script to help finding / operators that may need
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
230 while index < len(list) and list[index][0] <= endlineno: |
|
60f290a7eae8
The beginnings of a script to help finding / operators that may need
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
231 warnings.append(list[index]) |
|
60f290a7eae8
The beginnings of a script to help finding / operators that may need
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
232 index += 1 |
|
60f290a7eae8
The beginnings of a script to help finding / operators that may need
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
233 if not slashes and not warnings: |
|
60f290a7eae8
The beginnings of a script to help finding / operators that may need
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
234 pass |
|
60f290a7eae8
The beginnings of a script to help finding / operators that may need
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
235 elif slashes and not warnings: |
|
19653
79e2327e51d6
Added more text to the docstring, updated the way the exit status is
Guido van Rossum <guido@python.org>
parents:
19639
diff
changeset
|
236 report(slashes, "No conclusive evidence") |
|
19639
60f290a7eae8
The beginnings of a script to help finding / operators that may need
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
237 elif warnings and not slashes: |
|
60f290a7eae8
The beginnings of a script to help finding / operators that may need
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
238 reportphantomwarnings(warnings, f) |
|
60f290a7eae8
The beginnings of a script to help finding / operators that may need
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
239 else: |
|
60f290a7eae8
The beginnings of a script to help finding / operators that may need
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
240 if len(slashes) > 1: |
|
19660
b4edce9d6e99
Implement what the docstring said: multiple slashes per line are
Guido van Rossum <guido@python.org>
parents:
19653
diff
changeset
|
241 if not multi_ok: |
|
19686
749e49c947c0
- Reverse the meaning of the -m option: warnings about multiple /
Guido van Rossum <guido@python.org>
parents:
19679
diff
changeset
|
242 rows = [] |
|
749e49c947c0
- Reverse the meaning of the -m option: warnings about multiple /
Guido van Rossum <guido@python.org>
parents:
19679
diff
changeset
|
243 lastrow = None |
|
749e49c947c0
- Reverse the meaning of the -m option: warnings about multiple /
Guido van Rossum <guido@python.org>
parents:
19679
diff
changeset
|
244 for (row, col), line in slashes: |
|
749e49c947c0
- Reverse the meaning of the -m option: warnings about multiple /
Guido van Rossum <guido@python.org>
parents:
19679
diff
changeset
|
245 if row == lastrow: |
|
749e49c947c0
- Reverse the meaning of the -m option: warnings about multiple /
Guido van Rossum <guido@python.org>
parents:
19679
diff
changeset
|
246 continue |
|
749e49c947c0
- Reverse the meaning of the -m option: warnings about multiple /
Guido van Rossum <guido@python.org>
parents:
19679
diff
changeset
|
247 rows.append(row) |
|
749e49c947c0
- Reverse the meaning of the -m option: warnings about multiple /
Guido van Rossum <guido@python.org>
parents:
19679
diff
changeset
|
248 lastrow = row |
|
749e49c947c0
- Reverse the meaning of the -m option: warnings about multiple /
Guido van Rossum <guido@python.org>
parents:
19679
diff
changeset
|
249 assert rows |
|
749e49c947c0
- Reverse the meaning of the -m option: warnings about multiple /
Guido van Rossum <guido@python.org>
parents:
19679
diff
changeset
|
250 if len(rows) == 1: |
|
42427
46625e6122e9
Convert print statements to function calls in Tools/.
Collin Winter <collinw@gmail.com>
parents:
40587
diff
changeset
|
251 print("*** More than one / operator in line", rows[0]) |
|
19686
749e49c947c0
- Reverse the meaning of the -m option: warnings about multiple /
Guido van Rossum <guido@python.org>
parents:
19679
diff
changeset
|
252 else: |
|
42427
46625e6122e9
Convert print statements to function calls in Tools/.
Collin Winter <collinw@gmail.com>
parents:
40587
diff
changeset
|
253 print("*** More than one / operator per statement", end=' ') |
|
46625e6122e9
Convert print statements to function calls in Tools/.
Collin Winter <collinw@gmail.com>
parents:
40587
diff
changeset
|
254 print("in lines %d-%d" % (rows[0], rows[-1])) |
|
19660
b4edce9d6e99
Implement what the docstring said: multiple slashes per line are
Guido van Rossum <guido@python.org>
parents:
19653
diff
changeset
|
255 intlong = [] |
|
b4edce9d6e99
Implement what the docstring said: multiple slashes per line are
Guido van Rossum <guido@python.org>
parents:
19653
diff
changeset
|
256 floatcomplex = [] |
|
b4edce9d6e99
Implement what the docstring said: multiple slashes per line are
Guido van Rossum <guido@python.org>
parents:
19653
diff
changeset
|
257 bad = [] |
|
b4edce9d6e99
Implement what the docstring said: multiple slashes per line are
Guido van Rossum <guido@python.org>
parents:
19653
diff
changeset
|
258 for lineno, what in warnings: |
|
b4edce9d6e99
Implement what the docstring said: multiple slashes per line are
Guido van Rossum <guido@python.org>
parents:
19653
diff
changeset
|
259 if what in ("int", "long"): |
|
b4edce9d6e99
Implement what the docstring said: multiple slashes per line are
Guido van Rossum <guido@python.org>
parents:
19653
diff
changeset
|
260 intlong.append(what) |
|
b4edce9d6e99
Implement what the docstring said: multiple slashes per line are
Guido van Rossum <guido@python.org>
parents:
19653
diff
changeset
|
261 elif what in ("float", "complex"): |
|
b4edce9d6e99
Implement what the docstring said: multiple slashes per line are
Guido van Rossum <guido@python.org>
parents:
19653
diff
changeset
|
262 floatcomplex.append(what) |
|
b4edce9d6e99
Implement what the docstring said: multiple slashes per line are
Guido van Rossum <guido@python.org>
parents:
19653
diff
changeset
|
263 else: |
|
b4edce9d6e99
Implement what the docstring said: multiple slashes per line are
Guido van Rossum <guido@python.org>
parents:
19653
diff
changeset
|
264 bad.append(what) |
|
b4edce9d6e99
Implement what the docstring said: multiple slashes per line are
Guido van Rossum <guido@python.org>
parents:
19653
diff
changeset
|
265 lastrow = None |
|
b4edce9d6e99
Implement what the docstring said: multiple slashes per line are
Guido van Rossum <guido@python.org>
parents:
19653
diff
changeset
|
266 for (row, col), line in slashes: |
|
b4edce9d6e99
Implement what the docstring said: multiple slashes per line are
Guido van Rossum <guido@python.org>
parents:
19653
diff
changeset
|
267 if row == lastrow: |
|
b4edce9d6e99
Implement what the docstring said: multiple slashes per line are
Guido van Rossum <guido@python.org>
parents:
19653
diff
changeset
|
268 continue |
|
b4edce9d6e99
Implement what the docstring said: multiple slashes per line are
Guido van Rossum <guido@python.org>
parents:
19653
diff
changeset
|
269 lastrow = row |
|
19639
60f290a7eae8
The beginnings of a script to help finding / operators that may need
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
270 line = chop(line) |
|
60f290a7eae8
The beginnings of a script to help finding / operators that may need
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
271 if line[col:col+1] != "/": |
|
42427
46625e6122e9
Convert print statements to function calls in Tools/.
Collin Winter <collinw@gmail.com>
parents:
40587
diff
changeset
|
272 print("*** Can't find the / operator in line %d:" % row) |
|
46625e6122e9
Convert print statements to function calls in Tools/.
Collin Winter <collinw@gmail.com>
parents:
40587
diff
changeset
|
273 print("*", line) |
|
19639
60f290a7eae8
The beginnings of a script to help finding / operators that may need
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
274 continue |
|
60f290a7eae8
The beginnings of a script to help finding / operators that may need
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
275 if bad: |
|
42427
46625e6122e9
Convert print statements to function calls in Tools/.
Collin Winter <collinw@gmail.com>
parents:
40587
diff
changeset
|
276 print("*** Bad warning for line %d:" % row, bad) |
|
46625e6122e9
Convert print statements to function calls in Tools/.
Collin Winter <collinw@gmail.com>
parents:
40587
diff
changeset
|
277 print("*", line) |
|
19639
60f290a7eae8
The beginnings of a script to help finding / operators that may need
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
278 elif intlong and not floatcomplex: |
|
42427
46625e6122e9
Convert print statements to function calls in Tools/.
Collin Winter <collinw@gmail.com>
parents:
40587
diff
changeset
|
279 print("%dc%d" % (row, row)) |
|
46625e6122e9
Convert print statements to function calls in Tools/.
Collin Winter <collinw@gmail.com>
parents:
40587
diff
changeset
|
280 print("<", line) |
|
46625e6122e9
Convert print statements to function calls in Tools/.
Collin Winter <collinw@gmail.com>
parents:
40587
diff
changeset
|
281 print("---") |
|
46625e6122e9
Convert print statements to function calls in Tools/.
Collin Winter <collinw@gmail.com>
parents:
40587
diff
changeset
|
282 print(">", line[:col] + "/" + line[col:]) |
|
19639
60f290a7eae8
The beginnings of a script to help finding / operators that may need
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
283 elif floatcomplex and not intlong: |
|
42427
46625e6122e9
Convert print statements to function calls in Tools/.
Collin Winter <collinw@gmail.com>
parents:
40587
diff
changeset
|
284 print("True division / operator at line %d:" % row) |
|
46625e6122e9
Convert print statements to function calls in Tools/.
Collin Winter <collinw@gmail.com>
parents:
40587
diff
changeset
|
285 print("=", line) |
|
19686
749e49c947c0
- Reverse the meaning of the -m option: warnings about multiple /
Guido van Rossum <guido@python.org>
parents:
19679
diff
changeset
|
286 elif intlong and floatcomplex: |
|
42427
46625e6122e9
Convert print statements to function calls in Tools/.
Collin Winter <collinw@gmail.com>
parents:
40587
diff
changeset
|
287 print("*** Ambiguous / operator (%s, %s) at line %d:" % ( |
|
46625e6122e9
Convert print statements to function calls in Tools/.
Collin Winter <collinw@gmail.com>
parents:
40587
diff
changeset
|
288 "|".join(intlong), "|".join(floatcomplex), row)) |
|
46625e6122e9
Convert print statements to function calls in Tools/.
Collin Winter <collinw@gmail.com>
parents:
40587
diff
changeset
|
289 print("?", line) |
|
19639
60f290a7eae8
The beginnings of a script to help finding / operators that may need
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
290 fp.close() |
|
60f290a7eae8
The beginnings of a script to help finding / operators that may need
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
291 |
|
60f290a7eae8
The beginnings of a script to help finding / operators that may need
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
292 def reportphantomwarnings(warnings, f): |
|
60f290a7eae8
The beginnings of a script to help finding / operators that may need
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
293 blocks = [] |
|
60f290a7eae8
The beginnings of a script to help finding / operators that may need
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
294 lastrow = None |
|
60f290a7eae8
The beginnings of a script to help finding / operators that may need
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
295 lastblock = None |
|
60f290a7eae8
The beginnings of a script to help finding / operators that may need
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
296 for row, what in warnings: |
|
60f290a7eae8
The beginnings of a script to help finding / operators that may need
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
297 if row != lastrow: |
|
60f290a7eae8
The beginnings of a script to help finding / operators that may need
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
298 lastblock = [row] |
|
60f290a7eae8
The beginnings of a script to help finding / operators that may need
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
299 blocks.append(lastblock) |
|
60f290a7eae8
The beginnings of a script to help finding / operators that may need
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
300 lastblock.append(what) |
|
60f290a7eae8
The beginnings of a script to help finding / operators that may need
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
301 for block in blocks: |
|
60f290a7eae8
The beginnings of a script to help finding / operators that may need
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
302 row = block[0] |
|
60f290a7eae8
The beginnings of a script to help finding / operators that may need
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
303 whats = "/".join(block[1:]) |
|
42427
46625e6122e9
Convert print statements to function calls in Tools/.
Collin Winter <collinw@gmail.com>
parents:
40587
diff
changeset
|
304 print("*** Phantom %s warnings for line %d:" % (whats, row)) |
|
19639
60f290a7eae8
The beginnings of a script to help finding / operators that may need
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
305 f.report(row, mark="*") |
|
60f290a7eae8
The beginnings of a script to help finding / operators that may need
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
306 |
|
60f290a7eae8
The beginnings of a script to help finding / operators that may need
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
307 def report(slashes, message): |
|
60f290a7eae8
The beginnings of a script to help finding / operators that may need
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
308 lastrow = None |
|
60f290a7eae8
The beginnings of a script to help finding / operators that may need
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
309 for (row, col), line in slashes: |
|
60f290a7eae8
The beginnings of a script to help finding / operators that may need
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
310 if row != lastrow: |
|
42427
46625e6122e9
Convert print statements to function calls in Tools/.
Collin Winter <collinw@gmail.com>
parents:
40587
diff
changeset
|
311 print("*** %s on line %d:" % (message, row)) |
|
46625e6122e9
Convert print statements to function calls in Tools/.
Collin Winter <collinw@gmail.com>
parents:
40587
diff
changeset
|
312 print("*", chop(line)) |
|
19639
60f290a7eae8
The beginnings of a script to help finding / operators that may need
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
313 lastrow = row |
|
60f290a7eae8
The beginnings of a script to help finding / operators that may need
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
314 |
|
60f290a7eae8
The beginnings of a script to help finding / operators that may need
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
315 class FileContext: |
|
60f290a7eae8
The beginnings of a script to help finding / operators that may need
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
316 def __init__(self, fp, window=5, lineno=1): |
|
60f290a7eae8
The beginnings of a script to help finding / operators that may need
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
317 self.fp = fp |
|
60f290a7eae8
The beginnings of a script to help finding / operators that may need
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
318 self.window = 5 |
|
60f290a7eae8
The beginnings of a script to help finding / operators that may need
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
319 self.lineno = 1 |
|
60f290a7eae8
The beginnings of a script to help finding / operators that may need
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
320 self.eoflookahead = 0 |
|
60f290a7eae8
The beginnings of a script to help finding / operators that may need
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
321 self.lookahead = [] |
|
60f290a7eae8
The beginnings of a script to help finding / operators that may need
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
322 self.buffer = [] |
|
60f290a7eae8
The beginnings of a script to help finding / operators that may need
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
323 def fill(self): |
|
60f290a7eae8
The beginnings of a script to help finding / operators that may need
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
324 while len(self.lookahead) < self.window and not self.eoflookahead: |
|
60f290a7eae8
The beginnings of a script to help finding / operators that may need
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
325 line = self.fp.readline() |
|
60f290a7eae8
The beginnings of a script to help finding / operators that may need
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
326 if not line: |
|
60f290a7eae8
The beginnings of a script to help finding / operators that may need
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
327 self.eoflookahead = 1 |
|
60f290a7eae8
The beginnings of a script to help finding / operators that may need
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
328 break |
|
60f290a7eae8
The beginnings of a script to help finding / operators that may need
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
329 self.lookahead.append(line) |
|
60f290a7eae8
The beginnings of a script to help finding / operators that may need
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
330 def readline(self): |
|
60f290a7eae8
The beginnings of a script to help finding / operators that may need
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
331 self.fill() |
|
60f290a7eae8
The beginnings of a script to help finding / operators that may need
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
332 if not self.lookahead: |
|
60f290a7eae8
The beginnings of a script to help finding / operators that may need
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
333 return "" |
|
60f290a7eae8
The beginnings of a script to help finding / operators that may need
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
334 line = self.lookahead.pop(0) |
|
60f290a7eae8
The beginnings of a script to help finding / operators that may need
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
335 self.buffer.append(line) |
|
60f290a7eae8
The beginnings of a script to help finding / operators that may need
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
336 self.lineno += 1 |
|
60f290a7eae8
The beginnings of a script to help finding / operators that may need
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
337 return line |
|
60f290a7eae8
The beginnings of a script to help finding / operators that may need
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
338 def truncate(self): |
|
60f290a7eae8
The beginnings of a script to help finding / operators that may need
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
339 del self.buffer[-window:] |
|
60f290a7eae8
The beginnings of a script to help finding / operators that may need
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
340 def __getitem__(self, index): |
|
60f290a7eae8
The beginnings of a script to help finding / operators that may need
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
341 self.fill() |
|
60f290a7eae8
The beginnings of a script to help finding / operators that may need
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
342 bufstart = self.lineno - len(self.buffer) |
|
60f290a7eae8
The beginnings of a script to help finding / operators that may need
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
343 lookend = self.lineno + len(self.lookahead) |
|
60f290a7eae8
The beginnings of a script to help finding / operators that may need
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
344 if bufstart <= index < self.lineno: |
|
60f290a7eae8
The beginnings of a script to help finding / operators that may need
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
345 return self.buffer[index - bufstart] |
|
60f290a7eae8
The beginnings of a script to help finding / operators that may need
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
346 if self.lineno <= index < lookend: |
|
60f290a7eae8
The beginnings of a script to help finding / operators that may need
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
347 return self.lookahead[index - self.lineno] |
|
60f290a7eae8
The beginnings of a script to help finding / operators that may need
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
348 raise KeyError |
|
60f290a7eae8
The beginnings of a script to help finding / operators that may need
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
349 def report(self, first, last=None, mark="*"): |
|
60f290a7eae8
The beginnings of a script to help finding / operators that may need
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
350 if last is None: |
|
60f290a7eae8
The beginnings of a script to help finding / operators that may need
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
351 last = first |
|
60f290a7eae8
The beginnings of a script to help finding / operators that may need
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
352 for i in range(first, last+1): |
|
60f290a7eae8
The beginnings of a script to help finding / operators that may need
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
353 try: |
|
60f290a7eae8
The beginnings of a script to help finding / operators that may need
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
354 line = self[first] |
|
60f290a7eae8
The beginnings of a script to help finding / operators that may need
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
355 except KeyError: |
|
60f290a7eae8
The beginnings of a script to help finding / operators that may need
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
356 line = "<missing line>" |
|
42427
46625e6122e9
Convert print statements to function calls in Tools/.
Collin Winter <collinw@gmail.com>
parents:
40587
diff
changeset
|
357 print(mark, chop(line)) |
|
19639
60f290a7eae8
The beginnings of a script to help finding / operators that may need
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
358 |
|
60f290a7eae8
The beginnings of a script to help finding / operators that may need
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
359 def scanline(g): |
|
60f290a7eae8
The beginnings of a script to help finding / operators that may need
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
360 slashes = [] |
|
60f290a7eae8
The beginnings of a script to help finding / operators that may need
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
361 startlineno = None |
|
60f290a7eae8
The beginnings of a script to help finding / operators that may need
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
362 endlineno = None |
|
60f290a7eae8
The beginnings of a script to help finding / operators that may need
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
363 for type, token, start, end, line in g: |
|
60f290a7eae8
The beginnings of a script to help finding / operators that may need
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
364 endlineno = end[0] |
|
60f290a7eae8
The beginnings of a script to help finding / operators that may need
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
365 if startlineno is None: |
|
60f290a7eae8
The beginnings of a script to help finding / operators that may need
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
366 startlineno = endlineno |
|
60f290a7eae8
The beginnings of a script to help finding / operators that may need
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
367 if token in ("/", "/="): |
|
60f290a7eae8
The beginnings of a script to help finding / operators that may need
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
368 slashes.append((start, line)) |
|
60f290a7eae8
The beginnings of a script to help finding / operators that may need
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
369 if type == tokenize.NEWLINE: |
|
60f290a7eae8
The beginnings of a script to help finding / operators that may need
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
370 break |
|
60f290a7eae8
The beginnings of a script to help finding / operators that may need
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
371 return startlineno, endlineno, slashes |
|
60f290a7eae8
The beginnings of a script to help finding / operators that may need
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
372 |
|
60f290a7eae8
The beginnings of a script to help finding / operators that may need
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
373 def chop(line): |
|
60f290a7eae8
The beginnings of a script to help finding / operators that may need
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
374 if line.endswith("\n"): |
|
60f290a7eae8
The beginnings of a script to help finding / operators that may need
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
375 return line[:-1] |
|
60f290a7eae8
The beginnings of a script to help finding / operators that may need
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
376 else: |
|
60f290a7eae8
The beginnings of a script to help finding / operators that may need
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
377 return line |
|
60f290a7eae8
The beginnings of a script to help finding / operators that may need
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
378 |
|
60f290a7eae8
The beginnings of a script to help finding / operators that may need
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
379 if __name__ == "__main__": |
|
19653
79e2327e51d6
Added more text to the docstring, updated the way the exit status is
Guido van Rossum <guido@python.org>
parents:
19639
diff
changeset
|
380 sys.exit(main()) |
