55from collections .abc import Sequence
66from typing import IO
77
8+ LF = b'\n '
9+ CR = b'\r '
10+ CRLF = b'\r \n '
11+
812
913def fix_file (file_obj : IO [bytes ]) -> int :
1014 # Test for newline at end of file
@@ -15,13 +19,13 @@ def fix_file(file_obj: IO[bytes]) -> int:
1519 return 0
1620 last_character = file_obj .read (1 )
1721 # last_character will be '' for an empty file
18- if last_character not in {b' \n ' , b' \r ' } and last_character != b'' :
22+ if last_character not in {LF , CR } and last_character != b'' :
1923 # Needs this seek for windows, otherwise IOError
2024 file_obj .seek (0 , os .SEEK_END )
21- file_obj .write (b' \n ' )
25+ file_obj .write (LF )
2226 return 1
2327
24- while last_character in {b' \n ' , b' \r ' }:
28+ while last_character in {LF , CR }:
2529 # Deal with the beginning of the file
2630 if file_obj .tell () == 1 :
2731 # If we've reached the beginning of the file and it is all
@@ -38,7 +42,7 @@ def fix_file(file_obj: IO[bytes]) -> int:
3842 # newlines. If we find extraneous newlines, then backtrack and trim them.
3943 position = file_obj .tell ()
4044 remaining = file_obj .read ()
41- for sequence in (b' \n ' , b' \r \n ' , b' \r ' ):
45+ for sequence in (LF , CRLF , CR ):
4246 if remaining == sequence :
4347 return 0
4448 elif remaining .startswith (sequence ):
0 commit comments