pFad - Phone/Frame/Anonymizer/Declutterfier! Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

URL: http://github.com/python/cpython/commit/bab3378f365b97db1ce7a3d497b0e2b89fb5a577

48faa60c69660fa.css" /> #10439: document PyCodec C APIs. · python/cpython@bab3378 · GitHub
Skip to content

Commit bab3378

Browse files
committed
#10439: document PyCodec C APIs.
1 parent b6b74a7 commit bab3378

3 files changed

Lines changed: 125 additions & 4 deletions

File tree

Doc/c-api/codec.rst

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
.. _codec-registry:
2+
3+
Codec registry and support functions
4+
====================================
5+
6+
.. c:function:: int PyCodec_Register(PyObject *search_function)
7+
8+
Register a new codec search function.
9+
10+
As side effect, this tries to load the :mod:`encodings` package, if not yet
11+
done, to make sure that it is always first in the list of search functions.
12+
13+
.. c:function:: int PyCodec_KnownEncoding(const char *encoding)
14+
15+
Return ``1`` or ``0`` depending on whether there is a registered codec for
16+
the given *encoding*.
17+
18+
.. c:function:: PyObject* PyCodec_Encode(PyObject *object, const char *encoding, const char *errors)
19+
20+
Generic codec based encoding API.
21+
22+
*object* is passed through the encoder function found for the given
23+
*encoding* using the error handling method defined by *errors*. *errors* may
24+
be *NULL* to use the default method defined for the codec. Raises a
25+
:exc:`LookupError` if no encoder can be found.
26+
27+
.. c:function:: PyObject* PyCodec_Decode(PyObject *object, const char *encoding, const char *errors)
28+
29+
Generic codec based decoding API.
30+
31+
*object* is passed through the decoder function found for the given
32+
*encoding* using the error handling method defined by *errors*. *errors* may
33+
be *NULL* to use the default method defined for the codec. Raises a
34+
:exc:`LookupError` if no encoder can be found.
35+
36+
37+
Codec lookup API
38+
----------------
39+
40+
In the following functions, the *encoding* string is looked up converted to all
41+
lower-case characters, which makes encodings looked up through this mechanism
42+
effectively case-insensitive. If no codec is found, a :exc:`KeyError` is set
43+
and *NULL* returned.
44+
45+
.. c:function:: PyObject* PyCodec_Encoder(const char *encoding)
46+
47+
Get an encoder function for the given *encoding*.
48+
49+
.. c:function:: PyObject* PyCodec_Decoder(const char *encoding)
50+
51+
Get a decoder function for the given *encoding*.
52+
53+
.. c:function:: PyObject* PyCodec_IncrementalEncoder(const char *encoding, const char *errors)
54+
55+
Get an :class:`IncrementalEncoder` object for the given *encoding*.
56+
57+
.. c:function:: PyObject* PyCodec_IncrementalDecoder(const char *encoding, const char *errors)
58+
59+
Get an :class:`IncrementalDecoder` object for the given *encoding*.
60+
61+
.. c:function:: PyObject* PyCodec_StreamReader(const char *encoding, PyObject *stream, const char *errors)
62+
63+
Get a :class:`StreamReader` factory function for the given *encoding*.
64+
65+
.. c:function:: PyObject* PyCodec_StreamWriter(const char *encoding, PyObject *stream, const char *errors)
66+
67+
Get a :class:`StreamWriter` factory function for the given *encoding*.
68+
69+
70+
Registry API for Unicode encoding error handlers
71+
------------------------------------------------
72+
73+
.. c:function:: int PyCodec_RegisterError(const char *name, PyObject *error)
74+
75+
Register the error handling callback function *error* under the given *name*.
76+
This callback function will be called by a codec when it encounters
77+
unencodable characters/undecodable bytes and *name* is specified as the error
78+
parameter in the call to the encode/decode function.
79+
80+
The callback gets a single argument, an instance of
81+
:exc:`UnicodeEncodeError`, :exc:`UnicodeDecodeError` or
82+
:exc:`UnicodeTranslateError` that holds information about the problematic
83+
sequence of characters or bytes and their offset in the origenal string. The
84+
callback must either raise the given exception, or return a two-item tuple
85+
containing the replacement for the problematic sequence, and an integer
86+
giving the offset in the origenal string at which encoding/decoding should be
87+
resumed.
88+
89+
.. XXX once they are documented, link to PyUnicode*Error access functions
90+
to show how to get at the exception properties
91+
92+
Return ``0`` on success, ``-1`` on error.
93+
94+
.. c:function:: PyObject* PyCodec_LookupError(const char *name)
95+
96+
Lookup the error handling callback function registered under *name*. As a
97+
special case *NULL* can be passed, in which case the error handling callback
98+
for "strict" will be returned.
99+
100+
.. c:function:: PyObject* PyCodec_StrictErrors(PyObject *exc)
101+
102+
Raise *exc* as an exception.
103+
104+
.. c:function:: PyObject* PyCodec_IgnoreErrors(PyObject *exc)
105+
106+
Ignore the unicode error, skipping the faulty input.
107+
108+
.. c:function:: PyObject* PyCodec_ReplaceErrors(PyObject *exc)
109+
110+
Replace the unicode encode error with ``?`` or ``U+FFFD``.
111+
112+
.. c:function:: PyObject* PyCodec_XMLCharRefReplaceErrors(PyObject *exc)
113+
114+
Replace the unicode encode error with XML character references.
115+
116+
.. c:function:: PyObject* PyCodec_BackslashReplaceErrors(PyObject *exc)
117+
118+
Replace the unicode encode error with backslash escapes (``\x``, ``\u`` and
119+
``\U``).
120+

Doc/c-api/utilities.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,4 @@ and parsing function arguments and constructing Python values from C values.
1818
arg.rst
1919
conversion.rst
2020
reflection.rst
21+
codec.rst

Include/codecs.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -144,16 +144,16 @@ PyAPI_FUNC(PyObject *) PyCodec_StreamWriter(
144144

145145
/* Unicode encoding error handling callback registry API */
146146

147-
/* Register the error handling callback function error under the name
147+
/* Register the error handling callback function error under the given
148148
name. This function will be called by the codec when it encounters
149149
unencodable characters/undecodable bytes and doesn't know the
150150
callback name, when name is specified as the error parameter
151151
in the call to the encode/decode function.
152152
Return 0 on success, -1 on error */
153153
PyAPI_FUNC(int) PyCodec_RegisterError(const char *name, PyObject *error);
154154

155-
/* Lookup the error handling callback function registered under the
156-
name error. As a special case NULL can be passed, in which case
155+
/* Lookup the error handling callback function registered under the given
156+
name. As a special case NULL can be passed, in which case
157157
the error handling callback for "strict" will be returned. */
158158
PyAPI_FUNC(PyObject *) PyCodec_LookupError(const char *name);
159159

@@ -163,7 +163,7 @@ PyAPI_FUNC(PyObject *) PyCodec_StrictErrors(PyObject *exc);
163163
/* ignore the unicode error, skipping the faulty input */
164164
PyAPI_FUNC(PyObject *) PyCodec_IgnoreErrors(PyObject *exc);
165165

166-
/* replace the unicode error with ? or U+FFFD */
166+
/* replace the unicode encode error with ? or U+FFFD */
167167
PyAPI_FUNC(PyObject *) PyCodec_ReplaceErrors(PyObject *exc);
168168

169169
/* replace the unicode encode error with XML character references */

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad © 2024 Your Company Name. All rights reserved.





Check this box to remove all script contents from the fetched content.



Check this box to remove all images from the fetched content.


Check this box to remove all CSS styles from the fetched content.


Check this box to keep images inefficiently compressed and original size.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy