@@ -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
3737def get_declaration (line ):
3838 match = decl_re .search (line )
@@ -50,21 +50,21 @@ def has_correct_encoding(text, codec):
5050
5151def 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
0 commit comments