@@ -431,12 +431,11 @@ def substitute_one_repo(
431431 with open (input_path , "rb" ) as f_input :
432432 lines = f_input .readlines ()
433433
434- # 文件已关闭,现在处理数据
435- for line in lines :
436- try :
437- # 尝试解码行以检查内容
434+ # 文件已关闭,现在处理数据
435+ for line in lines :
436+ # 使用 errors='replace' 确保解码不会失败
438437 decoded = line .decode ("utf-8" , errors = "replace" )
439-
438+
440439 # 只处理以 b' 或 b" 开头的行
441440 if decoded .startswith (("b'" , 'b"' )):
442441 if decoded .startswith ("b'" ) and decoded .endswith ("'\n " ):
@@ -445,11 +444,11 @@ def substitute_one_repo(
445444 byte_data = line [2 :- 2 ]
446445 else :
447446 continue
448-
447+
449448 if 0 < len (byte_data ) <= max_len :
450449 valid_inputs .append (byte_data )
451- except UnicodeDecodeError :
452- if 0 < len (line ) <= max_len :
450+ # 对于其他行,如果长度在范围内且不是以 b' 或 b" 开头,也考虑加入
451+ elif 0 < len (line ) <= max_len :
453452 valid_inputs .append (line )
454453
455454 if not valid_inputs :
@@ -492,12 +491,19 @@ def substitute_one_repo(
492491 with open (out_path , "w" ) as f_out :
493492 f_out .write (new_code )
494493
495- # 格式化代码
496- try :
497- subprocess .run (["black" , out_path ], check = False )
498- except FileNotFoundError :
499- logging .warning ("Black formatter not found, skipping formatting" )
500-
494+ # 格式化代码
495+ formatter_installed = True
496+ try :
497+ subprocess .run (["black" , out_path ],
498+ check = False ,
499+ stdout = subprocess .DEVNULL , # 隐藏输出
500+ stderr = subprocess .DEVNULL ) # 隐藏错误
501+ except FileNotFoundError :
502+ if formatter_installed : # 避免多次记录
503+ logging .warning ("Black code formatter not found. For better formatting, install with:" )
504+ logging .warning ("pip install black" )
505+ formatter_installed = False
506+
501507 except SyntaxError as e :
502508 logging .error (f"Syntax error when processing { source_file } : { e } " )
503509 except Exception as e :
0 commit comments