Back to index

python3.2  3.2.2
Classes | Functions | Variables
fixdiv Namespace Reference

Classes

class  FileContext

Functions

def main
def usage
def readwarnings
def process
def reportphantomwarnings
def report
def scanline
def chop

Variables

int multi_ok = 0
tuple PATTERN

Function Documentation

def fixdiv.chop (   line)

Definition at line 373 of file fixdiv.py.

00373 
00374 def chop(line):
00375     if line.endswith("\n"):
00376         return line[:-1]
00377     else:
00378         return line

Here is the call graph for this function:

Here is the caller graph for this function:

def fixdiv.main ( void  )

Definition at line 140 of file fixdiv.py.

00140 
00141 def main():
00142     try:
00143         opts, args = getopt.getopt(sys.argv[1:], "hm")
00144     except getopt.error as msg:
00145         usage(msg)
00146         return 2
00147     for o, a in opts:
00148         if o == "-h":
00149             print(__doc__)
00150             return
00151         if o == "-m":
00152             global multi_ok
00153             multi_ok = 1
00154     if not args:
00155         usage("at least one file argument is required")
00156         return 2
00157     if args[1:]:
00158         sys.stderr.write("%s: extra file arguments ignored\n", sys.argv[0])
00159     warnings = readwarnings(args[0])
00160     if warnings is None:
00161         return 1
00162     files = list(warnings.keys())
00163     if not files:
00164         print("No classic division warnings read from", args[0])
00165         return
00166     files.sort()
00167     exit = None
00168     for filename in files:
00169         x = process(filename, warnings[filename])
00170         exit = exit or x
00171     return exit

Here is the call graph for this function:

Here is the caller graph for this function:

def fixdiv.process (   filename,
  list 
)

Definition at line 205 of file fixdiv.py.

00205 
00206 def process(filename, list):
00207     print("-"*70)
00208     assert list # if this fails, readwarnings() is broken
00209     try:
00210         fp = open(filename)
00211     except IOError as msg:
00212         sys.stderr.write("can't open: %s\n" % msg)
00213         return 1
00214     print("Index:", filename)
00215     f = FileContext(fp)
00216     list.sort()
00217     index = 0 # list[:index] has been processed, list[index:] is still to do
00218     g = tokenize.generate_tokens(f.readline)
00219     while 1:
00220         startlineno, endlineno, slashes = lineinfo = scanline(g)
00221         if startlineno is None:
00222             break
00223         assert startlineno <= endlineno is not None
00224         orphans = []
00225         while index < len(list) and list[index][0] < startlineno:
00226             orphans.append(list[index])
00227             index += 1
00228         if orphans:
00229             reportphantomwarnings(orphans, f)
00230         warnings = []
00231         while index < len(list) and list[index][0] <= endlineno:
00232             warnings.append(list[index])
00233             index += 1
00234         if not slashes and not warnings:
00235             pass
00236         elif slashes and not warnings:
00237             report(slashes, "No conclusive evidence")
00238         elif warnings and not slashes:
00239             reportphantomwarnings(warnings, f)
00240         else:
00241             if len(slashes) > 1:
00242                 if not multi_ok:
00243                     rows = []
00244                     lastrow = None
00245                     for (row, col), line in slashes:
00246                         if row == lastrow:
00247                             continue
00248                         rows.append(row)
00249                         lastrow = row
00250                     assert rows
00251                     if len(rows) == 1:
00252                         print("*** More than one / operator in line", rows[0])
00253                     else:
00254                         print("*** More than one / operator per statement", end=' ')
00255                         print("in lines %d-%d" % (rows[0], rows[-1]))
00256             intlong = []
00257             floatcomplex = []
00258             bad = []
00259             for lineno, what in warnings:
00260                 if what in ("int", "long"):
00261                     intlong.append(what)
00262                 elif what in ("float", "complex"):
00263                     floatcomplex.append(what)
00264                 else:
00265                     bad.append(what)
00266             lastrow = None
00267             for (row, col), line in slashes:
00268                 if row == lastrow:
00269                     continue
00270                 lastrow = row
00271                 line = chop(line)
00272                 if line[col:col+1] != "/":
00273                     print("*** Can't find the / operator in line %d:" % row)
00274                     print("*", line)
00275                     continue
00276                 if bad:
00277                     print("*** Bad warning for line %d:" % row, bad)
00278                     print("*", line)
00279                 elif intlong and not floatcomplex:
00280                     print("%dc%d" % (row, row))
00281                     print("<", line)
00282                     print("---")
00283                     print(">", line[:col] + "/" + line[col:])
00284                 elif floatcomplex and not intlong:
00285                     print("True division / operator at line %d:" % row)
00286                     print("=", line)
00287                 elif intlong and floatcomplex:
00288                     print("*** Ambiguous / operator (%s, %s) at line %d:" % (
00289                         "|".join(intlong), "|".join(floatcomplex), row))
00290                     print("?", line)
00291     fp.close()

Here is the call graph for this function:

Here is the caller graph for this function:

def fixdiv.readwarnings (   warningsfile)

Definition at line 180 of file fixdiv.py.

00180 
00181 def readwarnings(warningsfile):
00182     prog = re.compile(PATTERN)
00183     try:
00184         f = open(warningsfile)
00185     except IOError as msg:
00186         sys.stderr.write("can't open: %s\n" % msg)
00187         return
00188     warnings = {}
00189     while 1:
00190         line = f.readline()
00191         if not line:
00192             break
00193         m = prog.match(line)
00194         if not m:
00195             if line.find("division") >= 0:
00196                 sys.stderr.write("Warning: ignored input " + line)
00197             continue
00198         filename, lineno, what = m.groups()
00199         list = warnings.get(filename)
00200         if list is None:
00201             warnings[filename] = list = []
00202         list.append((int(lineno), sys.intern(what)))
00203     f.close()
00204     return warnings

Here is the call graph for this function:

Here is the caller graph for this function:

def fixdiv.report (   slashes,
  message 
)

Definition at line 307 of file fixdiv.py.

00307 
00308 def report(slashes, message):
00309     lastrow = None
00310     for (row, col), line in slashes:
00311         if row != lastrow:
00312             print("*** %s on line %d:" % (message, row))
00313             print("*", chop(line))
00314             lastrow = row

Here is the call graph for this function:

Here is the caller graph for this function:

def fixdiv.reportphantomwarnings (   warnings,
  f 
)

Definition at line 292 of file fixdiv.py.

00292 
00293 def reportphantomwarnings(warnings, f):
00294     blocks = []
00295     lastrow = None
00296     lastblock = None
00297     for row, what in warnings:
00298         if row != lastrow:
00299             lastblock = [row]
00300             blocks.append(lastblock)
00301         lastblock.append(what)
00302     for block in blocks:
00303         row = block[0]
00304         whats = "/".join(block[1:])
00305         print("*** Phantom %s warnings for line %d:" % (whats, row))
00306         f.report(row, mark="*")

Here is the caller graph for this function:

def fixdiv.scanline (   g)

Definition at line 359 of file fixdiv.py.

00359 
00360 def scanline(g):
00361     slashes = []
00362     startlineno = None
00363     endlineno = None
00364     for type, token, start, end, line in g:
00365         endlineno = end[0]
00366         if startlineno is None:
00367             startlineno = endlineno
00368         if token in ("/", "/="):
00369             slashes.append((start, line))
00370         if type == tokenize.NEWLINE:
00371             break
00372     return startlineno, endlineno, slashes

Here is the caller graph for this function:

def fixdiv.usage (   msg)

Definition at line 172 of file fixdiv.py.

00172 
00173 def usage(msg):
00174     sys.stderr.write("%s: %s\n" % (sys.argv[0], msg))
00175     sys.stderr.write("Usage: %s [-m] warnings\n" % sys.argv[0])
00176     sys.stderr.write("Try `%s -h' for more information.\n" % sys.argv[0])

Here is the caller graph for this function:


Variable Documentation

Definition at line 138 of file fixdiv.py.

Initial value:
00001 ("^(.+?):(\d+): DeprecationWarning: "
00002            "classic (int|long|float|complex) division$")

Definition at line 177 of file fixdiv.py.