]> granicus.if.org Git - python/commitdiff
Fix findnocoding.p and pysource.py scripts
authorVictor Stinner <victor.stinner@gmail.com>
Wed, 1 Aug 2012 18:12:51 +0000 (20:12 +0200)
committerVictor Stinner <victor.stinner@gmail.com>
Wed, 1 Aug 2012 18:12:51 +0000 (20:12 +0200)
I suppose that these scripts didn't work since Python 3.0.

Tools/scripts/findnocoding.py
Tools/scripts/pysource.py

index 5aa1febade8dd8551fd2e32f388f04f746b9f0e9..d6643860a2497229000327ed9b5b02eb5324c20f 100755 (executable)
@@ -32,7 +32,7 @@ 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"coding[=:]\s*([-\w.]+)")
 
 def get_declaration(line):
     match = decl_re.search(line)
@@ -50,21 +50,21 @@ def has_correct_encoding(text, codec):
 
 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
index c7dbe605d49b2109498b4a37be10be43086c2f78..7348a68f21702b5c1b2ed44924d37741e42337dd 100755 (executable)
@@ -22,7 +22,7 @@ __all__ = ["has_python_ext", "looks_like_python", "can_be_compiled", "walk_pytho
 
 import os, re
 
-binary_re = re.compile('[\x00-\x08\x0E-\x1F\x7F]')
+binary_re = re.compile(br'[\x00-\x08\x0E-\x1F\x7F]')
 
 debug = False
 
@@ -42,7 +42,7 @@ def _open(fullpath):
         return None
 
     try:
-        return open(fullpath)
+        return open(fullpath, "rb")
     except IOError as err: # Access denied, or a special file - ignore it
         print_debug("%s: access denied: %s" % (fullpath, err))
         return None
@@ -65,7 +65,7 @@ def looks_like_python(fullpath):
 
     if fullpath.endswith(".py") or fullpath.endswith(".pyw"):
         return True
-    elif "python" in line:
+    elif b"python" in line:
         # disguised Python script (e.g. CGI)
         return True