codecs.h 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. #ifndef Py_CODECREGISTRY_H
  2. #define Py_CODECREGISTRY_H
  3. #ifdef __cplusplus
  4. extern "C" {
  5. #endif
  6. /* ------------------------------------------------------------------------
  7. Python Codec Registry and support functions
  8. Written by Marc-Andre Lemburg (mal@lemburg.com).
  9. Copyright (c) Corporation for National Research Initiatives.
  10. ------------------------------------------------------------------------ */
  11. /* Register a new codec search function.
  12. As side effect, this tries to load the encodings package, if not
  13. yet done, to make sure that it is always first in the list of
  14. search functions.
  15. The search_function's refcount is incremented by this function. */
  16. PyAPI_FUNC(int) PyCodec_Register(
  17. PyObject *search_function
  18. );
  19. /* Unregister a codec search function and clear the registry's cache.
  20. If the search function is not registered, do nothing.
  21. Return 0 on success. Raise an exception and return -1 on error. */
  22. PyAPI_FUNC(int) PyCodec_Unregister(
  23. PyObject *search_function
  24. );
  25. /* Codec registry lookup API.
  26. Looks up the given encoding and returns a CodecInfo object with
  27. function attributes which implement the different aspects of
  28. processing the encoding.
  29. The encoding string is looked up converted to all lower-case
  30. characters. This makes encodings looked up through this mechanism
  31. effectively case-insensitive.
  32. If no codec is found, a KeyError is set and NULL returned.
  33. As side effect, this tries to load the encodings package, if not
  34. yet done. This is part of the lazy load strategy for the encodings
  35. package.
  36. */
  37. #ifndef Py_LIMITED_API
  38. PyAPI_FUNC(PyObject *) _PyCodec_Lookup(
  39. const char *encoding
  40. );
  41. PyAPI_FUNC(int) _PyCodec_Forget(
  42. const char *encoding
  43. );
  44. #endif
  45. /* Codec registry encoding check API.
  46. Returns 1/0 depending on whether there is a registered codec for
  47. the given encoding.
  48. */
  49. PyAPI_FUNC(int) PyCodec_KnownEncoding(
  50. const char *encoding
  51. );
  52. /* Generic codec based encoding API.
  53. object is passed through the encoder function found for the given
  54. encoding using the error handling method defined by errors. errors
  55. may be NULL to use the default method defined for the codec.
  56. Raises a LookupError in case no encoder can be found.
  57. */
  58. PyAPI_FUNC(PyObject *) PyCodec_Encode(
  59. PyObject *object,
  60. const char *encoding,
  61. const char *errors
  62. );
  63. /* Generic codec based decoding API.
  64. object is passed through the decoder function found for the given
  65. encoding using the error handling method defined by errors. errors
  66. may be NULL to use the default method defined for the codec.
  67. Raises a LookupError in case no encoder can be found.
  68. */
  69. PyAPI_FUNC(PyObject *) PyCodec_Decode(
  70. PyObject *object,
  71. const char *encoding,
  72. const char *errors
  73. );
  74. #ifndef Py_LIMITED_API
  75. /* Text codec specific encoding and decoding API.
  76. Checks the encoding against a list of codecs which do not
  77. implement a str<->bytes encoding before attempting the
  78. operation.
  79. Please note that these APIs are internal and should not
  80. be used in Python C extensions.
  81. XXX (ncoghlan): should we make these, or something like them, public
  82. in Python 3.5+?
  83. */
  84. PyAPI_FUNC(PyObject *) _PyCodec_LookupTextEncoding(
  85. const char *encoding,
  86. const char *alternate_command
  87. );
  88. PyAPI_FUNC(PyObject *) _PyCodec_EncodeText(
  89. PyObject *object,
  90. const char *encoding,
  91. const char *errors
  92. );
  93. PyAPI_FUNC(PyObject *) _PyCodec_DecodeText(
  94. PyObject *object,
  95. const char *encoding,
  96. const char *errors
  97. );
  98. /* These two aren't actually text encoding specific, but _io.TextIOWrapper
  99. * is the only current API consumer.
  100. */
  101. PyAPI_FUNC(PyObject *) _PyCodecInfo_GetIncrementalDecoder(
  102. PyObject *codec_info,
  103. const char *errors
  104. );
  105. PyAPI_FUNC(PyObject *) _PyCodecInfo_GetIncrementalEncoder(
  106. PyObject *codec_info,
  107. const char *errors
  108. );
  109. #endif
  110. /* --- Codec Lookup APIs --------------------------------------------------
  111. All APIs return a codec object with incremented refcount and are
  112. based on _PyCodec_Lookup(). The same comments w/r to the encoding
  113. name also apply to these APIs.
  114. */
  115. /* Get an encoder function for the given encoding. */
  116. PyAPI_FUNC(PyObject *) PyCodec_Encoder(
  117. const char *encoding
  118. );
  119. /* Get a decoder function for the given encoding. */
  120. PyAPI_FUNC(PyObject *) PyCodec_Decoder(
  121. const char *encoding
  122. );
  123. /* Get an IncrementalEncoder object for the given encoding. */
  124. PyAPI_FUNC(PyObject *) PyCodec_IncrementalEncoder(
  125. const char *encoding,
  126. const char *errors
  127. );
  128. /* Get an IncrementalDecoder object function for the given encoding. */
  129. PyAPI_FUNC(PyObject *) PyCodec_IncrementalDecoder(
  130. const char *encoding,
  131. const char *errors
  132. );
  133. /* Get a StreamReader factory function for the given encoding. */
  134. PyAPI_FUNC(PyObject *) PyCodec_StreamReader(
  135. const char *encoding,
  136. PyObject *stream,
  137. const char *errors
  138. );
  139. /* Get a StreamWriter factory function for the given encoding. */
  140. PyAPI_FUNC(PyObject *) PyCodec_StreamWriter(
  141. const char *encoding,
  142. PyObject *stream,
  143. const char *errors
  144. );
  145. /* Unicode encoding error handling callback registry API */
  146. /* Register the error handling callback function error under the given
  147. name. This function will be called by the codec when it encounters
  148. unencodable characters/undecodable bytes and doesn't know the
  149. callback name, when name is specified as the error parameter
  150. in the call to the encode/decode function.
  151. Return 0 on success, -1 on error */
  152. PyAPI_FUNC(int) PyCodec_RegisterError(const char *name, PyObject *error);
  153. /* Lookup the error handling callback function registered under the given
  154. name. As a special case NULL can be passed, in which case
  155. the error handling callback for "strict" will be returned. */
  156. PyAPI_FUNC(PyObject *) PyCodec_LookupError(const char *name);
  157. /* raise exc as an exception */
  158. PyAPI_FUNC(PyObject *) PyCodec_StrictErrors(PyObject *exc);
  159. /* ignore the unicode error, skipping the faulty input */
  160. PyAPI_FUNC(PyObject *) PyCodec_IgnoreErrors(PyObject *exc);
  161. /* replace the unicode encode error with ? or U+FFFD */
  162. PyAPI_FUNC(PyObject *) PyCodec_ReplaceErrors(PyObject *exc);
  163. /* replace the unicode encode error with XML character references */
  164. PyAPI_FUNC(PyObject *) PyCodec_XMLCharRefReplaceErrors(PyObject *exc);
  165. /* replace the unicode encode error with backslash escapes (\x, \u and \U) */
  166. PyAPI_FUNC(PyObject *) PyCodec_BackslashReplaceErrors(PyObject *exc);
  167. #if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x03050000
  168. /* replace the unicode encode error with backslash escapes (\N, \x, \u and \U) */
  169. PyAPI_FUNC(PyObject *) PyCodec_NameReplaceErrors(PyObject *exc);
  170. #endif
  171. #ifndef Py_LIMITED_API
  172. PyAPI_DATA(const char *) Py_hexdigits;
  173. #endif
  174. #ifdef __cplusplus
  175. }
  176. #endif
  177. #endif /* !Py_CODECREGISTRY_H */