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