@@ -946,7 +946,7 @@ def format_file_in_place(
946946 with open (src , "rb" ) as buf :
947947 if mode .skip_source_first_line :
948948 header = buf .readline ()
949- src_contents , encoding , newline = decode_bytes (buf .read ())
949+ src_contents , encoding , newline = decode_bytes (buf .read (), mode )
950950 try :
951951 dst_contents = format_file_contents (
952952 src_contents , fast = fast , mode = mode , lines = lines
@@ -1008,7 +1008,9 @@ def format_stdin_to_stdout(
10081008 then = datetime .now (timezone .utc )
10091009
10101010 if content is None :
1011- src , encoding , newline = decode_bytes (sys .stdin .buffer .read ())
1011+ src , encoding , newline = decode_bytes (sys .stdin .buffer .read (), mode )
1012+ elif Preview .normalize_cr_newlines in mode :
1013+ src , encoding , newline = content , "utf-8" , "\n "
10121014 else :
10131015 src , encoding , newline = content , "utf-8" , ""
10141016
@@ -1026,8 +1028,12 @@ def format_stdin_to_stdout(
10261028 )
10271029 if write_back == WriteBack .YES :
10281030 # Make sure there's a newline after the content
1029- if dst and dst [- 1 ] != "\n " :
1030- dst += "\n "
1031+ if Preview .normalize_cr_newlines in mode :
1032+ if dst and dst [- 1 ] != "\n " and dst [- 1 ] != "\r " :
1033+ dst += newline
1034+ else :
1035+ if dst and dst [- 1 ] != "\n " :
1036+ dst += "\n "
10311037 f .write (dst )
10321038 elif write_back in (WriteBack .DIFF , WriteBack .COLOR_DIFF ):
10331039 now = datetime .now (timezone .utc )
@@ -1217,7 +1223,17 @@ def f(
12171223def _format_str_once (
12181224 src_contents : str , * , mode : Mode , lines : Collection [tuple [int , int ]] = ()
12191225) -> str :
1220- src_node = lib2to3_parse (src_contents .lstrip (), mode .target_versions )
1226+ if Preview .normalize_cr_newlines in mode :
1227+ normalized_contents , _ , newline_type = decode_bytes (
1228+ src_contents .encode ("utf-8" ), mode
1229+ )
1230+
1231+ src_node = lib2to3_parse (
1232+ normalized_contents .lstrip (), target_versions = mode .target_versions
1233+ )
1234+ else :
1235+ src_node = lib2to3_parse (src_contents .lstrip (), mode .target_versions )
1236+
12211237 dst_blocks : list [LinesBlock ] = []
12221238 if mode .target_versions :
12231239 versions = mode .target_versions
@@ -1262,16 +1278,25 @@ def _format_str_once(
12621278 for block in dst_blocks :
12631279 dst_contents .extend (block .all_lines ())
12641280 if not dst_contents :
1265- # Use decode_bytes to retrieve the correct source newline (CRLF or LF),
1266- # and check if normalized_content has more than one line
1267- normalized_content , _ , newline = decode_bytes (src_contents .encode ("utf-8" ))
1268- if "\n " in normalized_content :
1269- return newline
1281+ if Preview .normalize_cr_newlines in mode :
1282+ if "\n " in normalized_contents :
1283+ return newline_type
1284+ else :
1285+ # Use decode_bytes to retrieve the correct source newline (CRLF or LF),
1286+ # and check if normalized_content has more than one line
1287+ normalized_content , _ , newline = decode_bytes (
1288+ src_contents .encode ("utf-8" ), mode
1289+ )
1290+ if "\n " in normalized_content :
1291+ return newline
12701292 return ""
1271- return "" .join (dst_contents )
1293+ if Preview .normalize_cr_newlines in mode :
1294+ return "" .join (dst_contents ).replace ("\n " , newline_type )
1295+ else :
1296+ return "" .join (dst_contents )
12721297
12731298
1274- def decode_bytes (src : bytes ) -> tuple [FileContent , Encoding , NewLine ]:
1299+ def decode_bytes (src : bytes , mode : Mode ) -> tuple [FileContent , Encoding , NewLine ]:
12751300 """Return a tuple of (decoded_contents, encoding, newline).
12761301
12771302 `newline` is either CRLF or LF but `decoded_contents` is decoded with
@@ -1282,7 +1307,25 @@ def decode_bytes(src: bytes) -> tuple[FileContent, Encoding, NewLine]:
12821307 if not lines :
12831308 return "" , encoding , "\n "
12841309
1285- newline = "\r \n " if lines [0 ][- 2 :] == b"\r \n " else "\n "
1310+ if Preview .normalize_cr_newlines in mode :
1311+ if lines [0 ][- 2 :] == b"\r \n " :
1312+ if b"\r " in lines [0 ][:- 2 ]:
1313+ newline = "\r "
1314+ else :
1315+ newline = "\r \n "
1316+ elif lines [0 ][- 1 :] == b"\n " :
1317+ if b"\r " in lines [0 ][:- 1 ]:
1318+ newline = "\r "
1319+ else :
1320+ newline = "\n "
1321+ else :
1322+ if b"\r " in lines [0 ]:
1323+ newline = "\r "
1324+ else :
1325+ newline = "\n "
1326+ else :
1327+ newline = "\r \n " if lines [0 ][- 2 :] == b"\r \n " else "\n "
1328+
12861329 srcbuf .seek (0 )
12871330 with io .TextIOWrapper (srcbuf , encoding ) as tiow :
12881331 return tiow .read (), encoding , newline
0 commit comments