diff Tools/scripts/findnocoding.py @ 78375:67d36e8ddcfc

Fix findnocoding.p and pysource.py scripts I suppose that these scripts didn't work since Python 3.0.
author Victor Stinner <victor.stinner@gmail.com>
date Wed, 01 Aug 2012 20:12:51 +0200
parents 696cb524322a
children 8ace059cdffd
line wrap: on
line diff
--- a/Tools/scripts/findnocoding.py	Wed Aug 01 20:03:49 2012 +0200
+++ b/Tools/scripts/findnocoding.py	Wed Aug 01 20:12:51 2012 +0200
@@ -32,7 +32,7 @@
                          "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"coding[=:]\s*([-\w.]+)")
 
 def get_declaration(line):
     match = decl_re.search(line)
@@ -50,21 +50,21 @@
 
 def needs_declaration(fullpath):
     try:
-        infile = open(fullpath)
+        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 get_declaration(line2):
+            # the file does have an encoding declaration, so trust it
+            infile.close()
+            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