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