blob: 91c0944edf8c1bcb38ee60e596d1ac9027ce7095 [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
19\begin{cfuncdesc}{int}{PyObject_HasAttrString}{PyObject *o, char *attr_name}
20 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,
27 char *attr_name}
28 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,
53 char *attr_name, PyObject *v}
54 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
70\begin{cfuncdesc}{int}{PyObject_DelAttrString}{PyObject *o, char *attr_name}
71 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}
239\end{cfuncdesc}
240
241
Fred Drake3adf79e2001-10-12 19:01:43 +0000242\begin{cfuncdesc}{PyObject*}{PyObject_CallObject}{PyObject *callable_object,
243 PyObject *args}
244 Call a callable Python object \var{callable_object}, with arguments
245 given by the tuple \var{args}. If no arguments are needed, then
246 \var{args} may be \NULL. Returns the result of the call on
247 success, or \NULL{} on failure. This is the equivalent of the
248 Python expression \samp{apply(\var{callable_object}, \var{args})} or
249 \samp{\var{callable_object}(*\var{args})}.
250 \bifuncindex{apply}
251\end{cfuncdesc}
252
Fred Drakec44e9ec2001-10-26 16:38:38 +0000253\begin{cfuncdesc}{PyObject*}{PyObject_CallFunction}{PyObject *callable,
254 char *format, \moreargs}
255 Call a callable Python object \var{callable}, with a variable
Fred Drake3adf79e2001-10-12 19:01:43 +0000256 number of C arguments. The C arguments are described using a
257 \cfunction{Py_BuildValue()} style format string. The format may be
258 \NULL, indicating that no arguments are provided. Returns the
259 result of the call on success, or \NULL{} on failure. This is the
Fred Drakec44e9ec2001-10-26 16:38:38 +0000260 equivalent of the Python expression \samp{apply(\var{callable},
261 \var{args})} or \samp{\var{callable}(*\var{args})}.
Fred Drake3adf79e2001-10-12 19:01:43 +0000262 \bifuncindex{apply}
263\end{cfuncdesc}
264
265
266\begin{cfuncdesc}{PyObject*}{PyObject_CallMethod}{PyObject *o,
Fred Drakec44e9ec2001-10-26 16:38:38 +0000267 char *method, char *format,
268 \moreargs}
Fred Drake3adf79e2001-10-12 19:01:43 +0000269 Call the method named \var{m} of object \var{o} with a variable
270 number of C arguments. The C arguments are described by a
271 \cfunction{Py_BuildValue()} format string. The format may be \NULL,
272 indicating that no arguments are provided. Returns the result of the
273 call on success, or \NULL{} on failure. This is the equivalent of
Fred Drakec44e9ec2001-10-26 16:38:38 +0000274 the Python expression \samp{\var{o}.\var{method}(\var{args})}.
275\end{cfuncdesc}
276
277
Fred Drakeb0c079e2001-10-28 02:39:03 +0000278\begin{cfuncdesc}{PyObject*}{PyObject_CallFunctionObjArgs}{PyObject *callable,
279 \moreargs,
280 \code{NULL}}
Fred Drakec44e9ec2001-10-26 16:38:38 +0000281 Call a callable Python object \var{callable}, with a variable
282 number of \ctype{PyObject*} arguments. The arguments are provided
283 as a variable number of parameters followed by \NULL.
284 Returns the result of the call on success, or \NULL{} on failure.
285 \versionadded{2.2}
286\end{cfuncdesc}
287
288
Fred Drakeb0c079e2001-10-28 02:39:03 +0000289\begin{cfuncdesc}{PyObject*}{PyObject_CallMethodObjArgs}{PyObject *o,
290 PyObject *name,
291 \moreargs,
292 \code{NULL}}
Fred Drakec44e9ec2001-10-26 16:38:38 +0000293 Calls a method of the object \var{o}, where the name of the method
294 is given as a Python string object in \var{name}. It is called with
295 a variable number of \ctype{PyObject*} arguments. The arguments are
296 provided as a variable number of parameters followed by \NULL.
297 Returns the result of the call on success, or \NULL{} on failure.
298 \versionadded{2.2}
Fred Drake3adf79e2001-10-12 19:01:43 +0000299\end{cfuncdesc}
300
301
302\begin{cfuncdesc}{int}{PyObject_Hash}{PyObject *o}
303 Compute and return the hash value of an object \var{o}. On failure,
304 return \code{-1}. This is the equivalent of the Python expression
305 \samp{hash(\var{o})}.\bifuncindex{hash}
306\end{cfuncdesc}
307
308
309\begin{cfuncdesc}{int}{PyObject_IsTrue}{PyObject *o}
310 Returns \code{1} if the object \var{o} is considered to be true, and
311 \code{0} otherwise. This is equivalent to the Python expression
312 \samp{not not \var{o}}. This function always succeeds.
313\end{cfuncdesc}
314
315
316\begin{cfuncdesc}{PyObject*}{PyObject_Type}{PyObject *o}
317 When \var{o} is non-\NULL, returns a type object corresponding to
318 the object type of object \var{o}. On failure, raises
319 \exception{SystemError} and returns \NULL. This is equivalent to
320 the Python expression \code{type(\var{o})}.
321 \bifuncindex{type}
322\end{cfuncdesc}
323
324\begin{cfuncdesc}{int}{PyObject_TypeCheck}{PyObject *o, PyTypeObject *type}
325 Return true if the object \var{o} is of type \var{type} or a subtype
326 of \var{type}. Both parameters must be non-\NULL.
327 \versionadded{2.2}
328\end{cfuncdesc}
329
330\begin{cfuncdesc}{int}{PyObject_Length}{PyObject *o}
Fred Drake0e0b6182002-04-15 20:51:19 +0000331\cfuncline{int}{PyObject_Size}{PyObject *o}
Fred Drake3adf79e2001-10-12 19:01:43 +0000332 Return the length of object \var{o}. If the object \var{o} provides
333 both sequence and mapping protocols, the sequence length is
334 returned. On error, \code{-1} is returned. This is the equivalent
335 to the Python expression \samp{len(\var{o})}.\bifuncindex{len}
336\end{cfuncdesc}
337
338
339\begin{cfuncdesc}{PyObject*}{PyObject_GetItem}{PyObject *o, PyObject *key}
340 Return element of \var{o} corresponding to the object \var{key} or
341 \NULL{} on failure. This is the equivalent of the Python expression
342 \samp{\var{o}[\var{key}]}.
343\end{cfuncdesc}
344
345
346\begin{cfuncdesc}{int}{PyObject_SetItem}{PyObject *o,
347 PyObject *key, PyObject *v}
348 Map the object \var{key} to the value \var{v}. Returns \code{-1} on
349 failure. This is the equivalent of the Python statement
350 \samp{\var{o}[\var{key}] = \var{v}}.
351\end{cfuncdesc}
352
353
354\begin{cfuncdesc}{int}{PyObject_DelItem}{PyObject *o, PyObject *key}
355 Delete the mapping for \var{key} from \var{o}. Returns \code{-1} on
356 failure. This is the equivalent of the Python statement \samp{del
357 \var{o}[\var{key}]}.
358\end{cfuncdesc}
359
360\begin{cfuncdesc}{int}{PyObject_AsFileDescriptor}{PyObject *o}
361 Derives a file-descriptor from a Python object. If the object is an
362 integer or long integer, its value is returned. If not, the
363 object's \method{fileno()} method is called if it exists; the method
364 must return an integer or long integer, which is returned as the
365 file descriptor value. Returns \code{-1} on failure.
366\end{cfuncdesc}
367
368\begin{cfuncdesc}{PyObject*}{PyObject_Dir}{PyObject *o}
369 This is equivalent to the Python expression \samp{dir(\var{o})},
370 returning a (possibly empty) list of strings appropriate for the
371 object argument, or \NULL{} if there was an error. If the argument
372 is \NULL, this is like the Python \samp{dir()}, returning the names
373 of the current locals; in this case, if no execution frame is active
374 then \NULL{} is returned but \cfunction{PyErr_Occurred()} will
375 return false.
376\end{cfuncdesc}
377
Fred Drake314bae52002-03-11 18:46:29 +0000378\begin{cfuncdesc}{PyObject*}{PyObject_GetIter}{PyObject *o}
379 This is equivalent to the Python expression \samp{iter(\var{o})}.
380 It returns a new iterator for the object argument, or the object
381 itself if the object is already an iterator. Raises
382 \exception{TypeError} and returns \NULL{} if the object cannot be
383 iterated.
384\end{cfuncdesc}
385
Fred Drake3adf79e2001-10-12 19:01:43 +0000386
387\section{Number Protocol \label{number}}
388
389\begin{cfuncdesc}{int}{PyNumber_Check}{PyObject *o}
390 Returns \code{1} if the object \var{o} provides numeric protocols,
391 and false otherwise. This function always succeeds.
392\end{cfuncdesc}
393
394
395\begin{cfuncdesc}{PyObject*}{PyNumber_Add}{PyObject *o1, PyObject *o2}
396 Returns the result of adding \var{o1} and \var{o2}, or \NULL{} on
397 failure. This is the equivalent of the Python expression
398 \samp{\var{o1} + \var{o2}}.
399\end{cfuncdesc}
400
401
402\begin{cfuncdesc}{PyObject*}{PyNumber_Subtract}{PyObject *o1, PyObject *o2}
403 Returns the result of subtracting \var{o2} from \var{o1}, or \NULL{}
404 on failure. This is the equivalent of the Python expression
405 \samp{\var{o1} - \var{o2}}.
406\end{cfuncdesc}
407
408
409\begin{cfuncdesc}{PyObject*}{PyNumber_Multiply}{PyObject *o1, PyObject *o2}
410 Returns the result of multiplying \var{o1} and \var{o2}, or \NULL{}
411 on failure. This is the equivalent of the Python expression
412 \samp{\var{o1} * \var{o2}}.
413\end{cfuncdesc}
414
415
416\begin{cfuncdesc}{PyObject*}{PyNumber_Divide}{PyObject *o1, PyObject *o2}
417 Returns the result of dividing \var{o1} by \var{o2}, or \NULL{} on
418 failure. This is the equivalent of the Python expression
419 \samp{\var{o1} / \var{o2}}.
420\end{cfuncdesc}
421
422
423\begin{cfuncdesc}{PyObject*}{PyNumber_FloorDivide}{PyObject *o1, PyObject *o2}
424 Return the floor of \var{o1} divided by \var{o2}, or \NULL{} on
425 failure. This is equivalent to the ``classic'' division of
426 integers.
427 \versionadded{2.2}
428\end{cfuncdesc}
429
430
431\begin{cfuncdesc}{PyObject*}{PyNumber_TrueDivide}{PyObject *o1, PyObject *o2}
432 Return a reasonable approximation for the mathematical value of
433 \var{o1} divided by \var{o2}, or \NULL{} on failure. The return
434 value is ``approximate'' because binary floating point numbers are
435 approximate; it is not possible to represent all real numbers in
436 base two. This function can return a floating point value when
437 passed two integers.
438 \versionadded{2.2}
439\end{cfuncdesc}
440
441
442\begin{cfuncdesc}{PyObject*}{PyNumber_Remainder}{PyObject *o1, PyObject *o2}
443 Returns the remainder of dividing \var{o1} by \var{o2}, or \NULL{}
444 on failure. This is the equivalent of the Python expression
445 \samp{\var{o1} \%\ \var{o2}}.
446\end{cfuncdesc}
447
448
449\begin{cfuncdesc}{PyObject*}{PyNumber_Divmod}{PyObject *o1, PyObject *o2}
450 See the built-in function \function{divmod()}\bifuncindex{divmod}.
451 Returns \NULL{} on failure. This is the equivalent of the Python
452 expression \samp{divmod(\var{o1}, \var{o2})}.
453\end{cfuncdesc}
454
455
456\begin{cfuncdesc}{PyObject*}{PyNumber_Power}{PyObject *o1,
457 PyObject *o2, PyObject *o3}
458 See the built-in function \function{pow()}\bifuncindex{pow}.
459 Returns \NULL{} on failure. This is the equivalent of the Python
460 expression \samp{pow(\var{o1}, \var{o2}, \var{o3})}, where \var{o3}
461 is optional. If \var{o3} is to be ignored, pass \cdata{Py_None} in
462 its place (passing \NULL{} for \var{o3} would cause an illegal
463 memory access).
464\end{cfuncdesc}
465
466
467\begin{cfuncdesc}{PyObject*}{PyNumber_Negative}{PyObject *o}
468 Returns the negation of \var{o} on success, or \NULL{} on failure.
469 This is the equivalent of the Python expression \samp{-\var{o}}.
470\end{cfuncdesc}
471
472
473\begin{cfuncdesc}{PyObject*}{PyNumber_Positive}{PyObject *o}
474 Returns \var{o} on success, or \NULL{} on failure. This is the
475 equivalent of the Python expression \samp{+\var{o}}.
476\end{cfuncdesc}
477
478
479\begin{cfuncdesc}{PyObject*}{PyNumber_Absolute}{PyObject *o}
480 Returns the absolute value of \var{o}, or \NULL{} on failure. This
481 is the equivalent of the Python expression \samp{abs(\var{o})}.
482 \bifuncindex{abs}
483\end{cfuncdesc}
484
485
486\begin{cfuncdesc}{PyObject*}{PyNumber_Invert}{PyObject *o}
487 Returns the bitwise negation of \var{o} on success, or \NULL{} on
488 failure. This is the equivalent of the Python expression
489 \samp{\~\var{o}}.
490\end{cfuncdesc}
491
492
493\begin{cfuncdesc}{PyObject*}{PyNumber_Lshift}{PyObject *o1, PyObject *o2}
494 Returns the result of left shifting \var{o1} by \var{o2} on success,
495 or \NULL{} on failure. This is the equivalent of the Python
496 expression \samp{\var{o1} <\code{<} \var{o2}}.
497\end{cfuncdesc}
498
499
500\begin{cfuncdesc}{PyObject*}{PyNumber_Rshift}{PyObject *o1, PyObject *o2}
501 Returns the result of right shifting \var{o1} by \var{o2} on
502 success, or \NULL{} on failure. This is the equivalent of the
503 Python expression \samp{\var{o1} >\code{>} \var{o2}}.
504\end{cfuncdesc}
505
506
507\begin{cfuncdesc}{PyObject*}{PyNumber_And}{PyObject *o1, PyObject *o2}
508 Returns the ``bitwise and'' of \var{o2} and \var{o2} on success and
509 \NULL{} on failure. This is the equivalent of the Python expression
510 \samp{\var{o1} \&\ \var{o2}}.
511\end{cfuncdesc}
512
513
514\begin{cfuncdesc}{PyObject*}{PyNumber_Xor}{PyObject *o1, PyObject *o2}
515 Returns the ``bitwise exclusive or'' of \var{o1} by \var{o2} on
516 success, or \NULL{} on failure. This is the equivalent of the
517 Python expression \samp{\var{o1} \textasciicircum{} \var{o2}}.
518\end{cfuncdesc}
519
520\begin{cfuncdesc}{PyObject*}{PyNumber_Or}{PyObject *o1, PyObject *o2}
521 Returns the ``bitwise or'' of \var{o1} and \var{o2} on success, or
522 \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_InPlaceAdd}{PyObject *o1, PyObject *o2}
528 Returns the result of adding \var{o1} and \var{o2}, or \NULL{} on
529 failure. The operation is done \emph{in-place} when \var{o1}
530 supports it. This is the equivalent of the Python statement
531 \samp{\var{o1} += \var{o2}}.
532\end{cfuncdesc}
533
534
535\begin{cfuncdesc}{PyObject*}{PyNumber_InPlaceSubtract}{PyObject *o1,
536 PyObject *o2}
537 Returns the result of subtracting \var{o2} from \var{o1}, or \NULL{}
538 on failure. The operation is done \emph{in-place} when \var{o1}
539 supports it. This is the equivalent of the Python statement
540 \samp{\var{o1} -= \var{o2}}.
541\end{cfuncdesc}
542
543
544\begin{cfuncdesc}{PyObject*}{PyNumber_InPlaceMultiply}{PyObject *o1,
545 PyObject *o2}
546 Returns the result of multiplying \var{o1} and \var{o2}, or \NULL{}
547 on failure. The operation is done \emph{in-place} when \var{o1}
548 supports it. This is the equivalent of the Python statement
549 \samp{\var{o1} *= \var{o2}}.
550\end{cfuncdesc}
551
552
553\begin{cfuncdesc}{PyObject*}{PyNumber_InPlaceDivide}{PyObject *o1,
554 PyObject *o2}
555 Returns the result of dividing \var{o1} by \var{o2}, or \NULL{} on
556 failure. The operation is done \emph{in-place} when \var{o1}
557 supports it. This is the equivalent of the Python statement
558 \samp{\var{o1} /= \var{o2}}.
559\end{cfuncdesc}
560
561
562\begin{cfuncdesc}{PyObject*}{PyNumber_InPlaceFloorDivide}{PyObject *o1,
563 PyObject *o2}
564 Returns the mathematical of dividing \var{o1} by \var{o2}, or
565 \NULL{} on failure. The operation is done \emph{in-place} when
566 \var{o1} supports it. This is the equivalent of the Python
567 statement \samp{\var{o1} //= \var{o2}}.
568 \versionadded{2.2}
569\end{cfuncdesc}
570
571
572\begin{cfuncdesc}{PyObject*}{PyNumber_InPlaceTrueDivide}{PyObject *o1,
573 PyObject *o2}
574 Return a reasonable approximation for the mathematical value of
575 \var{o1} divided by \var{o2}, or \NULL{} on failure. The return
576 value is ``approximate'' because binary floating point numbers are
577 approximate; it is not possible to represent all real numbers in
578 base two. This function can return a floating point value when
579 passed two integers. The operation is done \emph{in-place} when
580 \var{o1} supports it.
581 \versionadded{2.2}
582\end{cfuncdesc}
583
584
585\begin{cfuncdesc}{PyObject*}{PyNumber_InPlaceRemainder}{PyObject *o1,
586 PyObject *o2}
587 Returns the remainder of dividing \var{o1} by \var{o2}, or \NULL{}
588 on failure. The operation is done \emph{in-place} when \var{o1}
589 supports it. This is the equivalent of the Python statement
590 \samp{\var{o1} \%= \var{o2}}.
591\end{cfuncdesc}
592
593
594\begin{cfuncdesc}{PyObject*}{PyNumber_InPlacePower}{PyObject *o1,
595 PyObject *o2, PyObject *o3}
596 See the built-in function \function{pow()}.\bifuncindex{pow}
597 Returns \NULL{} on failure. The operation is done \emph{in-place}
598 when \var{o1} supports it. This is the equivalent of the Python
599 statement \samp{\var{o1} **= \var{o2}} when o3 is \cdata{Py_None},
600 or an in-place variant of \samp{pow(\var{o1}, \var{o2}, \var{o3})}
601 otherwise. If \var{o3} is to be ignored, pass \cdata{Py_None} in its
602 place (passing \NULL{} for \var{o3} would cause an illegal memory
603 access).
604\end{cfuncdesc}
605
606\begin{cfuncdesc}{PyObject*}{PyNumber_InPlaceLshift}{PyObject *o1,
607 PyObject *o2}
608 Returns the result of left shifting \var{o1} by \var{o2} on success,
609 or \NULL{} on failure. The operation is done \emph{in-place} when
610 \var{o1} supports it. This is the equivalent of the Python
611 statement \samp{\var{o1} <\code{<=} \var{o2}}.
612\end{cfuncdesc}
613
614
615\begin{cfuncdesc}{PyObject*}{PyNumber_InPlaceRshift}{PyObject *o1,
616 PyObject *o2}
617 Returns the result of right shifting \var{o1} by \var{o2} on
618 success, or \NULL{} on failure. The operation is done
619 \emph{in-place} when \var{o1} supports it. This is the equivalent
620 of the Python statement \samp{\var{o1} >\code{>=} \var{o2}}.
621\end{cfuncdesc}
622
623
624\begin{cfuncdesc}{PyObject*}{PyNumber_InPlaceAnd}{PyObject *o1, PyObject *o2}
625 Returns the ``bitwise and'' of \var{o1} and \var{o2} on success and
626 \NULL{} on failure. The operation is done \emph{in-place} when
627 \var{o1} supports it. This is the equivalent of the Python
628 statement \samp{\var{o1} \&= \var{o2}}.
629\end{cfuncdesc}
630
631
632\begin{cfuncdesc}{PyObject*}{PyNumber_InPlaceXor}{PyObject *o1, PyObject *o2}
633 Returns the ``bitwise exclusive or'' of \var{o1} by \var{o2} on
634 success, or \NULL{} on failure. The operation is done
635 \emph{in-place} when \var{o1} supports it. This is the equivalent
636 of the Python statement \samp{\var{o1} \textasciicircum= \var{o2}}.
637\end{cfuncdesc}
638
639\begin{cfuncdesc}{PyObject*}{PyNumber_InPlaceOr}{PyObject *o1, PyObject *o2}
640 Returns the ``bitwise or'' of \var{o1} and \var{o2} on success, or
641 \NULL{} on failure. The operation is done \emph{in-place} when
642 \var{o1} supports it. This is the equivalent of the Python
643 statement \samp{\var{o1} |= \var{o2}}.
644\end{cfuncdesc}
645
646\begin{cfuncdesc}{int}{PyNumber_Coerce}{PyObject **p1, PyObject **p2}
647 This function takes the addresses of two variables of type
648 \ctype{PyObject*}. If the objects pointed to by \code{*\var{p1}}
649 and \code{*\var{p2}} have the same type, increment their reference
650 count and return \code{0} (success). If the objects can be converted
651 to a common numeric type, replace \code{*p1} and \code{*p2} by their
652 converted value (with 'new' reference counts), and return \code{0}.
653 If no conversion is possible, or if some other error occurs, return
654 \code{-1} (failure) and don't increment the reference counts. The
655 call \code{PyNumber_Coerce(\&o1, \&o2)} is equivalent to the Python
656 statement \samp{\var{o1}, \var{o2} = coerce(\var{o1}, \var{o2})}.
657 \bifuncindex{coerce}
658\end{cfuncdesc}
659
660\begin{cfuncdesc}{PyObject*}{PyNumber_Int}{PyObject *o}
661 Returns the \var{o} converted to an integer object on success, or
Walter Dörwaldf1715402002-11-19 20:49:15 +0000662 \NULL{} on failure. If the argument is outside the integer range
663 a long object will be returned instead. This is the equivalent
664 of the Python expression \samp{int(\var{o})}.\bifuncindex{int}
Fred Drake3adf79e2001-10-12 19:01:43 +0000665\end{cfuncdesc}
666
667\begin{cfuncdesc}{PyObject*}{PyNumber_Long}{PyObject *o}
668 Returns the \var{o} converted to a long integer object on success,
669 or \NULL{} on failure. This is the equivalent of the Python
670 expression \samp{long(\var{o})}.\bifuncindex{long}
671\end{cfuncdesc}
672
673\begin{cfuncdesc}{PyObject*}{PyNumber_Float}{PyObject *o}
674 Returns the \var{o} converted to a float object on success, or
675 \NULL{} on failure. This is the equivalent of the Python expression
676 \samp{float(\var{o})}.\bifuncindex{float}
677\end{cfuncdesc}
678
679
680\section{Sequence Protocol \label{sequence}}
681
682\begin{cfuncdesc}{int}{PySequence_Check}{PyObject *o}
683 Return \code{1} if the object provides sequence protocol, and
684 \code{0} otherwise. This function always succeeds.
685\end{cfuncdesc}
686
687\begin{cfuncdesc}{int}{PySequence_Size}{PyObject *o}
688 Returns the number of objects in sequence \var{o} on success, and
689 \code{-1} on failure. For objects that do not provide sequence
690 protocol, this is equivalent to the Python expression
691 \samp{len(\var{o})}.\bifuncindex{len}
692\end{cfuncdesc}
693
694\begin{cfuncdesc}{int}{PySequence_Length}{PyObject *o}
695 Alternate name for \cfunction{PySequence_Size()}.
696\end{cfuncdesc}
697
698\begin{cfuncdesc}{PyObject*}{PySequence_Concat}{PyObject *o1, PyObject *o2}
699 Return the concatenation of \var{o1} and \var{o2} on success, and
700 \NULL{} on failure. This is the equivalent of the Python
701 expression \samp{\var{o1} + \var{o2}}.
702\end{cfuncdesc}
703
704
705\begin{cfuncdesc}{PyObject*}{PySequence_Repeat}{PyObject *o, int count}
706 Return the result of repeating sequence object \var{o} \var{count}
707 times, or \NULL{} on failure. This is the equivalent of the Python
708 expression \samp{\var{o} * \var{count}}.
709\end{cfuncdesc}
710
711\begin{cfuncdesc}{PyObject*}{PySequence_InPlaceConcat}{PyObject *o1,
712 PyObject *o2}
713 Return the concatenation of \var{o1} and \var{o2} on success, and
714 \NULL{} on failure. The operation is done \emph{in-place} when
715 \var{o1} supports it. This is the equivalent of the Python
716 expression \samp{\var{o1} += \var{o2}}.
717\end{cfuncdesc}
718
719
720\begin{cfuncdesc}{PyObject*}{PySequence_InPlaceRepeat}{PyObject *o, int count}
721 Return the result of repeating sequence object \var{o} \var{count}
722 times, or \NULL{} on failure. The operation is done \emph{in-place}
723 when \var{o} supports it. This is the equivalent of the Python
724 expression \samp{\var{o} *= \var{count}}.
725\end{cfuncdesc}
726
727
728\begin{cfuncdesc}{PyObject*}{PySequence_GetItem}{PyObject *o, int i}
729 Return the \var{i}th element of \var{o}, or \NULL{} on failure.
730 This is the equivalent of the Python expression
731 \samp{\var{o}[\var{i}]}.
732\end{cfuncdesc}
733
734
735\begin{cfuncdesc}{PyObject*}{PySequence_GetSlice}{PyObject *o, int i1, int i2}
736 Return the slice of sequence object \var{o} between \var{i1} and
737 \var{i2}, or \NULL{} on failure. This is the equivalent of the
738 Python expression \samp{\var{o}[\var{i1}:\var{i2}]}.
739\end{cfuncdesc}
740
741
742\begin{cfuncdesc}{int}{PySequence_SetItem}{PyObject *o, int i, PyObject *v}
743 Assign object \var{v} to the \var{i}th element of \var{o}. Returns
744 \code{-1} on failure. This is the equivalent of the Python
745 statement \samp{\var{o}[\var{i}] = \var{v}}.
746\end{cfuncdesc}
747
748\begin{cfuncdesc}{int}{PySequence_DelItem}{PyObject *o, int i}
749 Delete the \var{i}th element of object \var{o}. Returns \code{-1}
750 on failure. This is the equivalent of the Python statement
751 \samp{del \var{o}[\var{i}]}.
752\end{cfuncdesc}
753
754\begin{cfuncdesc}{int}{PySequence_SetSlice}{PyObject *o, int i1,
755 int i2, PyObject *v}
756 Assign the sequence object \var{v} to the slice in sequence object
757 \var{o} from \var{i1} to \var{i2}. This is the equivalent of the
758 Python statement \samp{\var{o}[\var{i1}:\var{i2}] = \var{v}}.
759\end{cfuncdesc}
760
761\begin{cfuncdesc}{int}{PySequence_DelSlice}{PyObject *o, int i1, int i2}
762 Delete the slice in sequence object \var{o} from \var{i1} to
763 \var{i2}. Returns \code{-1} on failure. This is the equivalent of
764 the Python statement \samp{del \var{o}[\var{i1}:\var{i2}]}.
765\end{cfuncdesc}
766
767\begin{cfuncdesc}{PyObject*}{PySequence_Tuple}{PyObject *o}
768 Returns the \var{o} as a tuple on success, and \NULL{} on failure.
769 This is equivalent to the Python expression \samp{tuple(\var{o})}.
770 \bifuncindex{tuple}
771\end{cfuncdesc}
772
773\begin{cfuncdesc}{int}{PySequence_Count}{PyObject *o, PyObject *value}
774 Return the number of occurrences of \var{value} in \var{o}, that is,
775 return the number of keys for which \code{\var{o}[\var{key}] ==
776 \var{value}}. On failure, return \code{-1}. This is equivalent to
777 the Python expression \samp{\var{o}.count(\var{value})}.
778\end{cfuncdesc}
779
780\begin{cfuncdesc}{int}{PySequence_Contains}{PyObject *o, PyObject *value}
781 Determine if \var{o} contains \var{value}. If an item in \var{o} is
782 equal to \var{value}, return \code{1}, otherwise return \code{0}.
783 On error, return \code{-1}. This is equivalent to the Python
784 expression \samp{\var{value} in \var{o}}.
785\end{cfuncdesc}
786
787\begin{cfuncdesc}{int}{PySequence_Index}{PyObject *o, PyObject *value}
788 Return the first index \var{i} for which \code{\var{o}[\var{i}] ==
789 \var{value}}. On error, return \code{-1}. This is equivalent to
790 the Python expression \samp{\var{o}.index(\var{value})}.
791\end{cfuncdesc}
792
793\begin{cfuncdesc}{PyObject*}{PySequence_List}{PyObject *o}
794 Return a list object with the same contents as the arbitrary
795 sequence \var{o}. The returned list is guaranteed to be new.
796\end{cfuncdesc}
797
798\begin{cfuncdesc}{PyObject*}{PySequence_Tuple}{PyObject *o}
799 Return a tuple object with the same contents as the arbitrary
800 sequence \var{o}. If \var{o} is a tuple, a new reference will be
801 returned, otherwise a tuple will be constructed with the appropriate
802 contents.
803\end{cfuncdesc}
804
805\begin{cfuncdesc}{PyObject*}{PySequence_Fast}{PyObject *o, const char *m}
806 Returns the sequence \var{o} as a tuple, unless it is already a
807 tuple or list, in which case \var{o} is returned. Use
808 \cfunction{PySequence_Fast_GET_ITEM()} to access the members of the
809 result. Returns \NULL{} on failure. If the object is not a
810 sequence, raises \exception{TypeError} with \var{m} as the message
811 text.
812\end{cfuncdesc}
813
814\begin{cfuncdesc}{PyObject*}{PySequence_Fast_GET_ITEM}{PyObject *o, int i}
815 Return the \var{i}th element of \var{o}, assuming that \var{o} was
Fred Drakec37b65e2001-11-28 07:26:15 +0000816 returned by \cfunction{PySequence_Fast()}, \var{o} is not \NULL,
Tim Peters1fc240e2001-10-26 05:06:50 +0000817 and that \var{i} is within bounds.
818\end{cfuncdesc}
819
Martin v. Löwis01f94bd2002-05-08 08:44:21 +0000820\begin{cfuncdesc}{PyObject*}{PySequence_ITEM}{PyObject *o, int i}
821 Return the \var{i}th element of \var{o} or \NULL on failure.
822 Macro form of \cfunction{PySequence_GetItem()} but without checking
823 that \cfunction{PySequence_Check(\var{o})} is true and without
Fred Drake86228e42002-05-23 16:02:28 +0000824 adjustment for negative indices.
825 \versionadded{2.3}
Martin v. Löwis01f94bd2002-05-08 08:44:21 +0000826\end{cfuncdesc}
827
Tim Peters1fc240e2001-10-26 05:06:50 +0000828\begin{cfuncdesc}{int}{PySequence_Fast_GET_SIZE}{PyObject *o}
829 Returns the length of \var{o}, assuming that \var{o} was
830 returned by \cfunction{PySequence_Fast()} and that \var{o} is
Fred Drakec37b65e2001-11-28 07:26:15 +0000831 not \NULL. The size can also be gotten by calling
Tim Peters1fc240e2001-10-26 05:06:50 +0000832 \cfunction{PySequence_Size()} on \var{o}, but
833 \cfunction{PySequence_Fast_GET_SIZE()} is faster because it can
834 assume \var{o} is a list or tuple.
Fred Drake3adf79e2001-10-12 19:01:43 +0000835\end{cfuncdesc}
836
837
838\section{Mapping Protocol \label{mapping}}
839
840\begin{cfuncdesc}{int}{PyMapping_Check}{PyObject *o}
841 Return \code{1} if the object provides mapping protocol, and
842 \code{0} otherwise. This function always succeeds.
843\end{cfuncdesc}
844
845
846\begin{cfuncdesc}{int}{PyMapping_Length}{PyObject *o}
847 Returns the number of keys in object \var{o} on success, and
848 \code{-1} on failure. For objects that do not provide mapping
849 protocol, this is equivalent to the Python expression
850 \samp{len(\var{o})}.\bifuncindex{len}
851\end{cfuncdesc}
852
853
854\begin{cfuncdesc}{int}{PyMapping_DelItemString}{PyObject *o, char *key}
855 Remove the mapping for object \var{key} from the object \var{o}.
856 Return \code{-1} on failure. This is equivalent to the Python
857 statement \samp{del \var{o}[\var{key}]}.
858\end{cfuncdesc}
859
860
861\begin{cfuncdesc}{int}{PyMapping_DelItem}{PyObject *o, PyObject *key}
862 Remove the mapping for object \var{key} from the object \var{o}.
863 Return \code{-1} on failure. This is equivalent to the Python
864 statement \samp{del \var{o}[\var{key}]}.
865\end{cfuncdesc}
866
867
868\begin{cfuncdesc}{int}{PyMapping_HasKeyString}{PyObject *o, char *key}
869 On success, return \code{1} if the mapping object has the key
870 \var{key} and \code{0} otherwise. This is equivalent to the Python
871 expression \samp{\var{o}.has_key(\var{key})}. This function always
872 succeeds.
873\end{cfuncdesc}
874
875
876\begin{cfuncdesc}{int}{PyMapping_HasKey}{PyObject *o, PyObject *key}
877 Return \code{1} if the mapping object has the key \var{key} and
878 \code{0} otherwise. This is equivalent to the Python expression
879 \samp{\var{o}.has_key(\var{key})}. This function always succeeds.
880\end{cfuncdesc}
881
882
883\begin{cfuncdesc}{PyObject*}{PyMapping_Keys}{PyObject *o}
884 On success, return a list of the keys in object \var{o}. On
885 failure, return \NULL. This is equivalent to the Python expression
886 \samp{\var{o}.keys()}.
887\end{cfuncdesc}
888
889
890\begin{cfuncdesc}{PyObject*}{PyMapping_Values}{PyObject *o}
891 On success, return a list of the values in object \var{o}. On
892 failure, return \NULL. This is equivalent to the Python expression
893 \samp{\var{o}.values()}.
894\end{cfuncdesc}
895
896
897\begin{cfuncdesc}{PyObject*}{PyMapping_Items}{PyObject *o}
898 On success, return a list of the items in object \var{o}, where each
899 item is a tuple containing a key-value pair. On failure, return
900 \NULL. This is equivalent to the Python expression
901 \samp{\var{o}.items()}.
902\end{cfuncdesc}
903
904
905\begin{cfuncdesc}{PyObject*}{PyMapping_GetItemString}{PyObject *o, char *key}
906 Return element of \var{o} corresponding to the object \var{key} or
907 \NULL{} on failure. This is the equivalent of the Python expression
908 \samp{\var{o}[\var{key}]}.
909\end{cfuncdesc}
910
911\begin{cfuncdesc}{int}{PyMapping_SetItemString}{PyObject *o, char *key,
912 PyObject *v}
913 Map the object \var{key} to the value \var{v} in object \var{o}.
914 Returns \code{-1} on failure. This is the equivalent of the Python
915 statement \samp{\var{o}[\var{key}] = \var{v}}.
916\end{cfuncdesc}
917
918
919\section{Iterator Protocol \label{iterator}}
920
921\versionadded{2.2}
922
923There are only a couple of functions specifically for working with
924iterators.
925
926\begin{cfuncdesc}{int}{PyIter_Check}{PyObject *o}
927 Return true if the object \var{o} supports the iterator protocol.
928\end{cfuncdesc}
929
930\begin{cfuncdesc}{PyObject*}{PyIter_Next}{PyObject *o}
931 Return the next value from the iteration \var{o}. If the object is
932 an iterator, this retrieves the next value from the iteration, and
933 returns \NULL{} with no exception set if there are no remaining
934 items. If the object is not an iterator, \exception{TypeError} is
935 raised, or if there is an error in retrieving the item, returns
936 \NULL{} and passes along the exception.
937\end{cfuncdesc}
938
939To write a loop which iterates over an iterator, the C code should
940look something like this:
941
942\begin{verbatim}
Fred Drake314bae52002-03-11 18:46:29 +0000943PyObject *iterator = PyObject_GetIter(obj);
Fred Drake3adf79e2001-10-12 19:01:43 +0000944PyObject *item;
945
Fred Drake314bae52002-03-11 18:46:29 +0000946if (iterator == NULL) {
947 /* propagate error */
948}
949
950while (item = PyIter_Next(iterator)) {
Fred Drake3adf79e2001-10-12 19:01:43 +0000951 /* do something with item */
Fred Drake8b8fe282001-12-26 16:53:48 +0000952 ...
953 /* release reference when done */
954 Py_DECREF(item);
Fred Drake3adf79e2001-10-12 19:01:43 +0000955}
Fred Drake314bae52002-03-11 18:46:29 +0000956
957Py_DECREF(iterator);
958
Fred Drake3adf79e2001-10-12 19:01:43 +0000959if (PyErr_Occurred()) {
Fred Drake314bae52002-03-11 18:46:29 +0000960 /* propagate error */
Fred Drake3adf79e2001-10-12 19:01:43 +0000961}
962else {
963 /* continue doing useful work */
964}
Fred Drake8b8fe282001-12-26 16:53:48 +0000965\end{verbatim}
966
Jeremy Hylton89c3a222001-11-09 21:59:42 +0000967
Fred Drake94ead572001-11-09 23:34:26 +0000968\section{Buffer Protocol \label{abstract-buffer}}
Jeremy Hylton89c3a222001-11-09 21:59:42 +0000969
970\begin{cfuncdesc}{int}{PyObject_AsCharBuffer}{PyObject *obj,
Fred Drake94ead572001-11-09 23:34:26 +0000971 const char **buffer,
972 int *buffer_len}
Jeremy Hylton89c3a222001-11-09 21:59:42 +0000973 Returns a pointer to a read-only memory location useable as character-
974 based input. The \var{obj} argument must support the single-segment
Fred Drake243ea712002-04-04 04:10:36 +0000975 character buffer interface. On success, returns \code{0}, sets
Thomas Hellerbfeeeee2001-11-12 07:46:31 +0000976 \var{buffer} to the memory location and \var{buffer_len} to the buffer
Fred Drake243ea712002-04-04 04:10:36 +0000977 length. Returns \code{-1} and sets a \exception{TypeError} on error.
Fred Drake94ead572001-11-09 23:34:26 +0000978 \versionadded{1.6}
Jeremy Hylton89c3a222001-11-09 21:59:42 +0000979\end{cfuncdesc}
980
981\begin{cfuncdesc}{int}{PyObject_AsReadBuffer}{PyObject *obj,
Fred Drake94ead572001-11-09 23:34:26 +0000982 const char **buffer,
983 int *buffer_len}
Jeremy Hylton89c3a222001-11-09 21:59:42 +0000984 Returns a pointer to a read-only memory location containing
985 arbitrary data. The \var{obj} argument must support the
986 single-segment readable buffer interface. On success, returns
Fred Drake243ea712002-04-04 04:10:36 +0000987 \code{0}, sets \var{buffer} to the memory location and \var{buffer_len}
988 to the buffer length. Returns \code{-1} and sets a
Jeremy Hylton89c3a222001-11-09 21:59:42 +0000989 \exception{TypeError} on error.
Fred Drake94ead572001-11-09 23:34:26 +0000990 \versionadded{1.6}
Jeremy Hylton89c3a222001-11-09 21:59:42 +0000991\end{cfuncdesc}
992
993\begin{cfuncdesc}{int}{PyObject_CheckReadBuffer}{PyObject *o}
994 Returns \code{1} if \var{o} supports the single-segment readable
995 buffer interface. Otherwise returns \code{0}.
Fred Drake94ead572001-11-09 23:34:26 +0000996 \versionadded{2.2}
Fred Drake8b8fe282001-12-26 16:53:48 +0000997\end{cfuncdesc}
Jeremy Hylton89c3a222001-11-09 21:59:42 +0000998
999\begin{cfuncdesc}{int}{PyObject_AsWriteBuffer}{PyObject *obj,
Raymond Hettinger51306902002-09-08 04:39:28 +00001000 char **buffer,
Fred Drake94ead572001-11-09 23:34:26 +00001001 int *buffer_len}
Jeremy Hylton89c3a222001-11-09 21:59:42 +00001002 Returns a pointer to a writeable memory location. The \var{obj}
1003 argument must support the single-segment, character buffer
Fred Drake243ea712002-04-04 04:10:36 +00001004 interface. On success, returns \code{0}, sets \var{buffer} to the
Thomas Hellerbfeeeee2001-11-12 07:46:31 +00001005 memory location and \var{buffer_len} to the buffer length. Returns
Fred Drake243ea712002-04-04 04:10:36 +00001006 \code{-1} and sets a \exception{TypeError} on error.
Fred Drake94ead572001-11-09 23:34:26 +00001007 \versionadded{1.6}
Jeremy Hylton89c3a222001-11-09 21:59:42 +00001008\end{cfuncdesc}