codecs.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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 register 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. PyAPI_FUNC(PyObject *) _PyCodec_Lookup(
  32. const char *encoding
  33. );
  34. /* Generic codec based encoding API.
  35. object is passed through the encoder function found for the given
  36. encoding using the error handling method defined by errors. errors
  37. may be NULL to use the default method defined for the codec.
  38. Raises a LookupError in case no encoder can be found.
  39. */
  40. PyAPI_FUNC(PyObject *) PyCodec_Encode(
  41. PyObject *object,
  42. const char *encoding,
  43. const char *errors
  44. );
  45. /* Generic codec based decoding API.
  46. object is passed through the decoder function found for the given
  47. encoding using the error handling method defined by errors. errors
  48. may be NULL to use the default method defined for the codec.
  49. Raises a LookupError in case no encoder can be found.
  50. */
  51. PyAPI_FUNC(PyObject *) PyCodec_Decode(
  52. PyObject *object,
  53. const char *encoding,
  54. const char *errors
  55. );
  56. /* Text codec specific encoding and decoding API.
  57. Checks the encoding against a list of codecs which do not
  58. implement a unicode<->bytes encoding before attempting the
  59. operation.
  60. Please note that these APIs are internal and should not
  61. be used in Python C extensions.
  62. XXX (ncoghlan): should we make these, or something like them, public
  63. in Python 3.5+?
  64. */
  65. PyAPI_FUNC(PyObject *) _PyCodec_LookupTextEncoding(
  66. const char *encoding,
  67. const char *alternate_command
  68. );
  69. PyAPI_FUNC(PyObject *) _PyCodec_EncodeText(
  70. PyObject *object,
  71. const char *encoding,
  72. const char *errors
  73. );
  74. PyAPI_FUNC(PyObject *) _PyCodec_DecodeText(
  75. PyObject *object,
  76. const char *encoding,
  77. const char *errors
  78. );
  79. /* These two aren't actually text encoding specific, but _io.TextIOWrapper
  80. * is the only current API consumer.
  81. */
  82. PyAPI_FUNC(PyObject *) _PyCodecInfo_GetIncrementalDecoder(
  83. PyObject *codec_info,
  84. const char *errors
  85. );
  86. PyAPI_FUNC(PyObject *) _PyCodecInfo_GetIncrementalEncoder(
  87. PyObject *codec_info,
  88. const char *errors
  89. );
  90. /* --- Codec Lookup APIs --------------------------------------------------
  91. All APIs return a codec object with incremented refcount and are
  92. based on _PyCodec_Lookup(). The same comments w/r to the encoding
  93. name also apply to these APIs.
  94. */
  95. /* Get an encoder function for the given encoding. */
  96. PyAPI_FUNC(PyObject *) PyCodec_Encoder(
  97. const char *encoding
  98. );
  99. /* Get a decoder function for the given encoding. */
  100. PyAPI_FUNC(PyObject *) PyCodec_Decoder(
  101. const char *encoding
  102. );
  103. /* Get a IncrementalEncoder object for the given encoding. */
  104. PyAPI_FUNC(PyObject *) PyCodec_IncrementalEncoder(
  105. const char *encoding,
  106. const char *errors
  107. );
  108. /* Get a IncrementalDecoder object function for the given encoding. */
  109. PyAPI_FUNC(PyObject *) PyCodec_IncrementalDecoder(
  110. const char *encoding,
  111. const char *errors
  112. );
  113. /* Get a StreamReader factory function for the given encoding. */
  114. PyAPI_FUNC(PyObject *) PyCodec_StreamReader(
  115. const char *encoding,
  116. PyObject *stream,
  117. const char *errors
  118. );
  119. /* Get a StreamWriter factory function for the given encoding. */
  120. PyAPI_FUNC(PyObject *) PyCodec_StreamWriter(
  121. const char *encoding,
  122. PyObject *stream,
  123. const char *errors
  124. );
  125. /* Unicode encoding error handling callback registry API */
  126. /* Register the error handling callback function error under the given
  127. name. This function will be called by the codec when it encounters
  128. unencodable characters/undecodable bytes and doesn't know the
  129. callback name, when name is specified as the error parameter
  130. in the call to the encode/decode function.
  131. Return 0 on success, -1 on error */
  132. PyAPI_FUNC(int) PyCodec_RegisterError(const char *name, PyObject *error);
  133. /* Lookup the error handling callback function registered under the given
  134. name. As a special case NULL can be passed, in which case
  135. the error handling callback for "strict" will be returned. */
  136. PyAPI_FUNC(PyObject *) PyCodec_LookupError(const char *name);
  137. /* raise exc as an exception */
  138. PyAPI_FUNC(PyObject *) PyCodec_StrictErrors(PyObject *exc);
  139. /* ignore the unicode error, skipping the faulty input */
  140. PyAPI_FUNC(PyObject *) PyCodec_IgnoreErrors(PyObject *exc);
  141. /* replace the unicode encode error with ? or U+FFFD */
  142. PyAPI_FUNC(PyObject *) PyCodec_ReplaceErrors(PyObject *exc);
  143. /* replace the unicode encode error with XML character references */
  144. PyAPI_FUNC(PyObject *) PyCodec_XMLCharRefReplaceErrors(PyObject *exc);
  145. /* replace the unicode encode error with backslash escapes (\x, \u and \U) */
  146. PyAPI_FUNC(PyObject *) PyCodec_BackslashReplaceErrors(PyObject *exc);
  147. #ifdef __cplusplus
  148. }
  149. #endif
  150. #endif /* !Py_CODECREGISTRY_H */