"Fossies" - the Fresh Open Source Software Archive

Member "numpy-1.16.4/tools/find_deprecated_escaped_characters.py" (22 Feb 2019, 2091 Bytes) of package /linux/misc/numpy-1.16.4.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Python source code syntax highlighting (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file. For more information about "find_deprecated_escaped_characters.py" see the Fossies "Dox" file reference documentation.

    1 #! /usr/bin/env python
    2 r"""
    3 Look for escape sequences deprecated in Python 3.6.
    4 
    5 Python 3.6 deprecates a number of non-escape sequences starting with '\' that
    6 were accepted before. For instance, '\(' was previously accepted but must now
    7 be written as '\\(' or r'\('.
    8 
    9 """
   10 from __future__ import division, absolute_import, print_function
   11 
   12 import sys
   13 
   14 def main(root):
   15     """Find deprecated escape sequences.
   16 
   17     Checks for deprecated escape sequences in ``*.py files``. If `root` is a
   18     file, that file is checked, if `root` is a directory all ``*.py`` files
   19     found in a recursive descent are checked.
   20 
   21     If a deprecated escape sequence is found, the file and line where found is
   22     printed. Note that for multiline strings the line where the string ends is
   23     printed and the error(s) are somewhere in the body of the string.
   24 
   25     Parameters
   26     ----------
   27     root : str
   28         File or directory to check.
   29     Returns
   30     -------
   31     None
   32 
   33     """
   34     import ast
   35     import tokenize
   36     import warnings
   37     from pathlib import Path
   38 
   39     count = 0
   40     base = Path(root)
   41     paths = base.rglob("*.py") if base.is_dir() else [base]
   42     for path in paths:
   43         # use tokenize to auto-detect encoding on systems where no
   44         # default encoding is defined (e.g. LANG='C')
   45         with tokenize.open(str(path)) as f:
   46             with warnings.catch_warnings(record=True) as w:
   47                 warnings.simplefilter('always')
   48                 tree = ast.parse(f.read())
   49             if w:
   50                 print("file: ", str(path))
   51                 for e in w:
   52                     print('line: ', e.lineno, ': ', e.message)
   53                 print()
   54                 count += len(w)
   55     print("Errors Found", count)
   56 
   57 
   58 if __name__ == "__main__":
   59     from argparse import ArgumentParser
   60 
   61     if sys.version_info[:2] < (3, 6):
   62         raise RuntimeError("Python version must be >= 3.6")
   63 
   64     parser = ArgumentParser(description="Find deprecated escaped characters")
   65     parser.add_argument('root', help='directory or file to be checked')
   66     args = parser.parse_args()
   67     main(args.root)