blob: 8d271df571f5b7a1b37bf960e959e57780276bc1 [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
128 \function{unistr()}\bifuncindex{unistr} built-in function.
129\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
186\begin{cfuncdesc}{PyObject*}{PyObject_CallFunction}{PyObject *callable_object,
187 char *format, ...}
188 Call a callable Python object \var{callable_object}, with a variable
189 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
193 equivalent of the Python expression
194 \samp{apply(\var{callable_object}\var{args})} or
195 \samp{\var{callable_object}(*\var{args})}.
196 \bifuncindex{apply}
197\end{cfuncdesc}
198
199
200\begin{cfuncdesc}{PyObject*}{PyObject_CallMethod}{PyObject *o,
201 char *method, char *format, ...}
202 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
207 the Python expression \samp{\var{o}.\var{method}(\var{args})}. Note
208 that special method names, such as \method{__add__()},
209 \method{__getitem__()}, and so on are not supported. The specific
210 abstract-object routines for these must be used.
211\end{cfuncdesc}
212
213
214\begin{cfuncdesc}{int}{PyObject_Hash}{PyObject *o}
215 Compute and return the hash value of an object \var{o}. On failure,
216 return \code{-1}. This is the equivalent of the Python expression
217 \samp{hash(\var{o})}.\bifuncindex{hash}
218\end{cfuncdesc}
219
220
221\begin{cfuncdesc}{int}{PyObject_IsTrue}{PyObject *o}
222 Returns \code{1} if the object \var{o} is considered to be true, and
223 \code{0} otherwise. This is equivalent to the Python expression
224 \samp{not not \var{o}}. This function always succeeds.
225\end{cfuncdesc}
226
227
228\begin{cfuncdesc}{PyObject*}{PyObject_Type}{PyObject *o}
229 When \var{o} is non-\NULL, returns a type object corresponding to
230 the object type of object \var{o}. On failure, raises
231 \exception{SystemError} and returns \NULL. This is equivalent to
232 the Python expression \code{type(\var{o})}.
233 \bifuncindex{type}
234\end{cfuncdesc}
235
236\begin{cfuncdesc}{int}{PyObject_TypeCheck}{PyObject *o, PyTypeObject *type}
237 Return true if the object \var{o} is of type \var{type} or a subtype
238 of \var{type}. Both parameters must be non-\NULL.
239 \versionadded{2.2}
240\end{cfuncdesc}
241
242\begin{cfuncdesc}{int}{PyObject_Length}{PyObject *o}
243 Return the length of object \var{o}. If the object \var{o} provides
244 both sequence and mapping protocols, the sequence length is
245 returned. On error, \code{-1} is returned. This is the equivalent
246 to the Python expression \samp{len(\var{o})}.\bifuncindex{len}
247\end{cfuncdesc}
248
249
250\begin{cfuncdesc}{PyObject*}{PyObject_GetItem}{PyObject *o, PyObject *key}
251 Return element of \var{o} corresponding to the object \var{key} or
252 \NULL{} on failure. This is the equivalent of the Python expression
253 \samp{\var{o}[\var{key}]}.
254\end{cfuncdesc}
255
256
257\begin{cfuncdesc}{int}{PyObject_SetItem}{PyObject *o,
258 PyObject *key, PyObject *v}
259 Map the object \var{key} to the value \var{v}. Returns \code{-1} on
260 failure. This is the equivalent of the Python statement
261 \samp{\var{o}[\var{key}] = \var{v}}.
262\end{cfuncdesc}
263
264
265\begin{cfuncdesc}{int}{PyObject_DelItem}{PyObject *o, PyObject *key}
266 Delete the mapping for \var{key} from \var{o}. Returns \code{-1} on
267 failure. This is the equivalent of the Python statement \samp{del
268 \var{o}[\var{key}]}.
269\end{cfuncdesc}
270
271\begin{cfuncdesc}{int}{PyObject_AsFileDescriptor}{PyObject *o}
272 Derives a file-descriptor from a Python object. If the object is an
273 integer or long integer, its value is returned. If not, the
274 object's \method{fileno()} method is called if it exists; the method
275 must return an integer or long integer, which is returned as the
276 file descriptor value. Returns \code{-1} on failure.
277\end{cfuncdesc}
278
279\begin{cfuncdesc}{PyObject*}{PyObject_Dir}{PyObject *o}
280 This is equivalent to the Python expression \samp{dir(\var{o})},
281 returning a (possibly empty) list of strings appropriate for the
282 object argument, or \NULL{} if there was an error. If the argument
283 is \NULL, this is like the Python \samp{dir()}, returning the names
284 of the current locals; in this case, if no execution frame is active
285 then \NULL{} is returned but \cfunction{PyErr_Occurred()} will
286 return false.
287\end{cfuncdesc}
288
289
290\section{Number Protocol \label{number}}
291
292\begin{cfuncdesc}{int}{PyNumber_Check}{PyObject *o}
293 Returns \code{1} if the object \var{o} provides numeric protocols,
294 and false otherwise. This function always succeeds.
295\end{cfuncdesc}
296
297
298\begin{cfuncdesc}{PyObject*}{PyNumber_Add}{PyObject *o1, PyObject *o2}
299 Returns the result of adding \var{o1} and \var{o2}, or \NULL{} on
300 failure. This is the equivalent of the Python expression
301 \samp{\var{o1} + \var{o2}}.
302\end{cfuncdesc}
303
304
305\begin{cfuncdesc}{PyObject*}{PyNumber_Subtract}{PyObject *o1, PyObject *o2}
306 Returns the result of subtracting \var{o2} from \var{o1}, or \NULL{}
307 on failure. This is the equivalent of the Python expression
308 \samp{\var{o1} - \var{o2}}.
309\end{cfuncdesc}
310
311
312\begin{cfuncdesc}{PyObject*}{PyNumber_Multiply}{PyObject *o1, PyObject *o2}
313 Returns the result of multiplying \var{o1} and \var{o2}, or \NULL{}
314 on failure. This is the equivalent of the Python expression
315 \samp{\var{o1} * \var{o2}}.
316\end{cfuncdesc}
317
318
319\begin{cfuncdesc}{PyObject*}{PyNumber_Divide}{PyObject *o1, PyObject *o2}
320 Returns the result of dividing \var{o1} by \var{o2}, or \NULL{} on
321 failure. This is the equivalent of the Python expression
322 \samp{\var{o1} / \var{o2}}.
323\end{cfuncdesc}
324
325
326\begin{cfuncdesc}{PyObject*}{PyNumber_FloorDivide}{PyObject *o1, PyObject *o2}
327 Return the floor of \var{o1} divided by \var{o2}, or \NULL{} on
328 failure. This is equivalent to the ``classic'' division of
329 integers.
330 \versionadded{2.2}
331\end{cfuncdesc}
332
333
334\begin{cfuncdesc}{PyObject*}{PyNumber_TrueDivide}{PyObject *o1, PyObject *o2}
335 Return a reasonable approximation for the mathematical value of
336 \var{o1} divided by \var{o2}, or \NULL{} on failure. The return
337 value is ``approximate'' because binary floating point numbers are
338 approximate; it is not possible to represent all real numbers in
339 base two. This function can return a floating point value when
340 passed two integers.
341 \versionadded{2.2}
342\end{cfuncdesc}
343
344
345\begin{cfuncdesc}{PyObject*}{PyNumber_Remainder}{PyObject *o1, PyObject *o2}
346 Returns the remainder of dividing \var{o1} by \var{o2}, or \NULL{}
347 on failure. This is the equivalent of the Python expression
348 \samp{\var{o1} \%\ \var{o2}}.
349\end{cfuncdesc}
350
351
352\begin{cfuncdesc}{PyObject*}{PyNumber_Divmod}{PyObject *o1, PyObject *o2}
353 See the built-in function \function{divmod()}\bifuncindex{divmod}.
354 Returns \NULL{} on failure. This is the equivalent of the Python
355 expression \samp{divmod(\var{o1}, \var{o2})}.
356\end{cfuncdesc}
357
358
359\begin{cfuncdesc}{PyObject*}{PyNumber_Power}{PyObject *o1,
360 PyObject *o2, PyObject *o3}
361 See the built-in function \function{pow()}\bifuncindex{pow}.
362 Returns \NULL{} on failure. This is the equivalent of the Python
363 expression \samp{pow(\var{o1}, \var{o2}, \var{o3})}, where \var{o3}
364 is optional. If \var{o3} is to be ignored, pass \cdata{Py_None} in
365 its place (passing \NULL{} for \var{o3} would cause an illegal
366 memory access).
367\end{cfuncdesc}
368
369
370\begin{cfuncdesc}{PyObject*}{PyNumber_Negative}{PyObject *o}
371 Returns the negation of \var{o} on success, or \NULL{} on failure.
372 This is the equivalent of the Python expression \samp{-\var{o}}.
373\end{cfuncdesc}
374
375
376\begin{cfuncdesc}{PyObject*}{PyNumber_Positive}{PyObject *o}
377 Returns \var{o} on success, or \NULL{} on failure. This is the
378 equivalent of the Python expression \samp{+\var{o}}.
379\end{cfuncdesc}
380
381
382\begin{cfuncdesc}{PyObject*}{PyNumber_Absolute}{PyObject *o}
383 Returns the absolute value of \var{o}, or \NULL{} on failure. This
384 is the equivalent of the Python expression \samp{abs(\var{o})}.
385 \bifuncindex{abs}
386\end{cfuncdesc}
387
388
389\begin{cfuncdesc}{PyObject*}{PyNumber_Invert}{PyObject *o}
390 Returns the bitwise negation of \var{o} on success, or \NULL{} on
391 failure. This is the equivalent of the Python expression
392 \samp{\~\var{o}}.
393\end{cfuncdesc}
394
395
396\begin{cfuncdesc}{PyObject*}{PyNumber_Lshift}{PyObject *o1, PyObject *o2}
397 Returns the result of left shifting \var{o1} by \var{o2} on success,
398 or \NULL{} on failure. This is the equivalent of the Python
399 expression \samp{\var{o1} <\code{<} \var{o2}}.
400\end{cfuncdesc}
401
402
403\begin{cfuncdesc}{PyObject*}{PyNumber_Rshift}{PyObject *o1, PyObject *o2}
404 Returns the result of right shifting \var{o1} by \var{o2} on
405 success, or \NULL{} on failure. This is the equivalent of the
406 Python expression \samp{\var{o1} >\code{>} \var{o2}}.
407\end{cfuncdesc}
408
409
410\begin{cfuncdesc}{PyObject*}{PyNumber_And}{PyObject *o1, PyObject *o2}
411 Returns the ``bitwise and'' of \var{o2} and \var{o2} on success and
412 \NULL{} on failure. This is the equivalent of the Python expression
413 \samp{\var{o1} \&\ \var{o2}}.
414\end{cfuncdesc}
415
416
417\begin{cfuncdesc}{PyObject*}{PyNumber_Xor}{PyObject *o1, PyObject *o2}
418 Returns the ``bitwise exclusive or'' of \var{o1} by \var{o2} on
419 success, or \NULL{} on failure. This is the equivalent of the
420 Python expression \samp{\var{o1} \textasciicircum{} \var{o2}}.
421\end{cfuncdesc}
422
423\begin{cfuncdesc}{PyObject*}{PyNumber_Or}{PyObject *o1, PyObject *o2}
424 Returns the ``bitwise or'' of \var{o1} and \var{o2} on success, or
425 \NULL{} on failure. This is the equivalent of the Python expression
426 \samp{\var{o1} | \var{o2}}.
427\end{cfuncdesc}
428
429
430\begin{cfuncdesc}{PyObject*}{PyNumber_InPlaceAdd}{PyObject *o1, PyObject *o2}
431 Returns the result of adding \var{o1} and \var{o2}, or \NULL{} on
432 failure. The operation is done \emph{in-place} when \var{o1}
433 supports it. This is the equivalent of the Python statement
434 \samp{\var{o1} += \var{o2}}.
435\end{cfuncdesc}
436
437
438\begin{cfuncdesc}{PyObject*}{PyNumber_InPlaceSubtract}{PyObject *o1,
439 PyObject *o2}
440 Returns the result of subtracting \var{o2} from \var{o1}, or \NULL{}
441 on failure. The operation is done \emph{in-place} when \var{o1}
442 supports it. This is the equivalent of the Python statement
443 \samp{\var{o1} -= \var{o2}}.
444\end{cfuncdesc}
445
446
447\begin{cfuncdesc}{PyObject*}{PyNumber_InPlaceMultiply}{PyObject *o1,
448 PyObject *o2}
449 Returns the result of multiplying \var{o1} and \var{o2}, or \NULL{}
450 on failure. The operation is done \emph{in-place} when \var{o1}
451 supports it. This is the equivalent of the Python statement
452 \samp{\var{o1} *= \var{o2}}.
453\end{cfuncdesc}
454
455
456\begin{cfuncdesc}{PyObject*}{PyNumber_InPlaceDivide}{PyObject *o1,
457 PyObject *o2}
458 Returns the result of dividing \var{o1} by \var{o2}, or \NULL{} on
459 failure. The operation is done \emph{in-place} when \var{o1}
460 supports it. This is the equivalent of the Python statement
461 \samp{\var{o1} /= \var{o2}}.
462\end{cfuncdesc}
463
464
465\begin{cfuncdesc}{PyObject*}{PyNumber_InPlaceFloorDivide}{PyObject *o1,
466 PyObject *o2}
467 Returns the mathematical of dividing \var{o1} by \var{o2}, or
468 \NULL{} on failure. The operation is done \emph{in-place} when
469 \var{o1} supports it. This is the equivalent of the Python
470 statement \samp{\var{o1} //= \var{o2}}.
471 \versionadded{2.2}
472\end{cfuncdesc}
473
474
475\begin{cfuncdesc}{PyObject*}{PyNumber_InPlaceTrueDivide}{PyObject *o1,
476 PyObject *o2}
477 Return a reasonable approximation for the mathematical value of
478 \var{o1} divided by \var{o2}, or \NULL{} on failure. The return
479 value is ``approximate'' because binary floating point numbers are
480 approximate; it is not possible to represent all real numbers in
481 base two. This function can return a floating point value when
482 passed two integers. The operation is done \emph{in-place} when
483 \var{o1} supports it.
484 \versionadded{2.2}
485\end{cfuncdesc}
486
487
488\begin{cfuncdesc}{PyObject*}{PyNumber_InPlaceRemainder}{PyObject *o1,
489 PyObject *o2}
490 Returns the remainder of dividing \var{o1} by \var{o2}, or \NULL{}
491 on failure. The operation is done \emph{in-place} when \var{o1}
492 supports it. This is the equivalent of the Python statement
493 \samp{\var{o1} \%= \var{o2}}.
494\end{cfuncdesc}
495
496
497\begin{cfuncdesc}{PyObject*}{PyNumber_InPlacePower}{PyObject *o1,
498 PyObject *o2, PyObject *o3}
499 See the built-in function \function{pow()}.\bifuncindex{pow}
500 Returns \NULL{} on failure. The operation is done \emph{in-place}
501 when \var{o1} supports it. This is the equivalent of the Python
502 statement \samp{\var{o1} **= \var{o2}} when o3 is \cdata{Py_None},
503 or an in-place variant of \samp{pow(\var{o1}, \var{o2}, \var{o3})}
504 otherwise. If \var{o3} is to be ignored, pass \cdata{Py_None} in its
505 place (passing \NULL{} for \var{o3} would cause an illegal memory
506 access).
507\end{cfuncdesc}
508
509\begin{cfuncdesc}{PyObject*}{PyNumber_InPlaceLshift}{PyObject *o1,
510 PyObject *o2}
511 Returns the result of left shifting \var{o1} by \var{o2} on success,
512 or \NULL{} on failure. The operation is done \emph{in-place} when
513 \var{o1} supports it. This is the equivalent of the Python
514 statement \samp{\var{o1} <\code{<=} \var{o2}}.
515\end{cfuncdesc}
516
517
518\begin{cfuncdesc}{PyObject*}{PyNumber_InPlaceRshift}{PyObject *o1,
519 PyObject *o2}
520 Returns the result of right shifting \var{o1} by \var{o2} on
521 success, or \NULL{} on failure. The operation is done
522 \emph{in-place} when \var{o1} supports it. This is the equivalent
523 of the Python statement \samp{\var{o1} >\code{>=} \var{o2}}.
524\end{cfuncdesc}
525
526
527\begin{cfuncdesc}{PyObject*}{PyNumber_InPlaceAnd}{PyObject *o1, PyObject *o2}
528 Returns the ``bitwise and'' of \var{o1} and \var{o2} on success and
529 \NULL{} on failure. The operation is done \emph{in-place} when
530 \var{o1} supports it. This is the equivalent of the Python
531 statement \samp{\var{o1} \&= \var{o2}}.
532\end{cfuncdesc}
533
534
535\begin{cfuncdesc}{PyObject*}{PyNumber_InPlaceXor}{PyObject *o1, PyObject *o2}
536 Returns the ``bitwise exclusive or'' of \var{o1} by \var{o2} on
537 success, or \NULL{} on failure. The operation is done
538 \emph{in-place} when \var{o1} supports it. This is the equivalent
539 of the Python statement \samp{\var{o1} \textasciicircum= \var{o2}}.
540\end{cfuncdesc}
541
542\begin{cfuncdesc}{PyObject*}{PyNumber_InPlaceOr}{PyObject *o1, PyObject *o2}
543 Returns the ``bitwise or'' of \var{o1} and \var{o2} on success, or
544 \NULL{} on failure. The operation is done \emph{in-place} when
545 \var{o1} supports it. This is the equivalent of the Python
546 statement \samp{\var{o1} |= \var{o2}}.
547\end{cfuncdesc}
548
549\begin{cfuncdesc}{int}{PyNumber_Coerce}{PyObject **p1, PyObject **p2}
550 This function takes the addresses of two variables of type
551 \ctype{PyObject*}. If the objects pointed to by \code{*\var{p1}}
552 and \code{*\var{p2}} have the same type, increment their reference
553 count and return \code{0} (success). If the objects can be converted
554 to a common numeric type, replace \code{*p1} and \code{*p2} by their
555 converted value (with 'new' reference counts), and return \code{0}.
556 If no conversion is possible, or if some other error occurs, return
557 \code{-1} (failure) and don't increment the reference counts. The
558 call \code{PyNumber_Coerce(\&o1, \&o2)} is equivalent to the Python
559 statement \samp{\var{o1}, \var{o2} = coerce(\var{o1}, \var{o2})}.
560 \bifuncindex{coerce}
561\end{cfuncdesc}
562
563\begin{cfuncdesc}{PyObject*}{PyNumber_Int}{PyObject *o}
564 Returns the \var{o} converted to an integer object on success, or
565 \NULL{} on failure. This is the equivalent of the Python expression
566 \samp{int(\var{o})}.\bifuncindex{int}
567\end{cfuncdesc}
568
569\begin{cfuncdesc}{PyObject*}{PyNumber_Long}{PyObject *o}
570 Returns the \var{o} converted to a long integer object on success,
571 or \NULL{} on failure. This is the equivalent of the Python
572 expression \samp{long(\var{o})}.\bifuncindex{long}
573\end{cfuncdesc}
574
575\begin{cfuncdesc}{PyObject*}{PyNumber_Float}{PyObject *o}
576 Returns the \var{o} converted to a float object on success, or
577 \NULL{} on failure. This is the equivalent of the Python expression
578 \samp{float(\var{o})}.\bifuncindex{float}
579\end{cfuncdesc}
580
581
582\section{Sequence Protocol \label{sequence}}
583
584\begin{cfuncdesc}{int}{PySequence_Check}{PyObject *o}
585 Return \code{1} if the object provides sequence protocol, and
586 \code{0} otherwise. This function always succeeds.
587\end{cfuncdesc}
588
589\begin{cfuncdesc}{int}{PySequence_Size}{PyObject *o}
590 Returns the number of objects in sequence \var{o} on success, and
591 \code{-1} on failure. For objects that do not provide sequence
592 protocol, this is equivalent to the Python expression
593 \samp{len(\var{o})}.\bifuncindex{len}
594\end{cfuncdesc}
595
596\begin{cfuncdesc}{int}{PySequence_Length}{PyObject *o}
597 Alternate name for \cfunction{PySequence_Size()}.
598\end{cfuncdesc}
599
600\begin{cfuncdesc}{PyObject*}{PySequence_Concat}{PyObject *o1, PyObject *o2}
601 Return the concatenation of \var{o1} and \var{o2} on success, and
602 \NULL{} on failure. This is the equivalent of the Python
603 expression \samp{\var{o1} + \var{o2}}.
604\end{cfuncdesc}
605
606
607\begin{cfuncdesc}{PyObject*}{PySequence_Repeat}{PyObject *o, int count}
608 Return the result of repeating sequence object \var{o} \var{count}
609 times, or \NULL{} on failure. This is the equivalent of the Python
610 expression \samp{\var{o} * \var{count}}.
611\end{cfuncdesc}
612
613\begin{cfuncdesc}{PyObject*}{PySequence_InPlaceConcat}{PyObject *o1,
614 PyObject *o2}
615 Return the concatenation of \var{o1} and \var{o2} on success, and
616 \NULL{} on failure. The operation is done \emph{in-place} when
617 \var{o1} supports it. This is the equivalent of the Python
618 expression \samp{\var{o1} += \var{o2}}.
619\end{cfuncdesc}
620
621
622\begin{cfuncdesc}{PyObject*}{PySequence_InPlaceRepeat}{PyObject *o, int count}
623 Return the result of repeating sequence object \var{o} \var{count}
624 times, or \NULL{} on failure. The operation is done \emph{in-place}
625 when \var{o} supports it. This is the equivalent of the Python
626 expression \samp{\var{o} *= \var{count}}.
627\end{cfuncdesc}
628
629
630\begin{cfuncdesc}{PyObject*}{PySequence_GetItem}{PyObject *o, int i}
631 Return the \var{i}th element of \var{o}, or \NULL{} on failure.
632 This is the equivalent of the Python expression
633 \samp{\var{o}[\var{i}]}.
634\end{cfuncdesc}
635
636
637\begin{cfuncdesc}{PyObject*}{PySequence_GetSlice}{PyObject *o, int i1, int i2}
638 Return the slice of sequence object \var{o} between \var{i1} and
639 \var{i2}, or \NULL{} on failure. This is the equivalent of the
640 Python expression \samp{\var{o}[\var{i1}:\var{i2}]}.
641\end{cfuncdesc}
642
643
644\begin{cfuncdesc}{int}{PySequence_SetItem}{PyObject *o, int i, PyObject *v}
645 Assign object \var{v} to the \var{i}th element of \var{o}. Returns
646 \code{-1} on failure. This is the equivalent of the Python
647 statement \samp{\var{o}[\var{i}] = \var{v}}.
648\end{cfuncdesc}
649
650\begin{cfuncdesc}{int}{PySequence_DelItem}{PyObject *o, int i}
651 Delete the \var{i}th element of object \var{o}. Returns \code{-1}
652 on failure. This is the equivalent of the Python statement
653 \samp{del \var{o}[\var{i}]}.
654\end{cfuncdesc}
655
656\begin{cfuncdesc}{int}{PySequence_SetSlice}{PyObject *o, int i1,
657 int i2, PyObject *v}
658 Assign the sequence object \var{v} to the slice in sequence object
659 \var{o} from \var{i1} to \var{i2}. This is the equivalent of the
660 Python statement \samp{\var{o}[\var{i1}:\var{i2}] = \var{v}}.
661\end{cfuncdesc}
662
663\begin{cfuncdesc}{int}{PySequence_DelSlice}{PyObject *o, int i1, int i2}
664 Delete the slice in sequence object \var{o} from \var{i1} to
665 \var{i2}. Returns \code{-1} on failure. This is the equivalent of
666 the Python statement \samp{del \var{o}[\var{i1}:\var{i2}]}.
667\end{cfuncdesc}
668
669\begin{cfuncdesc}{PyObject*}{PySequence_Tuple}{PyObject *o}
670 Returns the \var{o} as a tuple on success, and \NULL{} on failure.
671 This is equivalent to the Python expression \samp{tuple(\var{o})}.
672 \bifuncindex{tuple}
673\end{cfuncdesc}
674
675\begin{cfuncdesc}{int}{PySequence_Count}{PyObject *o, PyObject *value}
676 Return the number of occurrences of \var{value} in \var{o}, that is,
677 return the number of keys for which \code{\var{o}[\var{key}] ==
678 \var{value}}. On failure, return \code{-1}. This is equivalent to
679 the Python expression \samp{\var{o}.count(\var{value})}.
680\end{cfuncdesc}
681
682\begin{cfuncdesc}{int}{PySequence_Contains}{PyObject *o, PyObject *value}
683 Determine if \var{o} contains \var{value}. If an item in \var{o} is
684 equal to \var{value}, return \code{1}, otherwise return \code{0}.
685 On error, return \code{-1}. This is equivalent to the Python
686 expression \samp{\var{value} in \var{o}}.
687\end{cfuncdesc}
688
689\begin{cfuncdesc}{int}{PySequence_Index}{PyObject *o, PyObject *value}
690 Return the first index \var{i} for which \code{\var{o}[\var{i}] ==
691 \var{value}}. On error, return \code{-1}. This is equivalent to
692 the Python expression \samp{\var{o}.index(\var{value})}.
693\end{cfuncdesc}
694
695\begin{cfuncdesc}{PyObject*}{PySequence_List}{PyObject *o}
696 Return a list object with the same contents as the arbitrary
697 sequence \var{o}. The returned list is guaranteed to be new.
698\end{cfuncdesc}
699
700\begin{cfuncdesc}{PyObject*}{PySequence_Tuple}{PyObject *o}
701 Return a tuple object with the same contents as the arbitrary
702 sequence \var{o}. If \var{o} is a tuple, a new reference will be
703 returned, otherwise a tuple will be constructed with the appropriate
704 contents.
705\end{cfuncdesc}
706
707\begin{cfuncdesc}{PyObject*}{PySequence_Fast}{PyObject *o, const char *m}
708 Returns the sequence \var{o} as a tuple, unless it is already a
709 tuple or list, in which case \var{o} is returned. Use
710 \cfunction{PySequence_Fast_GET_ITEM()} to access the members of the
711 result. Returns \NULL{} on failure. If the object is not a
712 sequence, raises \exception{TypeError} with \var{m} as the message
713 text.
714\end{cfuncdesc}
715
716\begin{cfuncdesc}{PyObject*}{PySequence_Fast_GET_ITEM}{PyObject *o, int i}
717 Return the \var{i}th element of \var{o}, assuming that \var{o} was
718 returned by \cfunction{PySequence_Fast()}, and that \var{i} is
719 within bounds. The caller is expected to get the length of the
720 sequence by calling \cfunction{PySequence_Size()} on \var{o}, since
721 lists and tuples are guaranteed to always return their true length.
722\end{cfuncdesc}
723
724
725\section{Mapping Protocol \label{mapping}}
726
727\begin{cfuncdesc}{int}{PyMapping_Check}{PyObject *o}
728 Return \code{1} if the object provides mapping protocol, and
729 \code{0} otherwise. This function always succeeds.
730\end{cfuncdesc}
731
732
733\begin{cfuncdesc}{int}{PyMapping_Length}{PyObject *o}
734 Returns the number of keys in object \var{o} on success, and
735 \code{-1} on failure. For objects that do not provide mapping
736 protocol, this is equivalent to the Python expression
737 \samp{len(\var{o})}.\bifuncindex{len}
738\end{cfuncdesc}
739
740
741\begin{cfuncdesc}{int}{PyMapping_DelItemString}{PyObject *o, char *key}
742 Remove the mapping for object \var{key} from the object \var{o}.
743 Return \code{-1} on failure. This is equivalent to the Python
744 statement \samp{del \var{o}[\var{key}]}.
745\end{cfuncdesc}
746
747
748\begin{cfuncdesc}{int}{PyMapping_DelItem}{PyObject *o, PyObject *key}
749 Remove the mapping for object \var{key} from the object \var{o}.
750 Return \code{-1} on failure. This is equivalent to the Python
751 statement \samp{del \var{o}[\var{key}]}.
752\end{cfuncdesc}
753
754
755\begin{cfuncdesc}{int}{PyMapping_HasKeyString}{PyObject *o, char *key}
756 On success, return \code{1} if the mapping object has the key
757 \var{key} and \code{0} otherwise. This is equivalent to the Python
758 expression \samp{\var{o}.has_key(\var{key})}. This function always
759 succeeds.
760\end{cfuncdesc}
761
762
763\begin{cfuncdesc}{int}{PyMapping_HasKey}{PyObject *o, PyObject *key}
764 Return \code{1} if the mapping object has the key \var{key} and
765 \code{0} otherwise. This is equivalent to the Python expression
766 \samp{\var{o}.has_key(\var{key})}. This function always succeeds.
767\end{cfuncdesc}
768
769
770\begin{cfuncdesc}{PyObject*}{PyMapping_Keys}{PyObject *o}
771 On success, return a list of the keys in object \var{o}. On
772 failure, return \NULL. This is equivalent to the Python expression
773 \samp{\var{o}.keys()}.
774\end{cfuncdesc}
775
776
777\begin{cfuncdesc}{PyObject*}{PyMapping_Values}{PyObject *o}
778 On success, return a list of the values in object \var{o}. On
779 failure, return \NULL. This is equivalent to the Python expression
780 \samp{\var{o}.values()}.
781\end{cfuncdesc}
782
783
784\begin{cfuncdesc}{PyObject*}{PyMapping_Items}{PyObject *o}
785 On success, return a list of the items in object \var{o}, where each
786 item is a tuple containing a key-value pair. On failure, return
787 \NULL. This is equivalent to the Python expression
788 \samp{\var{o}.items()}.
789\end{cfuncdesc}
790
791
792\begin{cfuncdesc}{PyObject*}{PyMapping_GetItemString}{PyObject *o, char *key}
793 Return element of \var{o} corresponding to the object \var{key} or
794 \NULL{} on failure. This is the equivalent of the Python expression
795 \samp{\var{o}[\var{key}]}.
796\end{cfuncdesc}
797
798\begin{cfuncdesc}{int}{PyMapping_SetItemString}{PyObject *o, char *key,
799 PyObject *v}
800 Map the object \var{key} to the value \var{v} in object \var{o}.
801 Returns \code{-1} on failure. This is the equivalent of the Python
802 statement \samp{\var{o}[\var{key}] = \var{v}}.
803\end{cfuncdesc}
804
805
806\section{Iterator Protocol \label{iterator}}
807
808\versionadded{2.2}
809
810There are only a couple of functions specifically for working with
811iterators.
812
813\begin{cfuncdesc}{int}{PyIter_Check}{PyObject *o}
814 Return true if the object \var{o} supports the iterator protocol.
815\end{cfuncdesc}
816
817\begin{cfuncdesc}{PyObject*}{PyIter_Next}{PyObject *o}
818 Return the next value from the iteration \var{o}. If the object is
819 an iterator, this retrieves the next value from the iteration, and
820 returns \NULL{} with no exception set if there are no remaining
821 items. If the object is not an iterator, \exception{TypeError} is
822 raised, or if there is an error in retrieving the item, returns
823 \NULL{} and passes along the exception.
824\end{cfuncdesc}
825
826To write a loop which iterates over an iterator, the C code should
827look something like this:
828
829\begin{verbatim}
830PyObject *iterator = ...;
831PyObject *item;
832
833while (item = PyIter_Next(iter)) {
834 /* do something with item */
835}
836if (PyErr_Occurred()) {
837 /* propogate error */
838}
839else {
840 /* continue doing useful work */
841}
842\end{verbatim}