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