codecs.h 6.6 KB

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