Skip to content

Commit 98516a6

Browse files
committed
Fix findnocoding.p and pysource.py scripts
I suppose that these scripts didn't work since Python 3.0.
1 parent 7c7ea62 commit 98516a6

2 files changed

Lines changed: 14 additions & 14 deletions

File tree

Tools/scripts/findnocoding.py

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ def walk_python_files(self, paths, *args, **kwargs):
3232
"no sophisticated Python source file search will be done.", file=sys.stderr)
3333

3434

35-
decl_re = re.compile(r"coding[=:]\s*([-\w.]+)")
35+
decl_re = re.compile(rb"coding[=:]\s*([-\w.]+)")
3636

3737
def get_declaration(line):
3838
match = decl_re.search(line)
@@ -50,21 +50,21 @@ def has_correct_encoding(text, codec):
5050

5151
def needs_declaration(fullpath):
5252
try:
53-
infile = open(fullpath)
53+
infile = open(fullpath, 'rb')
5454
except IOError: # Oops, the file was removed - ignore it
5555
return None
5656

57-
line1 = infile.readline()
58-
line2 = infile.readline()
57+
with infile:
58+
line1 = infile.readline()
59+
line2 = infile.readline()
5960

60-
if get_declaration(line1) or get_declaration(line2):
61-
# the file does have an encoding declaration, so trust it
62-
infile.close()
63-
return False
61+
if get_declaration(line1) or get_declaration(line2):
62+
# the file does have an encoding declaration, so trust it
63+
infile.close()
64+
return False
6465

65-
# check the whole file for non utf-8 characters
66-
rest = infile.read()
67-
infile.close()
66+
# check the whole file for non utf-8 characters
67+
rest = infile.read()
6868

6969
if has_correct_encoding(line1+line2+rest, "utf-8"):
7070
return False

Tools/scripts/pysource.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222

2323
import os, re
2424

25-
binary_re = re.compile('[\x00-\x08\x0E-\x1F\x7F]')
25+
binary_re = re.compile(br'[\x00-\x08\x0E-\x1F\x7F]')
2626

2727
debug = False
2828

@@ -42,7 +42,7 @@ def _open(fullpath):
4242
return None
4343

4444
try:
45-
return open(fullpath)
45+
return open(fullpath, "rb")
4646
except IOError as err: # Access denied, or a special file - ignore it
4747
print_debug("%s: access denied: %s" % (fullpath, err))
4848
return None
@@ -65,7 +65,7 @@ def looks_like_python(fullpath):
6565

6666
if fullpath.endswith(".py") or fullpath.endswith(".pyw"):
6767
return True
68-
elif "python" in line:
68+
elif b"python" in line:
6969
# disguised Python script (e.g. CGI)
7070
return True
7171

0 commit comments

Comments
 (0)