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