66import os
77import random
88import re
9- import sre_parse
109import sys
1110from encodings .aliases import aliases as ALIASES
1211from functools import reduce , update_wrapper , wraps
3736 from importlib import reload
3837except ImportError :
3938 pass
39+ try :
40+ import re ._parser as sre_parse
41+ except ImportError :
42+ import sre_parse
4043
41- # from Python 3.11, it seems that 'sre_parse' is not bound to 're' anymore
44+ # from Python 3.11, 'sre_parse' is bound as '_parser' ; monkey-patch it for backward-compatibility
4245re .sre_parse = sre_parse
4346
4447
@@ -870,10 +873,9 @@ def _handle_error(token, position, output="", eename=None):
870873 :param output: output, as decoded up to the position of the error
871874 """
872875 if errors == "strict" :
873- msg = "'%s' codec can't %scode %s '%s' in %s %d"
874- token = ensure_str (token )
875- token = token [:7 ] + "..." if len (token ) > 10 else token
876- err = getattr (builtins , exc )(msg % (eename or ename , ["en" , "de" ][decode ], kind , token , item , position ))
876+ token = f"{ token [:7 ]} ..." if len (token := ensure_str (token )) > 10 else token
877+ err = getattr (builtins , exc )(f"'{ eename or ename } ' codec can't { ['en' ,'de' ][decode ]} code { kind } '{ token } ' "
878+ f"in { item } { position } " )
877879 err .output = output
878880 err .__cause__ = err
879881 raise err
@@ -1264,8 +1266,8 @@ def __guess(prev_input, input, stop_func, depth, max_depth, min_depth, encodings
12641266 if not stop and (show or debug ) and found not in result :
12651267 s = repr (input )
12661268 s = s [2 :- 1 ] if s .startswith ("b'" ) and s .endswith ("'" ) else s
1267- s = "[+] {', '.join(found)}: {s}"
1268- print (s if len (s ) <= 80 else s [:77 ] + " ..." )
1269+ s = f "[+] { ', ' .join (found )} : { s } "
1270+ print (s if len (s ) <= 80 else f" { s [:77 ]} ..." )
12691271 result [found ] = input
12701272 if depth >= max_depth or len (result ) > 0 and stop :
12711273 return
@@ -1275,7 +1277,7 @@ def __guess(prev_input, input, stop_func, depth, max_depth, min_depth, encodings
12751277 if len (result ) > 0 and stop :
12761278 return
12771279 if debug :
1278- print (f"[*] Depth % 0{ len (str (max_depth ))} d/%d : { encoding } " % ( depth + 1 , max_depth ) )
1280+ print (f"[*] Depth { depth + 1 : 0{len (str (max_depth ))}} / { max_depth } : { encoding } " )
12791281 __guess (input , new_input , stop_func , depth + 1 , max_depth , min_depth , encodings , result , found + (encoding , ),
12801282 stop , show , scoring_heuristic , extended , debug )
12811283
0 commit comments