summaryrefslogtreecommitdiff
path: root/tools
diff options
context:
space:
mode:
authorCharles Harris <charlesr.harris@gmail.com>2016-12-14 12:58:48 -0700
committerCharles Harris <charlesr.harris@gmail.com>2017-02-24 12:47:50 -0700
commit579718f1546df1e539cd2f7fbaf1617f06412eca (patch)
treedbb5857c760afe9e61390ccca9cf42587efe284e /tools
parent5f5ccecbfc116284ed8c8d53cd8b203ceef5f7c7 (diff)
downloadnumpy-579718f1546df1e539cd2f7fbaf1617f06412eca.tar.gz
ENH: Add tool to check for deprecated escaped characters.
Python 3.6 deprecates a number of escaped characters that were accepted before. For instance, '\(' was previously accepted but must now be written as '\\(' or r'\('. [ci skip]
Diffstat (limited to 'tools')
-rw-r--r--tools/find_deprecated_escaped_characters.py69
1 files changed, 69 insertions, 0 deletions
diff --git a/tools/find_deprecated_escaped_characters.py b/tools/find_deprecated_escaped_characters.py
new file mode 100644
index 000000000..8015d3a42
--- /dev/null
+++ b/tools/find_deprecated_escaped_characters.py
@@ -0,0 +1,69 @@
+#! /usr/bin/env python
+"""
+Look for escape sequences deprecated in Python 3.6.
+
+Python 3.6 deprecates a number of non-escape sequences starting with `\` that
+were accepted before. For instance, '\(' was previously accepted but must now
+be written as '\\(' or r'\('.
+
+"""
+from __future__ import division, absolute_import, print_function
+
+import sys
+
+def main(root):
+ """Find deprecated escape sequences.
+
+ Checks for deprecated escape sequences in ``*.py files``. If `root` is a
+ file, that file is checked, if `root` is a directory all ``*.py`` files
+ found in a recursive descent are checked.
+
+ If a deprecated escape sequence is found, the file and line where found is
+ printed. Note that for multiline strings the line where the string ends is
+ printed and the error(s) are somewhere in the body of the string.
+
+ Parameters
+ ----------
+ root : str
+ File or directory to check.
+ Returns
+ -------
+ None
+
+ """
+ count = 0
+
+ if sys.version_info[:2] >= (3, 6):
+ import ast
+ import tokenize
+ import warnings
+ from pathlib import Path
+
+ base = Path(root)
+ paths = base.rglob("*.py") if base.is_dir() else [base]
+ for path in paths:
+ # use tokenize to auto-detect encoding on systems where no
+ # default encoding is defined (e.g. LANG='C')
+ with tokenize.open(str(path)) as f:
+ with warnings.catch_warnings(record=True) as w:
+ warnings.simplefilter('always')
+ tree = ast.parse(f.read())
+ if w:
+ print("file: ", str(path))
+ for e in w:
+ print('line: ', e.lineno, ': ', e.message)
+ print()
+ count += len(w)
+ else:
+ raise RuntimeError("Python version must be >= 3.6")
+
+ print("Errors Found", count)
+
+
+if __name__ == "__main__":
+ from argparse import ArgumentParser
+
+ parser = ArgumentParser(description="Find deprecated escaped characters")
+ parser.add_argument('root', help='directory or file to be checked')
+ args = parser.parse_args()
+ main(args.root)