@@ -187,7 +187,7 @@ def add_map(ename, encmap, repl_char="?", sep="", ignore_case=None, no_error=Fal
187187 raise ValueError ("Bad input type parameter while creating encoding map" )
188188 if outype not in [None , "str" , "bin" , "ord" ]:
189189 raise ValueError ("Bad output type parameter while creating encoding map" )
190-
190+
191191 def __generic_code (decode = False ):
192192 def _wrapper (param ):
193193 """
@@ -238,7 +238,7 @@ def _wrapper(param):
238238 elif isinstance (mapdict , dict ) and isinstance (list (mapdict .values ())[0 ], dict ):
239239 tmp = None
240240 for r , d in mapdict .items ():
241- if r == '' : # this is already handled in case 1 ; anyway, an empty regex always match , hence
241+ if r == '' : # this is already handled in case 1 ; anyway, an empty regex always matches , hence
242242 continue # it must be excluded
243243 if re .match (r , p ):
244244 tmp = d
@@ -312,6 +312,7 @@ def code(text, errors="strict"):
312312 text = "" .join (str (ord (c )).zfill (3 ) for c in text )
313313 r = ""
314314 lsep = "" if decode else sep if len (sep ) <= 1 else sep [0 ]
315+ error_func = handle_error (ename , errors , lsep , repl_char , rminlen , decode )
315316
316317 # get the value from the mapping dictionary, trying the token with its inverted case if relevant
317318 def __get_value (token , position , case_changed = False ):
@@ -321,7 +322,7 @@ def __get_value(token, position, case_changed=False):
321322 if icase and not case_changed :
322323 token_inv_case = getattr (token , case )()
323324 return __get_value (token_inv_case , position , True )
324- return handle_error ( ename , errors , lsep , repl_char , rminlen , decode ) (token , position )
325+ return error_func (token , position )
325326 if isinstance (result , list ):
326327 result = random .choice (result )
327328 return result + lsep
@@ -349,7 +350,7 @@ def __get_value(token, position, case_changed=False):
349350 # if the number of bad chars is the minimum token length, consume it and start a new buffer
350351 if len (bad ) == tminlen or errors == "leave" :
351352 posn = cursor - len (bad )
352- r += handle_error ( ename , errors , lsep , repl_char , rminlen , decode ) (bad , posn )
353+ r += error_func (bad , posn )
353354 bad = ""
354355 if decode :
355356 if outype in ["bin" , "ord" ]:
@@ -524,13 +525,30 @@ def get_alphabet_from_mask(mask):
524525
525526# generic error handling function
526527def handle_error (ename , errors , sep = "" , repl_char = "?" , repl_minlen = 1 , decode = False , item = "position" ):
528+ """
529+ This shortcut function allows to handle error modes given some tuning parameters.
530+
531+ :param ename: encoding name
532+ :param errors: error handling mode
533+ :param sep: token separator
534+ :param repl_char: replacement character (for use when errors="replace")
535+ :param repl_minlen: repeat number for the replacement character
536+ :param decode: whether we are encoding or decoding
537+ :param item: position item description (for describing the error ; e.g. "group" or "token")
538+ """
527539 name = "" .join (t .capitalize () for t in re .split (r"[-_]" , ename ))
528540 # dynamically make dedicated exception classes bound to the related codec module
529541 exc = "%s%scodeError" % (name , ["En" , "De" ][decode ])
530542 glob = {'__name__' : "__main__" }
531543 exec ("class %s(ValueError): pass" % exc , glob )
532544
533545 def _handle_error (token , position ):
546+ """
547+ This handles an encoding/decoding error according to the selected handling mode.
548+
549+ :param token: input token to be encoded/decoded
550+ :param position: token position index
551+ """
534552 if errors == "strict" :
535553 msg = "'{}' codec can't {}code character '{}' in {} {}"
536554 raise glob [exc ](msg .format (ename , ["en" , "de" ][decode ], token , item , position ))
0 commit comments