- Notifications
You must be signed in to change notification settings - Fork 31.8k
/
Copy pathfixdiv.py
executable file
·378 lines (334 loc) · 13.5 KB
/
fixdiv.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
#! /usr/bin/env python
"""fixdiv - tool to fix division operators.
To use this tool, first run `python -Qwarnall yourscript.py 2>warnings'.
This runs the script `yourscript.py' while writing warning messages
about all uses of the classic division operator to the file
`warnings'. The warnings look like this:
<file>:<line>: DeprecationWarning: classic <type> division
The warnings are written to stderr, so you must use `2>' for the I/O
redirect. I know of no way to redirect stderr on Windows in a DOS
box, so you will have to modify the script to set sys.stderr to some
kind of log file if you want to do this on Windows.
The warnings are not limited to the script; modules imported by the
script may also trigger warnings. In fact a useful technique is to
write a test script specifically intended to exercise all code in a
particular module or set of modules.
Then run `python fixdiv.py warnings'. This first reads the warnings,
looking for classic division warnings, and sorts them by file name and
line number. Then, for each file that received at least one warning,
it parses the file and tries to match the warnings up to the division
operators found in the source code. If it is successful, it writes
its findings to stdout, preceded by a line of dashes and a line of the
form:
Index: <file>
If the only findings found are suggestions to change a / operator into
a // operator, the output is acceptable input for the Unix 'patch'
program.
Here are the possible messages on stdout (N stands for a line number):
- A plain-diff-style change ('NcN', a line marked by '<', a line
containing '---', and a line marked by '>'):
A / operator was found that should be changed to //. This is the
recommendation when only int and/or long arguments were seen.
- 'True division / operator at line N' and a line marked by '=':
A / operator was found that can remain unchanged. This is the
recommendation when only float and/or complex arguments were seen.
- 'Ambiguous / operator (..., ...) at line N', line marked by '?':
A / operator was found for which int or long as well as float or
complex arguments were seen. This is highly unlikely; if it occurs,
you may have to restructure the code to keep the classic semantics,
or maybe you don't care about the classic semantics.
- 'No conclusive evidence on line N', line marked by '*':
A / operator was found for which no warnings were seen. This could
be code that was never executed, or code that was only executed
with user-defined objects as arguments. You will have to
investigate further. Note that // can be overloaded separately from
/, using __floordiv__. True division can also be separately
overloaded, using __truediv__. Classic division should be the same
as either of those. (XXX should I add a warning for division on
user-defined objects, to disambiguate this case from code that was
never executed?)
- 'Phantom ... warnings for line N', line marked by '*':
A warning was seen for a line not containing a / operator. The most
likely cause is a warning about code executed by 'exec' or eval()
(see note below), or an indirect invocation of the / operator, for
example via the div() function in the operator module. It could
also be caused by a change to the file between the time the test
script was run to collect warnings and the time fixdiv was run.
- 'More than one / operator in line N'; or
'More than one / operator per statement in lines N-N':
The scanner found more than one / operator on a single line, or in a
statement split across multiple lines. Because the warnings
framework doesn't (and can't) show the offset within the line, and
the code generator doesn't always give the correct line number for
operations in a multi-line statement, we can't be sure whether all
operators in the statement were executed. To be on the safe side,
by default a warning is issued about this case. In practice, these
cases are usually safe, and the -m option suppresses these warning.
- 'Can't find the / operator in line N', line marked by '*':
This really shouldn't happen. It means that the tokenize module
reported a '/' operator but the line it returns didn't contain a '/'
character at the indicated position.
- 'Bad warning for line N: XYZ', line marked by '*':
This really shouldn't happen. It means that a 'classic XYZ
division' warning was read with XYZ being something other than
'int', 'long', 'float', or 'complex'.
Notes:
- The augmented assignment operator /= is handled the same way as the
/ operator.
- This tool never looks at the // operator; no warnings are ever
generated for use of this operator.
- This tool never looks at the / operator when a future division
statement is in effect; no warnings are generated in this case, and
because the tool only looks at files for which at least one classic
division warning was seen, it will never look at files containing a
future division statement.
- Warnings may be issued for code not read from a file, but executed
using an exec statement or the eval() function. These may have
<string> in the filename position, in which case the fixdiv script
will attempt and fail to open a file named '<string>' and issue a
warning about this failure; or these may be reported as 'Phantom'
warnings (see above). You're on your own to deal with these. You
could make all recommended changes and add a future division
statement to all affected files, and then re-run the test script; it
should not issue any warnings. If there are any, and you have a
hard time tracking down where they are generated, you can use the
-Werror option to force an error instead of a first warning,
generating a traceback.
- The tool should be run from the same directory as that from which
the original script was run, otherwise it won't be able to open
files given by relative pathnames.
"""
importsys
importgetopt
importre
importtokenize
multi_ok=0
defmain():
try:
opts, args=getopt.getopt(sys.argv[1:], "hm")
exceptgetopt.error, msg:
usage(msg)
return2
foro, ainopts:
ifo=="-h":
print__doc__
return
ifo=="-m":
globalmulti_ok
multi_ok=1
ifnotargs:
usage("at least one file argument is required")
return2
ifargs[1:]:
sys.stderr.write("%s: extra file arguments ignored\n", sys.argv[0])
warnings=readwarnings(args[0])
ifwarningsisNone:
return1
files=warnings.keys()
ifnotfiles:
print"No classic division warnings read from", args[0]
return
files.sort()
exit=None
forfilenameinfiles:
x=process(filename, warnings[filename])
exit=exitorx
returnexit
defusage(msg):
sys.stderr.write("%s: %s\n"% (sys.argv[0], msg))
sys.stderr.write("Usage: %s [-m] warnings\n"%sys.argv[0])
sys.stderr.write("Try `%s -h' for more information.\n"%sys.argv[0])
PATTERN= ("^(.+?):(\d+): DeprecationWarning: "
"classic (int|long|float|complex) division$")
defreadwarnings(warningsfile):
prog=re.compile(PATTERN)
try:
f=open(warningsfile)
exceptIOError, msg:
sys.stderr.write("can't open: %s\n"%msg)
return
warnings= {}
while1:
line=f.readline()
ifnotline:
break
m=prog.match(line)
ifnotm:
ifline.find("division") >=0:
sys.stderr.write("Warning: ignored input "+line)
continue
filename, lineno, what=m.groups()
list=warnings.get(filename)
iflistisNone:
warnings[filename] =list= []
list.append((int(lineno), intern(what)))
f.close()
returnwarnings
defprocess(filename, list):
print"-"*70
assertlist# if this fails, readwarnings() is broken
try:
fp=open(filename)
exceptIOError, msg:
sys.stderr.write("can't open: %s\n"%msg)
return1
print"Index:", filename
f=FileContext(fp)
list.sort()
index=0# list[:index] has been processed, list[index:] is still to do
g=tokenize.generate_tokens(f.readline)
while1:
startlineno, endlineno, slashes=lineinfo=scanline(g)
ifstartlinenoisNone:
break
assertstartlineno<=endlinenoisnotNone
orphans= []
whileindex<len(list) andlist[index][0] <startlineno:
orphans.append(list[index])
index+=1
iforphans:
reportphantomwarnings(orphans, f)
warnings= []
whileindex<len(list) andlist[index][0] <=endlineno:
warnings.append(list[index])
index+=1
ifnotslashesandnotwarnings:
pass
elifslashesandnotwarnings:
report(slashes, "No conclusive evidence")
elifwarningsandnotslashes:
reportphantomwarnings(warnings, f)
else:
iflen(slashes) >1:
ifnotmulti_ok:
rows= []
lastrow=None
for (row, col), lineinslashes:
ifrow==lastrow:
continue
rows.append(row)
lastrow=row
assertrows
iflen(rows) ==1:
print"*** More than one / operator in line", rows[0]
else:
print"*** More than one / operator per statement",
print"in lines %d-%d"% (rows[0], rows[-1])
intlong= []
floatcomplex= []
bad= []
forlineno, whatinwarnings:
ifwhatin ("int", "long"):
intlong.append(what)
elifwhatin ("float", "complex"):
floatcomplex.append(what)
else:
bad.append(what)
lastrow=None
for (row, col), lineinslashes:
ifrow==lastrow:
continue
lastrow=row
line=chop(line)
ifline[col:col+1] !="/":
print"*** Can't find the / operator in line %d:"%row
print"*", line
continue
ifbad:
print"*** Bad warning for line %d:"%row, bad
print"*", line
elifintlongandnotfloatcomplex:
print"%dc%d"% (row, row)
print"<", line
print"---"
print">", line[:col] +"/"+line[col:]
eliffloatcomplexandnotintlong:
print"True division / operator at line %d:"%row
print"=", line
elifintlongandfloatcomplex:
print"*** Ambiguous / operator (%s, %s) at line %d:"% (
"|".join(intlong), "|".join(floatcomplex), row)
print"?", line
fp.close()
defreportphantomwarnings(warnings, f):
blocks= []
lastrow=None
lastblock=None
forrow, whatinwarnings:
ifrow!=lastrow:
lastblock= [row]
blocks.append(lastblock)
lastblock.append(what)
forblockinblocks:
row=block[0]
whats="/".join(block[1:])
print"*** Phantom %s warnings for line %d:"% (whats, row)
f.report(row, mark="*")
defreport(slashes, message):
lastrow=None
for (row, col), lineinslashes:
ifrow!=lastrow:
print"*** %s on line %d:"% (message, row)
print"*", chop(line)
lastrow=row
classFileContext:
def__init__(self, fp, window=5, lineno=1):
self.fp=fp
self.window=5
self.lineno=1
self.eoflookahead=0
self.lookahead= []
self.buffer= []
deffill(self):
whilelen(self.lookahead) <self.windowandnotself.eoflookahead:
line=self.fp.readline()
ifnotline:
self.eoflookahead=1
break
self.lookahead.append(line)
defreadline(self):
self.fill()
ifnotself.lookahead:
return""
line=self.lookahead.pop(0)
self.buffer.append(line)
self.lineno+=1
returnline
def__getitem__(self, index):
self.fill()
bufstart=self.lineno-len(self.buffer)
lookend=self.lineno+len(self.lookahead)
ifbufstart<=index<self.lineno:
returnself.buffer[index-bufstart]
ifself.lineno<=index<lookend:
returnself.lookahead[index-self.lineno]
raiseKeyError
defreport(self, first, last=None, mark="*"):
iflastisNone:
last=first
foriinrange(first, last+1):
try:
line=self[first]
exceptKeyError:
line="<missing line>"
printmark, chop(line)
defscanline(g):
slashes= []
startlineno=None
endlineno=None
fortype, token, start, end, lineing:
endlineno=end[0]
ifstartlinenoisNone:
startlineno=endlineno
iftokenin ("/", "/="):
slashes.append((start, line))
iftype==tokenize.NEWLINE:
break
returnstartlineno, endlineno, slashes
defchop(line):
ifline.endswith("\n"):
returnline[:-1]
else:
returnline
if__name__=="__main__":
sys.exit(main())