bytes.pxd 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. from .object cimport PyObject
  2. cdef extern from "Python.h":
  3. ctypedef struct va_list
  4. ############################################################################
  5. # 7.3.1 String Objects
  6. ############################################################################
  7. # These functions raise TypeError when expecting a string
  8. # parameter and are called with a non-string parameter.
  9. # PyStringObject
  10. # This subtype of PyObject represents a Python bytes object.
  11. # PyTypeObject PyBytes_Type
  12. # This instance of PyTypeObject represents the Python bytes type;
  13. # it is the same object as bytes and types.BytesType in the Python
  14. # layer.
  15. bint PyBytes_Check(object o)
  16. # Return true if the object o is a string object or an instance of
  17. # a subtype of the string type.
  18. bint PyBytes_CheckExact(object o)
  19. # Return true if the object o is a string object, but not an instance of a subtype of the string type.
  20. bytes PyBytes_FromString(char *v)
  21. # Return value: New reference.
  22. # Return a new string object with the value v on success, and NULL
  23. # on failure. The parameter v must not be NULL; it will not be
  24. # checked.
  25. bytes PyBytes_FromStringAndSize(char *v, Py_ssize_t len)
  26. # Return value: New reference.
  27. # Return a new string object with the value v and length len on
  28. # success, and NULL on failure. If v is NULL, the contents of the
  29. # string are uninitialized.
  30. bytes PyBytes_FromFormat(char *format, ...)
  31. # Return value: New reference.
  32. # Take a C printf()-style format string and a variable number of
  33. # arguments, calculate the size of the resulting Python string and
  34. # return a string with the values formatted into it. The variable
  35. # arguments must be C types and must correspond exactly to the
  36. # format characters in the format string. The following format
  37. # characters are allowed:
  38. # Format Characters Type Comment
  39. # %% n/a The literal % character.
  40. # %c int A single character, represented as an C int.
  41. # %d int Exactly equivalent to printf("%d").
  42. # %u unsigned int Exactly equivalent to printf("%u").
  43. # %ld long Exactly equivalent to printf("%ld").
  44. # %lu unsigned long Exactly equivalent to printf("%lu").
  45. # %zd Py_ssize_t Exactly equivalent to printf("%zd").
  46. # %zu size_t Exactly equivalent to printf("%zu").
  47. # %i int Exactly equivalent to printf("%i").
  48. # %x int Exactly equivalent to printf("%x").
  49. # %s char* A null-terminated C character array.
  50. # %p void* The hex representation of a C pointer.
  51. # Mostly equivalent to printf("%p") except that it is guaranteed to
  52. # start with the literal 0x regardless of what the platform's printf
  53. # yields.
  54. # An unrecognized format character causes all the rest of the
  55. # format string to be copied as-is to the result string, and any
  56. # extra arguments discarded.
  57. bytes PyBytes_FromFormatV(char *format, va_list vargs)
  58. # Return value: New reference.
  59. # Identical to PyBytes_FromFormat() except that it takes exactly two arguments.
  60. Py_ssize_t PyBytes_Size(object string) except -1
  61. # Return the length of the string in string object string.
  62. Py_ssize_t PyBytes_GET_SIZE(object string)
  63. # Macro form of PyBytes_Size() but without error checking.
  64. char* PyBytes_AsString(object string) except NULL
  65. # Return a NUL-terminated representation of the contents of
  66. # string. The pointer refers to the internal buffer of string, not
  67. # a copy. The data must not be modified in any way, unless the
  68. # string was just created using PyBytes_FromStringAndSize(NULL,
  69. # size). It must not be deallocated. If string is a Unicode
  70. # object, this function computes the default encoding of string
  71. # and operates on that. If string is not a string object at all,
  72. # PyBytes_AsString() returns NULL and raises TypeError.
  73. char* PyBytes_AS_STRING(object string)
  74. # Macro form of PyBytes_AsString() but without error
  75. # checking. Only string objects are supported; no Unicode objects
  76. # should be passed.
  77. int PyBytes_AsStringAndSize(object obj, char **buffer, Py_ssize_t *length) except -1
  78. # Return a NULL-terminated representation of the contents of the
  79. # object obj through the output variables buffer and length.
  80. #
  81. # The function accepts both string and Unicode objects as
  82. # input. For Unicode objects it returns the default encoded
  83. # version of the object. If length is NULL, the resulting buffer
  84. # may not contain NUL characters; if it does, the function returns
  85. # -1 and a TypeError is raised.
  86. # The buffer refers to an internal string buffer of obj, not a
  87. # copy. The data must not be modified in any way, unless the
  88. # string was just created using PyBytes_FromStringAndSize(NULL,
  89. # size). It must not be deallocated. If string is a Unicode
  90. # object, this function computes the default encoding of string
  91. # and operates on that. If string is not a string object at all,
  92. # PyBytes_AsStringAndSize() returns -1 and raises TypeError.
  93. void PyBytes_Concat(PyObject **string, object newpart)
  94. # Create a new string object in *string containing the contents of
  95. # newpart appended to string; the caller will own the new
  96. # reference. The reference to the old value of string will be
  97. # stolen. If the new string cannot be created, the old reference
  98. # to string will still be discarded and the value of *string will
  99. # be set to NULL; the appropriate exception will be set.
  100. void PyBytes_ConcatAndDel(PyObject **string, object newpart)
  101. # Create a new string object in *string containing the contents of
  102. # newpart appended to string. This version decrements the
  103. # reference count of newpart.
  104. int _PyBytes_Resize(PyObject **string, Py_ssize_t newsize) except -1
  105. # A way to resize a string object even though it is
  106. # ``immutable''. Only use this to build up a brand new string
  107. # object; don't use this if the string may already be known in
  108. # other parts of the code. It is an error to call this function if
  109. # the refcount on the input string object is not one. Pass the
  110. # address of an existing string object as an lvalue (it may be
  111. # written into), and the new size desired. On success, *string
  112. # holds the resized string object and 0 is returned; the address
  113. # in *string may differ from its input value. If the reallocation
  114. # fails, the original string object at *string is deallocated,
  115. # *string is set to NULL, a memory exception is set, and -1 is
  116. # returned.
  117. bytes PyBytes_Format(object format, object args)
  118. # Return value: New reference. Return a new string object from
  119. # format and args. Analogous to format % args. The args argument
  120. # must be a tuple.
  121. void PyBytes_InternInPlace(PyObject **string)
  122. # Intern the argument *string in place. The argument must be the
  123. # address of a pointer variable pointing to a Python string
  124. # object. If there is an existing interned string that is the same
  125. # as *string, it sets *string to it (decrementing the reference
  126. # count of the old string object and incrementing the reference
  127. # count of the interned string object), otherwise it leaves
  128. # *string alone and interns it (incrementing its reference
  129. # count). (Clarification: even though there is a lot of talk about
  130. # reference counts, think of this function as
  131. # reference-count-neutral; you own the object after the call if
  132. # and only if you owned it before the call.)
  133. bytes PyBytes_InternFromString(char *v)
  134. # Return value: New reference.
  135. # A combination of PyBytes_FromString() and
  136. # PyBytes_InternInPlace(), returning either a new string object
  137. # that has been interned, or a new (``owned'') reference to an
  138. # earlier interned string object with the same value.
  139. object PyBytes_Decode(char *s, Py_ssize_t size, char *encoding, char *errors)
  140. # Return value: New reference.
  141. # Create an object by decoding size bytes of the encoded buffer s
  142. # using the codec registered for encoding. encoding and errors
  143. # have the same meaning as the parameters of the same name in the
  144. # unicode() built-in function. The codec to be used is looked up
  145. # using the Python codec registry. Return NULL if an exception was
  146. # raised by the codec.
  147. object PyBytes_AsDecodedObject(object str, char *encoding, char *errors)
  148. # Return value: New reference.
  149. # Decode a string object by passing it to the codec registered for
  150. # encoding and return the result as Python object. encoding and
  151. # errors have the same meaning as the parameters of the same name
  152. # in the string encode() method. The codec to be used is looked up
  153. # using the Python codec registry. Return NULL if an exception was
  154. # raised by the codec.
  155. object PyBytes_Encode(char *s, Py_ssize_t size, char *encoding, char *errors)
  156. # Return value: New reference.
  157. # Encode the char buffer of the given size by passing it to the
  158. # codec registered for encoding and return a Python
  159. # object. encoding and errors have the same meaning as the
  160. # parameters of the same name in the string encode() method. The
  161. # codec to be used is looked up using the Python codec
  162. # registry. Return NULL if an exception was raised by the codec.
  163. object PyBytes_AsEncodedObject(object str, char *encoding, char *errors)
  164. # Return value: New reference.
  165. # Encode a string object using the codec registered for encoding
  166. # and return the result as Python object. encoding and errors have
  167. # the same meaning as the parameters of the same name in the
  168. # string encode() method. The codec to be used is looked up using
  169. # the Python codec registry. Return NULL if an exception was
  170. # raised by the codec.