Python codecs Library Error Handler register_error and lookup_error functions














































Python codecs Library Error Handler register_error and lookup_error functions



Python codecs Library: Error Handler Functions

The purpose of codecs Library is Encoding and decoding i.e. conversion of the texts between different representations.
This module defines base classes for standard Python codecs (encoders and decoders) and provides access to the internal Python codec registry, which manages the codec and error handling lookup process.

Some of the functions in codecs module related to error handling in encoding and decoding are:

1. codecs.register_error(name, error_handler):

This function is used to register a custom error handling function with the name 'name'. The error handler argument will be called during encoding and decoding in case an error occurs, when the name is specified as the errors parameter.
For encoding, error handler will be called if the error argument given to the encoding function is the name of the custom error handler with which it has been registered. The error handler will be called with a UnicodeEncodeError instance. This instance contains information about the location of the error. Care must be taken while making an error handling function 'error-handler' that it must raise this instance, or any different exception, o return a tuple with the replacement for the unencodable part of the input and the position where encoding should continue and this replacement may be either string or bytes. If the replacement is bytes, the encoder will simply copy them to the output buffer, otherwise if the replacement is a string, the encoder will encode the replacement. Talking about the positive positions are properly understood that it is relative to the starting of the string and negative positions are relative to the end of the string. If there is an out of bound index position, the IndexError will be raised.
Decoding and translating also works similarly just in those cases the UnicodeDecodeError and UnicodeTranslateError is raised.

    

2. codecs.lookup_error(name):

Another function for lookup of already registered error handlers.
When passed an error handler name, this function returns the error handler registered under the specified name.
It returns a LookupError if the handler under the specified name is not found.

    


More Articles of Arkaja Sharan:

Name Views Likes
Python codecs Library Error Handling schemes module functions 96 0
Python codecs Library Error Handler register_error and lookup_error functions 92 0
Python codecs Library Error Handlers 96 0
Python codecs Library open and EncodedFile functions 82 0
Python codecs Library iterencode and iterdecode functions 106 0
Python codecs Library register and unregister functions 79 0
Python codecs Library getreader and getwriter functions 91 0
Python codecs Library getincrementalencoder and getincrementaldecoder 75 0
Python codecs Library getencoder and getdecoder functions 84 0
Python Introduction to codecs Library 110 0
Python fcntl Library flock and lockf functions 95 0
Python fcntl Library fcntl and ioctl functions 112 0
Python Resource Library resource usage functions 103 0
Python Resource Library resource usage symbolic constants 83 0
Python Resource Library Resource Limit Functions 98 0
Python resource library resource limit symbolic constants 97 0
Python Introduction to Resource Library 81 0
Python stringprep Library in_table_d1 and in_table_d2 functions 88 0
Python stringprep Library in_table_c8 and in_table_c9 functions 92 0
Python stringprep Library in_table_c5 in_table_c6 and in_table_c7 functions 81 0
Python stringprep Library in_table_c3 and in_table_c4 functions 88 0
Python stringprep library in_table_c21 in_table_c22 and in_table_c21_c22 89 0
Python stringprep library functions in_table_c11 in_table_c12 and in_table_c11_c12 89 0
Python Introduction to stringprep Library 93 0
Python unicodedata library is_normalized unidata_version and ucd_3_2_0 85 0
Python Unicodedata Library functions normalize and decomposition 148 0
Python Unicodedata Library functions east_asian_width and mirrored 94 1
Python Unicodedata Library category bidirectional and combining functions 131 0
Introduction to Unicodedata library lookup and name functions 87 0
Unicode Library decimal digit and numeric functions 91 0
Introduction to Unicode Data library 0 0

Comments