blob: 4d922685bd67ba2c14632bea1cc2533b2a1c2646 [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
84\begin{cfuncdesc}{int}{PyObject_Cmp}{PyObject *o1, PyObject *o2, int *result}
85 Compare the values of \var{o1} and \var{o2} using a routine provided
86 by \var{o1}, if one exists, otherwise with a routine provided by
87 \var{o2}. The result of the comparison is returned in
88 \var{result}. Returns \code{-1} on failure. This is the equivalent
89 of the Python statement\bifuncindex{cmp} \samp{\var{result} =
90 cmp(\var{o1}, \var{o2})}.
91\end{cfuncdesc}
92
93
94\begin{cfuncdesc}{int}{PyObject_Compare}{PyObject *o1, PyObject *o2}
95 Compare the values of \var{o1} and \var{o2} using a routine provided
96 by \var{o1}, if one exists, otherwise with a routine provided by
97 \var{o2}. Returns the result of the comparison on success. On
98 error, the value returned is undefined; use
99 \cfunction{PyErr_Occurred()} to detect an error. This is equivalent
100 to the Python expression\bifuncindex{cmp} \samp{cmp(\var{o1},
101 \var{o2})}.
102\end{cfuncdesc}
103
104
105\begin{cfuncdesc}{PyObject*}{PyObject_Repr}{PyObject *o}
106 Compute a string representation of object \var{o}. Returns the
107 string representation on success, \NULL{} on failure. This is the
108 equivalent of the Python expression \samp{repr(\var{o})}. Called by
109 the \function{repr()}\bifuncindex{repr} built-in function and by
110 reverse quotes.
111\end{cfuncdesc}
112
113
114\begin{cfuncdesc}{PyObject*}{PyObject_Str}{PyObject *o}
115 Compute a string representation of object \var{o}. Returns the
116 string representation on success, \NULL{} on failure. This is the
117 equivalent of the Python expression \samp{str(\var{o})}. Called by
118 the \function{str()}\bifuncindex{str} built-in function and by the
119 \keyword{print} statement.
120\end{cfuncdesc}
121
122
123\begin{cfuncdesc}{PyObject*}{PyObject_Unicode}{PyObject *o}
124 Compute a Unicode string representation of object \var{o}. Returns
125 the Unicode string representation on success, \NULL{} on failure.
126 This is the equivalent of the Python expression
127 \samp{unistr(\var{o})}. Called by the
Tim Peters1fc240e2001-10-26 05:06:50 +0000128 \function{unistr()}\bifuncindex{unistr} built-in function.
Fred Drake3adf79e2001-10-12 19:01:43 +0000129\end{cfuncdesc}
130
131\begin{cfuncdesc}{int}{PyObject_IsInstance}{PyObject *inst, PyObject *cls}
132 Return \code{1} if \var{inst} is an instance of the class \var{cls}
133 or a subclass of \var{cls}. If \var{cls} is a type object rather
134 than a class object, \cfunction{PyObject_IsInstance()} returns
135 \code{1} if \var{inst} is of type \var{cls}. If \var{inst} is not a
136 class instance and \var{cls} is neither a type object or class
137 object, \var{inst} must have a \member{__class__} attribute --- the
138 class relationship of the value of that attribute with \var{cls}
139 will be used to determine the result of this function.
140 \versionadded{2.1}
141\end{cfuncdesc}
142
143Subclass determination is done in a fairly straightforward way, but
144includes a wrinkle that implementors of extensions to the class system
145may want to be aware of. If \class{A} and \class{B} are class
146objects, \class{B} is a subclass of \class{A} if it inherits from
147\class{A} either directly or indirectly. If either is not a class
148object, a more general mechanism is used to determine the class
149relationship of the two objects. When testing if \var{B} is a
150subclass of \var{A}, if \var{A} is \var{B},
151\cfunction{PyObject_IsSubclass()} returns true. If \var{A} and
152\var{B} are different objects, \var{B}'s \member{__bases__} attribute
153is searched in a depth-first fashion for \var{A} --- the presence of
154the \member{__bases__} attribute is considered sufficient for this
155determination.
156
157\begin{cfuncdesc}{int}{PyObject_IsSubclass}{PyObject *derived,
158 PyObject *cls}
159 Returns \code{1} if the class \var{derived} is identical to or
160 derived from the class \var{cls}, otherwise returns \code{0}. In
161 case of an error, returns \code{-1}. If either \var{derived} or
162 \var{cls} is not an actual class object, this function uses the
163 generic algorithm described above.
164 \versionadded{2.1}
165\end{cfuncdesc}
166
167
168\begin{cfuncdesc}{int}{PyCallable_Check}{PyObject *o}
169 Determine if the object \var{o} is callable. Return \code{1} if the
170 object is callable and \code{0} otherwise. This function always
171 succeeds.
172\end{cfuncdesc}
173
174
175\begin{cfuncdesc}{PyObject*}{PyObject_CallObject}{PyObject *callable_object,
176 PyObject *args}
177 Call a callable Python object \var{callable_object}, with arguments
178 given by the tuple \var{args}. If no arguments are needed, then
179 \var{args} may be \NULL. Returns the result of the call on
180 success, or \NULL{} on failure. This is the equivalent of the
181 Python expression \samp{apply(\var{callable_object}, \var{args})} or
182 \samp{\var{callable_object}(*\var{args})}.
183 \bifuncindex{apply}
184\end{cfuncdesc}
185
Fred Drakec44e9ec2001-10-26 16:38:38 +0000186\begin{cfuncdesc}{PyObject*}{PyObject_CallFunction}{PyObject *callable,
187 char *format, \moreargs}
188 Call a callable Python object \var{callable}, with a variable
Fred Drake3adf79e2001-10-12 19:01:43 +0000189 number of C arguments. The C arguments are described using a
190 \cfunction{Py_BuildValue()} style format string. The format may be
191 \NULL, indicating that no arguments are provided. Returns the
192 result of the call on success, or \NULL{} on failure. This is the
Fred Drakec44e9ec2001-10-26 16:38:38 +0000193 equivalent of the Python expression \samp{apply(\var{callable},
194 \var{args})} or \samp{\var{callable}(*\var{args})}.
Fred Drake3adf79e2001-10-12 19:01:43 +0000195 \bifuncindex{apply}
196\end{cfuncdesc}
197
198
199\begin{cfuncdesc}{PyObject*}{PyObject_CallMethod}{PyObject *o,
Fred Drakec44e9ec2001-10-26 16:38:38 +0000200 char *method, char *format,
201 \moreargs}
Fred Drake3adf79e2001-10-12 19:01:43 +0000202 Call the method named \var{m} of object \var{o} with a variable
203 number of C arguments. The C arguments are described by a
204 \cfunction{Py_BuildValue()} format string. The format may be \NULL,
205 indicating that no arguments are provided. Returns the result of the
206 call on success, or \NULL{} on failure. This is the equivalent of
Fred Drakec44e9ec2001-10-26 16:38:38 +0000207 the Python expression \samp{\var{o}.\var{method}(\var{args})}.
208\end{cfuncdesc}
209
210
Fred Drakeb0c079e2001-10-28 02:39:03 +0000211\begin{cfuncdesc}{PyObject*}{PyObject_CallFunctionObjArgs}{PyObject *callable,
212 \moreargs,
213 \code{NULL}}
Fred Drakec44e9ec2001-10-26 16:38:38 +0000214 Call a callable Python object \var{callable}, with a variable
215 number of \ctype{PyObject*} arguments. The arguments are provided
216 as a variable number of parameters followed by \NULL.
217 Returns the result of the call on success, or \NULL{} on failure.
218 \versionadded{2.2}
219\end{cfuncdesc}
220
221
Fred Drakeb0c079e2001-10-28 02:39:03 +0000222\begin{cfuncdesc}{PyObject*}{PyObject_CallMethodObjArgs}{PyObject *o,
223 PyObject *name,
224 \moreargs,
225 \code{NULL}}
Fred Drakec44e9ec2001-10-26 16:38:38 +0000226 Calls a method of the object \var{o}, where the name of the method
227 is given as a Python string object in \var{name}. It is called with
228 a variable number of \ctype{PyObject*} arguments. The arguments are
229 provided as a variable number of parameters followed by \NULL.
230 Returns the result of the call on success, or \NULL{} on failure.
231 \versionadded{2.2}
Fred Drake3adf79e2001-10-12 19:01:43 +0000232\end{cfuncdesc}
233
234
235\begin{cfuncdesc}{int}{PyObject_Hash}{PyObject *o}
236 Compute and return the hash value of an object \var{o}. On failure,
237 return \code{-1}. This is the equivalent of the Python expression
238 \samp{hash(\var{o})}.\bifuncindex{hash}
239\end{cfuncdesc}
240
241
242\begin{cfuncdesc}{int}{PyObject_IsTrue}{PyObject *o}
243 Returns \code{1} if the object \var{o} is considered to be true, and
244 \code{0} otherwise. This is equivalent to the Python expression
245 \samp{not not \var{o}}. This function always succeeds.
246\end{cfuncdesc}
247
248
249\begin{cfuncdesc}{PyObject*}{PyObject_Type}{PyObject *o}
250 When \var{o} is non-\NULL, returns a type object corresponding to
251 the object type of object \var{o}. On failure, raises
252 \exception{SystemError} and returns \NULL. This is equivalent to
253 the Python expression \code{type(\var{o})}.
254 \bifuncindex{type}
255\end{cfuncdesc}
256
257\begin{cfuncdesc}{int}{PyObject_TypeCheck}{PyObject *o, PyTypeObject *type}
258 Return true if the object \var{o} is of type \var{type} or a subtype
259 of \var{type}. Both parameters must be non-\NULL.
260 \versionadded{2.2}
261\end{cfuncdesc}
262
263\begin{cfuncdesc}{int}{PyObject_Length}{PyObject *o}
264 Return the length of object \var{o}. If the object \var{o} provides
265 both sequence and mapping protocols, the sequence length is
266 returned. On error, \code{-1} is returned. This is the equivalent
267 to the Python expression \samp{len(\var{o})}.\bifuncindex{len}
268\end{cfuncdesc}
269
270
271\begin{cfuncdesc}{PyObject*}{PyObject_GetItem}{PyObject *o, PyObject *key}
272 Return element of \var{o} corresponding to the object \var{key} or
273 \NULL{} on failure. This is the equivalent of the Python expression
274 \samp{\var{o}[\var{key}]}.
275\end{cfuncdesc}
276
277
278\begin{cfuncdesc}{int}{PyObject_SetItem}{PyObject *o,
279 PyObject *key, PyObject *v}
280 Map the object \var{key} to the value \var{v}. Returns \code{-1} on
281 failure. This is the equivalent of the Python statement
282 \samp{\var{o}[\var{key}] = \var{v}}.
283\end{cfuncdesc}
284
285
286\begin{cfuncdesc}{int}{PyObject_DelItem}{PyObject *o, PyObject *key}
287 Delete the mapping for \var{key} from \var{o}. Returns \code{-1} on
288 failure. This is the equivalent of the Python statement \samp{del
289 \var{o}[\var{key}]}.
290\end{cfuncdesc}
291
292\begin{cfuncdesc}{int}{PyObject_AsFileDescriptor}{PyObject *o}
293 Derives a file-descriptor from a Python object. If the object is an
294 integer or long integer, its value is returned. If not, the
295 object's \method{fileno()} method is called if it exists; the method
296 must return an integer or long integer, which is returned as the
297 file descriptor value. Returns \code{-1} on failure.
298\end{cfuncdesc}
299
300\begin{cfuncdesc}{PyObject*}{PyObject_Dir}{PyObject *o}
301 This is equivalent to the Python expression \samp{dir(\var{o})},
302 returning a (possibly empty) list of strings appropriate for the
303 object argument, or \NULL{} if there was an error. If the argument
304 is \NULL, this is like the Python \samp{dir()}, returning the names
305 of the current locals; in this case, if no execution frame is active
306 then \NULL{} is returned but \cfunction{PyErr_Occurred()} will
307 return false.
308\end{cfuncdesc}
309
Fred Drake314bae52002-03-11 18:46:29 +0000310\begin{cfuncdesc}{PyObject*}{PyObject_GetIter}{PyObject *o}
311 This is equivalent to the Python expression \samp{iter(\var{o})}.
312 It returns a new iterator for the object argument, or the object
313 itself if the object is already an iterator. Raises
314 \exception{TypeError} and returns \NULL{} if the object cannot be
315 iterated.
316\end{cfuncdesc}
317
Fred Drake3adf79e2001-10-12 19:01:43 +0000318
319\section{Number Protocol \label{number}}
320
321\begin{cfuncdesc}{int}{PyNumber_Check}{PyObject *o}
322 Returns \code{1} if the object \var{o} provides numeric protocols,
323 and false otherwise. This function always succeeds.
324\end{cfuncdesc}
325
326
327\begin{cfuncdesc}{PyObject*}{PyNumber_Add}{PyObject *o1, PyObject *o2}
328 Returns the result of adding \var{o1} and \var{o2}, or \NULL{} on
329 failure. This is the equivalent of the Python expression
330 \samp{\var{o1} + \var{o2}}.
331\end{cfuncdesc}
332
333
334\begin{cfuncdesc}{PyObject*}{PyNumber_Subtract}{PyObject *o1, PyObject *o2}
335 Returns the result of subtracting \var{o2} from \var{o1}, or \NULL{}
336 on failure. This is the equivalent of the Python expression
337 \samp{\var{o1} - \var{o2}}.
338\end{cfuncdesc}
339
340
341\begin{cfuncdesc}{PyObject*}{PyNumber_Multiply}{PyObject *o1, PyObject *o2}
342 Returns the result of multiplying \var{o1} and \var{o2}, or \NULL{}
343 on failure. This is the equivalent of the Python expression
344 \samp{\var{o1} * \var{o2}}.
345\end{cfuncdesc}
346
347
348\begin{cfuncdesc}{PyObject*}{PyNumber_Divide}{PyObject *o1, PyObject *o2}
349 Returns the result of dividing \var{o1} by \var{o2}, or \NULL{} on
350 failure. This is the equivalent of the Python expression
351 \samp{\var{o1} / \var{o2}}.
352\end{cfuncdesc}
353
354
355\begin{cfuncdesc}{PyObject*}{PyNumber_FloorDivide}{PyObject *o1, PyObject *o2}
356 Return the floor of \var{o1} divided by \var{o2}, or \NULL{} on
357 failure. This is equivalent to the ``classic'' division of
358 integers.
359 \versionadded{2.2}
360\end{cfuncdesc}
361
362
363\begin{cfuncdesc}{PyObject*}{PyNumber_TrueDivide}{PyObject *o1, PyObject *o2}
364 Return a reasonable approximation for the mathematical value of
365 \var{o1} divided by \var{o2}, or \NULL{} on failure. The return
366 value is ``approximate'' because binary floating point numbers are
367 approximate; it is not possible to represent all real numbers in
368 base two. This function can return a floating point value when
369 passed two integers.
370 \versionadded{2.2}
371\end{cfuncdesc}
372
373
374\begin{cfuncdesc}{PyObject*}{PyNumber_Remainder}{PyObject *o1, PyObject *o2}
375 Returns the remainder of dividing \var{o1} by \var{o2}, or \NULL{}
376 on failure. This is the equivalent of the Python expression
377 \samp{\var{o1} \%\ \var{o2}}.
378\end{cfuncdesc}
379
380
381\begin{cfuncdesc}{PyObject*}{PyNumber_Divmod}{PyObject *o1, PyObject *o2}
382 See the built-in function \function{divmod()}\bifuncindex{divmod}.
383 Returns \NULL{} on failure. This is the equivalent of the Python
384 expression \samp{divmod(\var{o1}, \var{o2})}.
385\end{cfuncdesc}
386
387
388\begin{cfuncdesc}{PyObject*}{PyNumber_Power}{PyObject *o1,
389 PyObject *o2, PyObject *o3}
390 See the built-in function \function{pow()}\bifuncindex{pow}.
391 Returns \NULL{} on failure. This is the equivalent of the Python
392 expression \samp{pow(\var{o1}, \var{o2}, \var{o3})}, where \var{o3}
393 is optional. If \var{o3} is to be ignored, pass \cdata{Py_None} in
394 its place (passing \NULL{} for \var{o3} would cause an illegal
395 memory access).
396\end{cfuncdesc}
397
398
399\begin{cfuncdesc}{PyObject*}{PyNumber_Negative}{PyObject *o}
400 Returns the negation of \var{o} on success, or \NULL{} on failure.
401 This is the equivalent of the Python expression \samp{-\var{o}}.
402\end{cfuncdesc}
403
404
405\begin{cfuncdesc}{PyObject*}{PyNumber_Positive}{PyObject *o}
406 Returns \var{o} on success, or \NULL{} on failure. This is the
407 equivalent of the Python expression \samp{+\var{o}}.
408\end{cfuncdesc}
409
410
411\begin{cfuncdesc}{PyObject*}{PyNumber_Absolute}{PyObject *o}
412 Returns the absolute value of \var{o}, or \NULL{} on failure. This
413 is the equivalent of the Python expression \samp{abs(\var{o})}.
414 \bifuncindex{abs}
415\end{cfuncdesc}
416
417
418\begin{cfuncdesc}{PyObject*}{PyNumber_Invert}{PyObject *o}
419 Returns the bitwise negation of \var{o} on success, or \NULL{} on
420 failure. This is the equivalent of the Python expression
421 \samp{\~\var{o}}.
422\end{cfuncdesc}
423
424
425\begin{cfuncdesc}{PyObject*}{PyNumber_Lshift}{PyObject *o1, PyObject *o2}
426 Returns the result of left shifting \var{o1} by \var{o2} on success,
427 or \NULL{} on failure. This is the equivalent of the Python
428 expression \samp{\var{o1} <\code{<} \var{o2}}.
429\end{cfuncdesc}
430
431
432\begin{cfuncdesc}{PyObject*}{PyNumber_Rshift}{PyObject *o1, PyObject *o2}
433 Returns the result of right shifting \var{o1} by \var{o2} on
434 success, or \NULL{} on failure. This is the equivalent of the
435 Python expression \samp{\var{o1} >\code{>} \var{o2}}.
436\end{cfuncdesc}
437
438
439\begin{cfuncdesc}{PyObject*}{PyNumber_And}{PyObject *o1, PyObject *o2}
440 Returns the ``bitwise and'' of \var{o2} and \var{o2} on success and
441 \NULL{} on failure. This is the equivalent of the Python expression
442 \samp{\var{o1} \&\ \var{o2}}.
443\end{cfuncdesc}
444
445
446\begin{cfuncdesc}{PyObject*}{PyNumber_Xor}{PyObject *o1, PyObject *o2}
447 Returns the ``bitwise exclusive or'' of \var{o1} by \var{o2} on
448 success, or \NULL{} on failure. This is the equivalent of the
449 Python expression \samp{\var{o1} \textasciicircum{} \var{o2}}.
450\end{cfuncdesc}
451
452\begin{cfuncdesc}{PyObject*}{PyNumber_Or}{PyObject *o1, PyObject *o2}
453 Returns the ``bitwise or'' of \var{o1} and \var{o2} on success, or
454 \NULL{} on failure. This is the equivalent of the Python expression
455 \samp{\var{o1} | \var{o2}}.
456\end{cfuncdesc}
457
458
459\begin{cfuncdesc}{PyObject*}{PyNumber_InPlaceAdd}{PyObject *o1, PyObject *o2}
460 Returns the result of adding \var{o1} and \var{o2}, or \NULL{} on
461 failure. The operation is done \emph{in-place} when \var{o1}
462 supports it. This is the equivalent of the Python statement
463 \samp{\var{o1} += \var{o2}}.
464\end{cfuncdesc}
465
466
467\begin{cfuncdesc}{PyObject*}{PyNumber_InPlaceSubtract}{PyObject *o1,
468 PyObject *o2}
469 Returns the result of subtracting \var{o2} from \var{o1}, or \NULL{}
470 on failure. The operation is done \emph{in-place} when \var{o1}
471 supports it. This is the equivalent of the Python statement
472 \samp{\var{o1} -= \var{o2}}.
473\end{cfuncdesc}
474
475
476\begin{cfuncdesc}{PyObject*}{PyNumber_InPlaceMultiply}{PyObject *o1,
477 PyObject *o2}
478 Returns the result of multiplying \var{o1} and \var{o2}, or \NULL{}
479 on failure. The operation is done \emph{in-place} when \var{o1}
480 supports it. This is the equivalent of the Python statement
481 \samp{\var{o1} *= \var{o2}}.
482\end{cfuncdesc}
483
484
485\begin{cfuncdesc}{PyObject*}{PyNumber_InPlaceDivide}{PyObject *o1,
486 PyObject *o2}
487 Returns the result of dividing \var{o1} by \var{o2}, or \NULL{} on
488 failure. The operation is done \emph{in-place} when \var{o1}
489 supports it. This is the equivalent of the Python statement
490 \samp{\var{o1} /= \var{o2}}.
491\end{cfuncdesc}
492
493
494\begin{cfuncdesc}{PyObject*}{PyNumber_InPlaceFloorDivide}{PyObject *o1,
495 PyObject *o2}
496 Returns the mathematical of dividing \var{o1} by \var{o2}, or
497 \NULL{} on failure. The operation is done \emph{in-place} when
498 \var{o1} supports it. This is the equivalent of the Python
499 statement \samp{\var{o1} //= \var{o2}}.
500 \versionadded{2.2}
501\end{cfuncdesc}
502
503
504\begin{cfuncdesc}{PyObject*}{PyNumber_InPlaceTrueDivide}{PyObject *o1,
505 PyObject *o2}
506 Return a reasonable approximation for the mathematical value of
507 \var{o1} divided by \var{o2}, or \NULL{} on failure. The return
508 value is ``approximate'' because binary floating point numbers are
509 approximate; it is not possible to represent all real numbers in
510 base two. This function can return a floating point value when
511 passed two integers. The operation is done \emph{in-place} when
512 \var{o1} supports it.
513 \versionadded{2.2}
514\end{cfuncdesc}
515
516
517\begin{cfuncdesc}{PyObject*}{PyNumber_InPlaceRemainder}{PyObject *o1,
518 PyObject *o2}
519 Returns the remainder of dividing \var{o1} by \var{o2}, or \NULL{}
520 on failure. The operation is done \emph{in-place} when \var{o1}
521 supports it. This is the equivalent of the Python statement
522 \samp{\var{o1} \%= \var{o2}}.
523\end{cfuncdesc}
524
525
526\begin{cfuncdesc}{PyObject*}{PyNumber_InPlacePower}{PyObject *o1,
527 PyObject *o2, PyObject *o3}
528 See the built-in function \function{pow()}.\bifuncindex{pow}
529 Returns \NULL{} on failure. The operation is done \emph{in-place}
530 when \var{o1} supports it. This is the equivalent of the Python
531 statement \samp{\var{o1} **= \var{o2}} when o3 is \cdata{Py_None},
532 or an in-place variant of \samp{pow(\var{o1}, \var{o2}, \var{o3})}
533 otherwise. If \var{o3} is to be ignored, pass \cdata{Py_None} in its
534 place (passing \NULL{} for \var{o3} would cause an illegal memory
535 access).
536\end{cfuncdesc}
537
538\begin{cfuncdesc}{PyObject*}{PyNumber_InPlaceLshift}{PyObject *o1,
539 PyObject *o2}
540 Returns the result of left shifting \var{o1} by \var{o2} on success,
541 or \NULL{} on failure. The operation is done \emph{in-place} when
542 \var{o1} supports it. This is the equivalent of the Python
543 statement \samp{\var{o1} <\code{<=} \var{o2}}.
544\end{cfuncdesc}
545
546
547\begin{cfuncdesc}{PyObject*}{PyNumber_InPlaceRshift}{PyObject *o1,
548 PyObject *o2}
549 Returns the result of right shifting \var{o1} by \var{o2} on
550 success, or \NULL{} on failure. The operation is done
551 \emph{in-place} when \var{o1} supports it. This is the equivalent
552 of the Python statement \samp{\var{o1} >\code{>=} \var{o2}}.
553\end{cfuncdesc}
554
555
556\begin{cfuncdesc}{PyObject*}{PyNumber_InPlaceAnd}{PyObject *o1, PyObject *o2}
557 Returns the ``bitwise and'' of \var{o1} and \var{o2} on success and
558 \NULL{} on failure. The operation is done \emph{in-place} when
559 \var{o1} supports it. This is the equivalent of the Python
560 statement \samp{\var{o1} \&= \var{o2}}.
561\end{cfuncdesc}
562
563
564\begin{cfuncdesc}{PyObject*}{PyNumber_InPlaceXor}{PyObject *o1, PyObject *o2}
565 Returns the ``bitwise exclusive or'' of \var{o1} by \var{o2} on
566 success, or \NULL{} on failure. The operation is done
567 \emph{in-place} when \var{o1} supports it. This is the equivalent
568 of the Python statement \samp{\var{o1} \textasciicircum= \var{o2}}.
569\end{cfuncdesc}
570
571\begin{cfuncdesc}{PyObject*}{PyNumber_InPlaceOr}{PyObject *o1, PyObject *o2}
572 Returns the ``bitwise or'' of \var{o1} and \var{o2} on success, or
573 \NULL{} on failure. The operation is done \emph{in-place} when
574 \var{o1} supports it. This is the equivalent of the Python
575 statement \samp{\var{o1} |= \var{o2}}.
576\end{cfuncdesc}
577
578\begin{cfuncdesc}{int}{PyNumber_Coerce}{PyObject **p1, PyObject **p2}
579 This function takes the addresses of two variables of type
580 \ctype{PyObject*}. If the objects pointed to by \code{*\var{p1}}
581 and \code{*\var{p2}} have the same type, increment their reference
582 count and return \code{0} (success). If the objects can be converted
583 to a common numeric type, replace \code{*p1} and \code{*p2} by their
584 converted value (with 'new' reference counts), and return \code{0}.
585 If no conversion is possible, or if some other error occurs, return
586 \code{-1} (failure) and don't increment the reference counts. The
587 call \code{PyNumber_Coerce(\&o1, \&o2)} is equivalent to the Python
588 statement \samp{\var{o1}, \var{o2} = coerce(\var{o1}, \var{o2})}.
589 \bifuncindex{coerce}
590\end{cfuncdesc}
591
592\begin{cfuncdesc}{PyObject*}{PyNumber_Int}{PyObject *o}
593 Returns the \var{o} converted to an integer object on success, or
594 \NULL{} on failure. This is the equivalent of the Python expression
595 \samp{int(\var{o})}.\bifuncindex{int}
596\end{cfuncdesc}
597
598\begin{cfuncdesc}{PyObject*}{PyNumber_Long}{PyObject *o}
599 Returns the \var{o} converted to a long integer object on success,
600 or \NULL{} on failure. This is the equivalent of the Python
601 expression \samp{long(\var{o})}.\bifuncindex{long}
602\end{cfuncdesc}
603
604\begin{cfuncdesc}{PyObject*}{PyNumber_Float}{PyObject *o}
605 Returns the \var{o} converted to a float object on success, or
606 \NULL{} on failure. This is the equivalent of the Python expression
607 \samp{float(\var{o})}.\bifuncindex{float}
608\end{cfuncdesc}
609
610
611\section{Sequence Protocol \label{sequence}}
612
613\begin{cfuncdesc}{int}{PySequence_Check}{PyObject *o}
614 Return \code{1} if the object provides sequence protocol, and
615 \code{0} otherwise. This function always succeeds.
616\end{cfuncdesc}
617
618\begin{cfuncdesc}{int}{PySequence_Size}{PyObject *o}
619 Returns the number of objects in sequence \var{o} on success, and
620 \code{-1} on failure. For objects that do not provide sequence
621 protocol, this is equivalent to the Python expression
622 \samp{len(\var{o})}.\bifuncindex{len}
623\end{cfuncdesc}
624
625\begin{cfuncdesc}{int}{PySequence_Length}{PyObject *o}
626 Alternate name for \cfunction{PySequence_Size()}.
627\end{cfuncdesc}
628
629\begin{cfuncdesc}{PyObject*}{PySequence_Concat}{PyObject *o1, PyObject *o2}
630 Return the concatenation of \var{o1} and \var{o2} on success, and
631 \NULL{} on failure. This is the equivalent of the Python
632 expression \samp{\var{o1} + \var{o2}}.
633\end{cfuncdesc}
634
635
636\begin{cfuncdesc}{PyObject*}{PySequence_Repeat}{PyObject *o, int count}
637 Return the result of repeating sequence object \var{o} \var{count}
638 times, or \NULL{} on failure. This is the equivalent of the Python
639 expression \samp{\var{o} * \var{count}}.
640\end{cfuncdesc}
641
642\begin{cfuncdesc}{PyObject*}{PySequence_InPlaceConcat}{PyObject *o1,
643 PyObject *o2}
644 Return the concatenation of \var{o1} and \var{o2} on success, and
645 \NULL{} on failure. The operation is done \emph{in-place} when
646 \var{o1} supports it. This is the equivalent of the Python
647 expression \samp{\var{o1} += \var{o2}}.
648\end{cfuncdesc}
649
650
651\begin{cfuncdesc}{PyObject*}{PySequence_InPlaceRepeat}{PyObject *o, int count}
652 Return the result of repeating sequence object \var{o} \var{count}
653 times, or \NULL{} on failure. The operation is done \emph{in-place}
654 when \var{o} supports it. This is the equivalent of the Python
655 expression \samp{\var{o} *= \var{count}}.
656\end{cfuncdesc}
657
658
659\begin{cfuncdesc}{PyObject*}{PySequence_GetItem}{PyObject *o, int i}
660 Return the \var{i}th element of \var{o}, or \NULL{} on failure.
661 This is the equivalent of the Python expression
662 \samp{\var{o}[\var{i}]}.
663\end{cfuncdesc}
664
665
666\begin{cfuncdesc}{PyObject*}{PySequence_GetSlice}{PyObject *o, int i1, int i2}
667 Return the slice of sequence object \var{o} between \var{i1} and
668 \var{i2}, or \NULL{} on failure. This is the equivalent of the
669 Python expression \samp{\var{o}[\var{i1}:\var{i2}]}.
670\end{cfuncdesc}
671
672
673\begin{cfuncdesc}{int}{PySequence_SetItem}{PyObject *o, int i, PyObject *v}
674 Assign object \var{v} to the \var{i}th element of \var{o}. Returns
675 \code{-1} on failure. This is the equivalent of the Python
676 statement \samp{\var{o}[\var{i}] = \var{v}}.
677\end{cfuncdesc}
678
679\begin{cfuncdesc}{int}{PySequence_DelItem}{PyObject *o, int i}
680 Delete the \var{i}th element of object \var{o}. Returns \code{-1}
681 on failure. This is the equivalent of the Python statement
682 \samp{del \var{o}[\var{i}]}.
683\end{cfuncdesc}
684
685\begin{cfuncdesc}{int}{PySequence_SetSlice}{PyObject *o, int i1,
686 int i2, PyObject *v}
687 Assign the sequence object \var{v} to the slice in sequence object
688 \var{o} from \var{i1} to \var{i2}. This is the equivalent of the
689 Python statement \samp{\var{o}[\var{i1}:\var{i2}] = \var{v}}.
690\end{cfuncdesc}
691
692\begin{cfuncdesc}{int}{PySequence_DelSlice}{PyObject *o, int i1, int i2}
693 Delete the slice in sequence object \var{o} from \var{i1} to
694 \var{i2}. Returns \code{-1} on failure. This is the equivalent of
695 the Python statement \samp{del \var{o}[\var{i1}:\var{i2}]}.
696\end{cfuncdesc}
697
698\begin{cfuncdesc}{PyObject*}{PySequence_Tuple}{PyObject *o}
699 Returns the \var{o} as a tuple on success, and \NULL{} on failure.
700 This is equivalent to the Python expression \samp{tuple(\var{o})}.
701 \bifuncindex{tuple}
702\end{cfuncdesc}
703
704\begin{cfuncdesc}{int}{PySequence_Count}{PyObject *o, PyObject *value}
705 Return the number of occurrences of \var{value} in \var{o}, that is,
706 return the number of keys for which \code{\var{o}[\var{key}] ==
707 \var{value}}. On failure, return \code{-1}. This is equivalent to
708 the Python expression \samp{\var{o}.count(\var{value})}.
709\end{cfuncdesc}
710
711\begin{cfuncdesc}{int}{PySequence_Contains}{PyObject *o, PyObject *value}
712 Determine if \var{o} contains \var{value}. If an item in \var{o} is
713 equal to \var{value}, return \code{1}, otherwise return \code{0}.
714 On error, return \code{-1}. This is equivalent to the Python
715 expression \samp{\var{value} in \var{o}}.
716\end{cfuncdesc}
717
718\begin{cfuncdesc}{int}{PySequence_Index}{PyObject *o, PyObject *value}
719 Return the first index \var{i} for which \code{\var{o}[\var{i}] ==
720 \var{value}}. On error, return \code{-1}. This is equivalent to
721 the Python expression \samp{\var{o}.index(\var{value})}.
722\end{cfuncdesc}
723
724\begin{cfuncdesc}{PyObject*}{PySequence_List}{PyObject *o}
725 Return a list object with the same contents as the arbitrary
726 sequence \var{o}. The returned list is guaranteed to be new.
727\end{cfuncdesc}
728
729\begin{cfuncdesc}{PyObject*}{PySequence_Tuple}{PyObject *o}
730 Return a tuple object with the same contents as the arbitrary
731 sequence \var{o}. If \var{o} is a tuple, a new reference will be
732 returned, otherwise a tuple will be constructed with the appropriate
733 contents.
734\end{cfuncdesc}
735
736\begin{cfuncdesc}{PyObject*}{PySequence_Fast}{PyObject *o, const char *m}
737 Returns the sequence \var{o} as a tuple, unless it is already a
738 tuple or list, in which case \var{o} is returned. Use
739 \cfunction{PySequence_Fast_GET_ITEM()} to access the members of the
740 result. Returns \NULL{} on failure. If the object is not a
741 sequence, raises \exception{TypeError} with \var{m} as the message
742 text.
743\end{cfuncdesc}
744
745\begin{cfuncdesc}{PyObject*}{PySequence_Fast_GET_ITEM}{PyObject *o, int i}
746 Return the \var{i}th element of \var{o}, assuming that \var{o} was
Fred Drakec37b65e2001-11-28 07:26:15 +0000747 returned by \cfunction{PySequence_Fast()}, \var{o} is not \NULL,
Tim Peters1fc240e2001-10-26 05:06:50 +0000748 and that \var{i} is within bounds.
749\end{cfuncdesc}
750
751\begin{cfuncdesc}{int}{PySequence_Fast_GET_SIZE}{PyObject *o}
752 Returns the length of \var{o}, assuming that \var{o} was
753 returned by \cfunction{PySequence_Fast()} and that \var{o} is
Fred Drakec37b65e2001-11-28 07:26:15 +0000754 not \NULL. The size can also be gotten by calling
Tim Peters1fc240e2001-10-26 05:06:50 +0000755 \cfunction{PySequence_Size()} on \var{o}, but
756 \cfunction{PySequence_Fast_GET_SIZE()} is faster because it can
757 assume \var{o} is a list or tuple.
Fred Drake3adf79e2001-10-12 19:01:43 +0000758\end{cfuncdesc}
759
760
761\section{Mapping Protocol \label{mapping}}
762
763\begin{cfuncdesc}{int}{PyMapping_Check}{PyObject *o}
764 Return \code{1} if the object provides mapping protocol, and
765 \code{0} otherwise. This function always succeeds.
766\end{cfuncdesc}
767
768
769\begin{cfuncdesc}{int}{PyMapping_Length}{PyObject *o}
770 Returns the number of keys in object \var{o} on success, and
771 \code{-1} on failure. For objects that do not provide mapping
772 protocol, this is equivalent to the Python expression
773 \samp{len(\var{o})}.\bifuncindex{len}
774\end{cfuncdesc}
775
776
777\begin{cfuncdesc}{int}{PyMapping_DelItemString}{PyObject *o, char *key}
778 Remove the mapping for object \var{key} from the object \var{o}.
779 Return \code{-1} on failure. This is equivalent to the Python
780 statement \samp{del \var{o}[\var{key}]}.
781\end{cfuncdesc}
782
783
784\begin{cfuncdesc}{int}{PyMapping_DelItem}{PyObject *o, PyObject *key}
785 Remove the mapping for object \var{key} from the object \var{o}.
786 Return \code{-1} on failure. This is equivalent to the Python
787 statement \samp{del \var{o}[\var{key}]}.
788\end{cfuncdesc}
789
790
791\begin{cfuncdesc}{int}{PyMapping_HasKeyString}{PyObject *o, char *key}
792 On success, return \code{1} if the mapping object has the key
793 \var{key} and \code{0} otherwise. This is equivalent to the Python
794 expression \samp{\var{o}.has_key(\var{key})}. This function always
795 succeeds.
796\end{cfuncdesc}
797
798
799\begin{cfuncdesc}{int}{PyMapping_HasKey}{PyObject *o, PyObject *key}
800 Return \code{1} if the mapping object has the key \var{key} and
801 \code{0} otherwise. This is equivalent to the Python expression
802 \samp{\var{o}.has_key(\var{key})}. This function always succeeds.
803\end{cfuncdesc}
804
805
806\begin{cfuncdesc}{PyObject*}{PyMapping_Keys}{PyObject *o}
807 On success, return a list of the keys in object \var{o}. On
808 failure, return \NULL. This is equivalent to the Python expression
809 \samp{\var{o}.keys()}.
810\end{cfuncdesc}
811
812
813\begin{cfuncdesc}{PyObject*}{PyMapping_Values}{PyObject *o}
814 On success, return a list of the values in object \var{o}. On
815 failure, return \NULL. This is equivalent to the Python expression
816 \samp{\var{o}.values()}.
817\end{cfuncdesc}
818
819
820\begin{cfuncdesc}{PyObject*}{PyMapping_Items}{PyObject *o}
821 On success, return a list of the items in object \var{o}, where each
822 item is a tuple containing a key-value pair. On failure, return
823 \NULL. This is equivalent to the Python expression
824 \samp{\var{o}.items()}.
825\end{cfuncdesc}
826
827
828\begin{cfuncdesc}{PyObject*}{PyMapping_GetItemString}{PyObject *o, char *key}
829 Return element of \var{o} corresponding to the object \var{key} or
830 \NULL{} on failure. This is the equivalent of the Python expression
831 \samp{\var{o}[\var{key}]}.
832\end{cfuncdesc}
833
834\begin{cfuncdesc}{int}{PyMapping_SetItemString}{PyObject *o, char *key,
835 PyObject *v}
836 Map the object \var{key} to the value \var{v} in object \var{o}.
837 Returns \code{-1} on failure. This is the equivalent of the Python
838 statement \samp{\var{o}[\var{key}] = \var{v}}.
839\end{cfuncdesc}
840
841
842\section{Iterator Protocol \label{iterator}}
843
844\versionadded{2.2}
845
846There are only a couple of functions specifically for working with
847iterators.
848
849\begin{cfuncdesc}{int}{PyIter_Check}{PyObject *o}
850 Return true if the object \var{o} supports the iterator protocol.
851\end{cfuncdesc}
852
853\begin{cfuncdesc}{PyObject*}{PyIter_Next}{PyObject *o}
854 Return the next value from the iteration \var{o}. If the object is
855 an iterator, this retrieves the next value from the iteration, and
856 returns \NULL{} with no exception set if there are no remaining
857 items. If the object is not an iterator, \exception{TypeError} is
858 raised, or if there is an error in retrieving the item, returns
859 \NULL{} and passes along the exception.
860\end{cfuncdesc}
861
862To write a loop which iterates over an iterator, the C code should
863look something like this:
864
865\begin{verbatim}
Fred Drake314bae52002-03-11 18:46:29 +0000866PyObject *iterator = PyObject_GetIter(obj);
Fred Drake3adf79e2001-10-12 19:01:43 +0000867PyObject *item;
868
Fred Drake314bae52002-03-11 18:46:29 +0000869if (iterator == NULL) {
870 /* propagate error */
871}
872
873while (item = PyIter_Next(iterator)) {
Fred Drake3adf79e2001-10-12 19:01:43 +0000874 /* do something with item */
Fred Drake8b8fe282001-12-26 16:53:48 +0000875 ...
876 /* release reference when done */
877 Py_DECREF(item);
Fred Drake3adf79e2001-10-12 19:01:43 +0000878}
Fred Drake314bae52002-03-11 18:46:29 +0000879
880Py_DECREF(iterator);
881
Fred Drake3adf79e2001-10-12 19:01:43 +0000882if (PyErr_Occurred()) {
Fred Drake314bae52002-03-11 18:46:29 +0000883 /* propagate error */
Fred Drake3adf79e2001-10-12 19:01:43 +0000884}
885else {
886 /* continue doing useful work */
887}
Fred Drake8b8fe282001-12-26 16:53:48 +0000888\end{verbatim}
889
Jeremy Hylton89c3a222001-11-09 21:59:42 +0000890
Fred Drake94ead572001-11-09 23:34:26 +0000891\section{Buffer Protocol \label{abstract-buffer}}
Jeremy Hylton89c3a222001-11-09 21:59:42 +0000892
893\begin{cfuncdesc}{int}{PyObject_AsCharBuffer}{PyObject *obj,
Fred Drake94ead572001-11-09 23:34:26 +0000894 const char **buffer,
895 int *buffer_len}
Jeremy Hylton89c3a222001-11-09 21:59:42 +0000896 Returns a pointer to a read-only memory location useable as character-
897 based input. The \var{obj} argument must support the single-segment
898 character buffer interface. On success, returns \code{1}, sets
Thomas Hellerbfeeeee2001-11-12 07:46:31 +0000899 \var{buffer} to the memory location and \var{buffer_len} to the buffer
Jeremy Hylton89c3a222001-11-09 21:59:42 +0000900 length. Returns \code{0} and sets a \exception{TypeError} on error.
Fred Drake94ead572001-11-09 23:34:26 +0000901 \versionadded{1.6}
Jeremy Hylton89c3a222001-11-09 21:59:42 +0000902\end{cfuncdesc}
903
904\begin{cfuncdesc}{int}{PyObject_AsReadBuffer}{PyObject *obj,
Fred Drake94ead572001-11-09 23:34:26 +0000905 const char **buffer,
906 int *buffer_len}
Jeremy Hylton89c3a222001-11-09 21:59:42 +0000907 Returns a pointer to a read-only memory location containing
908 arbitrary data. The \var{obj} argument must support the
909 single-segment readable buffer interface. On success, returns
Thomas Hellerbfeeeee2001-11-12 07:46:31 +0000910 \code{1}, sets \var{buffer} to the memory location and \var{buffer_len}
Jeremy Hylton89c3a222001-11-09 21:59:42 +0000911 to the buffer length. Returns \code{0} and sets a
912 \exception{TypeError} on error.
Fred Drake94ead572001-11-09 23:34:26 +0000913 \versionadded{1.6}
Jeremy Hylton89c3a222001-11-09 21:59:42 +0000914\end{cfuncdesc}
915
916\begin{cfuncdesc}{int}{PyObject_CheckReadBuffer}{PyObject *o}
917 Returns \code{1} if \var{o} supports the single-segment readable
918 buffer interface. Otherwise returns \code{0}.
Fred Drake94ead572001-11-09 23:34:26 +0000919 \versionadded{2.2}
Fred Drake8b8fe282001-12-26 16:53:48 +0000920\end{cfuncdesc}
Jeremy Hylton89c3a222001-11-09 21:59:42 +0000921
922\begin{cfuncdesc}{int}{PyObject_AsWriteBuffer}{PyObject *obj,
Fred Drake94ead572001-11-09 23:34:26 +0000923 const char **buffer,
924 int *buffer_len}
Jeremy Hylton89c3a222001-11-09 21:59:42 +0000925 Returns a pointer to a writeable memory location. The \var{obj}
926 argument must support the single-segment, character buffer
927 interface. On success, returns \code{1}, sets \var{buffer} to the
Thomas Hellerbfeeeee2001-11-12 07:46:31 +0000928 memory location and \var{buffer_len} to the buffer length. Returns
Jeremy Hylton89c3a222001-11-09 21:59:42 +0000929 \code{0} and sets a \exception{TypeError} on error.
Fred Drake94ead572001-11-09 23:34:26 +0000930 \versionadded{1.6}
Jeremy Hylton89c3a222001-11-09 21:59:42 +0000931\end{cfuncdesc}