@@ -15,13 +15,13 @@ def fix_file(file_obj):
1515 return 0
1616 last_character = file_obj .read (1 )
1717 # last_character will be '' for an empty file
18- if last_character != b'\n ' and last_character != b'' :
18+ if last_character not in { b'\n ' , b' \r ' } and last_character != b'' :
1919 # Needs this seek for windows, otherwise IOError
2020 file_obj .seek (0 , os .SEEK_END )
2121 file_obj .write (b'\n ' )
2222 return 1
2323
24- while last_character == b'\n ' or last_character == b'\r ' :
24+ while last_character in { b'\n ' , b'\r ' } :
2525 # Deal with the beginning of the file
2626 if file_obj .tell () == 1 :
2727 # If we've reached the beginning of the file and it is all
@@ -38,8 +38,10 @@ def fix_file(file_obj):
3838 # newlines. If we find extraneous newlines, then backtrack and trim them.
3939 position = file_obj .tell ()
4040 remaining = file_obj .read ()
41- for sequence in [b'\n ' , b'\r \n ' ]:
42- if remaining .startswith (sequence ) and len (remaining ) > len (sequence ):
41+ for sequence in (b'\n ' , b'\r \n ' , b'\r ' ):
42+ if remaining == sequence :
43+ return 0
44+ elif remaining .startswith (sequence ):
4345 file_obj .seek (position + len (sequence ))
4446 file_obj .truncate ()
4547 return 1
0 commit comments