blob: f01512c9e420c7635b74bb83351b660ab1f1ccbe [file] [log] [blame]
Fred Drake3adf79e2001-10-12 19:01:43 +00001\chapter{Abstract Objects Layer \label{abstract}}
2
3The functions in this chapter interact with Python objects regardless
4of their type, or with wide classes of object types (e.g. all
5numerical types, or all sequence types). When used on object types
6for which they do not apply, they will raise a Python exception.
7
8
9\section{Object Protocol \label{object}}
10
11\begin{cfuncdesc}{int}{PyObject_Print}{PyObject *o, FILE *fp, int flags}
12 Print an object \var{o}, on file \var{fp}. Returns \code{-1} on
13 error. The flags argument is used to enable certain printing
14 options. The only option currently supported is
15 \constant{Py_PRINT_RAW}; if given, the \function{str()} of the
16 object is written instead of the \function{repr()}.
17\end{cfuncdesc}
18
Martin v. Löwis29fafd82006-03-01 05:16:03 +000019\begin{cfuncdesc}{int}{PyObject_HasAttrString}{PyObject *o, const char *attr_name}
Fred Drake3adf79e2001-10-12 19:01:43 +000020 Returns \code{1} if \var{o} has the attribute \var{attr_name}, and
21 \code{0} otherwise. This is equivalent to the Python expression
22 \samp{hasattr(\var{o}, \var{attr_name})}. This function always
23 succeeds.
24\end{cfuncdesc}
25
26\begin{cfuncdesc}{PyObject*}{PyObject_GetAttrString}{PyObject *o,
Martin v. Löwis29fafd82006-03-01 05:16:03 +000027 const char *attr_name}
Fred Drake3adf79e2001-10-12 19:01:43 +000028 Retrieve an attribute named \var{attr_name} from object \var{o}.
29 Returns the attribute value on success, or \NULL{} on failure.
30 This is the equivalent of the Python expression
31 \samp{\var{o}.\var{attr_name}}.
32\end{cfuncdesc}
33
34
35\begin{cfuncdesc}{int}{PyObject_HasAttr}{PyObject *o, PyObject *attr_name}
36 Returns \code{1} if \var{o} has the attribute \var{attr_name}, and
37 \code{0} otherwise. This is equivalent to the Python expression
38 \samp{hasattr(\var{o}, \var{attr_name})}. This function always
39 succeeds.
40\end{cfuncdesc}
41
42
43\begin{cfuncdesc}{PyObject*}{PyObject_GetAttr}{PyObject *o,
44 PyObject *attr_name}
45 Retrieve an attribute named \var{attr_name} from object \var{o}.
46 Returns the attribute value on success, or \NULL{} on failure. This
47 is the equivalent of the Python expression
48 \samp{\var{o}.\var{attr_name}}.
49\end{cfuncdesc}
50
51
52\begin{cfuncdesc}{int}{PyObject_SetAttrString}{PyObject *o,
Martin v. Löwis29fafd82006-03-01 05:16:03 +000053 const char *attr_name, PyObject *v}
Fred Drake3adf79e2001-10-12 19:01:43 +000054 Set the value of the attribute named \var{attr_name}, for object
55 \var{o}, to the value \var{v}. Returns \code{-1} on failure. This
56 is the equivalent of the Python statement
57 \samp{\var{o}.\var{attr_name} = \var{v}}.
58\end{cfuncdesc}
59
60
61\begin{cfuncdesc}{int}{PyObject_SetAttr}{PyObject *o,
62 PyObject *attr_name, PyObject *v}
63 Set the value of the attribute named \var{attr_name}, for object
64 \var{o}, to the value \var{v}. Returns \code{-1} on failure. This
65 is the equivalent of the Python statement
66 \samp{\var{o}.\var{attr_name} = \var{v}}.
67\end{cfuncdesc}
68
69
Martin v. Löwis29fafd82006-03-01 05:16:03 +000070\begin{cfuncdesc}{int}{PyObject_DelAttrString}{PyObject *o, const char *attr_name}
Fred Drake3adf79e2001-10-12 19:01:43 +000071 Delete attribute named \var{attr_name}, for object \var{o}. Returns
72 \code{-1} on failure. This is the equivalent of the Python
73 statement: \samp{del \var{o}.\var{attr_name}}.
74\end{cfuncdesc}
75
76
77\begin{cfuncdesc}{int}{PyObject_DelAttr}{PyObject *o, PyObject *attr_name}
78 Delete attribute named \var{attr_name}, for object \var{o}. Returns
79 \code{-1} on failure. This is the equivalent of the Python
80 statement \samp{del \var{o}.\var{attr_name}}.
81\end{cfuncdesc}
82
83
Fred Drake178153f2002-06-13 11:51:48 +000084\begin{cfuncdesc}{PyObject*}{PyObject_RichCompare}{PyObject *o1,
Fred Drakea0c5e9f2002-06-14 14:35:56 +000085 PyObject *o2, int opid}
Fred Drake178153f2002-06-13 11:51:48 +000086 Compare the values of \var{o1} and \var{o2} using the operation
Fred Drakea0c5e9f2002-06-14 14:35:56 +000087 specified by \var{opid}, which must be one of
Fred Drake178153f2002-06-13 11:51:48 +000088 \constant{Py_LT},
89 \constant{Py_LE},
90 \constant{Py_EQ},
91 \constant{Py_NE},
92 \constant{Py_GT}, or
93 \constant{Py_GE}, corresponding to
94 \code{<},
95 \code{<=},
96 \code{==},
97 \code{!=},
98 \code{>}, or
99 \code{>=} respectively. This is the equivalent of the Python expression
Fred Drakea0c5e9f2002-06-14 14:35:56 +0000100 \samp{\var{o1} op \var{o2}}, where \code{op} is the operator
101 corresponding to \var{opid}. Returns the value of the comparison on
Fred Drake178153f2002-06-13 11:51:48 +0000102 success, or \NULL{} on failure.
103\end{cfuncdesc}
104
105\begin{cfuncdesc}{int}{PyObject_RichCompareBool}{PyObject *o1,
Fred Drakea0c5e9f2002-06-14 14:35:56 +0000106 PyObject *o2, int opid}
Fred Drake178153f2002-06-13 11:51:48 +0000107 Compare the values of \var{o1} and \var{o2} using the operation
Fred Drakea0c5e9f2002-06-14 14:35:56 +0000108 specified by \var{opid}, which must be one of
Fred Drake178153f2002-06-13 11:51:48 +0000109 \constant{Py_LT},
110 \constant{Py_LE},
111 \constant{Py_EQ},
112 \constant{Py_NE},
113 \constant{Py_GT}, or
114 \constant{Py_GE}, corresponding to
115 \code{<},
116 \code{<=},
117 \code{==},
118 \code{!=},
119 \code{>}, or
120 \code{>=} respectively. Returns \code{-1} on error, \code{0} if the
121 result is false, \code{1} otherwise. This is the equivalent of the
Fred Drakea0c5e9f2002-06-14 14:35:56 +0000122 Python expression \samp{\var{o1} op \var{o2}}, where
123 \code{op} is the operator corresponding to \var{opid}.
Fred Drake178153f2002-06-13 11:51:48 +0000124\end{cfuncdesc}
125
Fred Drake3adf79e2001-10-12 19:01:43 +0000126\begin{cfuncdesc}{int}{PyObject_Cmp}{PyObject *o1, PyObject *o2, int *result}
127 Compare the values of \var{o1} and \var{o2} using a routine provided
128 by \var{o1}, if one exists, otherwise with a routine provided by
129 \var{o2}. The result of the comparison is returned in
130 \var{result}. Returns \code{-1} on failure. This is the equivalent
131 of the Python statement\bifuncindex{cmp} \samp{\var{result} =
132 cmp(\var{o1}, \var{o2})}.
133\end{cfuncdesc}
134
135
136\begin{cfuncdesc}{int}{PyObject_Compare}{PyObject *o1, PyObject *o2}
137 Compare the values of \var{o1} and \var{o2} using a routine provided
138 by \var{o1}, if one exists, otherwise with a routine provided by
139 \var{o2}. Returns the result of the comparison on success. On
140 error, the value returned is undefined; use
141 \cfunction{PyErr_Occurred()} to detect an error. This is equivalent
142 to the Python expression\bifuncindex{cmp} \samp{cmp(\var{o1},
143 \var{o2})}.
144\end{cfuncdesc}
145
146
147\begin{cfuncdesc}{PyObject*}{PyObject_Repr}{PyObject *o}
148 Compute a string representation of object \var{o}. Returns the
149 string representation on success, \NULL{} on failure. This is the
150 equivalent of the Python expression \samp{repr(\var{o})}. Called by
151 the \function{repr()}\bifuncindex{repr} built-in function and by
152 reverse quotes.
153\end{cfuncdesc}
154
155
156\begin{cfuncdesc}{PyObject*}{PyObject_Str}{PyObject *o}
157 Compute a string representation of object \var{o}. Returns the
158 string representation on success, \NULL{} on failure. This is the
159 equivalent of the Python expression \samp{str(\var{o})}. Called by
160 the \function{str()}\bifuncindex{str} built-in function and by the
161 \keyword{print} statement.
162\end{cfuncdesc}
163
164
165\begin{cfuncdesc}{PyObject*}{PyObject_Unicode}{PyObject *o}
166 Compute a Unicode string representation of object \var{o}. Returns
167 the Unicode string representation on success, \NULL{} on failure.
168 This is the equivalent of the Python expression
Neal Norwitz4ddfd502002-07-28 13:55:20 +0000169 \samp{unicode(\var{o})}. Called by the
170 \function{unicode()}\bifuncindex{unicode} built-in function.
Fred Drake3adf79e2001-10-12 19:01:43 +0000171\end{cfuncdesc}
172
173\begin{cfuncdesc}{int}{PyObject_IsInstance}{PyObject *inst, PyObject *cls}
Fred Drakeb957bc32002-04-23 18:15:44 +0000174 Returns \code{1} if \var{inst} is an instance of the class \var{cls}
175 or a subclass of \var{cls}, or \code{0} if not. On error, returns
176 \code{-1} and sets an exception. If \var{cls} is a type object
177 rather than a class object, \cfunction{PyObject_IsInstance()}
Walter Dörwald6d5f30e2002-12-06 10:09:16 +0000178 returns \code{1} if \var{inst} is of type \var{cls}. If \var{cls}
179 is a tuple, the check will be done against every entry in \var{cls}.
180 The result will be \code{1} when at least one of the checks returns
181 \code{1}, otherwise it will be \code{0}. If \var{inst} is not a class
182 instance and \var{cls} is neither a type object, nor a class object,
183 nor a tuple, \var{inst} must have a \member{__class__} attribute
Fred Drakeb957bc32002-04-23 18:15:44 +0000184 --- the class relationship of the value of that attribute with
185 \var{cls} will be used to determine the result of this function.
Fred Drake3adf79e2001-10-12 19:01:43 +0000186 \versionadded{2.1}
Walter Dörwald6d5f30e2002-12-06 10:09:16 +0000187 \versionchanged[Support for a tuple as the second argument added]{2.2}
Fred Drake3adf79e2001-10-12 19:01:43 +0000188\end{cfuncdesc}
189
190Subclass determination is done in a fairly straightforward way, but
191includes a wrinkle that implementors of extensions to the class system
192may want to be aware of. If \class{A} and \class{B} are class
193objects, \class{B} is a subclass of \class{A} if it inherits from
194\class{A} either directly or indirectly. If either is not a class
195object, a more general mechanism is used to determine the class
196relationship of the two objects. When testing if \var{B} is a
197subclass of \var{A}, if \var{A} is \var{B},
198\cfunction{PyObject_IsSubclass()} returns true. If \var{A} and
199\var{B} are different objects, \var{B}'s \member{__bases__} attribute
200is searched in a depth-first fashion for \var{A} --- the presence of
201the \member{__bases__} attribute is considered sufficient for this
202determination.
203
204\begin{cfuncdesc}{int}{PyObject_IsSubclass}{PyObject *derived,
205 PyObject *cls}
206 Returns \code{1} if the class \var{derived} is identical to or
207 derived from the class \var{cls}, otherwise returns \code{0}. In
Walter Dörwaldd9a6ad32002-12-12 16:41:44 +0000208 case of an error, returns \code{-1}. If \var{cls}
209 is a tuple, the check will be done against every entry in \var{cls}.
210 The result will be \code{1} when at least one of the checks returns
211 \code{1}, otherwise it will be \code{0}. If either \var{derived} or
212 \var{cls} is not an actual class object (or tuple), this function
213 uses the generic algorithm described above.
Fred Drake3adf79e2001-10-12 19:01:43 +0000214 \versionadded{2.1}
Walter Dörwaldd9a6ad32002-12-12 16:41:44 +0000215 \versionchanged[Older versions of Python did not support a tuple
216 as the second argument]{2.3}
Fred Drake3adf79e2001-10-12 19:01:43 +0000217\end{cfuncdesc}
218
219
220\begin{cfuncdesc}{int}{PyCallable_Check}{PyObject *o}
221 Determine if the object \var{o} is callable. Return \code{1} if the
222 object is callable and \code{0} otherwise. This function always
223 succeeds.
224\end{cfuncdesc}
225
226
Fred Drake0e0b6182002-04-15 20:51:19 +0000227\begin{cfuncdesc}{PyObject*}{PyObject_Call}{PyObject *callable_object,
228 PyObject *args,
229 PyObject *kw}
230 Call a callable Python object \var{callable_object}, with arguments
231 given by the tuple \var{args}, and named arguments given by the
232 dictionary \var{kw}. If no named arguments are needed, \var{kw} may
233 be \NULL{}. \var{args} must not be \NULL{}, use an empty tuple if
234 no arguments are needed. Returns the result of the call on success,
235 or \NULL{} on failure. This is the equivalent of the Python
236 expression \samp{apply(\var{callable_object}, \var{args}, \var{kw})}
237 or \samp{\var{callable_object}(*\var{args}, **\var{kw})}.
238 \bifuncindex{apply}
Fred Drakef5368272003-01-25 07:48:13 +0000239 \versionadded{2.2}
Fred Drake0e0b6182002-04-15 20:51:19 +0000240\end{cfuncdesc}
241
242
Fred Drake3adf79e2001-10-12 19:01:43 +0000243\begin{cfuncdesc}{PyObject*}{PyObject_CallObject}{PyObject *callable_object,
244 PyObject *args}
245 Call a callable Python object \var{callable_object}, with arguments
246 given by the tuple \var{args}. If no arguments are needed, then
247 \var{args} may be \NULL. Returns the result of the call on
248 success, or \NULL{} on failure. This is the equivalent of the
249 Python expression \samp{apply(\var{callable_object}, \var{args})} or
250 \samp{\var{callable_object}(*\var{args})}.
251 \bifuncindex{apply}
252\end{cfuncdesc}
253
Fred Drakec44e9ec2001-10-26 16:38:38 +0000254\begin{cfuncdesc}{PyObject*}{PyObject_CallFunction}{PyObject *callable,
255 char *format, \moreargs}
256 Call a callable Python object \var{callable}, with a variable
Fred Drake3adf79e2001-10-12 19:01:43 +0000257 number of C arguments. The C arguments are described using a
258 \cfunction{Py_BuildValue()} style format string. The format may be
259 \NULL, indicating that no arguments are provided. Returns the
260 result of the call on success, or \NULL{} on failure. This is the
Fred Drakec44e9ec2001-10-26 16:38:38 +0000261 equivalent of the Python expression \samp{apply(\var{callable},
262 \var{args})} or \samp{\var{callable}(*\var{args})}.
Fred Drake3adf79e2001-10-12 19:01:43 +0000263 \bifuncindex{apply}
264\end{cfuncdesc}
265
266
267\begin{cfuncdesc}{PyObject*}{PyObject_CallMethod}{PyObject *o,
Fred Drakec44e9ec2001-10-26 16:38:38 +0000268 char *method, char *format,
269 \moreargs}
Raymond Hettinger2619c9e2003-12-07 11:40:17 +0000270 Call the method named \var{method} of object \var{o} with a variable
Fred Drake3adf79e2001-10-12 19:01:43 +0000271 number of C arguments. The C arguments are described by a
Andrew M. Kuchlingfe80b632004-08-07 17:53:05 +0000272 \cfunction{Py_BuildValue()} format string that should
273 produce a tuple. The format may be \NULL,
Fred Drake3adf79e2001-10-12 19:01:43 +0000274 indicating that no arguments are provided. Returns the result of the
275 call on success, or \NULL{} on failure. This is the equivalent of
Fred Drakec44e9ec2001-10-26 16:38:38 +0000276 the Python expression \samp{\var{o}.\var{method}(\var{args})}.
277\end{cfuncdesc}
278
279
Fred Drakeb0c079e2001-10-28 02:39:03 +0000280\begin{cfuncdesc}{PyObject*}{PyObject_CallFunctionObjArgs}{PyObject *callable,
281 \moreargs,
282 \code{NULL}}
Fred Drakec44e9ec2001-10-26 16:38:38 +0000283 Call a callable Python object \var{callable}, with a variable
284 number of \ctype{PyObject*} arguments. The arguments are provided
285 as a variable number of parameters followed by \NULL.
286 Returns the result of the call on success, or \NULL{} on failure.
287 \versionadded{2.2}
288\end{cfuncdesc}
289
290
Fred Drakeb0c079e2001-10-28 02:39:03 +0000291\begin{cfuncdesc}{PyObject*}{PyObject_CallMethodObjArgs}{PyObject *o,
292 PyObject *name,
293 \moreargs,
294 \code{NULL}}
Fred Drakec44e9ec2001-10-26 16:38:38 +0000295 Calls a method of the object \var{o}, where the name of the method
296 is given as a Python string object in \var{name}. It is called with
297 a variable number of \ctype{PyObject*} arguments. The arguments are
298 provided as a variable number of parameters followed by \NULL.
299 Returns the result of the call on success, or \NULL{} on failure.
300 \versionadded{2.2}
Fred Drake3adf79e2001-10-12 19:01:43 +0000301\end{cfuncdesc}
302
303
Martin v. Löwis29fafd82006-03-01 05:16:03 +0000304\begin{cfuncdesc}{long}{PyObject_Hash}{PyObject *o}
Fred Drake3adf79e2001-10-12 19:01:43 +0000305 Compute and return the hash value of an object \var{o}. On failure,
306 return \code{-1}. This is the equivalent of the Python expression
307 \samp{hash(\var{o})}.\bifuncindex{hash}
308\end{cfuncdesc}
309
310
311\begin{cfuncdesc}{int}{PyObject_IsTrue}{PyObject *o}
312 Returns \code{1} if the object \var{o} is considered to be true, and
313 \code{0} otherwise. This is equivalent to the Python expression
Raymond Hettinger2ed6dff2003-04-16 17:28:12 +0000314 \samp{not not \var{o}}. On failure, return \code{-1}.
315\end{cfuncdesc}
316
317
318\begin{cfuncdesc}{int}{PyObject_Not}{PyObject *o}
319 Returns \code{0} if the object \var{o} is considered to be true, and
320 \code{1} otherwise. This is equivalent to the Python expression
321 \samp{not \var{o}}. On failure, return \code{-1}.
Fred Drake3adf79e2001-10-12 19:01:43 +0000322\end{cfuncdesc}
323
324
325\begin{cfuncdesc}{PyObject*}{PyObject_Type}{PyObject *o}
326 When \var{o} is non-\NULL, returns a type object corresponding to
327 the object type of object \var{o}. On failure, raises
328 \exception{SystemError} and returns \NULL. This is equivalent to
Fred Drake12dd7b12003-04-09 18:15:57 +0000329 the Python expression \code{type(\var{o})}.\bifuncindex{type}
Guido van Rossum6db77182003-04-09 18:02:23 +0000330 This function increments the reference count of the return value.
331 There's really no reason to use this function instead of the
332 common expression \code{\var{o}->ob_type}, which returns a pointer
Fred Drake12dd7b12003-04-09 18:15:57 +0000333 of type \ctype{PyTypeObject*}, except when the incremented reference
Guido van Rossum6db77182003-04-09 18:02:23 +0000334 count is needed.
Fred Drake3adf79e2001-10-12 19:01:43 +0000335\end{cfuncdesc}
336
337\begin{cfuncdesc}{int}{PyObject_TypeCheck}{PyObject *o, PyTypeObject *type}
338 Return true if the object \var{o} is of type \var{type} or a subtype
339 of \var{type}. Both parameters must be non-\NULL.
340 \versionadded{2.2}
341\end{cfuncdesc}
342
Martin v. Löwis29fafd82006-03-01 05:16:03 +0000343\begin{cfuncdesc}{Py_ssize_t}{PyObject_Length}{PyObject *o}
344\cfuncline{Py_ssize_t}{PyObject_Size}{PyObject *o}
Fred Drake3adf79e2001-10-12 19:01:43 +0000345 Return the length of object \var{o}. If the object \var{o} provides
Raymond Hettinger4cd5a082004-01-04 03:11:45 +0000346 either the sequence and mapping protocols, the sequence length is
Fred Drake3adf79e2001-10-12 19:01:43 +0000347 returned. On error, \code{-1} is returned. This is the equivalent
348 to the Python expression \samp{len(\var{o})}.\bifuncindex{len}
349\end{cfuncdesc}
350
351
352\begin{cfuncdesc}{PyObject*}{PyObject_GetItem}{PyObject *o, PyObject *key}
353 Return element of \var{o} corresponding to the object \var{key} or
354 \NULL{} on failure. This is the equivalent of the Python expression
355 \samp{\var{o}[\var{key}]}.
356\end{cfuncdesc}
357
358
359\begin{cfuncdesc}{int}{PyObject_SetItem}{PyObject *o,
360 PyObject *key, PyObject *v}
361 Map the object \var{key} to the value \var{v}. Returns \code{-1} on
362 failure. This is the equivalent of the Python statement
363 \samp{\var{o}[\var{key}] = \var{v}}.
364\end{cfuncdesc}
365
366
367\begin{cfuncdesc}{int}{PyObject_DelItem}{PyObject *o, PyObject *key}
368 Delete the mapping for \var{key} from \var{o}. Returns \code{-1} on
369 failure. This is the equivalent of the Python statement \samp{del
370 \var{o}[\var{key}]}.
371\end{cfuncdesc}
372
373\begin{cfuncdesc}{int}{PyObject_AsFileDescriptor}{PyObject *o}
374 Derives a file-descriptor from a Python object. If the object is an
375 integer or long integer, its value is returned. If not, the
376 object's \method{fileno()} method is called if it exists; the method
377 must return an integer or long integer, which is returned as the
378 file descriptor value. Returns \code{-1} on failure.
379\end{cfuncdesc}
380
381\begin{cfuncdesc}{PyObject*}{PyObject_Dir}{PyObject *o}
382 This is equivalent to the Python expression \samp{dir(\var{o})},
383 returning a (possibly empty) list of strings appropriate for the
384 object argument, or \NULL{} if there was an error. If the argument
385 is \NULL, this is like the Python \samp{dir()}, returning the names
386 of the current locals; in this case, if no execution frame is active
387 then \NULL{} is returned but \cfunction{PyErr_Occurred()} will
388 return false.
389\end{cfuncdesc}
390
Fred Drake314bae52002-03-11 18:46:29 +0000391\begin{cfuncdesc}{PyObject*}{PyObject_GetIter}{PyObject *o}
392 This is equivalent to the Python expression \samp{iter(\var{o})}.
393 It returns a new iterator for the object argument, or the object
394 itself if the object is already an iterator. Raises
395 \exception{TypeError} and returns \NULL{} if the object cannot be
396 iterated.
397\end{cfuncdesc}
398
Fred Drake3adf79e2001-10-12 19:01:43 +0000399
400\section{Number Protocol \label{number}}
401
402\begin{cfuncdesc}{int}{PyNumber_Check}{PyObject *o}
403 Returns \code{1} if the object \var{o} provides numeric protocols,
404 and false otherwise. This function always succeeds.
405\end{cfuncdesc}
406
407
408\begin{cfuncdesc}{PyObject*}{PyNumber_Add}{PyObject *o1, PyObject *o2}
409 Returns the result of adding \var{o1} and \var{o2}, or \NULL{} on
410 failure. This is the equivalent of the Python expression
411 \samp{\var{o1} + \var{o2}}.
412\end{cfuncdesc}
413
414
415\begin{cfuncdesc}{PyObject*}{PyNumber_Subtract}{PyObject *o1, PyObject *o2}
416 Returns the result of subtracting \var{o2} from \var{o1}, or \NULL{}
417 on failure. This is the equivalent of the Python expression
418 \samp{\var{o1} - \var{o2}}.
419\end{cfuncdesc}
420
421
422\begin{cfuncdesc}{PyObject*}{PyNumber_Multiply}{PyObject *o1, PyObject *o2}
423 Returns the result of multiplying \var{o1} and \var{o2}, or \NULL{}
424 on failure. This is the equivalent of the Python expression
425 \samp{\var{o1} * \var{o2}}.
426\end{cfuncdesc}
427
428
429\begin{cfuncdesc}{PyObject*}{PyNumber_Divide}{PyObject *o1, PyObject *o2}
430 Returns the result of dividing \var{o1} by \var{o2}, or \NULL{} on
431 failure. This is the equivalent of the Python expression
432 \samp{\var{o1} / \var{o2}}.
433\end{cfuncdesc}
434
435
436\begin{cfuncdesc}{PyObject*}{PyNumber_FloorDivide}{PyObject *o1, PyObject *o2}
437 Return the floor of \var{o1} divided by \var{o2}, or \NULL{} on
438 failure. This is equivalent to the ``classic'' division of
439 integers.
440 \versionadded{2.2}
441\end{cfuncdesc}
442
443
444\begin{cfuncdesc}{PyObject*}{PyNumber_TrueDivide}{PyObject *o1, PyObject *o2}
445 Return a reasonable approximation for the mathematical value of
446 \var{o1} divided by \var{o2}, or \NULL{} on failure. The return
447 value is ``approximate'' because binary floating point numbers are
448 approximate; it is not possible to represent all real numbers in
449 base two. This function can return a floating point value when
450 passed two integers.
451 \versionadded{2.2}
452\end{cfuncdesc}
453
454
455\begin{cfuncdesc}{PyObject*}{PyNumber_Remainder}{PyObject *o1, PyObject *o2}
456 Returns the remainder of dividing \var{o1} by \var{o2}, or \NULL{}
457 on failure. This is the equivalent of the Python expression
458 \samp{\var{o1} \%\ \var{o2}}.
459\end{cfuncdesc}
460
461
462\begin{cfuncdesc}{PyObject*}{PyNumber_Divmod}{PyObject *o1, PyObject *o2}
463 See the built-in function \function{divmod()}\bifuncindex{divmod}.
464 Returns \NULL{} on failure. This is the equivalent of the Python
465 expression \samp{divmod(\var{o1}, \var{o2})}.
466\end{cfuncdesc}
467
468
469\begin{cfuncdesc}{PyObject*}{PyNumber_Power}{PyObject *o1,
470 PyObject *o2, PyObject *o3}
471 See the built-in function \function{pow()}\bifuncindex{pow}.
472 Returns \NULL{} on failure. This is the equivalent of the Python
473 expression \samp{pow(\var{o1}, \var{o2}, \var{o3})}, where \var{o3}
474 is optional. If \var{o3} is to be ignored, pass \cdata{Py_None} in
475 its place (passing \NULL{} for \var{o3} would cause an illegal
476 memory access).
477\end{cfuncdesc}
478
479
480\begin{cfuncdesc}{PyObject*}{PyNumber_Negative}{PyObject *o}
481 Returns the negation of \var{o} on success, or \NULL{} on failure.
482 This is the equivalent of the Python expression \samp{-\var{o}}.
483\end{cfuncdesc}
484
485
486\begin{cfuncdesc}{PyObject*}{PyNumber_Positive}{PyObject *o}
487 Returns \var{o} on success, or \NULL{} on failure. This is the
488 equivalent of the Python expression \samp{+\var{o}}.
489\end{cfuncdesc}
490
491
492\begin{cfuncdesc}{PyObject*}{PyNumber_Absolute}{PyObject *o}
493 Returns the absolute value of \var{o}, or \NULL{} on failure. This
494 is the equivalent of the Python expression \samp{abs(\var{o})}.
495 \bifuncindex{abs}
496\end{cfuncdesc}
497
498
499\begin{cfuncdesc}{PyObject*}{PyNumber_Invert}{PyObject *o}
500 Returns the bitwise negation of \var{o} on success, or \NULL{} on
501 failure. This is the equivalent of the Python expression
502 \samp{\~\var{o}}.
503\end{cfuncdesc}
504
505
506\begin{cfuncdesc}{PyObject*}{PyNumber_Lshift}{PyObject *o1, PyObject *o2}
507 Returns the result of left shifting \var{o1} by \var{o2} on success,
508 or \NULL{} on failure. This is the equivalent of the Python
509 expression \samp{\var{o1} <\code{<} \var{o2}}.
510\end{cfuncdesc}
511
512
513\begin{cfuncdesc}{PyObject*}{PyNumber_Rshift}{PyObject *o1, PyObject *o2}
514 Returns the result of right shifting \var{o1} by \var{o2} on
515 success, or \NULL{} on failure. This is the equivalent of the
516 Python expression \samp{\var{o1} >\code{>} \var{o2}}.
517\end{cfuncdesc}
518
519
520\begin{cfuncdesc}{PyObject*}{PyNumber_And}{PyObject *o1, PyObject *o2}
Raymond Hettingere5fced72004-04-17 11:57:40 +0000521 Returns the ``bitwise and'' of \var{o1} and \var{o2} on success and
Fred Drake3adf79e2001-10-12 19:01:43 +0000522 \NULL{} on failure. This is the equivalent of the Python expression
523 \samp{\var{o1} \&\ \var{o2}}.
524\end{cfuncdesc}
525
526
527\begin{cfuncdesc}{PyObject*}{PyNumber_Xor}{PyObject *o1, PyObject *o2}
528 Returns the ``bitwise exclusive or'' of \var{o1} by \var{o2} on
529 success, or \NULL{} on failure. This is the equivalent of the
530 Python expression \samp{\var{o1} \textasciicircum{} \var{o2}}.
531\end{cfuncdesc}
532
533\begin{cfuncdesc}{PyObject*}{PyNumber_Or}{PyObject *o1, PyObject *o2}
534 Returns the ``bitwise or'' of \var{o1} and \var{o2} on success, or
535 \NULL{} on failure. This is the equivalent of the Python expression
536 \samp{\var{o1} | \var{o2}}.
537\end{cfuncdesc}
538
539
540\begin{cfuncdesc}{PyObject*}{PyNumber_InPlaceAdd}{PyObject *o1, PyObject *o2}
541 Returns the result of adding \var{o1} and \var{o2}, or \NULL{} on
542 failure. The operation is done \emph{in-place} when \var{o1}
543 supports it. This is the equivalent of the Python statement
544 \samp{\var{o1} += \var{o2}}.
545\end{cfuncdesc}
546
547
548\begin{cfuncdesc}{PyObject*}{PyNumber_InPlaceSubtract}{PyObject *o1,
549 PyObject *o2}
550 Returns the result of subtracting \var{o2} from \var{o1}, or \NULL{}
551 on failure. The operation is done \emph{in-place} when \var{o1}
552 supports it. This is the equivalent of the Python statement
553 \samp{\var{o1} -= \var{o2}}.
554\end{cfuncdesc}
555
556
557\begin{cfuncdesc}{PyObject*}{PyNumber_InPlaceMultiply}{PyObject *o1,
558 PyObject *o2}
559 Returns the result of multiplying \var{o1} and \var{o2}, or \NULL{}
560 on failure. The operation is done \emph{in-place} when \var{o1}
561 supports it. This is the equivalent of the Python statement
562 \samp{\var{o1} *= \var{o2}}.
563\end{cfuncdesc}
564
565
566\begin{cfuncdesc}{PyObject*}{PyNumber_InPlaceDivide}{PyObject *o1,
567 PyObject *o2}
568 Returns the result of dividing \var{o1} by \var{o2}, or \NULL{} on
569 failure. The operation is done \emph{in-place} when \var{o1}
570 supports it. This is the equivalent of the Python statement
571 \samp{\var{o1} /= \var{o2}}.
572\end{cfuncdesc}
573
574
575\begin{cfuncdesc}{PyObject*}{PyNumber_InPlaceFloorDivide}{PyObject *o1,
576 PyObject *o2}
Andrew M. Kuchling1b50b432004-06-05 19:00:55 +0000577 Returns the mathematical floor of dividing \var{o1} by \var{o2}, or
Fred Drake3adf79e2001-10-12 19:01:43 +0000578 \NULL{} on failure. The operation is done \emph{in-place} when
579 \var{o1} supports it. This is the equivalent of the Python
580 statement \samp{\var{o1} //= \var{o2}}.
581 \versionadded{2.2}
582\end{cfuncdesc}
583
584
585\begin{cfuncdesc}{PyObject*}{PyNumber_InPlaceTrueDivide}{PyObject *o1,
586 PyObject *o2}
587 Return a reasonable approximation for the mathematical value of
588 \var{o1} divided by \var{o2}, or \NULL{} on failure. The return
589 value is ``approximate'' because binary floating point numbers are
590 approximate; it is not possible to represent all real numbers in
591 base two. This function can return a floating point value when
592 passed two integers. The operation is done \emph{in-place} when
593 \var{o1} supports it.
594 \versionadded{2.2}
595\end{cfuncdesc}
596
597
598\begin{cfuncdesc}{PyObject*}{PyNumber_InPlaceRemainder}{PyObject *o1,
599 PyObject *o2}
600 Returns the remainder of dividing \var{o1} by \var{o2}, or \NULL{}
601 on failure. The operation is done \emph{in-place} when \var{o1}
602 supports it. This is the equivalent of the Python statement
603 \samp{\var{o1} \%= \var{o2}}.
604\end{cfuncdesc}
605
606
607\begin{cfuncdesc}{PyObject*}{PyNumber_InPlacePower}{PyObject *o1,
608 PyObject *o2, PyObject *o3}
609 See the built-in function \function{pow()}.\bifuncindex{pow}
610 Returns \NULL{} on failure. The operation is done \emph{in-place}
611 when \var{o1} supports it. This is the equivalent of the Python
612 statement \samp{\var{o1} **= \var{o2}} when o3 is \cdata{Py_None},
613 or an in-place variant of \samp{pow(\var{o1}, \var{o2}, \var{o3})}
614 otherwise. If \var{o3} is to be ignored, pass \cdata{Py_None} in its
615 place (passing \NULL{} for \var{o3} would cause an illegal memory
616 access).
617\end{cfuncdesc}
618
619\begin{cfuncdesc}{PyObject*}{PyNumber_InPlaceLshift}{PyObject *o1,
620 PyObject *o2}
621 Returns the result of left shifting \var{o1} by \var{o2} on success,
622 or \NULL{} on failure. The operation is done \emph{in-place} when
623 \var{o1} supports it. This is the equivalent of the Python
624 statement \samp{\var{o1} <\code{<=} \var{o2}}.
625\end{cfuncdesc}
626
627
628\begin{cfuncdesc}{PyObject*}{PyNumber_InPlaceRshift}{PyObject *o1,
629 PyObject *o2}
630 Returns the result of right shifting \var{o1} by \var{o2} on
631 success, or \NULL{} on failure. The operation is done
632 \emph{in-place} when \var{o1} supports it. This is the equivalent
633 of the Python statement \samp{\var{o1} >\code{>=} \var{o2}}.
634\end{cfuncdesc}
635
636
637\begin{cfuncdesc}{PyObject*}{PyNumber_InPlaceAnd}{PyObject *o1, PyObject *o2}
638 Returns the ``bitwise and'' of \var{o1} and \var{o2} on success and
639 \NULL{} on failure. The operation is done \emph{in-place} when
640 \var{o1} supports it. This is the equivalent of the Python
641 statement \samp{\var{o1} \&= \var{o2}}.
642\end{cfuncdesc}
643
644
645\begin{cfuncdesc}{PyObject*}{PyNumber_InPlaceXor}{PyObject *o1, PyObject *o2}
646 Returns the ``bitwise exclusive or'' of \var{o1} by \var{o2} on
647 success, or \NULL{} on failure. The operation is done
648 \emph{in-place} when \var{o1} supports it. This is the equivalent
649 of the Python statement \samp{\var{o1} \textasciicircum= \var{o2}}.
650\end{cfuncdesc}
651
652\begin{cfuncdesc}{PyObject*}{PyNumber_InPlaceOr}{PyObject *o1, PyObject *o2}
653 Returns the ``bitwise or'' of \var{o1} and \var{o2} on success, or
654 \NULL{} on failure. The operation is done \emph{in-place} when
655 \var{o1} supports it. This is the equivalent of the Python
656 statement \samp{\var{o1} |= \var{o2}}.
657\end{cfuncdesc}
658
659\begin{cfuncdesc}{int}{PyNumber_Coerce}{PyObject **p1, PyObject **p2}
660 This function takes the addresses of two variables of type
661 \ctype{PyObject*}. If the objects pointed to by \code{*\var{p1}}
662 and \code{*\var{p2}} have the same type, increment their reference
663 count and return \code{0} (success). If the objects can be converted
664 to a common numeric type, replace \code{*p1} and \code{*p2} by their
665 converted value (with 'new' reference counts), and return \code{0}.
666 If no conversion is possible, or if some other error occurs, return
667 \code{-1} (failure) and don't increment the reference counts. The
668 call \code{PyNumber_Coerce(\&o1, \&o2)} is equivalent to the Python
669 statement \samp{\var{o1}, \var{o2} = coerce(\var{o1}, \var{o2})}.
670 \bifuncindex{coerce}
671\end{cfuncdesc}
672
673\begin{cfuncdesc}{PyObject*}{PyNumber_Int}{PyObject *o}
674 Returns the \var{o} converted to an integer object on success, or
Walter Dörwaldf1715402002-11-19 20:49:15 +0000675 \NULL{} on failure. If the argument is outside the integer range
676 a long object will be returned instead. This is the equivalent
677 of the Python expression \samp{int(\var{o})}.\bifuncindex{int}
Fred Drake3adf79e2001-10-12 19:01:43 +0000678\end{cfuncdesc}
679
680\begin{cfuncdesc}{PyObject*}{PyNumber_Long}{PyObject *o}
681 Returns the \var{o} converted to a long integer object on success,
682 or \NULL{} on failure. This is the equivalent of the Python
683 expression \samp{long(\var{o})}.\bifuncindex{long}
684\end{cfuncdesc}
685
686\begin{cfuncdesc}{PyObject*}{PyNumber_Float}{PyObject *o}
687 Returns the \var{o} converted to a float object on success, or
688 \NULL{} on failure. This is the equivalent of the Python expression
689 \samp{float(\var{o})}.\bifuncindex{float}
690\end{cfuncdesc}
691
Guido van Rossum38fff8c2006-03-07 18:50:55 +0000692\begin{cfuncdesc}{Py_ssize_t}{PyNumber_Index}{PyObject *o}
693 Returns the \var{o} converted to a Py_ssize_t integer on success, or
694 -1 with an exception raised on failure.
Neal Norwitz025f14b2006-03-08 05:29:18 +0000695 \versionadded{2.5}
Guido van Rossum38fff8c2006-03-07 18:50:55 +0000696\end{cfuncdesc}
Fred Drake3adf79e2001-10-12 19:01:43 +0000697
698\section{Sequence Protocol \label{sequence}}
699
700\begin{cfuncdesc}{int}{PySequence_Check}{PyObject *o}
701 Return \code{1} if the object provides sequence protocol, and
702 \code{0} otherwise. This function always succeeds.
703\end{cfuncdesc}
704
Martin v. Löwis29fafd82006-03-01 05:16:03 +0000705\begin{cfuncdesc}{Py_ssize_t}{PySequence_Size}{PyObject *o}
Fred Drake3adf79e2001-10-12 19:01:43 +0000706 Returns the number of objects in sequence \var{o} on success, and
707 \code{-1} on failure. For objects that do not provide sequence
708 protocol, this is equivalent to the Python expression
709 \samp{len(\var{o})}.\bifuncindex{len}
710\end{cfuncdesc}
711
Martin v. Löwis29fafd82006-03-01 05:16:03 +0000712\begin{cfuncdesc}{Py_ssize_t}{PySequence_Length}{PyObject *o}
Fred Drake3adf79e2001-10-12 19:01:43 +0000713 Alternate name for \cfunction{PySequence_Size()}.
714\end{cfuncdesc}
715
716\begin{cfuncdesc}{PyObject*}{PySequence_Concat}{PyObject *o1, PyObject *o2}
717 Return the concatenation of \var{o1} and \var{o2} on success, and
718 \NULL{} on failure. This is the equivalent of the Python
719 expression \samp{\var{o1} + \var{o2}}.
720\end{cfuncdesc}
721
722
Martin v. Löwis29fafd82006-03-01 05:16:03 +0000723\begin{cfuncdesc}{PyObject*}{PySequence_Repeat}{PyObject *o, Py_ssize_t count}
Fred Drake3adf79e2001-10-12 19:01:43 +0000724 Return the result of repeating sequence object \var{o} \var{count}
725 times, or \NULL{} on failure. This is the equivalent of the Python
726 expression \samp{\var{o} * \var{count}}.
727\end{cfuncdesc}
728
729\begin{cfuncdesc}{PyObject*}{PySequence_InPlaceConcat}{PyObject *o1,
730 PyObject *o2}
731 Return the concatenation of \var{o1} and \var{o2} on success, and
732 \NULL{} on failure. The operation is done \emph{in-place} when
733 \var{o1} supports it. This is the equivalent of the Python
734 expression \samp{\var{o1} += \var{o2}}.
735\end{cfuncdesc}
736
737
Martin v. Löwis29fafd82006-03-01 05:16:03 +0000738\begin{cfuncdesc}{PyObject*}{PySequence_InPlaceRepeat}{PyObject *o, Py_ssize_t count}
Fred Drake3adf79e2001-10-12 19:01:43 +0000739 Return the result of repeating sequence object \var{o} \var{count}
740 times, or \NULL{} on failure. The operation is done \emph{in-place}
741 when \var{o} supports it. This is the equivalent of the Python
742 expression \samp{\var{o} *= \var{count}}.
743\end{cfuncdesc}
744
745
Martin v. Löwis29fafd82006-03-01 05:16:03 +0000746\begin{cfuncdesc}{PyObject*}{PySequence_GetItem}{PyObject *o, Py_ssize_t i}
Fred Drake3adf79e2001-10-12 19:01:43 +0000747 Return the \var{i}th element of \var{o}, or \NULL{} on failure.
748 This is the equivalent of the Python expression
749 \samp{\var{o}[\var{i}]}.
750\end{cfuncdesc}
751
752
Martin v. Löwis29fafd82006-03-01 05:16:03 +0000753\begin{cfuncdesc}{PyObject*}{PySequence_GetSlice}{PyObject *o, Py_ssize_t i1, Py_ssize_t i2}
Fred Drake3adf79e2001-10-12 19:01:43 +0000754 Return the slice of sequence object \var{o} between \var{i1} and
755 \var{i2}, or \NULL{} on failure. This is the equivalent of the
756 Python expression \samp{\var{o}[\var{i1}:\var{i2}]}.
757\end{cfuncdesc}
758
759
Martin v. Löwis29fafd82006-03-01 05:16:03 +0000760\begin{cfuncdesc}{int}{PySequence_SetItem}{PyObject *o, Py_ssize_t i, PyObject *v}
Fred Drake3adf79e2001-10-12 19:01:43 +0000761 Assign object \var{v} to the \var{i}th element of \var{o}. Returns
762 \code{-1} on failure. This is the equivalent of the Python
Raymond Hettinger12c484d2003-08-09 04:37:14 +0000763 statement \samp{\var{o}[\var{i}] = \var{v}}. This function \emph{does not}
764 steal a reference to \var{v}.
Fred Drake3adf79e2001-10-12 19:01:43 +0000765\end{cfuncdesc}
766
Martin v. Löwis29fafd82006-03-01 05:16:03 +0000767\begin{cfuncdesc}{int}{PySequence_DelItem}{PyObject *o, Py_ssize_t i}
Fred Drake3adf79e2001-10-12 19:01:43 +0000768 Delete the \var{i}th element of object \var{o}. Returns \code{-1}
769 on failure. This is the equivalent of the Python statement
770 \samp{del \var{o}[\var{i}]}.
771\end{cfuncdesc}
772
Martin v. Löwis29fafd82006-03-01 05:16:03 +0000773\begin{cfuncdesc}{int}{PySequence_SetSlice}{PyObject *o, Py_ssize_t i1,
774 Py_ssize_t i2, PyObject *v}
Fred Drake3adf79e2001-10-12 19:01:43 +0000775 Assign the sequence object \var{v} to the slice in sequence object
776 \var{o} from \var{i1} to \var{i2}. This is the equivalent of the
777 Python statement \samp{\var{o}[\var{i1}:\var{i2}] = \var{v}}.
778\end{cfuncdesc}
779
Martin v. Löwis29fafd82006-03-01 05:16:03 +0000780\begin{cfuncdesc}{int}{PySequence_DelSlice}{PyObject *o, Py_ssize_t i1, Py_ssize_t i2}
Fred Drake3adf79e2001-10-12 19:01:43 +0000781 Delete the slice in sequence object \var{o} from \var{i1} to
782 \var{i2}. Returns \code{-1} on failure. This is the equivalent of
783 the Python statement \samp{del \var{o}[\var{i1}:\var{i2}]}.
784\end{cfuncdesc}
785
Fred Drake3adf79e2001-10-12 19:01:43 +0000786\begin{cfuncdesc}{int}{PySequence_Count}{PyObject *o, PyObject *value}
787 Return the number of occurrences of \var{value} in \var{o}, that is,
788 return the number of keys for which \code{\var{o}[\var{key}] ==
789 \var{value}}. On failure, return \code{-1}. This is equivalent to
790 the Python expression \samp{\var{o}.count(\var{value})}.
791\end{cfuncdesc}
792
793\begin{cfuncdesc}{int}{PySequence_Contains}{PyObject *o, PyObject *value}
794 Determine if \var{o} contains \var{value}. If an item in \var{o} is
795 equal to \var{value}, return \code{1}, otherwise return \code{0}.
796 On error, return \code{-1}. This is equivalent to the Python
797 expression \samp{\var{value} in \var{o}}.
798\end{cfuncdesc}
799
800\begin{cfuncdesc}{int}{PySequence_Index}{PyObject *o, PyObject *value}
801 Return the first index \var{i} for which \code{\var{o}[\var{i}] ==
802 \var{value}}. On error, return \code{-1}. This is equivalent to
803 the Python expression \samp{\var{o}.index(\var{value})}.
804\end{cfuncdesc}
805
806\begin{cfuncdesc}{PyObject*}{PySequence_List}{PyObject *o}
807 Return a list object with the same contents as the arbitrary
808 sequence \var{o}. The returned list is guaranteed to be new.
809\end{cfuncdesc}
810
811\begin{cfuncdesc}{PyObject*}{PySequence_Tuple}{PyObject *o}
812 Return a tuple object with the same contents as the arbitrary
Neal Norwitz98fcaaf2005-10-12 03:58:14 +0000813 sequence \var{o} or \NULL{} on failure. If \var{o} is a tuple,
814 a new reference will be returned, otherwise a tuple will be
815 constructed with the appropriate contents. This is equivalent
816 to the Python expression \samp{tuple(\var{o})}.
817 \bifuncindex{tuple}
Fred Drake3adf79e2001-10-12 19:01:43 +0000818\end{cfuncdesc}
819
820\begin{cfuncdesc}{PyObject*}{PySequence_Fast}{PyObject *o, const char *m}
821 Returns the sequence \var{o} as a tuple, unless it is already a
822 tuple or list, in which case \var{o} is returned. Use
823 \cfunction{PySequence_Fast_GET_ITEM()} to access the members of the
824 result. Returns \NULL{} on failure. If the object is not a
825 sequence, raises \exception{TypeError} with \var{m} as the message
826 text.
827\end{cfuncdesc}
828
Martin v. Löwis29fafd82006-03-01 05:16:03 +0000829\begin{cfuncdesc}{PyObject*}{PySequence_Fast_GET_ITEM}{PyObject *o, Py_ssize_t i}
Fred Drake3adf79e2001-10-12 19:01:43 +0000830 Return the \var{i}th element of \var{o}, assuming that \var{o} was
Fred Drakec37b65e2001-11-28 07:26:15 +0000831 returned by \cfunction{PySequence_Fast()}, \var{o} is not \NULL,
Tim Peters1fc240e2001-10-26 05:06:50 +0000832 and that \var{i} is within bounds.
833\end{cfuncdesc}
834
Raymond Hettingerc1e4f9d2004-03-12 08:04:00 +0000835\begin{cfuncdesc}{PyObject**}{PySequence_Fast_ITEMS}{PyObject *o}
836 Return the underlying array of PyObject pointers. Assumes that
837 \var{o} was returned by \cfunction{PySequence_Fast()} and
838 \var{o} is not \NULL.
839 \versionadded{2.4}
840\end{cfuncdesc}
841
Martin v. Löwis29fafd82006-03-01 05:16:03 +0000842\begin{cfuncdesc}{PyObject*}{PySequence_ITEM}{PyObject *o, Py_ssize_t i}
Brett Cannon77e02122003-09-07 02:22:16 +0000843 Return the \var{i}th element of \var{o} or \NULL{} on failure.
Martin v. Löwis01f94bd2002-05-08 08:44:21 +0000844 Macro form of \cfunction{PySequence_GetItem()} but without checking
845 that \cfunction{PySequence_Check(\var{o})} is true and without
Fred Drake86228e42002-05-23 16:02:28 +0000846 adjustment for negative indices.
847 \versionadded{2.3}
Martin v. Löwis01f94bd2002-05-08 08:44:21 +0000848\end{cfuncdesc}
849
Tim Peters1fc240e2001-10-26 05:06:50 +0000850\begin{cfuncdesc}{int}{PySequence_Fast_GET_SIZE}{PyObject *o}
851 Returns the length of \var{o}, assuming that \var{o} was
852 returned by \cfunction{PySequence_Fast()} and that \var{o} is
Fred Drakec37b65e2001-11-28 07:26:15 +0000853 not \NULL. The size can also be gotten by calling
Tim Peters1fc240e2001-10-26 05:06:50 +0000854 \cfunction{PySequence_Size()} on \var{o}, but
855 \cfunction{PySequence_Fast_GET_SIZE()} is faster because it can
856 assume \var{o} is a list or tuple.
Fred Drake3adf79e2001-10-12 19:01:43 +0000857\end{cfuncdesc}
858
859
860\section{Mapping Protocol \label{mapping}}
861
862\begin{cfuncdesc}{int}{PyMapping_Check}{PyObject *o}
863 Return \code{1} if the object provides mapping protocol, and
864 \code{0} otherwise. This function always succeeds.
865\end{cfuncdesc}
866
867
Martin v. Löwis29fafd82006-03-01 05:16:03 +0000868\begin{cfuncdesc}{Py_ssize_t}{PyMapping_Length}{PyObject *o}
Fred Drake3adf79e2001-10-12 19:01:43 +0000869 Returns the number of keys in object \var{o} on success, and
870 \code{-1} on failure. For objects that do not provide mapping
871 protocol, this is equivalent to the Python expression
872 \samp{len(\var{o})}.\bifuncindex{len}
873\end{cfuncdesc}
874
875
876\begin{cfuncdesc}{int}{PyMapping_DelItemString}{PyObject *o, char *key}
877 Remove the mapping for object \var{key} from the object \var{o}.
878 Return \code{-1} on failure. This is equivalent to the Python
879 statement \samp{del \var{o}[\var{key}]}.
880\end{cfuncdesc}
881
882
883\begin{cfuncdesc}{int}{PyMapping_DelItem}{PyObject *o, PyObject *key}
884 Remove the mapping for object \var{key} from the object \var{o}.
885 Return \code{-1} on failure. This is equivalent to the Python
886 statement \samp{del \var{o}[\var{key}]}.
887\end{cfuncdesc}
888
889
890\begin{cfuncdesc}{int}{PyMapping_HasKeyString}{PyObject *o, char *key}
891 On success, return \code{1} if the mapping object has the key
892 \var{key} and \code{0} otherwise. This is equivalent to the Python
893 expression \samp{\var{o}.has_key(\var{key})}. This function always
894 succeeds.
895\end{cfuncdesc}
896
897
898\begin{cfuncdesc}{int}{PyMapping_HasKey}{PyObject *o, PyObject *key}
899 Return \code{1} if the mapping object has the key \var{key} and
900 \code{0} otherwise. This is equivalent to the Python expression
901 \samp{\var{o}.has_key(\var{key})}. This function always succeeds.
902\end{cfuncdesc}
903
904
905\begin{cfuncdesc}{PyObject*}{PyMapping_Keys}{PyObject *o}
906 On success, return a list of the keys in object \var{o}. On
907 failure, return \NULL. This is equivalent to the Python expression
908 \samp{\var{o}.keys()}.
909\end{cfuncdesc}
910
911
912\begin{cfuncdesc}{PyObject*}{PyMapping_Values}{PyObject *o}
913 On success, return a list of the values in object \var{o}. On
914 failure, return \NULL. This is equivalent to the Python expression
915 \samp{\var{o}.values()}.
916\end{cfuncdesc}
917
918
919\begin{cfuncdesc}{PyObject*}{PyMapping_Items}{PyObject *o}
920 On success, return a list of the items in object \var{o}, where each
921 item is a tuple containing a key-value pair. On failure, return
922 \NULL. This is equivalent to the Python expression
923 \samp{\var{o}.items()}.
924\end{cfuncdesc}
925
926
927\begin{cfuncdesc}{PyObject*}{PyMapping_GetItemString}{PyObject *o, char *key}
928 Return element of \var{o} corresponding to the object \var{key} or
929 \NULL{} on failure. This is the equivalent of the Python expression
930 \samp{\var{o}[\var{key}]}.
931\end{cfuncdesc}
932
933\begin{cfuncdesc}{int}{PyMapping_SetItemString}{PyObject *o, char *key,
934 PyObject *v}
935 Map the object \var{key} to the value \var{v} in object \var{o}.
936 Returns \code{-1} on failure. This is the equivalent of the Python
937 statement \samp{\var{o}[\var{key}] = \var{v}}.
938\end{cfuncdesc}
939
940
941\section{Iterator Protocol \label{iterator}}
942
943\versionadded{2.2}
944
945There are only a couple of functions specifically for working with
946iterators.
947
948\begin{cfuncdesc}{int}{PyIter_Check}{PyObject *o}
949 Return true if the object \var{o} supports the iterator protocol.
950\end{cfuncdesc}
951
952\begin{cfuncdesc}{PyObject*}{PyIter_Next}{PyObject *o}
953 Return the next value from the iteration \var{o}. If the object is
954 an iterator, this retrieves the next value from the iteration, and
955 returns \NULL{} with no exception set if there are no remaining
956 items. If the object is not an iterator, \exception{TypeError} is
957 raised, or if there is an error in retrieving the item, returns
958 \NULL{} and passes along the exception.
959\end{cfuncdesc}
960
961To write a loop which iterates over an iterator, the C code should
962look something like this:
963
964\begin{verbatim}
Fred Drake314bae52002-03-11 18:46:29 +0000965PyObject *iterator = PyObject_GetIter(obj);
Fred Drake3adf79e2001-10-12 19:01:43 +0000966PyObject *item;
967
Fred Drake314bae52002-03-11 18:46:29 +0000968if (iterator == NULL) {
969 /* propagate error */
970}
971
972while (item = PyIter_Next(iterator)) {
Fred Drake3adf79e2001-10-12 19:01:43 +0000973 /* do something with item */
Fred Drake8b8fe282001-12-26 16:53:48 +0000974 ...
975 /* release reference when done */
976 Py_DECREF(item);
Fred Drake3adf79e2001-10-12 19:01:43 +0000977}
Fred Drake314bae52002-03-11 18:46:29 +0000978
979Py_DECREF(iterator);
980
Fred Drake3adf79e2001-10-12 19:01:43 +0000981if (PyErr_Occurred()) {
Fred Drake314bae52002-03-11 18:46:29 +0000982 /* propagate error */
Fred Drake3adf79e2001-10-12 19:01:43 +0000983}
984else {
985 /* continue doing useful work */
986}
Fred Drake8b8fe282001-12-26 16:53:48 +0000987\end{verbatim}
988
Jeremy Hylton89c3a222001-11-09 21:59:42 +0000989
Fred Drake94ead572001-11-09 23:34:26 +0000990\section{Buffer Protocol \label{abstract-buffer}}
Jeremy Hylton89c3a222001-11-09 21:59:42 +0000991
992\begin{cfuncdesc}{int}{PyObject_AsCharBuffer}{PyObject *obj,
Fred Drake94ead572001-11-09 23:34:26 +0000993 const char **buffer,
Martin v. Löwis29fafd82006-03-01 05:16:03 +0000994 Py_ssize_t *buffer_len}
Jeremy Hylton89c3a222001-11-09 21:59:42 +0000995 Returns a pointer to a read-only memory location useable as character-
996 based input. The \var{obj} argument must support the single-segment
Fred Drake243ea712002-04-04 04:10:36 +0000997 character buffer interface. On success, returns \code{0}, sets
Thomas Hellerbfeeeee2001-11-12 07:46:31 +0000998 \var{buffer} to the memory location and \var{buffer_len} to the buffer
Fred Drake243ea712002-04-04 04:10:36 +0000999 length. Returns \code{-1} and sets a \exception{TypeError} on error.
Fred Drake94ead572001-11-09 23:34:26 +00001000 \versionadded{1.6}
Jeremy Hylton89c3a222001-11-09 21:59:42 +00001001\end{cfuncdesc}
1002
1003\begin{cfuncdesc}{int}{PyObject_AsReadBuffer}{PyObject *obj,
Andrew M. Kuchlingee5e4cd2004-07-07 13:07:47 +00001004 const void **buffer,
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001005 Py_ssize_t *buffer_len}
Jeremy Hylton89c3a222001-11-09 21:59:42 +00001006 Returns a pointer to a read-only memory location containing
1007 arbitrary data. The \var{obj} argument must support the
1008 single-segment readable buffer interface. On success, returns
Fred Drake243ea712002-04-04 04:10:36 +00001009 \code{0}, sets \var{buffer} to the memory location and \var{buffer_len}
1010 to the buffer length. Returns \code{-1} and sets a
Jeremy Hylton89c3a222001-11-09 21:59:42 +00001011 \exception{TypeError} on error.
Fred Drake94ead572001-11-09 23:34:26 +00001012 \versionadded{1.6}
Jeremy Hylton89c3a222001-11-09 21:59:42 +00001013\end{cfuncdesc}
1014
1015\begin{cfuncdesc}{int}{PyObject_CheckReadBuffer}{PyObject *o}
1016 Returns \code{1} if \var{o} supports the single-segment readable
1017 buffer interface. Otherwise returns \code{0}.
Fred Drake94ead572001-11-09 23:34:26 +00001018 \versionadded{2.2}
Fred Drake8b8fe282001-12-26 16:53:48 +00001019\end{cfuncdesc}
Jeremy Hylton89c3a222001-11-09 21:59:42 +00001020
1021\begin{cfuncdesc}{int}{PyObject_AsWriteBuffer}{PyObject *obj,
Andrew M. Kuchlingee5e4cd2004-07-07 13:07:47 +00001022 void **buffer,
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001023 Py_ssize_t *buffer_len}
Jeremy Hylton89c3a222001-11-09 21:59:42 +00001024 Returns a pointer to a writeable memory location. The \var{obj}
1025 argument must support the single-segment, character buffer
Fred Drake243ea712002-04-04 04:10:36 +00001026 interface. On success, returns \code{0}, sets \var{buffer} to the
Thomas Hellerbfeeeee2001-11-12 07:46:31 +00001027 memory location and \var{buffer_len} to the buffer length. Returns
Fred Drake243ea712002-04-04 04:10:36 +00001028 \code{-1} and sets a \exception{TypeError} on error.
Fred Drake94ead572001-11-09 23:34:26 +00001029 \versionadded{1.6}
Jeremy Hylton89c3a222001-11-09 21:59:42 +00001030\end{cfuncdesc}