You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

333 lines
15 KiB

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