/Doc/c-api/string.rst

http://unladen-swallow.googlecode.com/ · ReStructuredText · 291 lines · 203 code · 88 blank · 0 comment · 0 complexity · 76bec6b03df6945c2bfefe1d72c88cc6 MD5 · raw file

  1. .. highlightlang:: c
  2. .. _stringobjects:
  3. String/Bytes Objects
  4. --------------------
  5. These functions raise :exc:`TypeError` when expecting a string parameter and are
  6. called with a non-string parameter.
  7. .. note::
  8. These functions have been renamed to PyBytes_* in Python 3.x. The PyBytes
  9. names are also available in 2.6.
  10. .. index:: object: string
  11. .. ctype:: PyStringObject
  12. This subtype of :ctype:`PyObject` represents a Python string object.
  13. .. cvar:: PyTypeObject PyString_Type
  14. .. index:: single: StringType (in module types)
  15. This instance of :ctype:`PyTypeObject` represents the Python string type; it is
  16. the same object as ``str`` and ``types.StringType`` in the Python layer. .
  17. .. cfunction:: int PyString_Check(PyObject *o)
  18. Return true if the object *o* is a string object or an instance of a subtype of
  19. the string type.
  20. .. versionchanged:: 2.2
  21. Allowed subtypes to be accepted.
  22. .. cfunction:: int PyString_CheckExact(PyObject *o)
  23. Return true if the object *o* is a string object, but not an instance of a
  24. subtype of the string type.
  25. .. versionadded:: 2.2
  26. .. cfunction:: PyObject* PyString_FromString(const char *v)
  27. Return a new string object with a copy of the string *v* as value on success,
  28. and *NULL* on failure. The parameter *v* must not be *NULL*; it will not be
  29. checked.
  30. .. cfunction:: PyObject* PyString_FromStringAndSize(const char *v, Py_ssize_t len)
  31. Return a new string object with a copy of the string *v* as value and length
  32. *len* on success, and *NULL* on failure. If *v* is *NULL*, the contents of the
  33. string are uninitialized.
  34. .. versionchanged:: 2.5
  35. This function used an :ctype:`int` type for *len*. This might require
  36. changes in your code for properly supporting 64-bit systems.
  37. .. cfunction:: PyObject* PyString_FromFormat(const char *format, ...)
  38. Take a C :cfunc:`printf`\ -style *format* string and a variable number of
  39. arguments, calculate the size of the resulting Python string and return a string
  40. with the values formatted into it. The variable arguments must be C types and
  41. must correspond exactly to the format characters in the *format* string. The
  42. following format characters are allowed:
  43. .. % This should be exactly the same as the table in PyErr_Format.
  44. .. % One should just refer to the other.
  45. .. % The descriptions for %zd and %zu are wrong, but the truth is complicated
  46. .. % because not all compilers support the %z width modifier -- we fake it
  47. .. % when necessary via interpolating PY_FORMAT_SIZE_T.
  48. .. % %u, %lu, %zu should have "new in Python 2.5" blurbs.
  49. +-------------------+---------------+--------------------------------+
  50. | Format Characters | Type | Comment |
  51. +===================+===============+================================+
  52. | :attr:`%%` | *n/a* | The literal % character. |
  53. +-------------------+---------------+--------------------------------+
  54. | :attr:`%c` | int | A single character, |
  55. | | | represented as an C int. |
  56. +-------------------+---------------+--------------------------------+
  57. | :attr:`%d` | int | Exactly equivalent to |
  58. | | | ``printf("%d")``. |
  59. +-------------------+---------------+--------------------------------+
  60. | :attr:`%u` | unsigned int | Exactly equivalent to |
  61. | | | ``printf("%u")``. |
  62. +-------------------+---------------+--------------------------------+
  63. | :attr:`%ld` | long | Exactly equivalent to |
  64. | | | ``printf("%ld")``. |
  65. +-------------------+---------------+--------------------------------+
  66. | :attr:`%lu` | unsigned long | Exactly equivalent to |
  67. | | | ``printf("%lu")``. |
  68. +-------------------+---------------+--------------------------------+
  69. | :attr:`%zd` | Py_ssize_t | Exactly equivalent to |
  70. | | | ``printf("%zd")``. |
  71. +-------------------+---------------+--------------------------------+
  72. | :attr:`%zu` | size_t | Exactly equivalent to |
  73. | | | ``printf("%zu")``. |
  74. +-------------------+---------------+--------------------------------+
  75. | :attr:`%i` | int | Exactly equivalent to |
  76. | | | ``printf("%i")``. |
  77. +-------------------+---------------+--------------------------------+
  78. | :attr:`%x` | int | Exactly equivalent to |
  79. | | | ``printf("%x")``. |
  80. +-------------------+---------------+--------------------------------+
  81. | :attr:`%s` | char\* | A null-terminated C character |
  82. | | | array. |
  83. +-------------------+---------------+--------------------------------+
  84. | :attr:`%p` | void\* | The hex representation of a C |
  85. | | | pointer. Mostly equivalent to |
  86. | | | ``printf("%p")`` except that |
  87. | | | it is guaranteed to start with |
  88. | | | the literal ``0x`` regardless |
  89. | | | of what the platform's |
  90. | | | ``printf`` yields. |
  91. +-------------------+---------------+--------------------------------+
  92. An unrecognized format character causes all the rest of the format string to be
  93. copied as-is to the result string, and any extra arguments discarded.
  94. .. cfunction:: PyObject* PyString_FromFormatV(const char *format, va_list vargs)
  95. Identical to :cfunc:`PyString_FromFormat` except that it takes exactly two
  96. arguments.
  97. .. cfunction:: Py_ssize_t PyString_Size(PyObject *string)
  98. Return the length of the string in string object *string*.
  99. .. versionchanged:: 2.5
  100. This function returned an :ctype:`int` type. This might require changes
  101. in your code for properly supporting 64-bit systems.
  102. .. cfunction:: Py_ssize_t PyString_GET_SIZE(PyObject *string)
  103. Macro form of :cfunc:`PyString_Size` but without error checking.
  104. .. versionchanged:: 2.5
  105. This macro returned an :ctype:`int` type. This might require changes in
  106. your code for properly supporting 64-bit systems.
  107. .. cfunction:: char* PyString_AsString(PyObject *string)
  108. Return a NUL-terminated representation of the contents of *string*. The pointer
  109. refers to the internal buffer of *string*, not a copy. The data must not be
  110. modified in any way, unless the string was just created using
  111. ``PyString_FromStringAndSize(NULL, size)``. It must not be deallocated. If
  112. *string* is a Unicode object, this function computes the default encoding of
  113. *string* and operates on that. If *string* is not a string object at all,
  114. :cfunc:`PyString_AsString` returns *NULL* and raises :exc:`TypeError`.
  115. .. cfunction:: char* PyString_AS_STRING(PyObject *string)
  116. Macro form of :cfunc:`PyString_AsString` but without error checking. Only
  117. string objects are supported; no Unicode objects should be passed.
  118. .. cfunction:: int PyString_AsStringAndSize(PyObject *obj, char **buffer, Py_ssize_t *length)
  119. Return a NUL-terminated representation of the contents of the object *obj*
  120. through the output variables *buffer* and *length*.
  121. The function accepts both string and Unicode objects as input. For Unicode
  122. objects it returns the default encoded version of the object. If *length* is
  123. *NULL*, the resulting buffer may not contain NUL characters; if it does, the
  124. function returns ``-1`` and a :exc:`TypeError` is raised.
  125. The buffer refers to an internal string buffer of *obj*, not a copy. The data
  126. must not be modified in any way, unless the string was just created using
  127. ``PyString_FromStringAndSize(NULL, size)``. It must not be deallocated. If
  128. *string* is a Unicode object, this function computes the default encoding of
  129. *string* and operates on that. If *string* is not a string object at all,
  130. :cfunc:`PyString_AsStringAndSize` returns ``-1`` and raises :exc:`TypeError`.
  131. .. versionchanged:: 2.5
  132. This function used an :ctype:`int *` type for *length*. This might
  133. require changes in your code for properly supporting 64-bit systems.
  134. .. cfunction:: void PyString_Concat(PyObject **string, PyObject *newpart)
  135. Create a new string object in *\*string* containing the contents of *newpart*
  136. appended to *string*; the caller will own the new reference. The reference to
  137. the old value of *string* will be stolen. If the new string cannot be created,
  138. the old reference to *string* will still be discarded and the value of
  139. *\*string* will be set to *NULL*; the appropriate exception will be set.
  140. .. cfunction:: void PyString_ConcatAndDel(PyObject **string, PyObject *newpart)
  141. Create a new string object in *\*string* containing the contents of *newpart*
  142. appended to *string*. This version decrements the reference count of *newpart*.
  143. .. cfunction:: int _PyString_Resize(PyObject **string, Py_ssize_t newsize)
  144. A way to resize a string object even though it is "immutable". Only use this to
  145. build up a brand new string object; don't use this if the string may already be
  146. known in other parts of the code. It is an error to call this function if the
  147. refcount on the input string object is not one. Pass the address of an existing
  148. string object as an lvalue (it may be written into), and the new size desired.
  149. On success, *\*string* holds the resized string object and ``0`` is returned;
  150. the address in *\*string* may differ from its input value. If the reallocation
  151. fails, the original string object at *\*string* is deallocated, *\*string* is
  152. set to *NULL*, a memory exception is set, and ``-1`` is returned.
  153. .. versionchanged:: 2.5
  154. This function used an :ctype:`int` type for *newsize*. This might
  155. require changes in your code for properly supporting 64-bit systems.
  156. .. cfunction:: PyObject* PyString_Format(PyObject *format, PyObject *args)
  157. Return a new string object from *format* and *args*. Analogous to ``format %
  158. args``. The *args* argument must be a tuple.
  159. .. cfunction:: void PyString_InternInPlace(PyObject **string)
  160. Intern the argument *\*string* in place. The argument must be the address of a
  161. pointer variable pointing to a Python string object. If there is an existing
  162. interned string that is the same as *\*string*, it sets *\*string* to it
  163. (decrementing the reference count of the old string object and incrementing the
  164. reference count of the interned string object), otherwise it leaves *\*string*
  165. alone and interns it (incrementing its reference count). (Clarification: even
  166. though there is a lot of talk about reference counts, think of this function as
  167. reference-count-neutral; you own the object after the call if and only if you
  168. owned it before the call.)
  169. .. cfunction:: PyObject* PyString_InternFromString(const char *v)
  170. A combination of :cfunc:`PyString_FromString` and
  171. :cfunc:`PyString_InternInPlace`, returning either a new string object that has
  172. been interned, or a new ("owned") reference to an earlier interned string object
  173. with the same value.
  174. .. cfunction:: PyObject* PyString_Decode(const char *s, Py_ssize_t size, const char *encoding, const char *errors)
  175. Create an object by decoding *size* bytes of the encoded buffer *s* using the
  176. codec registered for *encoding*. *encoding* and *errors* have the same meaning
  177. as the parameters of the same name in the :func:`unicode` built-in function.
  178. The codec to be used is looked up using the Python codec registry. Return
  179. *NULL* if an exception was raised by the codec.
  180. .. versionchanged:: 2.5
  181. This function used an :ctype:`int` type for *size*. This might require
  182. changes in your code for properly supporting 64-bit systems.
  183. .. cfunction:: PyObject* PyString_AsDecodedObject(PyObject *str, const char *encoding, const char *errors)
  184. Decode a string object by passing it to the codec registered for *encoding* and
  185. return the result as Python object. *encoding* and *errors* have the same
  186. meaning as the parameters of the same name in the string :meth:`encode` method.
  187. The codec to be used is looked up using the Python codec registry. Return *NULL*
  188. if an exception was raised by the codec.
  189. .. cfunction:: PyObject* PyString_Encode(const char *s, Py_ssize_t size, const char *encoding, const char *errors)
  190. Encode the :ctype:`char` buffer of the given size by passing it to the codec
  191. registered for *encoding* and return a Python object. *encoding* and *errors*
  192. have the same meaning as the parameters of the same name in the string
  193. :meth:`encode` method. The codec to be used is looked up using the Python codec
  194. registry. Return *NULL* if an exception was raised by the codec.
  195. .. versionchanged:: 2.5
  196. This function used an :ctype:`int` type for *size*. This might require
  197. changes in your code for properly supporting 64-bit systems.
  198. .. cfunction:: PyObject* PyString_AsEncodedObject(PyObject *str, const char *encoding, const char *errors)
  199. Encode a string object using the codec registered for *encoding* and return the
  200. result as Python object. *encoding* and *errors* have the same meaning as the
  201. parameters of the same name in the string :meth:`encode` method. The codec to be
  202. used is looked up using the Python codec registry. Return *NULL* if an exception
  203. was raised by the codec.