This commit was manufactured by cvs2svn to create tag 'r234c1'.
[python/dscho.git] / Doc / api / abstract.tex
blob941d14a470bf3821f8d1c7e1b2fac12ff6498e5c
1 \chapter{Abstract Objects Layer \label{abstract}}
3 The functions in this chapter interact with Python objects regardless
4 of their type, or with wide classes of object types (e.g. all
5 numerical types, or all sequence types). When used on object types
6 for which they do not apply, they will raise a Python exception.
9 \section{Object Protocol \label{object}}
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}
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}
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}
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}
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}
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}
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}
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}
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}
84 \begin{cfuncdesc}{PyObject*}{PyObject_RichCompare}{PyObject *o1,
85 PyObject *o2, int opid}
86 Compare the values of \var{o1} and \var{o2} using the operation
87 specified by \var{opid}, which must be one of
88 \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
100 \samp{\var{o1} op \var{o2}}, where \code{op} is the operator
101 corresponding to \var{opid}. Returns the value of the comparison on
102 success, or \NULL{} on failure.
103 \end{cfuncdesc}
105 \begin{cfuncdesc}{int}{PyObject_RichCompareBool}{PyObject *o1,
106 PyObject *o2, int opid}
107 Compare the values of \var{o1} and \var{o2} using the operation
108 specified by \var{opid}, which must be one of
109 \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
122 Python expression \samp{\var{o1} op \var{o2}}, where
123 \code{op} is the operator corresponding to \var{opid}.
124 \end{cfuncdesc}
126 \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}
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}
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}
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}
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
169 \samp{unicode(\var{o})}. Called by the
170 \function{unicode()}\bifuncindex{unicode} built-in function.
171 \end{cfuncdesc}
173 \begin{cfuncdesc}{int}{PyObject_IsInstance}{PyObject *inst, PyObject *cls}
174 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()}
178 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
184 --- the class relationship of the value of that attribute with
185 \var{cls} will be used to determine the result of this function.
186 \versionadded{2.1}
187 \versionchanged[Support for a tuple as the second argument added]{2.2}
188 \end{cfuncdesc}
190 Subclass determination is done in a fairly straightforward way, but
191 includes a wrinkle that implementors of extensions to the class system
192 may want to be aware of. If \class{A} and \class{B} are class
193 objects, \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
195 object, a more general mechanism is used to determine the class
196 relationship of the two objects. When testing if \var{B} is a
197 subclass 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
200 is searched in a depth-first fashion for \var{A} --- the presence of
201 the \member{__bases__} attribute is considered sufficient for this
202 determination.
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 \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.
214 \versionadded{2.1}
215 \versionchanged[Older versions of Python did not support a tuple
216 as the second argument]{2.3}
217 \end{cfuncdesc}
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}
227 \begin{cfuncdesc}{PyObject*}{PyObject_Call}{PyObject *callable_object,
228 PyObject *args,
229 PyObject *kw}
230 Call a callable Python object \var{callable_object}, with arguments
231 given by the tuple \var{args}, and named arguments given by the
232 dictionary \var{kw}. If no named arguments are needed, \var{kw} may
233 be \NULL{}. \var{args} must not be \NULL{}, use an empty tuple if
234 no arguments are needed. Returns the result of the call on success,
235 or \NULL{} on failure. This is the equivalent of the Python
236 expression \samp{apply(\var{callable_object}, \var{args}, \var{kw})}
237 or \samp{\var{callable_object}(*\var{args}, **\var{kw})}.
238 \bifuncindex{apply}
239 \versionadded{2.2}
240 \end{cfuncdesc}
243 \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}
254 \begin{cfuncdesc}{PyObject*}{PyObject_CallFunction}{PyObject *callable,
255 char *format, \moreargs}
256 Call a callable Python object \var{callable}, with a variable
257 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
261 equivalent of the Python expression \samp{apply(\var{callable},
262 \var{args})} or \samp{\var{callable}(*\var{args})}.
263 \bifuncindex{apply}
264 \end{cfuncdesc}
267 \begin{cfuncdesc}{PyObject*}{PyObject_CallMethod}{PyObject *o,
268 char *method, char *format,
269 \moreargs}
270 Call the method named \var{method} 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
275 the Python expression \samp{\var{o}.\var{method}(\var{args})}.
276 \end{cfuncdesc}
279 \begin{cfuncdesc}{PyObject*}{PyObject_CallFunctionObjArgs}{PyObject *callable,
280 \moreargs,
281 \code{NULL}}
282 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}
290 \begin{cfuncdesc}{PyObject*}{PyObject_CallMethodObjArgs}{PyObject *o,
291 PyObject *name,
292 \moreargs,
293 \code{NULL}}
294 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}
300 \end{cfuncdesc}
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}
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}}. On failure, return \code{-1}.
314 \end{cfuncdesc}
317 \begin{cfuncdesc}{int}{PyObject_Not}{PyObject *o}
318 Returns \code{0} if the object \var{o} is considered to be true, and
319 \code{1} otherwise. This is equivalent to the Python expression
320 \samp{not \var{o}}. On failure, return \code{-1}.
321 \end{cfuncdesc}
324 \begin{cfuncdesc}{PyObject*}{PyObject_Type}{PyObject *o}
325 When \var{o} is non-\NULL, returns a type object corresponding to
326 the object type of object \var{o}. On failure, raises
327 \exception{SystemError} and returns \NULL. This is equivalent to
328 the Python expression \code{type(\var{o})}.\bifuncindex{type}
329 This function increments the reference count of the return value.
330 There's really no reason to use this function instead of the
331 common expression \code{\var{o}->ob_type}, which returns a pointer
332 of type \ctype{PyTypeObject*}, except when the incremented reference
333 count is needed.
334 \end{cfuncdesc}
336 \begin{cfuncdesc}{int}{PyObject_TypeCheck}{PyObject *o, PyTypeObject *type}
337 Return true if the object \var{o} is of type \var{type} or a subtype
338 of \var{type}. Both parameters must be non-\NULL.
339 \versionadded{2.2}
340 \end{cfuncdesc}
342 \begin{cfuncdesc}{int}{PyObject_Length}{PyObject *o}
343 \cfuncline{int}{PyObject_Size}{PyObject *o}
344 Return the length of object \var{o}. If the object \var{o} provides
345 either the sequence and mapping protocols, the sequence length is
346 returned. On error, \code{-1} is returned. This is the equivalent
347 to the Python expression \samp{len(\var{o})}.\bifuncindex{len}
348 \end{cfuncdesc}
351 \begin{cfuncdesc}{PyObject*}{PyObject_GetItem}{PyObject *o, PyObject *key}
352 Return element of \var{o} corresponding to the object \var{key} or
353 \NULL{} on failure. This is the equivalent of the Python expression
354 \samp{\var{o}[\var{key}]}.
355 \end{cfuncdesc}
358 \begin{cfuncdesc}{int}{PyObject_SetItem}{PyObject *o,
359 PyObject *key, PyObject *v}
360 Map the object \var{key} to the value \var{v}. Returns \code{-1} on
361 failure. This is the equivalent of the Python statement
362 \samp{\var{o}[\var{key}] = \var{v}}.
363 \end{cfuncdesc}
366 \begin{cfuncdesc}{int}{PyObject_DelItem}{PyObject *o, PyObject *key}
367 Delete the mapping for \var{key} from \var{o}. Returns \code{-1} on
368 failure. This is the equivalent of the Python statement \samp{del
369 \var{o}[\var{key}]}.
370 \end{cfuncdesc}
372 \begin{cfuncdesc}{int}{PyObject_AsFileDescriptor}{PyObject *o}
373 Derives a file-descriptor from a Python object. If the object is an
374 integer or long integer, its value is returned. If not, the
375 object's \method{fileno()} method is called if it exists; the method
376 must return an integer or long integer, which is returned as the
377 file descriptor value. Returns \code{-1} on failure.
378 \end{cfuncdesc}
380 \begin{cfuncdesc}{PyObject*}{PyObject_Dir}{PyObject *o}
381 This is equivalent to the Python expression \samp{dir(\var{o})},
382 returning a (possibly empty) list of strings appropriate for the
383 object argument, or \NULL{} if there was an error. If the argument
384 is \NULL, this is like the Python \samp{dir()}, returning the names
385 of the current locals; in this case, if no execution frame is active
386 then \NULL{} is returned but \cfunction{PyErr_Occurred()} will
387 return false.
388 \end{cfuncdesc}
390 \begin{cfuncdesc}{PyObject*}{PyObject_GetIter}{PyObject *o}
391 This is equivalent to the Python expression \samp{iter(\var{o})}.
392 It returns a new iterator for the object argument, or the object
393 itself if the object is already an iterator. Raises
394 \exception{TypeError} and returns \NULL{} if the object cannot be
395 iterated.
396 \end{cfuncdesc}
399 \section{Number Protocol \label{number}}
401 \begin{cfuncdesc}{int}{PyNumber_Check}{PyObject *o}
402 Returns \code{1} if the object \var{o} provides numeric protocols,
403 and false otherwise. This function always succeeds.
404 \end{cfuncdesc}
407 \begin{cfuncdesc}{PyObject*}{PyNumber_Add}{PyObject *o1, PyObject *o2}
408 Returns the result of adding \var{o1} and \var{o2}, or \NULL{} on
409 failure. This is the equivalent of the Python expression
410 \samp{\var{o1} + \var{o2}}.
411 \end{cfuncdesc}
414 \begin{cfuncdesc}{PyObject*}{PyNumber_Subtract}{PyObject *o1, PyObject *o2}
415 Returns the result of subtracting \var{o2} from \var{o1}, or \NULL{}
416 on failure. This is the equivalent of the Python expression
417 \samp{\var{o1} - \var{o2}}.
418 \end{cfuncdesc}
421 \begin{cfuncdesc}{PyObject*}{PyNumber_Multiply}{PyObject *o1, PyObject *o2}
422 Returns the result of multiplying \var{o1} and \var{o2}, or \NULL{}
423 on failure. This is the equivalent of the Python expression
424 \samp{\var{o1} * \var{o2}}.
425 \end{cfuncdesc}
428 \begin{cfuncdesc}{PyObject*}{PyNumber_Divide}{PyObject *o1, PyObject *o2}
429 Returns the result of dividing \var{o1} by \var{o2}, or \NULL{} on
430 failure. This is the equivalent of the Python expression
431 \samp{\var{o1} / \var{o2}}.
432 \end{cfuncdesc}
435 \begin{cfuncdesc}{PyObject*}{PyNumber_FloorDivide}{PyObject *o1, PyObject *o2}
436 Return the floor of \var{o1} divided by \var{o2}, or \NULL{} on
437 failure. This is equivalent to the ``classic'' division of
438 integers.
439 \versionadded{2.2}
440 \end{cfuncdesc}
443 \begin{cfuncdesc}{PyObject*}{PyNumber_TrueDivide}{PyObject *o1, PyObject *o2}
444 Return a reasonable approximation for the mathematical value of
445 \var{o1} divided by \var{o2}, or \NULL{} on failure. The return
446 value is ``approximate'' because binary floating point numbers are
447 approximate; it is not possible to represent all real numbers in
448 base two. This function can return a floating point value when
449 passed two integers.
450 \versionadded{2.2}
451 \end{cfuncdesc}
454 \begin{cfuncdesc}{PyObject*}{PyNumber_Remainder}{PyObject *o1, PyObject *o2}
455 Returns the remainder of dividing \var{o1} by \var{o2}, or \NULL{}
456 on failure. This is the equivalent of the Python expression
457 \samp{\var{o1} \%\ \var{o2}}.
458 \end{cfuncdesc}
461 \begin{cfuncdesc}{PyObject*}{PyNumber_Divmod}{PyObject *o1, PyObject *o2}
462 See the built-in function \function{divmod()}\bifuncindex{divmod}.
463 Returns \NULL{} on failure. This is the equivalent of the Python
464 expression \samp{divmod(\var{o1}, \var{o2})}.
465 \end{cfuncdesc}
468 \begin{cfuncdesc}{PyObject*}{PyNumber_Power}{PyObject *o1,
469 PyObject *o2, PyObject *o3}
470 See the built-in function \function{pow()}\bifuncindex{pow}.
471 Returns \NULL{} on failure. This is the equivalent of the Python
472 expression \samp{pow(\var{o1}, \var{o2}, \var{o3})}, where \var{o3}
473 is optional. If \var{o3} is to be ignored, pass \cdata{Py_None} in
474 its place (passing \NULL{} for \var{o3} would cause an illegal
475 memory access).
476 \end{cfuncdesc}
479 \begin{cfuncdesc}{PyObject*}{PyNumber_Negative}{PyObject *o}
480 Returns the negation of \var{o} on success, or \NULL{} on failure.
481 This is the equivalent of the Python expression \samp{-\var{o}}.
482 \end{cfuncdesc}
485 \begin{cfuncdesc}{PyObject*}{PyNumber_Positive}{PyObject *o}
486 Returns \var{o} on success, or \NULL{} on failure. This is the
487 equivalent of the Python expression \samp{+\var{o}}.
488 \end{cfuncdesc}
491 \begin{cfuncdesc}{PyObject*}{PyNumber_Absolute}{PyObject *o}
492 Returns the absolute value of \var{o}, or \NULL{} on failure. This
493 is the equivalent of the Python expression \samp{abs(\var{o})}.
494 \bifuncindex{abs}
495 \end{cfuncdesc}
498 \begin{cfuncdesc}{PyObject*}{PyNumber_Invert}{PyObject *o}
499 Returns the bitwise negation of \var{o} on success, or \NULL{} on
500 failure. This is the equivalent of the Python expression
501 \samp{\~\var{o}}.
502 \end{cfuncdesc}
505 \begin{cfuncdesc}{PyObject*}{PyNumber_Lshift}{PyObject *o1, PyObject *o2}
506 Returns the result of left shifting \var{o1} by \var{o2} on success,
507 or \NULL{} on failure. This is the equivalent of the Python
508 expression \samp{\var{o1} <\code{<} \var{o2}}.
509 \end{cfuncdesc}
512 \begin{cfuncdesc}{PyObject*}{PyNumber_Rshift}{PyObject *o1, PyObject *o2}
513 Returns the result of right shifting \var{o1} by \var{o2} on
514 success, or \NULL{} on failure. This is the equivalent of the
515 Python expression \samp{\var{o1} >\code{>} \var{o2}}.
516 \end{cfuncdesc}
519 \begin{cfuncdesc}{PyObject*}{PyNumber_And}{PyObject *o1, PyObject *o2}
520 Returns the ``bitwise and'' of \var{o1} and \var{o2} on success and
521 \NULL{} on failure. This is the equivalent of the Python expression
522 \samp{\var{o1} \&\ \var{o2}}.
523 \end{cfuncdesc}
526 \begin{cfuncdesc}{PyObject*}{PyNumber_Xor}{PyObject *o1, PyObject *o2}
527 Returns the ``bitwise exclusive or'' of \var{o1} by \var{o2} on
528 success, or \NULL{} on failure. This is the equivalent of the
529 Python expression \samp{\var{o1} \textasciicircum{} \var{o2}}.
530 \end{cfuncdesc}
532 \begin{cfuncdesc}{PyObject*}{PyNumber_Or}{PyObject *o1, PyObject *o2}
533 Returns the ``bitwise or'' of \var{o1} and \var{o2} on success, or
534 \NULL{} on failure. This is the equivalent of the Python expression
535 \samp{\var{o1} | \var{o2}}.
536 \end{cfuncdesc}
539 \begin{cfuncdesc}{PyObject*}{PyNumber_InPlaceAdd}{PyObject *o1, PyObject *o2}
540 Returns the result of adding \var{o1} and \var{o2}, or \NULL{} on
541 failure. The operation is done \emph{in-place} when \var{o1}
542 supports it. This is the equivalent of the Python statement
543 \samp{\var{o1} += \var{o2}}.
544 \end{cfuncdesc}
547 \begin{cfuncdesc}{PyObject*}{PyNumber_InPlaceSubtract}{PyObject *o1,
548 PyObject *o2}
549 Returns the result of subtracting \var{o2} from \var{o1}, or \NULL{}
550 on failure. The operation is done \emph{in-place} when \var{o1}
551 supports it. This is the equivalent of the Python statement
552 \samp{\var{o1} -= \var{o2}}.
553 \end{cfuncdesc}
556 \begin{cfuncdesc}{PyObject*}{PyNumber_InPlaceMultiply}{PyObject *o1,
557 PyObject *o2}
558 Returns the result of multiplying \var{o1} and \var{o2}, or \NULL{}
559 on failure. The operation is done \emph{in-place} when \var{o1}
560 supports it. This is the equivalent of the Python statement
561 \samp{\var{o1} *= \var{o2}}.
562 \end{cfuncdesc}
565 \begin{cfuncdesc}{PyObject*}{PyNumber_InPlaceDivide}{PyObject *o1,
566 PyObject *o2}
567 Returns the result of dividing \var{o1} by \var{o2}, or \NULL{} on
568 failure. The operation is done \emph{in-place} when \var{o1}
569 supports it. This is the equivalent of the Python statement
570 \samp{\var{o1} /= \var{o2}}.
571 \end{cfuncdesc}
574 \begin{cfuncdesc}{PyObject*}{PyNumber_InPlaceFloorDivide}{PyObject *o1,
575 PyObject *o2}
576 Returns the mathematical of dividing \var{o1} by \var{o2}, or
577 \NULL{} on failure. The operation is done \emph{in-place} when
578 \var{o1} supports it. This is the equivalent of the Python
579 statement \samp{\var{o1} //= \var{o2}}.
580 \versionadded{2.2}
581 \end{cfuncdesc}
584 \begin{cfuncdesc}{PyObject*}{PyNumber_InPlaceTrueDivide}{PyObject *o1,
585 PyObject *o2}
586 Return a reasonable approximation for the mathematical value of
587 \var{o1} divided by \var{o2}, or \NULL{} on failure. The return
588 value is ``approximate'' because binary floating point numbers are
589 approximate; it is not possible to represent all real numbers in
590 base two. This function can return a floating point value when
591 passed two integers. The operation is done \emph{in-place} when
592 \var{o1} supports it.
593 \versionadded{2.2}
594 \end{cfuncdesc}
597 \begin{cfuncdesc}{PyObject*}{PyNumber_InPlaceRemainder}{PyObject *o1,
598 PyObject *o2}
599 Returns the remainder of dividing \var{o1} by \var{o2}, or \NULL{}
600 on failure. The operation is done \emph{in-place} when \var{o1}
601 supports it. This is the equivalent of the Python statement
602 \samp{\var{o1} \%= \var{o2}}.
603 \end{cfuncdesc}
606 \begin{cfuncdesc}{PyObject*}{PyNumber_InPlacePower}{PyObject *o1,
607 PyObject *o2, PyObject *o3}
608 See the built-in function \function{pow()}.\bifuncindex{pow}
609 Returns \NULL{} on failure. The operation is done \emph{in-place}
610 when \var{o1} supports it. This is the equivalent of the Python
611 statement \samp{\var{o1} **= \var{o2}} when o3 is \cdata{Py_None},
612 or an in-place variant of \samp{pow(\var{o1}, \var{o2}, \var{o3})}
613 otherwise. If \var{o3} is to be ignored, pass \cdata{Py_None} in its
614 place (passing \NULL{} for \var{o3} would cause an illegal memory
615 access).
616 \end{cfuncdesc}
618 \begin{cfuncdesc}{PyObject*}{PyNumber_InPlaceLshift}{PyObject *o1,
619 PyObject *o2}
620 Returns the result of left shifting \var{o1} by \var{o2} on success,
621 or \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} <\code{<=} \var{o2}}.
624 \end{cfuncdesc}
627 \begin{cfuncdesc}{PyObject*}{PyNumber_InPlaceRshift}{PyObject *o1,
628 PyObject *o2}
629 Returns the result of right shifting \var{o1} by \var{o2} on
630 success, or \NULL{} on failure. The operation is done
631 \emph{in-place} when \var{o1} supports it. This is the equivalent
632 of the Python statement \samp{\var{o1} >\code{>=} \var{o2}}.
633 \end{cfuncdesc}
636 \begin{cfuncdesc}{PyObject*}{PyNumber_InPlaceAnd}{PyObject *o1, PyObject *o2}
637 Returns the ``bitwise and'' of \var{o1} and \var{o2} on success and
638 \NULL{} on failure. The operation is done \emph{in-place} when
639 \var{o1} supports it. This is the equivalent of the Python
640 statement \samp{\var{o1} \&= \var{o2}}.
641 \end{cfuncdesc}
644 \begin{cfuncdesc}{PyObject*}{PyNumber_InPlaceXor}{PyObject *o1, PyObject *o2}
645 Returns the ``bitwise exclusive or'' of \var{o1} by \var{o2} on
646 success, or \NULL{} on failure. The operation is done
647 \emph{in-place} when \var{o1} supports it. This is the equivalent
648 of the Python statement \samp{\var{o1} \textasciicircum= \var{o2}}.
649 \end{cfuncdesc}
651 \begin{cfuncdesc}{PyObject*}{PyNumber_InPlaceOr}{PyObject *o1, PyObject *o2}
652 Returns the ``bitwise or'' of \var{o1} and \var{o2} on success, or
653 \NULL{} on failure. The operation is done \emph{in-place} when
654 \var{o1} supports it. This is the equivalent of the Python
655 statement \samp{\var{o1} |= \var{o2}}.
656 \end{cfuncdesc}
658 \begin{cfuncdesc}{int}{PyNumber_Coerce}{PyObject **p1, PyObject **p2}
659 This function takes the addresses of two variables of type
660 \ctype{PyObject*}. If the objects pointed to by \code{*\var{p1}}
661 and \code{*\var{p2}} have the same type, increment their reference
662 count and return \code{0} (success). If the objects can be converted
663 to a common numeric type, replace \code{*p1} and \code{*p2} by their
664 converted value (with 'new' reference counts), and return \code{0}.
665 If no conversion is possible, or if some other error occurs, return
666 \code{-1} (failure) and don't increment the reference counts. The
667 call \code{PyNumber_Coerce(\&o1, \&o2)} is equivalent to the Python
668 statement \samp{\var{o1}, \var{o2} = coerce(\var{o1}, \var{o2})}.
669 \bifuncindex{coerce}
670 \end{cfuncdesc}
672 \begin{cfuncdesc}{PyObject*}{PyNumber_Int}{PyObject *o}
673 Returns the \var{o} converted to an integer object on success, or
674 \NULL{} on failure. If the argument is outside the integer range
675 a long object will be returned instead. This is the equivalent
676 of the Python expression \samp{int(\var{o})}.\bifuncindex{int}
677 \end{cfuncdesc}
679 \begin{cfuncdesc}{PyObject*}{PyNumber_Long}{PyObject *o}
680 Returns the \var{o} converted to a long integer object on success,
681 or \NULL{} on failure. This is the equivalent of the Python
682 expression \samp{long(\var{o})}.\bifuncindex{long}
683 \end{cfuncdesc}
685 \begin{cfuncdesc}{PyObject*}{PyNumber_Float}{PyObject *o}
686 Returns the \var{o} converted to a float object on success, or
687 \NULL{} on failure. This is the equivalent of the Python expression
688 \samp{float(\var{o})}.\bifuncindex{float}
689 \end{cfuncdesc}
692 \section{Sequence Protocol \label{sequence}}
694 \begin{cfuncdesc}{int}{PySequence_Check}{PyObject *o}
695 Return \code{1} if the object provides sequence protocol, and
696 \code{0} otherwise. This function always succeeds.
697 \end{cfuncdesc}
699 \begin{cfuncdesc}{int}{PySequence_Size}{PyObject *o}
700 Returns the number of objects in sequence \var{o} on success, and
701 \code{-1} on failure. For objects that do not provide sequence
702 protocol, this is equivalent to the Python expression
703 \samp{len(\var{o})}.\bifuncindex{len}
704 \end{cfuncdesc}
706 \begin{cfuncdesc}{int}{PySequence_Length}{PyObject *o}
707 Alternate name for \cfunction{PySequence_Size()}.
708 \end{cfuncdesc}
710 \begin{cfuncdesc}{PyObject*}{PySequence_Concat}{PyObject *o1, PyObject *o2}
711 Return the concatenation of \var{o1} and \var{o2} on success, and
712 \NULL{} on failure. This is the equivalent of the Python
713 expression \samp{\var{o1} + \var{o2}}.
714 \end{cfuncdesc}
717 \begin{cfuncdesc}{PyObject*}{PySequence_Repeat}{PyObject *o, int count}
718 Return the result of repeating sequence object \var{o} \var{count}
719 times, or \NULL{} on failure. This is the equivalent of the Python
720 expression \samp{\var{o} * \var{count}}.
721 \end{cfuncdesc}
723 \begin{cfuncdesc}{PyObject*}{PySequence_InPlaceConcat}{PyObject *o1,
724 PyObject *o2}
725 Return the concatenation of \var{o1} and \var{o2} on success, and
726 \NULL{} on failure. The operation is done \emph{in-place} when
727 \var{o1} supports it. This is the equivalent of the Python
728 expression \samp{\var{o1} += \var{o2}}.
729 \end{cfuncdesc}
732 \begin{cfuncdesc}{PyObject*}{PySequence_InPlaceRepeat}{PyObject *o, int count}
733 Return the result of repeating sequence object \var{o} \var{count}
734 times, or \NULL{} on failure. The operation is done \emph{in-place}
735 when \var{o} supports it. This is the equivalent of the Python
736 expression \samp{\var{o} *= \var{count}}.
737 \end{cfuncdesc}
740 \begin{cfuncdesc}{PyObject*}{PySequence_GetItem}{PyObject *o, int i}
741 Return the \var{i}th element of \var{o}, or \NULL{} on failure.
742 This is the equivalent of the Python expression
743 \samp{\var{o}[\var{i}]}.
744 \end{cfuncdesc}
747 \begin{cfuncdesc}{PyObject*}{PySequence_GetSlice}{PyObject *o, int i1, int i2}
748 Return the slice of sequence object \var{o} between \var{i1} and
749 \var{i2}, or \NULL{} on failure. This is the equivalent of the
750 Python expression \samp{\var{o}[\var{i1}:\var{i2}]}.
751 \end{cfuncdesc}
754 \begin{cfuncdesc}{int}{PySequence_SetItem}{PyObject *o, int i, PyObject *v}
755 Assign object \var{v} to the \var{i}th element of \var{o}. Returns
756 \code{-1} on failure. This is the equivalent of the Python
757 statement \samp{\var{o}[\var{i}] = \var{v}}. This function \emph{does not}
758 steal a reference to \var{v}.
759 \end{cfuncdesc}
761 \begin{cfuncdesc}{int}{PySequence_DelItem}{PyObject *o, int i}
762 Delete the \var{i}th element of object \var{o}. Returns \code{-1}
763 on failure. This is the equivalent of the Python statement
764 \samp{del \var{o}[\var{i}]}.
765 \end{cfuncdesc}
767 \begin{cfuncdesc}{int}{PySequence_SetSlice}{PyObject *o, int i1,
768 int i2, PyObject *v}
769 Assign the sequence object \var{v} to the slice in sequence object
770 \var{o} from \var{i1} to \var{i2}. This is the equivalent of the
771 Python statement \samp{\var{o}[\var{i1}:\var{i2}] = \var{v}}.
772 \end{cfuncdesc}
774 \begin{cfuncdesc}{int}{PySequence_DelSlice}{PyObject *o, int i1, int i2}
775 Delete the slice in sequence object \var{o} from \var{i1} to
776 \var{i2}. Returns \code{-1} on failure. This is the equivalent of
777 the Python statement \samp{del \var{o}[\var{i1}:\var{i2}]}.
778 \end{cfuncdesc}
780 \begin{cfuncdesc}{PyObject*}{PySequence_Tuple}{PyObject *o}
781 Returns the \var{o} as a tuple on success, and \NULL{} on failure.
782 This is equivalent to the Python expression \samp{tuple(\var{o})}.
783 \bifuncindex{tuple}
784 \end{cfuncdesc}
786 \begin{cfuncdesc}{int}{PySequence_Count}{PyObject *o, PyObject *value}
787 Return the number of occurrences of \var{value} in \var{o}, that is,
788 return the number of keys for which \code{\var{o}[\var{key}] ==
789 \var{value}}. On failure, return \code{-1}. This is equivalent to
790 the Python expression \samp{\var{o}.count(\var{value})}.
791 \end{cfuncdesc}
793 \begin{cfuncdesc}{int}{PySequence_Contains}{PyObject *o, PyObject *value}
794 Determine if \var{o} contains \var{value}. If an item in \var{o} is
795 equal to \var{value}, return \code{1}, otherwise return \code{0}.
796 On error, return \code{-1}. This is equivalent to the Python
797 expression \samp{\var{value} in \var{o}}.
798 \end{cfuncdesc}
800 \begin{cfuncdesc}{int}{PySequence_Index}{PyObject *o, PyObject *value}
801 Return the first index \var{i} for which \code{\var{o}[\var{i}] ==
802 \var{value}}. On error, return \code{-1}. This is equivalent to
803 the Python expression \samp{\var{o}.index(\var{value})}.
804 \end{cfuncdesc}
806 \begin{cfuncdesc}{PyObject*}{PySequence_List}{PyObject *o}
807 Return a list object with the same contents as the arbitrary
808 sequence \var{o}. The returned list is guaranteed to be new.
809 \end{cfuncdesc}
811 \begin{cfuncdesc}{PyObject*}{PySequence_Tuple}{PyObject *o}
812 Return a tuple object with the same contents as the arbitrary
813 sequence \var{o}. If \var{o} is a tuple, a new reference will be
814 returned, otherwise a tuple will be constructed with the appropriate
815 contents.
816 \end{cfuncdesc}
818 \begin{cfuncdesc}{PyObject*}{PySequence_Fast}{PyObject *o, const char *m}
819 Returns the sequence \var{o} as a tuple, unless it is already a
820 tuple or list, in which case \var{o} is returned. Use
821 \cfunction{PySequence_Fast_GET_ITEM()} to access the members of the
822 result. Returns \NULL{} on failure. If the object is not a
823 sequence, raises \exception{TypeError} with \var{m} as the message
824 text.
825 \end{cfuncdesc}
827 \begin{cfuncdesc}{PyObject*}{PySequence_Fast_GET_ITEM}{PyObject *o, int i}
828 Return the \var{i}th element of \var{o}, assuming that \var{o} was
829 returned by \cfunction{PySequence_Fast()}, \var{o} is not \NULL,
830 and that \var{i} is within bounds.
831 \end{cfuncdesc}
833 \begin{cfuncdesc}{PyObject*}{PySequence_ITEM}{PyObject *o, int i}
834 Return the \var{i}th element of \var{o} or \NULL{} on failure.
835 Macro form of \cfunction{PySequence_GetItem()} but without checking
836 that \cfunction{PySequence_Check(\var{o})} is true and without
837 adjustment for negative indices.
838 \versionadded{2.3}
839 \end{cfuncdesc}
841 \begin{cfuncdesc}{int}{PySequence_Fast_GET_SIZE}{PyObject *o}
842 Returns the length of \var{o}, assuming that \var{o} was
843 returned by \cfunction{PySequence_Fast()} and that \var{o} is
844 not \NULL. The size can also be gotten by calling
845 \cfunction{PySequence_Size()} on \var{o}, but
846 \cfunction{PySequence_Fast_GET_SIZE()} is faster because it can
847 assume \var{o} is a list or tuple.
848 \end{cfuncdesc}
851 \section{Mapping Protocol \label{mapping}}
853 \begin{cfuncdesc}{int}{PyMapping_Check}{PyObject *o}
854 Return \code{1} if the object provides mapping protocol, and
855 \code{0} otherwise. This function always succeeds.
856 \end{cfuncdesc}
859 \begin{cfuncdesc}{int}{PyMapping_Length}{PyObject *o}
860 Returns the number of keys in object \var{o} on success, and
861 \code{-1} on failure. For objects that do not provide mapping
862 protocol, this is equivalent to the Python expression
863 \samp{len(\var{o})}.\bifuncindex{len}
864 \end{cfuncdesc}
867 \begin{cfuncdesc}{int}{PyMapping_DelItemString}{PyObject *o, char *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}
874 \begin{cfuncdesc}{int}{PyMapping_DelItem}{PyObject *o, PyObject *key}
875 Remove the mapping for object \var{key} from the object \var{o}.
876 Return \code{-1} on failure. This is equivalent to the Python
877 statement \samp{del \var{o}[\var{key}]}.
878 \end{cfuncdesc}
881 \begin{cfuncdesc}{int}{PyMapping_HasKeyString}{PyObject *o, char *key}
882 On success, return \code{1} if the mapping object has the key
883 \var{key} and \code{0} otherwise. This is equivalent to the Python
884 expression \samp{\var{o}.has_key(\var{key})}. This function always
885 succeeds.
886 \end{cfuncdesc}
889 \begin{cfuncdesc}{int}{PyMapping_HasKey}{PyObject *o, PyObject *key}
890 Return \code{1} if the mapping object has the key \var{key} and
891 \code{0} otherwise. This is equivalent to the Python expression
892 \samp{\var{o}.has_key(\var{key})}. This function always succeeds.
893 \end{cfuncdesc}
896 \begin{cfuncdesc}{PyObject*}{PyMapping_Keys}{PyObject *o}
897 On success, return a list of the keys in object \var{o}. On
898 failure, return \NULL. This is equivalent to the Python expression
899 \samp{\var{o}.keys()}.
900 \end{cfuncdesc}
903 \begin{cfuncdesc}{PyObject*}{PyMapping_Values}{PyObject *o}
904 On success, return a list of the values in object \var{o}. On
905 failure, return \NULL. This is equivalent to the Python expression
906 \samp{\var{o}.values()}.
907 \end{cfuncdesc}
910 \begin{cfuncdesc}{PyObject*}{PyMapping_Items}{PyObject *o}
911 On success, return a list of the items in object \var{o}, where each
912 item is a tuple containing a key-value pair. On failure, return
913 \NULL. This is equivalent to the Python expression
914 \samp{\var{o}.items()}.
915 \end{cfuncdesc}
918 \begin{cfuncdesc}{PyObject*}{PyMapping_GetItemString}{PyObject *o, char *key}
919 Return element of \var{o} corresponding to the object \var{key} or
920 \NULL{} on failure. This is the equivalent of the Python expression
921 \samp{\var{o}[\var{key}]}.
922 \end{cfuncdesc}
924 \begin{cfuncdesc}{int}{PyMapping_SetItemString}{PyObject *o, char *key,
925 PyObject *v}
926 Map the object \var{key} to the value \var{v} in object \var{o}.
927 Returns \code{-1} on failure. This is the equivalent of the Python
928 statement \samp{\var{o}[\var{key}] = \var{v}}.
929 \end{cfuncdesc}
932 \section{Iterator Protocol \label{iterator}}
934 \versionadded{2.2}
936 There are only a couple of functions specifically for working with
937 iterators.
939 \begin{cfuncdesc}{int}{PyIter_Check}{PyObject *o}
940 Return true if the object \var{o} supports the iterator protocol.
941 \end{cfuncdesc}
943 \begin{cfuncdesc}{PyObject*}{PyIter_Next}{PyObject *o}
944 Return the next value from the iteration \var{o}. If the object is
945 an iterator, this retrieves the next value from the iteration, and
946 returns \NULL{} with no exception set if there are no remaining
947 items. If the object is not an iterator, \exception{TypeError} is
948 raised, or if there is an error in retrieving the item, returns
949 \NULL{} and passes along the exception.
950 \end{cfuncdesc}
952 To write a loop which iterates over an iterator, the C code should
953 look something like this:
955 \begin{verbatim}
956 PyObject *iterator = PyObject_GetIter(obj);
957 PyObject *item;
959 if (iterator == NULL) {
960 /* propagate error */
963 while (item = PyIter_Next(iterator)) {
964 /* do something with item */
966 /* release reference when done */
967 Py_DECREF(item);
970 Py_DECREF(iterator);
972 if (PyErr_Occurred()) {
973 /* propagate error */
975 else {
976 /* continue doing useful work */
978 \end{verbatim}
981 \section{Buffer Protocol \label{abstract-buffer}}
983 \begin{cfuncdesc}{int}{PyObject_AsCharBuffer}{PyObject *obj,
984 const char **buffer,
985 int *buffer_len}
986 Returns a pointer to a read-only memory location useable as character-
987 based input. The \var{obj} argument must support the single-segment
988 character buffer interface. On success, returns \code{0}, sets
989 \var{buffer} to the memory location and \var{buffer_len} to the buffer
990 length. Returns \code{-1} and sets a \exception{TypeError} on error.
991 \versionadded{1.6}
992 \end{cfuncdesc}
994 \begin{cfuncdesc}{int}{PyObject_AsReadBuffer}{PyObject *obj,
995 const char **buffer,
996 int *buffer_len}
997 Returns a pointer to a read-only memory location containing
998 arbitrary data. The \var{obj} argument must support the
999 single-segment readable buffer interface. On success, returns
1000 \code{0}, sets \var{buffer} to the memory location and \var{buffer_len}
1001 to the buffer length. Returns \code{-1} and sets a
1002 \exception{TypeError} on error.
1003 \versionadded{1.6}
1004 \end{cfuncdesc}
1006 \begin{cfuncdesc}{int}{PyObject_CheckReadBuffer}{PyObject *o}
1007 Returns \code{1} if \var{o} supports the single-segment readable
1008 buffer interface. Otherwise returns \code{0}.
1009 \versionadded{2.2}
1010 \end{cfuncdesc}
1012 \begin{cfuncdesc}{int}{PyObject_AsWriteBuffer}{PyObject *obj,
1013 char **buffer,
1014 int *buffer_len}
1015 Returns a pointer to a writeable memory location. The \var{obj}
1016 argument must support the single-segment, character buffer
1017 interface. On success, returns \code{0}, sets \var{buffer} to the
1018 memory location and \var{buffer_len} to the buffer length. Returns
1019 \code{-1} and sets a \exception{TypeError} on error.
1020 \versionadded{1.6}
1021 \end{cfuncdesc}