🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
### 导航 - [索引](../genindex.xhtml "总目录") - [模块](../py-modindex.xhtml "Python 模块索引") | - [下一页](abstract.xhtml "抽象对象层") | - [上一页](reflection.xhtml "反射") | - ![](https://box.kancloud.cn/a721fc7ec672275e257bbbfde49a4d4e_16x16.png) - [Python](https://www.python.org/) » - zh\_CN 3.7.3 [文档](../index.xhtml) » - [Python/C API 参考手册](index.xhtml) » - [工具](utilities.xhtml) » - $('.inline-search').show(0); | # 编解码器注册与支持功能 int `PyCodec_Register`([PyObject](structures.xhtml#c.PyObject "PyObject") *\*search\_function*)注册一个新的编解码器搜索函数。 作为副作用,其尝试加载 `encodings` 包,如果尚未完成,请确保它始终位于搜索函数列表的第一位。 int `PyCodec_KnownEncoding`(const char *\*encoding*)根据注册的给定 *encoding* 的编解码器是否已存在而返回 `1` 或 `0`。此函数总能成功。 [PyObject](structures.xhtml#c.PyObject "PyObject")\* `PyCodec_Encode`([PyObject](structures.xhtml#c.PyObject "PyObject") *\*object*, const char *\*encoding*, const char *\*errors*)*Return value: New reference.*泛型编解码器基本编码 API。 *object* 使用 *errors* 定义的错误处理方法传递给定 *encoding* 的编码器函数。 *errors* 可能是 *NULL* 以使用为编解码器定义的默认方法。如果找不到编码器,则抛出 [`LookupError`](../library/exceptions.xhtml#LookupError "LookupError")。 [PyObject](structures.xhtml#c.PyObject "PyObject")\* `PyCodec_Decode`([PyObject](structures.xhtml#c.PyObject "PyObject") *\*object*, const char *\*encoding*, const char *\*errors*)*Return value: New reference.*泛型编解码器基本解码 API。 *object* 使用 *errors* 定义的错误处理方法传递给定 *encoding* 的解码器函数。 *errors* 可能是 *NULL* 以使用为编解码器定义的默认方法。如果找不到编码器,则抛出 [`LookupError`](../library/exceptions.xhtml#LookupError "LookupError")。 ## Codec lookup API In the following functions, the *encoding* string is looked up converted to all lower-case characters, which makes encodings looked up through this mechanism effectively case-insensitive. If no codec is found, a [`KeyError`](../library/exceptions.xhtml#KeyError "KeyError") is set and *NULL* returned. [PyObject](structures.xhtml#c.PyObject "PyObject")\* `PyCodec_Encoder`(const char *\*encoding*)*Return value: New reference.*Get an encoder function for the given *encoding*. [PyObject](structures.xhtml#c.PyObject "PyObject")\* `PyCodec_Decoder`(const char *\*encoding*)*Return value: New reference.*Get a decoder function for the given *encoding*. [PyObject](structures.xhtml#c.PyObject "PyObject")\* `PyCodec_IncrementalEncoder`(const char *\*encoding*, const char *\*errors*)*Return value: New reference.*Get an [`IncrementalEncoder`](../library/codecs.xhtml#codecs.IncrementalEncoder "codecs.IncrementalEncoder") object for the given *encoding*. [PyObject](structures.xhtml#c.PyObject "PyObject")\* `PyCodec_IncrementalDecoder`(const char *\*encoding*, const char *\*errors*)*Return value: New reference.*Get an [`IncrementalDecoder`](../library/codecs.xhtml#codecs.IncrementalDecoder "codecs.IncrementalDecoder") object for the given *encoding*. [PyObject](structures.xhtml#c.PyObject "PyObject")\* `PyCodec_StreamReader`(const char *\*encoding*, [PyObject](structures.xhtml#c.PyObject "PyObject") *\*stream*, const char *\*errors*)*Return value: New reference.*Get a [`StreamReader`](../library/codecs.xhtml#codecs.StreamReader "codecs.StreamReader") factory function for the given *encoding*. [PyObject](structures.xhtml#c.PyObject "PyObject")\* `PyCodec_StreamWriter`(const char *\*encoding*, [PyObject](structures.xhtml#c.PyObject "PyObject") *\*stream*, const char *\*errors*)*Return value: New reference.*Get a [`StreamWriter`](../library/codecs.xhtml#codecs.StreamWriter "codecs.StreamWriter") factory function for the given *encoding*. ## Registry API for Unicode encoding error handlers int `PyCodec_RegisterError`(const char *\*name*, [PyObject](structures.xhtml#c.PyObject "PyObject") *\*error*)Register the error handling callback function *error* under the given *name*. This callback function will be called by a codec when it encounters unencodable characters/undecodable bytes and *name* is specified as the error parameter in the call to the encode/decode function. The callback gets a single argument, an instance of [`UnicodeEncodeError`](../library/exceptions.xhtml#UnicodeEncodeError "UnicodeEncodeError"), [`UnicodeDecodeError`](../library/exceptions.xhtml#UnicodeDecodeError "UnicodeDecodeError") or [`UnicodeTranslateError`](../library/exceptions.xhtml#UnicodeTranslateError "UnicodeTranslateError") that holds information about the problematic sequence of characters or bytes and their offset in the original string (see [Unicode Exception Objects](exceptions.xhtml#unicodeexceptions) for functions to extract this information). The callback must either raise the given exception, or return a two-item tuple containing the replacement for the problematic sequence, and an integer giving the offset in the original string at which encoding/decoding should be resumed. Return `0` on success, `-1` on error. [PyObject](structures.xhtml#c.PyObject "PyObject")\* `PyCodec_LookupError`(const char *\*name*)*Return value: New reference.*Lookup the error handling callback function registered under *name*. As a special case *NULL* can be passed, in which case the error handling callback for "strict" will be returned. [PyObject](structures.xhtml#c.PyObject "PyObject")\* `PyCodec_StrictErrors`([PyObject](structures.xhtml#c.PyObject "PyObject") *\*exc*)*Return value: Always NULL.*Raise *exc* as an exception. [PyObject](structures.xhtml#c.PyObject "PyObject")\* `PyCodec_IgnoreErrors`([PyObject](structures.xhtml#c.PyObject "PyObject") *\*exc*)*Return value: New reference.*Ignore the unicode error, skipping the faulty input. [PyObject](structures.xhtml#c.PyObject "PyObject")\* `PyCodec_ReplaceErrors`([PyObject](structures.xhtml#c.PyObject "PyObject") *\*exc*)*Return value: New reference.*Replace the unicode encode error with `?` or `U+FFFD`. [PyObject](structures.xhtml#c.PyObject "PyObject")\* `PyCodec_XMLCharRefReplaceErrors`([PyObject](structures.xhtml#c.PyObject "PyObject") *\*exc*)*Return value: New reference.*Replace the unicode encode error with XML character references. [PyObject](structures.xhtml#c.PyObject "PyObject")\* `PyCodec_BackslashReplaceErrors`([PyObject](structures.xhtml#c.PyObject "PyObject") *\*exc*)*Return value: New reference.*Replace the unicode encode error with backslash escapes (`\x`, `\u` and `\U`). [PyObject](structures.xhtml#c.PyObject "PyObject")\* `PyCodec_NameReplaceErrors`([PyObject](structures.xhtml#c.PyObject "PyObject") *\*exc*)*Return value: New reference.*Replace the unicode encode error with `\N{...}` escapes. 3\.5 新版功能. ### 导航 - [索引](../genindex.xhtml "总目录") - [模块](../py-modindex.xhtml "Python 模块索引") | - [下一页](abstract.xhtml "抽象对象层") | - [上一页](reflection.xhtml "反射") | - ![](https://box.kancloud.cn/a721fc7ec672275e257bbbfde49a4d4e_16x16.png) - [Python](https://www.python.org/) » - zh\_CN 3.7.3 [文档](../index.xhtml) » - [Python/C API 参考手册](index.xhtml) » - [工具](utilities.xhtml) » - $('.inline-search').show(0); | © [版权所有](../copyright.xhtml) 2001-2019, Python Software Foundation. Python 软件基金会是一个非盈利组织。 [请捐助。](https://www.python.org/psf/donations/) 最后更新于 5月 21, 2019. [发现了问题](../bugs.xhtml)? 使用[Sphinx](http://sphinx.pocoo.org/)1.8.4 创建。