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