summaryrefslogtreecommitdiffstats
path: root/Tools/scripts/findnocoding.py
diff options
context:
space:
mode:
Diffstat (limited to 'Tools/scripts/findnocoding.py')
-rwxr-xr-xTools/scripts/findnocoding.py29
1 files changed, 15 insertions, 14 deletions
diff --git a/Tools/scripts/findnocoding.py b/Tools/scripts/findnocoding.py
index a494a48..5f3795e 100755
--- a/Tools/scripts/findnocoding.py
+++ b/Tools/scripts/findnocoding.py
@@ -2,7 +2,7 @@
"""List all those Python files that require a coding directive
-Usage: nocoding.py dir1 [dir2...]
+Usage: findnocoding.py dir1 [dir2...]
"""
__author__ = "Oleg Broytmann, Georg Brandl"
@@ -32,13 +32,14 @@ except ImportError:
"no sophisticated Python source file search will be done.", file=sys.stderr)
-decl_re = re.compile(r"coding[=:]\s*([-\w.]+)")
+decl_re = re.compile(rb'^[ \t\f]*#.*coding[:=][ \t]*([-\w.]+)')
+blank_re = re.compile(rb'^[ \t\f]*(?:[#\r\n]|$)')
def get_declaration(line):
- match = decl_re.search(line)
+ match = decl_re.match(line)
if match:
return match.group(1)
- return ''
+ return b''
def has_correct_encoding(text, codec):
try:
@@ -50,21 +51,21 @@ def has_correct_encoding(text, codec):
def needs_declaration(fullpath):
try:
- infile = open(fullpath, 'rU')
+ infile = open(fullpath, 'rb')
except IOError: # Oops, the file was removed - ignore it
return None
- line1 = infile.readline()
- line2 = infile.readline()
+ with infile:
+ line1 = infile.readline()
+ line2 = infile.readline()
- if get_declaration(line1) or get_declaration(line2):
- # the file does have an encoding declaration, so trust it
- infile.close()
- return False
+ if (get_declaration(line1) or
+ blank_re.match(line1) and get_declaration(line2)):
+ # the file does have an encoding declaration, so trust it
+ return False
- # check the whole file for non utf-8 characters
- rest = infile.read()
- infile.close()
+ # check the whole file for non utf-8 characters
+ rest = infile.read()
if has_correct_encoding(line1+line2+rest, "utf-8"):
return False