blob: f4ed06f9e600e2b78808cd37620db1ed9dfdd8ae [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
Fred Drake0e0b6182002-04-15 20:51:19 +0000175\begin{cfuncdesc}{PyObject*}{PyObject_Call}{PyObject *callable_object,
176 PyObject *args,
177 PyObject *kw}
178 Call a callable Python object \var{callable_object}, with arguments
179 given by the tuple \var{args}, and named arguments given by the
180 dictionary \var{kw}. If no named arguments are needed, \var{kw} may
181 be \NULL{}. \var{args} must not be \NULL{}, use an empty tuple if
182 no arguments are needed. Returns the result of the call on success,
183 or \NULL{} on failure. This is the equivalent of the Python
184 expression \samp{apply(\var{callable_object}, \var{args}, \var{kw})}
185 or \samp{\var{callable_object}(*\var{args}, **\var{kw})}.
186 \bifuncindex{apply}
187\end{cfuncdesc}
188
189
Fred Drake3adf79e2001-10-12 19:01:43 +0000190\begin{cfuncdesc}{PyObject*}{PyObject_CallObject}{PyObject *callable_object,
191 PyObject *args}
192 Call a callable Python object \var{callable_object}, with arguments
193 given by the tuple \var{args}. If no arguments are needed, then
194 \var{args} may be \NULL. Returns the result of the call on
195 success, or \NULL{} on failure. This is the equivalent of the
196 Python expression \samp{apply(\var{callable_object}, \var{args})} or
197 \samp{\var{callable_object}(*\var{args})}.
198 \bifuncindex{apply}
199\end{cfuncdesc}
200
Fred Drakec44e9ec2001-10-26 16:38:38 +0000201\begin{cfuncdesc}{PyObject*}{PyObject_CallFunction}{PyObject *callable,
202 char *format, \moreargs}
203 Call a callable Python object \var{callable}, with a variable
Fred Drake3adf79e2001-10-12 19:01:43 +0000204 number of C arguments. The C arguments are described using a
205 \cfunction{Py_BuildValue()} style format string. The format may be
206 \NULL, indicating that no arguments are provided. Returns the
207 result of the call on success, or \NULL{} on failure. This is the
Fred Drakec44e9ec2001-10-26 16:38:38 +0000208 equivalent of the Python expression \samp{apply(\var{callable},
209 \var{args})} or \samp{\var{callable}(*\var{args})}.
Fred Drake3adf79e2001-10-12 19:01:43 +0000210 \bifuncindex{apply}
211\end{cfuncdesc}
212
213
214\begin{cfuncdesc}{PyObject*}{PyObject_CallMethod}{PyObject *o,
Fred Drakec44e9ec2001-10-26 16:38:38 +0000215 char *method, char *format,
216 \moreargs}
Fred Drake3adf79e2001-10-12 19:01:43 +0000217 Call the method named \var{m} of object \var{o} with a variable
218 number of C arguments. The C arguments are described by a
219 \cfunction{Py_BuildValue()} format string. The format may be \NULL,
220 indicating that no arguments are provided. Returns the result of the
221 call on success, or \NULL{} on failure. This is the equivalent of
Fred Drakec44e9ec2001-10-26 16:38:38 +0000222 the Python expression \samp{\var{o}.\var{method}(\var{args})}.
223\end{cfuncdesc}
224
225
Fred Drakeb0c079e2001-10-28 02:39:03 +0000226\begin{cfuncdesc}{PyObject*}{PyObject_CallFunctionObjArgs}{PyObject *callable,
227 \moreargs,
228 \code{NULL}}
Fred Drakec44e9ec2001-10-26 16:38:38 +0000229 Call a callable Python object \var{callable}, with a variable
230 number of \ctype{PyObject*} arguments. The arguments are provided
231 as a variable number of parameters followed by \NULL.
232 Returns the result of the call on success, or \NULL{} on failure.
233 \versionadded{2.2}
234\end{cfuncdesc}
235
236
Fred Drakeb0c079e2001-10-28 02:39:03 +0000237\begin{cfuncdesc}{PyObject*}{PyObject_CallMethodObjArgs}{PyObject *o,
238 PyObject *name,
239 \moreargs,
240 \code{NULL}}
Fred Drakec44e9ec2001-10-26 16:38:38 +0000241 Calls a method of the object \var{o}, where the name of the method
242 is given as a Python string object in \var{name}. It is called with
243 a variable number of \ctype{PyObject*} arguments. The arguments are
244 provided as a variable number of parameters followed by \NULL.
245 Returns the result of the call on success, or \NULL{} on failure.
246 \versionadded{2.2}
Fred Drake3adf79e2001-10-12 19:01:43 +0000247\end{cfuncdesc}
248
249
250\begin{cfuncdesc}{int}{PyObject_Hash}{PyObject *o}
251 Compute and return the hash value of an object \var{o}. On failure,
252 return \code{-1}. This is the equivalent of the Python expression
253 \samp{hash(\var{o})}.\bifuncindex{hash}
254\end{cfuncdesc}
255
256
257\begin{cfuncdesc}{int}{PyObject_IsTrue}{PyObject *o}
258 Returns \code{1} if the object \var{o} is considered to be true, and
259 \code{0} otherwise. This is equivalent to the Python expression
260 \samp{not not \var{o}}. This function always succeeds.
261\end{cfuncdesc}
262
263
264\begin{cfuncdesc}{PyObject*}{PyObject_Type}{PyObject *o}
265 When \var{o} is non-\NULL, returns a type object corresponding to
266 the object type of object \var{o}. On failure, raises
267 \exception{SystemError} and returns \NULL. This is equivalent to
268 the Python expression \code{type(\var{o})}.
269 \bifuncindex{type}
270\end{cfuncdesc}
271
272\begin{cfuncdesc}{int}{PyObject_TypeCheck}{PyObject *o, PyTypeObject *type}
273 Return true if the object \var{o} is of type \var{type} or a subtype
274 of \var{type}. Both parameters must be non-\NULL.
275 \versionadded{2.2}
276\end{cfuncdesc}
277
278\begin{cfuncdesc}{int}{PyObject_Length}{PyObject *o}
Fred Drake0e0b6182002-04-15 20:51:19 +0000279\cfuncline{int}{PyObject_Size}{PyObject *o}
Fred Drake3adf79e2001-10-12 19:01:43 +0000280 Return the length of object \var{o}. If the object \var{o} provides
281 both sequence and mapping protocols, the sequence length is
282 returned. On error, \code{-1} is returned. This is the equivalent
283 to the Python expression \samp{len(\var{o})}.\bifuncindex{len}
284\end{cfuncdesc}
285
286
287\begin{cfuncdesc}{PyObject*}{PyObject_GetItem}{PyObject *o, PyObject *key}
288 Return element of \var{o} corresponding to the object \var{key} or
289 \NULL{} on failure. This is the equivalent of the Python expression
290 \samp{\var{o}[\var{key}]}.
291\end{cfuncdesc}
292
293
294\begin{cfuncdesc}{int}{PyObject_SetItem}{PyObject *o,
295 PyObject *key, PyObject *v}
296 Map the object \var{key} to the value \var{v}. Returns \code{-1} on
297 failure. This is the equivalent of the Python statement
298 \samp{\var{o}[\var{key}] = \var{v}}.
299\end{cfuncdesc}
300
301
302\begin{cfuncdesc}{int}{PyObject_DelItem}{PyObject *o, PyObject *key}
303 Delete the mapping for \var{key} from \var{o}. Returns \code{-1} on
304 failure. This is the equivalent of the Python statement \samp{del
305 \var{o}[\var{key}]}.
306\end{cfuncdesc}
307
308\begin{cfuncdesc}{int}{PyObject_AsFileDescriptor}{PyObject *o}
309 Derives a file-descriptor from a Python object. If the object is an
310 integer or long integer, its value is returned. If not, the
311 object's \method{fileno()} method is called if it exists; the method
312 must return an integer or long integer, which is returned as the
313 file descriptor value. Returns \code{-1} on failure.
314\end{cfuncdesc}
315
316\begin{cfuncdesc}{PyObject*}{PyObject_Dir}{PyObject *o}
317 This is equivalent to the Python expression \samp{dir(\var{o})},
318 returning a (possibly empty) list of strings appropriate for the
319 object argument, or \NULL{} if there was an error. If the argument
320 is \NULL, this is like the Python \samp{dir()}, returning the names
321 of the current locals; in this case, if no execution frame is active
322 then \NULL{} is returned but \cfunction{PyErr_Occurred()} will
323 return false.
324\end{cfuncdesc}
325
Fred Drake314bae52002-03-11 18:46:29 +0000326\begin{cfuncdesc}{PyObject*}{PyObject_GetIter}{PyObject *o}
327 This is equivalent to the Python expression \samp{iter(\var{o})}.
328 It returns a new iterator for the object argument, or the object
329 itself if the object is already an iterator. Raises
330 \exception{TypeError} and returns \NULL{} if the object cannot be
331 iterated.
332\end{cfuncdesc}
333
Fred Drake3adf79e2001-10-12 19:01:43 +0000334
335\section{Number Protocol \label{number}}
336
337\begin{cfuncdesc}{int}{PyNumber_Check}{PyObject *o}
338 Returns \code{1} if the object \var{o} provides numeric protocols,
339 and false otherwise. This function always succeeds.
340\end{cfuncdesc}
341
342
343\begin{cfuncdesc}{PyObject*}{PyNumber_Add}{PyObject *o1, PyObject *o2}
344 Returns the result of adding \var{o1} and \var{o2}, or \NULL{} on
345 failure. This is the equivalent of the Python expression
346 \samp{\var{o1} + \var{o2}}.
347\end{cfuncdesc}
348
349
350\begin{cfuncdesc}{PyObject*}{PyNumber_Subtract}{PyObject *o1, PyObject *o2}
351 Returns the result of subtracting \var{o2} from \var{o1}, or \NULL{}
352 on failure. This is the equivalent of the Python expression
353 \samp{\var{o1} - \var{o2}}.
354\end{cfuncdesc}
355
356
357\begin{cfuncdesc}{PyObject*}{PyNumber_Multiply}{PyObject *o1, PyObject *o2}
358 Returns the result of multiplying \var{o1} and \var{o2}, or \NULL{}
359 on failure. This is the equivalent of the Python expression
360 \samp{\var{o1} * \var{o2}}.
361\end{cfuncdesc}
362
363
364\begin{cfuncdesc}{PyObject*}{PyNumber_Divide}{PyObject *o1, PyObject *o2}
365 Returns the result of dividing \var{o1} by \var{o2}, or \NULL{} on
366 failure. This is the equivalent of the Python expression
367 \samp{\var{o1} / \var{o2}}.
368\end{cfuncdesc}
369
370
371\begin{cfuncdesc}{PyObject*}{PyNumber_FloorDivide}{PyObject *o1, PyObject *o2}
372 Return the floor of \var{o1} divided by \var{o2}, or \NULL{} on
373 failure. This is equivalent to the ``classic'' division of
374 integers.
375 \versionadded{2.2}
376\end{cfuncdesc}
377
378
379\begin{cfuncdesc}{PyObject*}{PyNumber_TrueDivide}{PyObject *o1, PyObject *o2}
380 Return a reasonable approximation for the mathematical value of
381 \var{o1} divided by \var{o2}, or \NULL{} on failure. The return
382 value is ``approximate'' because binary floating point numbers are
383 approximate; it is not possible to represent all real numbers in
384 base two. This function can return a floating point value when
385 passed two integers.
386 \versionadded{2.2}
387\end{cfuncdesc}
388
389
390\begin{cfuncdesc}{PyObject*}{PyNumber_Remainder}{PyObject *o1, PyObject *o2}
391 Returns the remainder of dividing \var{o1} by \var{o2}, or \NULL{}
392 on failure. This is the equivalent of the Python expression
393 \samp{\var{o1} \%\ \var{o2}}.
394\end{cfuncdesc}
395
396
397\begin{cfuncdesc}{PyObject*}{PyNumber_Divmod}{PyObject *o1, PyObject *o2}
398 See the built-in function \function{divmod()}\bifuncindex{divmod}.
399 Returns \NULL{} on failure. This is the equivalent of the Python
400 expression \samp{divmod(\var{o1}, \var{o2})}.
401\end{cfuncdesc}
402
403
404\begin{cfuncdesc}{PyObject*}{PyNumber_Power}{PyObject *o1,
405 PyObject *o2, PyObject *o3}
406 See the built-in function \function{pow()}\bifuncindex{pow}.
407 Returns \NULL{} on failure. This is the equivalent of the Python
408 expression \samp{pow(\var{o1}, \var{o2}, \var{o3})}, where \var{o3}
409 is optional. If \var{o3} is to be ignored, pass \cdata{Py_None} in
410 its place (passing \NULL{} for \var{o3} would cause an illegal
411 memory access).
412\end{cfuncdesc}
413
414
415\begin{cfuncdesc}{PyObject*}{PyNumber_Negative}{PyObject *o}
416 Returns the negation of \var{o} on success, or \NULL{} on failure.
417 This is the equivalent of the Python expression \samp{-\var{o}}.
418\end{cfuncdesc}
419
420
421\begin{cfuncdesc}{PyObject*}{PyNumber_Positive}{PyObject *o}
422 Returns \var{o} on success, or \NULL{} on failure. This is the
423 equivalent of the Python expression \samp{+\var{o}}.
424\end{cfuncdesc}
425
426
427\begin{cfuncdesc}{PyObject*}{PyNumber_Absolute}{PyObject *o}
428 Returns the absolute value of \var{o}, or \NULL{} on failure. This
429 is the equivalent of the Python expression \samp{abs(\var{o})}.
430 \bifuncindex{abs}
431\end{cfuncdesc}
432
433
434\begin{cfuncdesc}{PyObject*}{PyNumber_Invert}{PyObject *o}
435 Returns the bitwise negation of \var{o} on success, or \NULL{} on
436 failure. This is the equivalent of the Python expression
437 \samp{\~\var{o}}.
438\end{cfuncdesc}
439
440
441\begin{cfuncdesc}{PyObject*}{PyNumber_Lshift}{PyObject *o1, PyObject *o2}
442 Returns the result of left shifting \var{o1} by \var{o2} on success,
443 or \NULL{} on failure. This is the equivalent of the Python
444 expression \samp{\var{o1} <\code{<} \var{o2}}.
445\end{cfuncdesc}
446
447
448\begin{cfuncdesc}{PyObject*}{PyNumber_Rshift}{PyObject *o1, PyObject *o2}
449 Returns the result of right shifting \var{o1} by \var{o2} on
450 success, or \NULL{} on failure. This is the equivalent of the
451 Python expression \samp{\var{o1} >\code{>} \var{o2}}.
452\end{cfuncdesc}
453
454
455\begin{cfuncdesc}{PyObject*}{PyNumber_And}{PyObject *o1, PyObject *o2}
456 Returns the ``bitwise and'' of \var{o2} and \var{o2} on success and
457 \NULL{} on failure. This is the equivalent of the Python expression
458 \samp{\var{o1} \&\ \var{o2}}.
459\end{cfuncdesc}
460
461
462\begin{cfuncdesc}{PyObject*}{PyNumber_Xor}{PyObject *o1, PyObject *o2}
463 Returns the ``bitwise exclusive or'' of \var{o1} by \var{o2} on
464 success, or \NULL{} on failure. This is the equivalent of the
465 Python expression \samp{\var{o1} \textasciicircum{} \var{o2}}.
466\end{cfuncdesc}
467
468\begin{cfuncdesc}{PyObject*}{PyNumber_Or}{PyObject *o1, PyObject *o2}
469 Returns the ``bitwise or'' of \var{o1} and \var{o2} on success, or
470 \NULL{} on failure. This is the equivalent of the Python expression
471 \samp{\var{o1} | \var{o2}}.
472\end{cfuncdesc}
473
474
475\begin{cfuncdesc}{PyObject*}{PyNumber_InPlaceAdd}{PyObject *o1, PyObject *o2}
476 Returns the result of adding \var{o1} and \var{o2}, or \NULL{} on
477 failure. The operation is done \emph{in-place} when \var{o1}
478 supports it. This is the equivalent of the Python statement
479 \samp{\var{o1} += \var{o2}}.
480\end{cfuncdesc}
481
482
483\begin{cfuncdesc}{PyObject*}{PyNumber_InPlaceSubtract}{PyObject *o1,
484 PyObject *o2}
485 Returns the result of subtracting \var{o2} from \var{o1}, or \NULL{}
486 on failure. The operation is done \emph{in-place} when \var{o1}
487 supports it. This is the equivalent of the Python statement
488 \samp{\var{o1} -= \var{o2}}.
489\end{cfuncdesc}
490
491
492\begin{cfuncdesc}{PyObject*}{PyNumber_InPlaceMultiply}{PyObject *o1,
493 PyObject *o2}
494 Returns the result of multiplying \var{o1} and \var{o2}, or \NULL{}
495 on failure. The operation is done \emph{in-place} when \var{o1}
496 supports it. This is the equivalent of the Python statement
497 \samp{\var{o1} *= \var{o2}}.
498\end{cfuncdesc}
499
500
501\begin{cfuncdesc}{PyObject*}{PyNumber_InPlaceDivide}{PyObject *o1,
502 PyObject *o2}
503 Returns the result of dividing \var{o1} by \var{o2}, or \NULL{} on
504 failure. The operation is done \emph{in-place} when \var{o1}
505 supports it. This is the equivalent of the Python statement
506 \samp{\var{o1} /= \var{o2}}.
507\end{cfuncdesc}
508
509
510\begin{cfuncdesc}{PyObject*}{PyNumber_InPlaceFloorDivide}{PyObject *o1,
511 PyObject *o2}
512 Returns the mathematical of dividing \var{o1} by \var{o2}, or
513 \NULL{} on failure. The operation is done \emph{in-place} when
514 \var{o1} supports it. This is the equivalent of the Python
515 statement \samp{\var{o1} //= \var{o2}}.
516 \versionadded{2.2}
517\end{cfuncdesc}
518
519
520\begin{cfuncdesc}{PyObject*}{PyNumber_InPlaceTrueDivide}{PyObject *o1,
521 PyObject *o2}
522 Return a reasonable approximation for the mathematical value of
523 \var{o1} divided by \var{o2}, or \NULL{} on failure. The return
524 value is ``approximate'' because binary floating point numbers are
525 approximate; it is not possible to represent all real numbers in
526 base two. This function can return a floating point value when
527 passed two integers. The operation is done \emph{in-place} when
528 \var{o1} supports it.
529 \versionadded{2.2}
530\end{cfuncdesc}
531
532
533\begin{cfuncdesc}{PyObject*}{PyNumber_InPlaceRemainder}{PyObject *o1,
534 PyObject *o2}
535 Returns the remainder of dividing \var{o1} by \var{o2}, or \NULL{}
536 on failure. The operation is done \emph{in-place} when \var{o1}
537 supports it. This is the equivalent of the Python statement
538 \samp{\var{o1} \%= \var{o2}}.
539\end{cfuncdesc}
540
541
542\begin{cfuncdesc}{PyObject*}{PyNumber_InPlacePower}{PyObject *o1,
543 PyObject *o2, PyObject *o3}
544 See the built-in function \function{pow()}.\bifuncindex{pow}
545 Returns \NULL{} on failure. The operation is done \emph{in-place}
546 when \var{o1} supports it. This is the equivalent of the Python
547 statement \samp{\var{o1} **= \var{o2}} when o3 is \cdata{Py_None},
548 or an in-place variant of \samp{pow(\var{o1}, \var{o2}, \var{o3})}
549 otherwise. If \var{o3} is to be ignored, pass \cdata{Py_None} in its
550 place (passing \NULL{} for \var{o3} would cause an illegal memory
551 access).
552\end{cfuncdesc}
553
554\begin{cfuncdesc}{PyObject*}{PyNumber_InPlaceLshift}{PyObject *o1,
555 PyObject *o2}
556 Returns the result of left shifting \var{o1} by \var{o2} on success,
557 or \NULL{} on failure. The operation is done \emph{in-place} when
558 \var{o1} supports it. This is the equivalent of the Python
559 statement \samp{\var{o1} <\code{<=} \var{o2}}.
560\end{cfuncdesc}
561
562
563\begin{cfuncdesc}{PyObject*}{PyNumber_InPlaceRshift}{PyObject *o1,
564 PyObject *o2}
565 Returns the result of right shifting \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} >\code{>=} \var{o2}}.
569\end{cfuncdesc}
570
571
572\begin{cfuncdesc}{PyObject*}{PyNumber_InPlaceAnd}{PyObject *o1, PyObject *o2}
573 Returns the ``bitwise and'' of \var{o1} and \var{o2} on success and
574 \NULL{} on failure. The operation is done \emph{in-place} when
575 \var{o1} supports it. This is the equivalent of the Python
576 statement \samp{\var{o1} \&= \var{o2}}.
577\end{cfuncdesc}
578
579
580\begin{cfuncdesc}{PyObject*}{PyNumber_InPlaceXor}{PyObject *o1, PyObject *o2}
581 Returns the ``bitwise exclusive or'' of \var{o1} by \var{o2} on
582 success, or \NULL{} on failure. The operation is done
583 \emph{in-place} when \var{o1} supports it. This is the equivalent
584 of the Python statement \samp{\var{o1} \textasciicircum= \var{o2}}.
585\end{cfuncdesc}
586
587\begin{cfuncdesc}{PyObject*}{PyNumber_InPlaceOr}{PyObject *o1, PyObject *o2}
588 Returns the ``bitwise or'' of \var{o1} and \var{o2} on success, or
589 \NULL{} on failure. The operation is done \emph{in-place} when
590 \var{o1} supports it. This is the equivalent of the Python
591 statement \samp{\var{o1} |= \var{o2}}.
592\end{cfuncdesc}
593
594\begin{cfuncdesc}{int}{PyNumber_Coerce}{PyObject **p1, PyObject **p2}
595 This function takes the addresses of two variables of type
596 \ctype{PyObject*}. If the objects pointed to by \code{*\var{p1}}
597 and \code{*\var{p2}} have the same type, increment their reference
598 count and return \code{0} (success). If the objects can be converted
599 to a common numeric type, replace \code{*p1} and \code{*p2} by their
600 converted value (with 'new' reference counts), and return \code{0}.
601 If no conversion is possible, or if some other error occurs, return
602 \code{-1} (failure) and don't increment the reference counts. The
603 call \code{PyNumber_Coerce(\&o1, \&o2)} is equivalent to the Python
604 statement \samp{\var{o1}, \var{o2} = coerce(\var{o1}, \var{o2})}.
605 \bifuncindex{coerce}
606\end{cfuncdesc}
607
608\begin{cfuncdesc}{PyObject*}{PyNumber_Int}{PyObject *o}
609 Returns the \var{o} converted to an integer object on success, or
610 \NULL{} on failure. This is the equivalent of the Python expression
611 \samp{int(\var{o})}.\bifuncindex{int}
612\end{cfuncdesc}
613
614\begin{cfuncdesc}{PyObject*}{PyNumber_Long}{PyObject *o}
615 Returns the \var{o} converted to a long integer object on success,
616 or \NULL{} on failure. This is the equivalent of the Python
617 expression \samp{long(\var{o})}.\bifuncindex{long}
618\end{cfuncdesc}
619
620\begin{cfuncdesc}{PyObject*}{PyNumber_Float}{PyObject *o}
621 Returns the \var{o} converted to a float object on success, or
622 \NULL{} on failure. This is the equivalent of the Python expression
623 \samp{float(\var{o})}.\bifuncindex{float}
624\end{cfuncdesc}
625
626
627\section{Sequence Protocol \label{sequence}}
628
629\begin{cfuncdesc}{int}{PySequence_Check}{PyObject *o}
630 Return \code{1} if the object provides sequence protocol, and
631 \code{0} otherwise. This function always succeeds.
632\end{cfuncdesc}
633
634\begin{cfuncdesc}{int}{PySequence_Size}{PyObject *o}
635 Returns the number of objects in sequence \var{o} on success, and
636 \code{-1} on failure. For objects that do not provide sequence
637 protocol, this is equivalent to the Python expression
638 \samp{len(\var{o})}.\bifuncindex{len}
639\end{cfuncdesc}
640
641\begin{cfuncdesc}{int}{PySequence_Length}{PyObject *o}
642 Alternate name for \cfunction{PySequence_Size()}.
643\end{cfuncdesc}
644
645\begin{cfuncdesc}{PyObject*}{PySequence_Concat}{PyObject *o1, PyObject *o2}
646 Return the concatenation of \var{o1} and \var{o2} on success, and
647 \NULL{} on failure. This is the equivalent of the Python
648 expression \samp{\var{o1} + \var{o2}}.
649\end{cfuncdesc}
650
651
652\begin{cfuncdesc}{PyObject*}{PySequence_Repeat}{PyObject *o, int count}
653 Return the result of repeating sequence object \var{o} \var{count}
654 times, or \NULL{} on failure. This is the equivalent of the Python
655 expression \samp{\var{o} * \var{count}}.
656\end{cfuncdesc}
657
658\begin{cfuncdesc}{PyObject*}{PySequence_InPlaceConcat}{PyObject *o1,
659 PyObject *o2}
660 Return the concatenation of \var{o1} and \var{o2} on success, and
661 \NULL{} on failure. The operation is done \emph{in-place} when
662 \var{o1} supports it. This is the equivalent of the Python
663 expression \samp{\var{o1} += \var{o2}}.
664\end{cfuncdesc}
665
666
667\begin{cfuncdesc}{PyObject*}{PySequence_InPlaceRepeat}{PyObject *o, int count}
668 Return the result of repeating sequence object \var{o} \var{count}
669 times, or \NULL{} on failure. The operation is done \emph{in-place}
670 when \var{o} supports it. This is the equivalent of the Python
671 expression \samp{\var{o} *= \var{count}}.
672\end{cfuncdesc}
673
674
675\begin{cfuncdesc}{PyObject*}{PySequence_GetItem}{PyObject *o, int i}
676 Return the \var{i}th element of \var{o}, or \NULL{} on failure.
677 This is the equivalent of the Python expression
678 \samp{\var{o}[\var{i}]}.
679\end{cfuncdesc}
680
681
682\begin{cfuncdesc}{PyObject*}{PySequence_GetSlice}{PyObject *o, int i1, int i2}
683 Return the slice of sequence object \var{o} between \var{i1} and
684 \var{i2}, or \NULL{} on failure. This is the equivalent of the
685 Python expression \samp{\var{o}[\var{i1}:\var{i2}]}.
686\end{cfuncdesc}
687
688
689\begin{cfuncdesc}{int}{PySequence_SetItem}{PyObject *o, int i, PyObject *v}
690 Assign object \var{v} to the \var{i}th element of \var{o}. Returns
691 \code{-1} on failure. This is the equivalent of the Python
692 statement \samp{\var{o}[\var{i}] = \var{v}}.
693\end{cfuncdesc}
694
695\begin{cfuncdesc}{int}{PySequence_DelItem}{PyObject *o, int i}
696 Delete the \var{i}th element of object \var{o}. Returns \code{-1}
697 on failure. This is the equivalent of the Python statement
698 \samp{del \var{o}[\var{i}]}.
699\end{cfuncdesc}
700
701\begin{cfuncdesc}{int}{PySequence_SetSlice}{PyObject *o, int i1,
702 int i2, PyObject *v}
703 Assign the sequence object \var{v} to the slice in sequence object
704 \var{o} from \var{i1} to \var{i2}. This is the equivalent of the
705 Python statement \samp{\var{o}[\var{i1}:\var{i2}] = \var{v}}.
706\end{cfuncdesc}
707
708\begin{cfuncdesc}{int}{PySequence_DelSlice}{PyObject *o, int i1, int i2}
709 Delete the slice in sequence object \var{o} from \var{i1} to
710 \var{i2}. Returns \code{-1} on failure. This is the equivalent of
711 the Python statement \samp{del \var{o}[\var{i1}:\var{i2}]}.
712\end{cfuncdesc}
713
714\begin{cfuncdesc}{PyObject*}{PySequence_Tuple}{PyObject *o}
715 Returns the \var{o} as a tuple on success, and \NULL{} on failure.
716 This is equivalent to the Python expression \samp{tuple(\var{o})}.
717 \bifuncindex{tuple}
718\end{cfuncdesc}
719
720\begin{cfuncdesc}{int}{PySequence_Count}{PyObject *o, PyObject *value}
721 Return the number of occurrences of \var{value} in \var{o}, that is,
722 return the number of keys for which \code{\var{o}[\var{key}] ==
723 \var{value}}. On failure, return \code{-1}. This is equivalent to
724 the Python expression \samp{\var{o}.count(\var{value})}.
725\end{cfuncdesc}
726
727\begin{cfuncdesc}{int}{PySequence_Contains}{PyObject *o, PyObject *value}
728 Determine if \var{o} contains \var{value}. If an item in \var{o} is
729 equal to \var{value}, return \code{1}, otherwise return \code{0}.
730 On error, return \code{-1}. This is equivalent to the Python
731 expression \samp{\var{value} in \var{o}}.
732\end{cfuncdesc}
733
734\begin{cfuncdesc}{int}{PySequence_Index}{PyObject *o, PyObject *value}
735 Return the first index \var{i} for which \code{\var{o}[\var{i}] ==
736 \var{value}}. On error, return \code{-1}. This is equivalent to
737 the Python expression \samp{\var{o}.index(\var{value})}.
738\end{cfuncdesc}
739
740\begin{cfuncdesc}{PyObject*}{PySequence_List}{PyObject *o}
741 Return a list object with the same contents as the arbitrary
742 sequence \var{o}. The returned list is guaranteed to be new.
743\end{cfuncdesc}
744
745\begin{cfuncdesc}{PyObject*}{PySequence_Tuple}{PyObject *o}
746 Return a tuple object with the same contents as the arbitrary
747 sequence \var{o}. If \var{o} is a tuple, a new reference will be
748 returned, otherwise a tuple will be constructed with the appropriate
749 contents.
750\end{cfuncdesc}
751
752\begin{cfuncdesc}{PyObject*}{PySequence_Fast}{PyObject *o, const char *m}
753 Returns the sequence \var{o} as a tuple, unless it is already a
754 tuple or list, in which case \var{o} is returned. Use
755 \cfunction{PySequence_Fast_GET_ITEM()} to access the members of the
756 result. Returns \NULL{} on failure. If the object is not a
757 sequence, raises \exception{TypeError} with \var{m} as the message
758 text.
759\end{cfuncdesc}
760
761\begin{cfuncdesc}{PyObject*}{PySequence_Fast_GET_ITEM}{PyObject *o, int i}
762 Return the \var{i}th element of \var{o}, assuming that \var{o} was
Fred Drakec37b65e2001-11-28 07:26:15 +0000763 returned by \cfunction{PySequence_Fast()}, \var{o} is not \NULL,
Tim Peters1fc240e2001-10-26 05:06:50 +0000764 and that \var{i} is within bounds.
765\end{cfuncdesc}
766
767\begin{cfuncdesc}{int}{PySequence_Fast_GET_SIZE}{PyObject *o}
768 Returns the length of \var{o}, assuming that \var{o} was
769 returned by \cfunction{PySequence_Fast()} and that \var{o} is
Fred Drakec37b65e2001-11-28 07:26:15 +0000770 not \NULL. The size can also be gotten by calling
Tim Peters1fc240e2001-10-26 05:06:50 +0000771 \cfunction{PySequence_Size()} on \var{o}, but
772 \cfunction{PySequence_Fast_GET_SIZE()} is faster because it can
773 assume \var{o} is a list or tuple.
Fred Drake3adf79e2001-10-12 19:01:43 +0000774\end{cfuncdesc}
775
776
777\section{Mapping Protocol \label{mapping}}
778
779\begin{cfuncdesc}{int}{PyMapping_Check}{PyObject *o}
780 Return \code{1} if the object provides mapping protocol, and
781 \code{0} otherwise. This function always succeeds.
782\end{cfuncdesc}
783
784
785\begin{cfuncdesc}{int}{PyMapping_Length}{PyObject *o}
786 Returns the number of keys in object \var{o} on success, and
787 \code{-1} on failure. For objects that do not provide mapping
788 protocol, this is equivalent to the Python expression
789 \samp{len(\var{o})}.\bifuncindex{len}
790\end{cfuncdesc}
791
792
793\begin{cfuncdesc}{int}{PyMapping_DelItemString}{PyObject *o, char *key}
794 Remove the mapping for object \var{key} from the object \var{o}.
795 Return \code{-1} on failure. This is equivalent to the Python
796 statement \samp{del \var{o}[\var{key}]}.
797\end{cfuncdesc}
798
799
800\begin{cfuncdesc}{int}{PyMapping_DelItem}{PyObject *o, PyObject *key}
801 Remove the mapping for object \var{key} from the object \var{o}.
802 Return \code{-1} on failure. This is equivalent to the Python
803 statement \samp{del \var{o}[\var{key}]}.
804\end{cfuncdesc}
805
806
807\begin{cfuncdesc}{int}{PyMapping_HasKeyString}{PyObject *o, char *key}
808 On success, return \code{1} if the mapping object has the key
809 \var{key} and \code{0} otherwise. This is equivalent to the Python
810 expression \samp{\var{o}.has_key(\var{key})}. This function always
811 succeeds.
812\end{cfuncdesc}
813
814
815\begin{cfuncdesc}{int}{PyMapping_HasKey}{PyObject *o, PyObject *key}
816 Return \code{1} if the mapping object has the key \var{key} and
817 \code{0} otherwise. This is equivalent to the Python expression
818 \samp{\var{o}.has_key(\var{key})}. This function always succeeds.
819\end{cfuncdesc}
820
821
822\begin{cfuncdesc}{PyObject*}{PyMapping_Keys}{PyObject *o}
823 On success, return a list of the keys in object \var{o}. On
824 failure, return \NULL. This is equivalent to the Python expression
825 \samp{\var{o}.keys()}.
826\end{cfuncdesc}
827
828
829\begin{cfuncdesc}{PyObject*}{PyMapping_Values}{PyObject *o}
830 On success, return a list of the values in object \var{o}. On
831 failure, return \NULL. This is equivalent to the Python expression
832 \samp{\var{o}.values()}.
833\end{cfuncdesc}
834
835
836\begin{cfuncdesc}{PyObject*}{PyMapping_Items}{PyObject *o}
837 On success, return a list of the items in object \var{o}, where each
838 item is a tuple containing a key-value pair. On failure, return
839 \NULL. This is equivalent to the Python expression
840 \samp{\var{o}.items()}.
841\end{cfuncdesc}
842
843
844\begin{cfuncdesc}{PyObject*}{PyMapping_GetItemString}{PyObject *o, char *key}
845 Return element of \var{o} corresponding to the object \var{key} or
846 \NULL{} on failure. This is the equivalent of the Python expression
847 \samp{\var{o}[\var{key}]}.
848\end{cfuncdesc}
849
850\begin{cfuncdesc}{int}{PyMapping_SetItemString}{PyObject *o, char *key,
851 PyObject *v}
852 Map the object \var{key} to the value \var{v} in object \var{o}.
853 Returns \code{-1} on failure. This is the equivalent of the Python
854 statement \samp{\var{o}[\var{key}] = \var{v}}.
855\end{cfuncdesc}
856
857
858\section{Iterator Protocol \label{iterator}}
859
860\versionadded{2.2}
861
862There are only a couple of functions specifically for working with
863iterators.
864
865\begin{cfuncdesc}{int}{PyIter_Check}{PyObject *o}
866 Return true if the object \var{o} supports the iterator protocol.
867\end{cfuncdesc}
868
869\begin{cfuncdesc}{PyObject*}{PyIter_Next}{PyObject *o}
870 Return the next value from the iteration \var{o}. If the object is
871 an iterator, this retrieves the next value from the iteration, and
872 returns \NULL{} with no exception set if there are no remaining
873 items. If the object is not an iterator, \exception{TypeError} is
874 raised, or if there is an error in retrieving the item, returns
875 \NULL{} and passes along the exception.
876\end{cfuncdesc}
877
878To write a loop which iterates over an iterator, the C code should
879look something like this:
880
881\begin{verbatim}
Fred Drake314bae52002-03-11 18:46:29 +0000882PyObject *iterator = PyObject_GetIter(obj);
Fred Drake3adf79e2001-10-12 19:01:43 +0000883PyObject *item;
884
Fred Drake314bae52002-03-11 18:46:29 +0000885if (iterator == NULL) {
886 /* propagate error */
887}
888
889while (item = PyIter_Next(iterator)) {
Fred Drake3adf79e2001-10-12 19:01:43 +0000890 /* do something with item */
Fred Drake8b8fe282001-12-26 16:53:48 +0000891 ...
892 /* release reference when done */
893 Py_DECREF(item);
Fred Drake3adf79e2001-10-12 19:01:43 +0000894}
Fred Drake314bae52002-03-11 18:46:29 +0000895
896Py_DECREF(iterator);
897
Fred Drake3adf79e2001-10-12 19:01:43 +0000898if (PyErr_Occurred()) {
Fred Drake314bae52002-03-11 18:46:29 +0000899 /* propagate error */
Fred Drake3adf79e2001-10-12 19:01:43 +0000900}
901else {
902 /* continue doing useful work */
903}
Fred Drake8b8fe282001-12-26 16:53:48 +0000904\end{verbatim}
905
Jeremy Hylton89c3a222001-11-09 21:59:42 +0000906
Fred Drake94ead572001-11-09 23:34:26 +0000907\section{Buffer Protocol \label{abstract-buffer}}
Jeremy Hylton89c3a222001-11-09 21:59:42 +0000908
909\begin{cfuncdesc}{int}{PyObject_AsCharBuffer}{PyObject *obj,
Fred Drake94ead572001-11-09 23:34:26 +0000910 const char **buffer,
911 int *buffer_len}
Jeremy Hylton89c3a222001-11-09 21:59:42 +0000912 Returns a pointer to a read-only memory location useable as character-
913 based input. The \var{obj} argument must support the single-segment
Fred Drake243ea712002-04-04 04:10:36 +0000914 character buffer interface. On success, returns \code{0}, sets
Thomas Hellerbfeeeee2001-11-12 07:46:31 +0000915 \var{buffer} to the memory location and \var{buffer_len} to the buffer
Fred Drake243ea712002-04-04 04:10:36 +0000916 length. Returns \code{-1} and sets a \exception{TypeError} on error.
Fred Drake94ead572001-11-09 23:34:26 +0000917 \versionadded{1.6}
Jeremy Hylton89c3a222001-11-09 21:59:42 +0000918\end{cfuncdesc}
919
920\begin{cfuncdesc}{int}{PyObject_AsReadBuffer}{PyObject *obj,
Fred Drake94ead572001-11-09 23:34:26 +0000921 const char **buffer,
922 int *buffer_len}
Jeremy Hylton89c3a222001-11-09 21:59:42 +0000923 Returns a pointer to a read-only memory location containing
924 arbitrary data. The \var{obj} argument must support the
925 single-segment readable buffer interface. On success, returns
Fred Drake243ea712002-04-04 04:10:36 +0000926 \code{0}, sets \var{buffer} to the memory location and \var{buffer_len}
927 to the buffer length. Returns \code{-1} and sets a
Jeremy Hylton89c3a222001-11-09 21:59:42 +0000928 \exception{TypeError} on error.
Fred Drake94ead572001-11-09 23:34:26 +0000929 \versionadded{1.6}
Jeremy Hylton89c3a222001-11-09 21:59:42 +0000930\end{cfuncdesc}
931
932\begin{cfuncdesc}{int}{PyObject_CheckReadBuffer}{PyObject *o}
933 Returns \code{1} if \var{o} supports the single-segment readable
934 buffer interface. Otherwise returns \code{0}.
Fred Drake94ead572001-11-09 23:34:26 +0000935 \versionadded{2.2}
Fred Drake8b8fe282001-12-26 16:53:48 +0000936\end{cfuncdesc}
Jeremy Hylton89c3a222001-11-09 21:59:42 +0000937
938\begin{cfuncdesc}{int}{PyObject_AsWriteBuffer}{PyObject *obj,
Fred Drake94ead572001-11-09 23:34:26 +0000939 const char **buffer,
940 int *buffer_len}
Jeremy Hylton89c3a222001-11-09 21:59:42 +0000941 Returns a pointer to a writeable memory location. The \var{obj}
942 argument must support the single-segment, character buffer
Fred Drake243ea712002-04-04 04:10:36 +0000943 interface. On success, returns \code{0}, sets \var{buffer} to the
Thomas Hellerbfeeeee2001-11-12 07:46:31 +0000944 memory location and \var{buffer_len} to the buffer length. Returns
Fred Drake243ea712002-04-04 04:10:36 +0000945 \code{-1} and sets a \exception{TypeError} on error.
Fred Drake94ead572001-11-09 23:34:26 +0000946 \versionadded{1.6}
Jeremy Hylton89c3a222001-11-09 21:59:42 +0000947\end{cfuncdesc}