Skip to content

Commit fccab36

Browse files
authored
"fix python encode error" (#7613)
1 parent 47753a9 commit fccab36

File tree

1 file changed

+13
-3
lines changed

1 file changed

+13
-3
lines changed

.copyright.hook

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,10 +77,13 @@ def lang_type(filename):
7777
elif filename.endswith(".proto"):
7878
return "C"
7979
else:
80-
print("Unsupported filetype")
80+
print("Unsupported filetype %s", filename)
8181
exit(0)
8282

8383

84+
PYTHON_ENCODE = re.compile("^[ \t\v]*#.*?coding[:=][ \t]*([-_.a-zA-Z0-9]+)")
85+
86+
8487
def main(argv=None):
8588
parser = argparse.ArgumentParser(
8689
description='Checker for copyright declaration.')
@@ -89,8 +92,15 @@ def main(argv=None):
8992

9093
retv = 0
9194
for filename in args.filenames:
92-
first_line = io.open(filename).readline()
93-
if "COPYRIGHT" in first_line.upper() : continue
95+
fd = io.open(filename)
96+
first_line = fd.readline()
97+
if "COPYRIGHT" in first_line.upper(): continue
98+
if filename.endswith(".py"):
99+
second_line = fd.readline()
100+
if first_line.startswith("#!") or PYTHON_ENCODE.match(
101+
second_line) != None or PYTHON_ENCODE.match(
102+
first_line) != None:
103+
continue
94104
original_contents = io.open(filename).read()
95105
new_contents = generate_copyright(
96106
COPYRIGHT, lang_type(filename)) + original_contents

0 commit comments

Comments
 (0)