blob: 7c742a0be6412f328e47dd81c6693010a7fb66c7 [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
Martin v. Löwis29fafd82006-03-01 05:16:03 +000019\begin{cfuncdesc}{int}{PyObject_HasAttrString}{PyObject *o, const char *attr_name}
Fred Drake3adf79e2001-10-12 19:01:43 +000020 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,
Martin v. Löwis29fafd82006-03-01 05:16:03 +000027 const char *attr_name}
Fred Drake3adf79e2001-10-12 19:01:43 +000028 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,
Martin v. Löwis29fafd82006-03-01 05:16:03 +000053 const char *attr_name, PyObject *v}
Fred Drake3adf79e2001-10-12 19:01:43 +000054 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
Martin v. Löwis29fafd82006-03-01 05:16:03 +000070\begin{cfuncdesc}{int}{PyObject_DelAttrString}{PyObject *o, const char *attr_name}
Fred Drake3adf79e2001-10-12 19:01:43 +000071 Delete attribute named \var{attr_name}, for object \var{o}. Returns
72 \code{-1} on failure. This is the equivalent of the Python
73 statement: \samp{del \var{o}.\var{attr_name}}.
74\end{cfuncdesc}
75
76
77\begin{cfuncdesc}{int}{PyObject_DelAttr}{PyObject *o, PyObject *attr_name}
78 Delete attribute named \var{attr_name}, for object \var{o}. Returns
79 \code{-1} on failure. This is the equivalent of the Python
80 statement \samp{del \var{o}.\var{attr_name}}.
81\end{cfuncdesc}
82
83
Fred Drake178153f2002-06-13 11:51:48 +000084\begin{cfuncdesc}{PyObject*}{PyObject_RichCompare}{PyObject *o1,
Fred Drakea0c5e9f2002-06-14 14:35:56 +000085 PyObject *o2, int opid}
Fred Drake178153f2002-06-13 11:51:48 +000086 Compare the values of \var{o1} and \var{o2} using the operation
Fred Drakea0c5e9f2002-06-14 14:35:56 +000087 specified by \var{opid}, which must be one of
Fred Drake178153f2002-06-13 11:51:48 +000088 \constant{Py_LT},
89 \constant{Py_LE},
90 \constant{Py_EQ},
91 \constant{Py_NE},
92 \constant{Py_GT}, or
93 \constant{Py_GE}, corresponding to
94 \code{<},
95 \code{<=},
96 \code{==},
97 \code{!=},
98 \code{>}, or
99 \code{>=} respectively. This is the equivalent of the Python expression
Fred Drakea0c5e9f2002-06-14 14:35:56 +0000100 \samp{\var{o1} op \var{o2}}, where \code{op} is the operator
101 corresponding to \var{opid}. Returns the value of the comparison on
Fred Drake178153f2002-06-13 11:51:48 +0000102 success, or \NULL{} on failure.
103\end{cfuncdesc}
104
105\begin{cfuncdesc}{int}{PyObject_RichCompareBool}{PyObject *o1,
Fred Drakea0c5e9f2002-06-14 14:35:56 +0000106 PyObject *o2, int opid}
Fred Drake178153f2002-06-13 11:51:48 +0000107 Compare the values of \var{o1} and \var{o2} using the operation
Fred Drakea0c5e9f2002-06-14 14:35:56 +0000108 specified by \var{opid}, which must be one of
Fred Drake178153f2002-06-13 11:51:48 +0000109 \constant{Py_LT},
110 \constant{Py_LE},
111 \constant{Py_EQ},
112 \constant{Py_NE},
113 \constant{Py_GT}, or
114 \constant{Py_GE}, corresponding to
115 \code{<},
116 \code{<=},
117 \code{==},
118 \code{!=},
119 \code{>}, or
120 \code{>=} respectively. Returns \code{-1} on error, \code{0} if the
121 result is false, \code{1} otherwise. This is the equivalent of the
Fred Drakea0c5e9f2002-06-14 14:35:56 +0000122 Python expression \samp{\var{o1} op \var{o2}}, where
123 \code{op} is the operator corresponding to \var{opid}.
Fred Drake178153f2002-06-13 11:51:48 +0000124\end{cfuncdesc}
125
Fred Drake3adf79e2001-10-12 19:01:43 +0000126\begin{cfuncdesc}{int}{PyObject_Cmp}{PyObject *o1, PyObject *o2, int *result}
127 Compare the values of \var{o1} and \var{o2} using a routine provided
128 by \var{o1}, if one exists, otherwise with a routine provided by
129 \var{o2}. The result of the comparison is returned in
130 \var{result}. Returns \code{-1} on failure. This is the equivalent
131 of the Python statement\bifuncindex{cmp} \samp{\var{result} =
132 cmp(\var{o1}, \var{o2})}.
133\end{cfuncdesc}
134
135
136\begin{cfuncdesc}{int}{PyObject_Compare}{PyObject *o1, PyObject *o2}
137 Compare the values of \var{o1} and \var{o2} using a routine provided
138 by \var{o1}, if one exists, otherwise with a routine provided by
139 \var{o2}. Returns the result of the comparison on success. On
140 error, the value returned is undefined; use
141 \cfunction{PyErr_Occurred()} to detect an error. This is equivalent
142 to the Python expression\bifuncindex{cmp} \samp{cmp(\var{o1},
143 \var{o2})}.
144\end{cfuncdesc}
145
146
147\begin{cfuncdesc}{PyObject*}{PyObject_Repr}{PyObject *o}
148 Compute a string representation of object \var{o}. Returns the
149 string representation on success, \NULL{} on failure. This is the
150 equivalent of the Python expression \samp{repr(\var{o})}. Called by
151 the \function{repr()}\bifuncindex{repr} built-in function and by
152 reverse quotes.
153\end{cfuncdesc}
154
155
156\begin{cfuncdesc}{PyObject*}{PyObject_Str}{PyObject *o}
157 Compute a string representation of object \var{o}. Returns the
158 string representation on success, \NULL{} on failure. This is the
159 equivalent of the Python expression \samp{str(\var{o})}. Called by
160 the \function{str()}\bifuncindex{str} built-in function and by the
161 \keyword{print} statement.
162\end{cfuncdesc}
163
164
165\begin{cfuncdesc}{PyObject*}{PyObject_Unicode}{PyObject *o}
166 Compute a Unicode string representation of object \var{o}. Returns
167 the Unicode string representation on success, \NULL{} on failure.
168 This is the equivalent of the Python expression
Neal Norwitz4ddfd502002-07-28 13:55:20 +0000169 \samp{unicode(\var{o})}. Called by the
170 \function{unicode()}\bifuncindex{unicode} built-in function.
Fred Drake3adf79e2001-10-12 19:01:43 +0000171\end{cfuncdesc}
172
173\begin{cfuncdesc}{int}{PyObject_IsInstance}{PyObject *inst, PyObject *cls}
Fred Drakeb957bc32002-04-23 18:15:44 +0000174 Returns \code{1} if \var{inst} is an instance of the class \var{cls}
175 or a subclass of \var{cls}, or \code{0} if not. On error, returns
176 \code{-1} and sets an exception. If \var{cls} is a type object
177 rather than a class object, \cfunction{PyObject_IsInstance()}
Walter Dörwald6d5f30e2002-12-06 10:09:16 +0000178 returns \code{1} if \var{inst} is of type \var{cls}. If \var{cls}
179 is a tuple, the check will be done against every entry in \var{cls}.
180 The result will be \code{1} when at least one of the checks returns
181 \code{1}, otherwise it will be \code{0}. If \var{inst} is not a class
182 instance and \var{cls} is neither a type object, nor a class object,
183 nor a tuple, \var{inst} must have a \member{__class__} attribute
Fred Drakeb957bc32002-04-23 18:15:44 +0000184 --- the class relationship of the value of that attribute with
185 \var{cls} will be used to determine the result of this function.
Fred Drake3adf79e2001-10-12 19:01:43 +0000186 \versionadded{2.1}
Walter Dörwald6d5f30e2002-12-06 10:09:16 +0000187 \versionchanged[Support for a tuple as the second argument added]{2.2}
Fred Drake3adf79e2001-10-12 19:01:43 +0000188\end{cfuncdesc}
189
190Subclass determination is done in a fairly straightforward way, but
191includes a wrinkle that implementors of extensions to the class system
192may want to be aware of. If \class{A} and \class{B} are class
193objects, \class{B} is a subclass of \class{A} if it inherits from
194\class{A} either directly or indirectly. If either is not a class
195object, a more general mechanism is used to determine the class
196relationship of the two objects. When testing if \var{B} is a
197subclass of \var{A}, if \var{A} is \var{B},
198\cfunction{PyObject_IsSubclass()} returns true. If \var{A} and
199\var{B} are different objects, \var{B}'s \member{__bases__} attribute
200is searched in a depth-first fashion for \var{A} --- the presence of
201the \member{__bases__} attribute is considered sufficient for this
202determination.
203
204\begin{cfuncdesc}{int}{PyObject_IsSubclass}{PyObject *derived,
205 PyObject *cls}
206 Returns \code{1} if the class \var{derived} is identical to or
207 derived from the class \var{cls}, otherwise returns \code{0}. In
Walter Dörwaldd9a6ad32002-12-12 16:41:44 +0000208 case of an error, returns \code{-1}. If \var{cls}
209 is a tuple, the check will be done against every entry in \var{cls}.
210 The result will be \code{1} when at least one of the checks returns
211 \code{1}, otherwise it will be \code{0}. If either \var{derived} or
212 \var{cls} is not an actual class object (or tuple), this function
213 uses the generic algorithm described above.
Fred Drake3adf79e2001-10-12 19:01:43 +0000214 \versionadded{2.1}
Walter Dörwaldd9a6ad32002-12-12 16:41:44 +0000215 \versionchanged[Older versions of Python did not support a tuple
216 as the second argument]{2.3}
Fred Drake3adf79e2001-10-12 19:01:43 +0000217\end{cfuncdesc}
218
219
220\begin{cfuncdesc}{int}{PyCallable_Check}{PyObject *o}
221 Determine if the object \var{o} is callable. Return \code{1} if the
222 object is callable and \code{0} otherwise. This function always
223 succeeds.
224\end{cfuncdesc}
225
226
Fred Drake0e0b6182002-04-15 20:51:19 +0000227\begin{cfuncdesc}{PyObject*}{PyObject_Call}{PyObject *callable_object,
228 PyObject *args,
229 PyObject *kw}
230 Call a callable Python object \var{callable_object}, with arguments
231 given by the tuple \var{args}, and named arguments given by the
232 dictionary \var{kw}. If no named arguments are needed, \var{kw} may
233 be \NULL{}. \var{args} must not be \NULL{}, use an empty tuple if
234 no arguments are needed. Returns the result of the call on success,
235 or \NULL{} on failure. This is the equivalent of the Python
Neal Norwitz28ad48e2006-03-17 08:04:59 +0000236 expression \samp{\var{callable_object}(*\var{args}, **\var{kw})}.
Fred Drakef5368272003-01-25 07:48:13 +0000237 \versionadded{2.2}
Fred Drake0e0b6182002-04-15 20:51:19 +0000238\end{cfuncdesc}
239
240
Fred Drake3adf79e2001-10-12 19:01:43 +0000241\begin{cfuncdesc}{PyObject*}{PyObject_CallObject}{PyObject *callable_object,
242 PyObject *args}
243 Call a callable Python object \var{callable_object}, with arguments
244 given by the tuple \var{args}. If no arguments are needed, then
245 \var{args} may be \NULL. Returns the result of the call on
246 success, or \NULL{} on failure. This is the equivalent of the
Neal Norwitz28ad48e2006-03-17 08:04:59 +0000247 Python expression \samp{\var{callable_object}(*\var{args})}.
Fred Drake3adf79e2001-10-12 19:01:43 +0000248\end{cfuncdesc}
249
Fred Drakec44e9ec2001-10-26 16:38:38 +0000250\begin{cfuncdesc}{PyObject*}{PyObject_CallFunction}{PyObject *callable,
251 char *format, \moreargs}
252 Call a callable Python object \var{callable}, with a variable
Fred Drake3adf79e2001-10-12 19:01:43 +0000253 number of C arguments. The C arguments are described using a
254 \cfunction{Py_BuildValue()} style format string. The format may be
255 \NULL, indicating that no arguments are provided. Returns the
256 result of the call on success, or \NULL{} on failure. This is the
Neal Norwitz28ad48e2006-03-17 08:04:59 +0000257 equivalent of the Python expression \samp{\var{callable}(*\var{args})}.
Thomas Wouters477c8d52006-05-27 19:21:47 +0000258 Note that if you only pass \ctype{PyObject *} args,
259 \cfunction{PyObject_CallFunctionObjArgs} is a faster alternative.
Fred Drake3adf79e2001-10-12 19:01:43 +0000260\end{cfuncdesc}
261
262
263\begin{cfuncdesc}{PyObject*}{PyObject_CallMethod}{PyObject *o,
Fred Drakec44e9ec2001-10-26 16:38:38 +0000264 char *method, char *format,
265 \moreargs}
Raymond Hettinger2619c9e2003-12-07 11:40:17 +0000266 Call the method named \var{method} of object \var{o} with a variable
Fred Drake3adf79e2001-10-12 19:01:43 +0000267 number of C arguments. The C arguments are described by a
Andrew M. Kuchlingfe80b632004-08-07 17:53:05 +0000268 \cfunction{Py_BuildValue()} format string that should
269 produce a tuple. The format may be \NULL,
Fred Drake3adf79e2001-10-12 19:01:43 +0000270 indicating that no arguments are provided. Returns the result of the
271 call on success, or \NULL{} on failure. This is the equivalent of
Fred Drakec44e9ec2001-10-26 16:38:38 +0000272 the Python expression \samp{\var{o}.\var{method}(\var{args})}.
Thomas Wouters477c8d52006-05-27 19:21:47 +0000273 Note that if you only pass \ctype{PyObject *} args,
274 \cfunction{PyObject_CallMethodObjArgs} is a faster alternative.
Fred Drakec44e9ec2001-10-26 16:38:38 +0000275\end{cfuncdesc}
276
277
Fred Drakeb0c079e2001-10-28 02:39:03 +0000278\begin{cfuncdesc}{PyObject*}{PyObject_CallFunctionObjArgs}{PyObject *callable,
279 \moreargs,
280 \code{NULL}}
Fred Drakec44e9ec2001-10-26 16:38:38 +0000281 Call a callable Python object \var{callable}, with a variable
282 number of \ctype{PyObject*} arguments. The arguments are provided
283 as a variable number of parameters followed by \NULL.
284 Returns the result of the call on success, or \NULL{} on failure.
285 \versionadded{2.2}
286\end{cfuncdesc}
287
288
Fred Drakeb0c079e2001-10-28 02:39:03 +0000289\begin{cfuncdesc}{PyObject*}{PyObject_CallMethodObjArgs}{PyObject *o,
290 PyObject *name,
291 \moreargs,
292 \code{NULL}}
Fred Drakec44e9ec2001-10-26 16:38:38 +0000293 Calls a method of the object \var{o}, where the name of the method
294 is given as a Python string object in \var{name}. It is called with
295 a variable number of \ctype{PyObject*} arguments. The arguments are
296 provided as a variable number of parameters followed by \NULL.
297 Returns the result of the call on success, or \NULL{} on failure.
298 \versionadded{2.2}
Fred Drake3adf79e2001-10-12 19:01:43 +0000299\end{cfuncdesc}
300
301
Martin v. Löwis29fafd82006-03-01 05:16:03 +0000302\begin{cfuncdesc}{long}{PyObject_Hash}{PyObject *o}
Fred Drake3adf79e2001-10-12 19:01:43 +0000303 Compute and return the hash value of an object \var{o}. On failure,
304 return \code{-1}. This is the equivalent of the Python expression
305 \samp{hash(\var{o})}.\bifuncindex{hash}
306\end{cfuncdesc}
307
308
309\begin{cfuncdesc}{int}{PyObject_IsTrue}{PyObject *o}
310 Returns \code{1} if the object \var{o} is considered to be true, and
311 \code{0} otherwise. This is equivalent to the Python expression
Raymond Hettinger2ed6dff2003-04-16 17:28:12 +0000312 \samp{not not \var{o}}. On failure, return \code{-1}.
313\end{cfuncdesc}
314
315
316\begin{cfuncdesc}{int}{PyObject_Not}{PyObject *o}
317 Returns \code{0} if the object \var{o} is considered to be true, and
318 \code{1} otherwise. This is equivalent to the Python expression
319 \samp{not \var{o}}. On failure, return \code{-1}.
Fred Drake3adf79e2001-10-12 19:01:43 +0000320\end{cfuncdesc}
321
322
323\begin{cfuncdesc}{PyObject*}{PyObject_Type}{PyObject *o}
324 When \var{o} is non-\NULL, returns a type object corresponding to
325 the object type of object \var{o}. On failure, raises
326 \exception{SystemError} and returns \NULL. This is equivalent to
Fred Drake12dd7b12003-04-09 18:15:57 +0000327 the Python expression \code{type(\var{o})}.\bifuncindex{type}
Guido van Rossum6db77182003-04-09 18:02:23 +0000328 This function increments the reference count of the return value.
329 There's really no reason to use this function instead of the
330 common expression \code{\var{o}->ob_type}, which returns a pointer
Fred Drake12dd7b12003-04-09 18:15:57 +0000331 of type \ctype{PyTypeObject*}, except when the incremented reference
Guido van Rossum6db77182003-04-09 18:02:23 +0000332 count is needed.
Fred Drake3adf79e2001-10-12 19:01:43 +0000333\end{cfuncdesc}
334
335\begin{cfuncdesc}{int}{PyObject_TypeCheck}{PyObject *o, PyTypeObject *type}
336 Return true if the object \var{o} is of type \var{type} or a subtype
337 of \var{type}. Both parameters must be non-\NULL.
338 \versionadded{2.2}
339\end{cfuncdesc}
340
Martin v. Löwis29fafd82006-03-01 05:16:03 +0000341\begin{cfuncdesc}{Py_ssize_t}{PyObject_Length}{PyObject *o}
342\cfuncline{Py_ssize_t}{PyObject_Size}{PyObject *o}
Fred Drake3adf79e2001-10-12 19:01:43 +0000343 Return the length of object \var{o}. If the object \var{o} provides
Raymond Hettinger4cd5a082004-01-04 03:11:45 +0000344 either the sequence and mapping protocols, the sequence length is
Fred Drake3adf79e2001-10-12 19:01:43 +0000345 returned. On error, \code{-1} is returned. This is the equivalent
346 to the Python expression \samp{len(\var{o})}.\bifuncindex{len}
347\end{cfuncdesc}
348
349
350\begin{cfuncdesc}{PyObject*}{PyObject_GetItem}{PyObject *o, PyObject *key}
351 Return element of \var{o} corresponding to the object \var{key} or
352 \NULL{} on failure. This is the equivalent of the Python expression
353 \samp{\var{o}[\var{key}]}.
354\end{cfuncdesc}
355
356
357\begin{cfuncdesc}{int}{PyObject_SetItem}{PyObject *o,
358 PyObject *key, PyObject *v}
359 Map the object \var{key} to the value \var{v}. Returns \code{-1} on
360 failure. This is the equivalent of the Python statement
361 \samp{\var{o}[\var{key}] = \var{v}}.
362\end{cfuncdesc}
363
364
365\begin{cfuncdesc}{int}{PyObject_DelItem}{PyObject *o, PyObject *key}
366 Delete the mapping for \var{key} from \var{o}. Returns \code{-1} on
367 failure. This is the equivalent of the Python statement \samp{del
368 \var{o}[\var{key}]}.
369\end{cfuncdesc}
370
371\begin{cfuncdesc}{int}{PyObject_AsFileDescriptor}{PyObject *o}
372 Derives a file-descriptor from a Python object. If the object is an
373 integer or long integer, its value is returned. If not, the
374 object's \method{fileno()} method is called if it exists; the method
375 must return an integer or long integer, which is returned as the
376 file descriptor value. Returns \code{-1} on failure.
377\end{cfuncdesc}
378
379\begin{cfuncdesc}{PyObject*}{PyObject_Dir}{PyObject *o}
380 This is equivalent to the Python expression \samp{dir(\var{o})},
381 returning a (possibly empty) list of strings appropriate for the
382 object argument, or \NULL{} if there was an error. If the argument
383 is \NULL, this is like the Python \samp{dir()}, returning the names
384 of the current locals; in this case, if no execution frame is active
385 then \NULL{} is returned but \cfunction{PyErr_Occurred()} will
386 return false.
387\end{cfuncdesc}
388
Fred Drake314bae52002-03-11 18:46:29 +0000389\begin{cfuncdesc}{PyObject*}{PyObject_GetIter}{PyObject *o}
390 This is equivalent to the Python expression \samp{iter(\var{o})}.
391 It returns a new iterator for the object argument, or the object
392 itself if the object is already an iterator. Raises
393 \exception{TypeError} and returns \NULL{} if the object cannot be
394 iterated.
395\end{cfuncdesc}
396
Fred Drake3adf79e2001-10-12 19:01:43 +0000397
398\section{Number Protocol \label{number}}
399
400\begin{cfuncdesc}{int}{PyNumber_Check}{PyObject *o}
401 Returns \code{1} if the object \var{o} provides numeric protocols,
402 and false otherwise. This function always succeeds.
403\end{cfuncdesc}
404
405
406\begin{cfuncdesc}{PyObject*}{PyNumber_Add}{PyObject *o1, PyObject *o2}
407 Returns the result of adding \var{o1} and \var{o2}, or \NULL{} on
408 failure. This is the equivalent of the Python expression
409 \samp{\var{o1} + \var{o2}}.
410\end{cfuncdesc}
411
412
413\begin{cfuncdesc}{PyObject*}{PyNumber_Subtract}{PyObject *o1, PyObject *o2}
414 Returns the result of subtracting \var{o2} from \var{o1}, or \NULL{}
415 on failure. This is the equivalent of the Python expression
416 \samp{\var{o1} - \var{o2}}.
417\end{cfuncdesc}
418
419
420\begin{cfuncdesc}{PyObject*}{PyNumber_Multiply}{PyObject *o1, PyObject *o2}
421 Returns the result of multiplying \var{o1} and \var{o2}, or \NULL{}
422 on failure. This is the equivalent of the Python expression
423 \samp{\var{o1} * \var{o2}}.
424\end{cfuncdesc}
425
426
427\begin{cfuncdesc}{PyObject*}{PyNumber_Divide}{PyObject *o1, PyObject *o2}
428 Returns the result of dividing \var{o1} by \var{o2}, or \NULL{} on
429 failure. This is the equivalent of the Python expression
430 \samp{\var{o1} / \var{o2}}.
431\end{cfuncdesc}
432
433
434\begin{cfuncdesc}{PyObject*}{PyNumber_FloorDivide}{PyObject *o1, PyObject *o2}
435 Return the floor of \var{o1} divided by \var{o2}, or \NULL{} on
436 failure. This is equivalent to the ``classic'' division of
437 integers.
438 \versionadded{2.2}
439\end{cfuncdesc}
440
441
442\begin{cfuncdesc}{PyObject*}{PyNumber_TrueDivide}{PyObject *o1, PyObject *o2}
443 Return a reasonable approximation for the mathematical value of
444 \var{o1} divided by \var{o2}, or \NULL{} on failure. The return
445 value is ``approximate'' because binary floating point numbers are
446 approximate; it is not possible to represent all real numbers in
447 base two. This function can return a floating point value when
448 passed two integers.
449 \versionadded{2.2}
450\end{cfuncdesc}
451
452
453\begin{cfuncdesc}{PyObject*}{PyNumber_Remainder}{PyObject *o1, PyObject *o2}
454 Returns the remainder of dividing \var{o1} by \var{o2}, or \NULL{}
455 on failure. This is the equivalent of the Python expression
456 \samp{\var{o1} \%\ \var{o2}}.
457\end{cfuncdesc}
458
459
460\begin{cfuncdesc}{PyObject*}{PyNumber_Divmod}{PyObject *o1, PyObject *o2}
461 See the built-in function \function{divmod()}\bifuncindex{divmod}.
462 Returns \NULL{} on failure. This is the equivalent of the Python
463 expression \samp{divmod(\var{o1}, \var{o2})}.
464\end{cfuncdesc}
465
466
467\begin{cfuncdesc}{PyObject*}{PyNumber_Power}{PyObject *o1,
468 PyObject *o2, PyObject *o3}
469 See the built-in function \function{pow()}\bifuncindex{pow}.
470 Returns \NULL{} on failure. This is the equivalent of the Python
471 expression \samp{pow(\var{o1}, \var{o2}, \var{o3})}, where \var{o3}
472 is optional. If \var{o3} is to be ignored, pass \cdata{Py_None} in
473 its place (passing \NULL{} for \var{o3} would cause an illegal
474 memory access).
475\end{cfuncdesc}
476
477
478\begin{cfuncdesc}{PyObject*}{PyNumber_Negative}{PyObject *o}
479 Returns the negation of \var{o} on success, or \NULL{} on failure.
480 This is the equivalent of the Python expression \samp{-\var{o}}.
481\end{cfuncdesc}
482
483
484\begin{cfuncdesc}{PyObject*}{PyNumber_Positive}{PyObject *o}
485 Returns \var{o} on success, or \NULL{} on failure. This is the
486 equivalent of the Python expression \samp{+\var{o}}.
487\end{cfuncdesc}
488
489
490\begin{cfuncdesc}{PyObject*}{PyNumber_Absolute}{PyObject *o}
491 Returns the absolute value of \var{o}, or \NULL{} on failure. This
492 is the equivalent of the Python expression \samp{abs(\var{o})}.
493 \bifuncindex{abs}
494\end{cfuncdesc}
495
496
497\begin{cfuncdesc}{PyObject*}{PyNumber_Invert}{PyObject *o}
498 Returns the bitwise negation of \var{o} on success, or \NULL{} on
499 failure. This is the equivalent of the Python expression
500 \samp{\~\var{o}}.
501\end{cfuncdesc}
502
503
504\begin{cfuncdesc}{PyObject*}{PyNumber_Lshift}{PyObject *o1, PyObject *o2}
505 Returns the result of left shifting \var{o1} by \var{o2} on success,
506 or \NULL{} on failure. This is the equivalent of the Python
507 expression \samp{\var{o1} <\code{<} \var{o2}}.
508\end{cfuncdesc}
509
510
511\begin{cfuncdesc}{PyObject*}{PyNumber_Rshift}{PyObject *o1, PyObject *o2}
512 Returns the result of right shifting \var{o1} by \var{o2} on
513 success, or \NULL{} on failure. This is the equivalent of the
514 Python expression \samp{\var{o1} >\code{>} \var{o2}}.
515\end{cfuncdesc}
516
517
518\begin{cfuncdesc}{PyObject*}{PyNumber_And}{PyObject *o1, PyObject *o2}
Raymond Hettingere5fced72004-04-17 11:57:40 +0000519 Returns the ``bitwise and'' of \var{o1} and \var{o2} on success and
Fred Drake3adf79e2001-10-12 19:01:43 +0000520 \NULL{} on failure. This is the equivalent of the Python expression
521 \samp{\var{o1} \&\ \var{o2}}.
522\end{cfuncdesc}
523
524
525\begin{cfuncdesc}{PyObject*}{PyNumber_Xor}{PyObject *o1, PyObject *o2}
526 Returns the ``bitwise exclusive or'' of \var{o1} by \var{o2} on
527 success, or \NULL{} on failure. This is the equivalent of the
528 Python expression \samp{\var{o1} \textasciicircum{} \var{o2}}.
529\end{cfuncdesc}
530
531\begin{cfuncdesc}{PyObject*}{PyNumber_Or}{PyObject *o1, PyObject *o2}
532 Returns the ``bitwise or'' of \var{o1} and \var{o2} on success, or
533 \NULL{} on failure. This is the equivalent of the Python expression
534 \samp{\var{o1} | \var{o2}}.
535\end{cfuncdesc}
536
537
538\begin{cfuncdesc}{PyObject*}{PyNumber_InPlaceAdd}{PyObject *o1, PyObject *o2}
539 Returns the result of adding \var{o1} and \var{o2}, or \NULL{} on
540 failure. The operation is done \emph{in-place} when \var{o1}
541 supports it. This is the equivalent of the Python statement
542 \samp{\var{o1} += \var{o2}}.
543\end{cfuncdesc}
544
545
546\begin{cfuncdesc}{PyObject*}{PyNumber_InPlaceSubtract}{PyObject *o1,
547 PyObject *o2}
548 Returns the result of subtracting \var{o2} from \var{o1}, or \NULL{}
549 on failure. The operation is done \emph{in-place} when \var{o1}
550 supports it. This is the equivalent of the Python statement
551 \samp{\var{o1} -= \var{o2}}.
552\end{cfuncdesc}
553
554
555\begin{cfuncdesc}{PyObject*}{PyNumber_InPlaceMultiply}{PyObject *o1,
556 PyObject *o2}
557 Returns the result of multiplying \var{o1} and \var{o2}, or \NULL{}
558 on failure. The operation is done \emph{in-place} when \var{o1}
559 supports it. This is the equivalent of the Python statement
560 \samp{\var{o1} *= \var{o2}}.
561\end{cfuncdesc}
562
563
564\begin{cfuncdesc}{PyObject*}{PyNumber_InPlaceDivide}{PyObject *o1,
565 PyObject *o2}
566 Returns the result of dividing \var{o1} by \var{o2}, or \NULL{} on
567 failure. The operation is done \emph{in-place} when \var{o1}
568 supports it. This is the equivalent of the Python statement
569 \samp{\var{o1} /= \var{o2}}.
570\end{cfuncdesc}
571
572
573\begin{cfuncdesc}{PyObject*}{PyNumber_InPlaceFloorDivide}{PyObject *o1,
574 PyObject *o2}
Andrew M. Kuchling1b50b432004-06-05 19:00:55 +0000575 Returns the mathematical floor of dividing \var{o1} by \var{o2}, or
Fred Drake3adf79e2001-10-12 19:01:43 +0000576 \NULL{} on failure. The operation is done \emph{in-place} when
577 \var{o1} supports it. This is the equivalent of the Python
578 statement \samp{\var{o1} //= \var{o2}}.
579 \versionadded{2.2}
580\end{cfuncdesc}
581
582
583\begin{cfuncdesc}{PyObject*}{PyNumber_InPlaceTrueDivide}{PyObject *o1,
584 PyObject *o2}
585 Return a reasonable approximation for the mathematical value of
586 \var{o1} divided by \var{o2}, or \NULL{} on failure. The return
587 value is ``approximate'' because binary floating point numbers are
588 approximate; it is not possible to represent all real numbers in
589 base two. This function can return a floating point value when
590 passed two integers. The operation is done \emph{in-place} when
591 \var{o1} supports it.
592 \versionadded{2.2}
593\end{cfuncdesc}
594
595
596\begin{cfuncdesc}{PyObject*}{PyNumber_InPlaceRemainder}{PyObject *o1,
597 PyObject *o2}
598 Returns the remainder of dividing \var{o1} by \var{o2}, or \NULL{}
599 on failure. The operation is done \emph{in-place} when \var{o1}
600 supports it. This is the equivalent of the Python statement
601 \samp{\var{o1} \%= \var{o2}}.
602\end{cfuncdesc}
603
604
605\begin{cfuncdesc}{PyObject*}{PyNumber_InPlacePower}{PyObject *o1,
606 PyObject *o2, PyObject *o3}
607 See the built-in function \function{pow()}.\bifuncindex{pow}
608 Returns \NULL{} on failure. The operation is done \emph{in-place}
609 when \var{o1} supports it. This is the equivalent of the Python
610 statement \samp{\var{o1} **= \var{o2}} when o3 is \cdata{Py_None},
611 or an in-place variant of \samp{pow(\var{o1}, \var{o2}, \var{o3})}
612 otherwise. If \var{o3} is to be ignored, pass \cdata{Py_None} in its
613 place (passing \NULL{} for \var{o3} would cause an illegal memory
614 access).
615\end{cfuncdesc}
616
617\begin{cfuncdesc}{PyObject*}{PyNumber_InPlaceLshift}{PyObject *o1,
618 PyObject *o2}
619 Returns the result of left shifting \var{o1} by \var{o2} on success,
620 or \NULL{} on failure. The operation is done \emph{in-place} when
621 \var{o1} supports it. This is the equivalent of the Python
622 statement \samp{\var{o1} <\code{<=} \var{o2}}.
623\end{cfuncdesc}
624
625
626\begin{cfuncdesc}{PyObject*}{PyNumber_InPlaceRshift}{PyObject *o1,
627 PyObject *o2}
628 Returns the result of right shifting \var{o1} by \var{o2} on
629 success, or \NULL{} on failure. The operation is done
630 \emph{in-place} when \var{o1} supports it. This is the equivalent
Thomas Wouters477c8d52006-05-27 19:21:47 +0000631 of the Python statement \samp{\var{o1} >>= \var{o2}}.
Fred Drake3adf79e2001-10-12 19:01:43 +0000632\end{cfuncdesc}
633
634
635\begin{cfuncdesc}{PyObject*}{PyNumber_InPlaceAnd}{PyObject *o1, PyObject *o2}
636 Returns the ``bitwise and'' of \var{o1} and \var{o2} on success and
637 \NULL{} on failure. The operation is done \emph{in-place} when
638 \var{o1} supports it. This is the equivalent of the Python
639 statement \samp{\var{o1} \&= \var{o2}}.
640\end{cfuncdesc}
641
642
643\begin{cfuncdesc}{PyObject*}{PyNumber_InPlaceXor}{PyObject *o1, PyObject *o2}
644 Returns the ``bitwise exclusive or'' of \var{o1} by \var{o2} on
645 success, or \NULL{} on failure. The operation is done
646 \emph{in-place} when \var{o1} supports it. This is the equivalent
647 of the Python statement \samp{\var{o1} \textasciicircum= \var{o2}}.
648\end{cfuncdesc}
649
650\begin{cfuncdesc}{PyObject*}{PyNumber_InPlaceOr}{PyObject *o1, PyObject *o2}
651 Returns the ``bitwise or'' of \var{o1} and \var{o2} on success, or
652 \NULL{} on failure. The operation is done \emph{in-place} when
653 \var{o1} supports it. This is the equivalent of the Python
654 statement \samp{\var{o1} |= \var{o2}}.
655\end{cfuncdesc}
656
657\begin{cfuncdesc}{int}{PyNumber_Coerce}{PyObject **p1, PyObject **p2}
658 This function takes the addresses of two variables of type
659 \ctype{PyObject*}. If the objects pointed to by \code{*\var{p1}}
660 and \code{*\var{p2}} have the same type, increment their reference
661 count and return \code{0} (success). If the objects can be converted
662 to a common numeric type, replace \code{*p1} and \code{*p2} by their
663 converted value (with 'new' reference counts), and return \code{0}.
664 If no conversion is possible, or if some other error occurs, return
665 \code{-1} (failure) and don't increment the reference counts. The
666 call \code{PyNumber_Coerce(\&o1, \&o2)} is equivalent to the Python
667 statement \samp{\var{o1}, \var{o2} = coerce(\var{o1}, \var{o2})}.
668 \bifuncindex{coerce}
669\end{cfuncdesc}
670
671\begin{cfuncdesc}{PyObject*}{PyNumber_Int}{PyObject *o}
672 Returns the \var{o} converted to an integer object on success, or
Walter Dörwaldf1715402002-11-19 20:49:15 +0000673 \NULL{} on failure. If the argument is outside the integer range
674 a long object will be returned instead. This is the equivalent
675 of the Python expression \samp{int(\var{o})}.\bifuncindex{int}
Fred Drake3adf79e2001-10-12 19:01:43 +0000676\end{cfuncdesc}
677
678\begin{cfuncdesc}{PyObject*}{PyNumber_Long}{PyObject *o}
679 Returns the \var{o} converted to a long integer object on success,
680 or \NULL{} on failure. This is the equivalent of the Python
681 expression \samp{long(\var{o})}.\bifuncindex{long}
682\end{cfuncdesc}
683
684\begin{cfuncdesc}{PyObject*}{PyNumber_Float}{PyObject *o}
685 Returns the \var{o} converted to a float object on success, or
686 \NULL{} on failure. This is the equivalent of the Python expression
687 \samp{float(\var{o})}.\bifuncindex{float}
688\end{cfuncdesc}
689
Guido van Rossum38fff8c2006-03-07 18:50:55 +0000690\begin{cfuncdesc}{Py_ssize_t}{PyNumber_Index}{PyObject *o}
691 Returns the \var{o} converted to a Py_ssize_t integer on success, or
692 -1 with an exception raised on failure.
Neal Norwitz025f14b2006-03-08 05:29:18 +0000693 \versionadded{2.5}
Guido van Rossum38fff8c2006-03-07 18:50:55 +0000694\end{cfuncdesc}
Fred Drake3adf79e2001-10-12 19:01:43 +0000695
696\section{Sequence Protocol \label{sequence}}
697
698\begin{cfuncdesc}{int}{PySequence_Check}{PyObject *o}
699 Return \code{1} if the object provides sequence protocol, and
700 \code{0} otherwise. This function always succeeds.
701\end{cfuncdesc}
702
Martin v. Löwis29fafd82006-03-01 05:16:03 +0000703\begin{cfuncdesc}{Py_ssize_t}{PySequence_Size}{PyObject *o}
Fred Drake3adf79e2001-10-12 19:01:43 +0000704 Returns the number of objects in sequence \var{o} on success, and
705 \code{-1} on failure. For objects that do not provide sequence
706 protocol, this is equivalent to the Python expression
707 \samp{len(\var{o})}.\bifuncindex{len}
708\end{cfuncdesc}
709
Martin v. Löwis29fafd82006-03-01 05:16:03 +0000710\begin{cfuncdesc}{Py_ssize_t}{PySequence_Length}{PyObject *o}
Fred Drake3adf79e2001-10-12 19:01:43 +0000711 Alternate name for \cfunction{PySequence_Size()}.
712\end{cfuncdesc}
713
714\begin{cfuncdesc}{PyObject*}{PySequence_Concat}{PyObject *o1, PyObject *o2}
715 Return the concatenation of \var{o1} and \var{o2} on success, and
716 \NULL{} on failure. This is the equivalent of the Python
717 expression \samp{\var{o1} + \var{o2}}.
718\end{cfuncdesc}
719
720
Martin v. Löwis29fafd82006-03-01 05:16:03 +0000721\begin{cfuncdesc}{PyObject*}{PySequence_Repeat}{PyObject *o, Py_ssize_t count}
Fred Drake3adf79e2001-10-12 19:01:43 +0000722 Return the result of repeating sequence object \var{o} \var{count}
723 times, or \NULL{} on failure. This is the equivalent of the Python
724 expression \samp{\var{o} * \var{count}}.
725\end{cfuncdesc}
726
727\begin{cfuncdesc}{PyObject*}{PySequence_InPlaceConcat}{PyObject *o1,
728 PyObject *o2}
729 Return the concatenation of \var{o1} and \var{o2} on success, and
730 \NULL{} on failure. The operation is done \emph{in-place} when
731 \var{o1} supports it. This is the equivalent of the Python
732 expression \samp{\var{o1} += \var{o2}}.
733\end{cfuncdesc}
734
735
Martin v. Löwis29fafd82006-03-01 05:16:03 +0000736\begin{cfuncdesc}{PyObject*}{PySequence_InPlaceRepeat}{PyObject *o, Py_ssize_t count}
Fred Drake3adf79e2001-10-12 19:01:43 +0000737 Return the result of repeating sequence object \var{o} \var{count}
738 times, or \NULL{} on failure. The operation is done \emph{in-place}
739 when \var{o} supports it. This is the equivalent of the Python
740 expression \samp{\var{o} *= \var{count}}.
741\end{cfuncdesc}
742
743
Martin v. Löwis29fafd82006-03-01 05:16:03 +0000744\begin{cfuncdesc}{PyObject*}{PySequence_GetItem}{PyObject *o, Py_ssize_t i}
Fred Drake3adf79e2001-10-12 19:01:43 +0000745 Return the \var{i}th element of \var{o}, or \NULL{} on failure.
746 This is the equivalent of the Python expression
747 \samp{\var{o}[\var{i}]}.
748\end{cfuncdesc}
749
750
Martin v. Löwis29fafd82006-03-01 05:16:03 +0000751\begin{cfuncdesc}{PyObject*}{PySequence_GetSlice}{PyObject *o, Py_ssize_t i1, Py_ssize_t i2}
Fred Drake3adf79e2001-10-12 19:01:43 +0000752 Return the slice of sequence object \var{o} between \var{i1} and
753 \var{i2}, or \NULL{} on failure. This is the equivalent of the
754 Python expression \samp{\var{o}[\var{i1}:\var{i2}]}.
755\end{cfuncdesc}
756
757
Martin v. Löwis29fafd82006-03-01 05:16:03 +0000758\begin{cfuncdesc}{int}{PySequence_SetItem}{PyObject *o, Py_ssize_t i, PyObject *v}
Fred Drake3adf79e2001-10-12 19:01:43 +0000759 Assign object \var{v} to the \var{i}th element of \var{o}. Returns
760 \code{-1} on failure. This is the equivalent of the Python
Raymond Hettinger12c484d2003-08-09 04:37:14 +0000761 statement \samp{\var{o}[\var{i}] = \var{v}}. This function \emph{does not}
762 steal a reference to \var{v}.
Fred Drake3adf79e2001-10-12 19:01:43 +0000763\end{cfuncdesc}
764
Martin v. Löwis29fafd82006-03-01 05:16:03 +0000765\begin{cfuncdesc}{int}{PySequence_DelItem}{PyObject *o, Py_ssize_t i}
Fred Drake3adf79e2001-10-12 19:01:43 +0000766 Delete the \var{i}th element of object \var{o}. Returns \code{-1}
767 on failure. This is the equivalent of the Python statement
768 \samp{del \var{o}[\var{i}]}.
769\end{cfuncdesc}
770
Martin v. Löwis29fafd82006-03-01 05:16:03 +0000771\begin{cfuncdesc}{int}{PySequence_SetSlice}{PyObject *o, Py_ssize_t i1,
772 Py_ssize_t i2, PyObject *v}
Fred Drake3adf79e2001-10-12 19:01:43 +0000773 Assign the sequence object \var{v} to the slice in sequence object
774 \var{o} from \var{i1} to \var{i2}. This is the equivalent of the
775 Python statement \samp{\var{o}[\var{i1}:\var{i2}] = \var{v}}.
776\end{cfuncdesc}
777
Martin v. Löwis29fafd82006-03-01 05:16:03 +0000778\begin{cfuncdesc}{int}{PySequence_DelSlice}{PyObject *o, Py_ssize_t i1, Py_ssize_t i2}
Fred Drake3adf79e2001-10-12 19:01:43 +0000779 Delete the slice in sequence object \var{o} from \var{i1} to
780 \var{i2}. Returns \code{-1} on failure. This is the equivalent of
781 the Python statement \samp{del \var{o}[\var{i1}:\var{i2}]}.
782\end{cfuncdesc}
783
Fred Drake3adf79e2001-10-12 19:01:43 +0000784\begin{cfuncdesc}{int}{PySequence_Count}{PyObject *o, PyObject *value}
785 Return the number of occurrences of \var{value} in \var{o}, that is,
786 return the number of keys for which \code{\var{o}[\var{key}] ==
787 \var{value}}. On failure, return \code{-1}. This is equivalent to
788 the Python expression \samp{\var{o}.count(\var{value})}.
789\end{cfuncdesc}
790
791\begin{cfuncdesc}{int}{PySequence_Contains}{PyObject *o, PyObject *value}
792 Determine if \var{o} contains \var{value}. If an item in \var{o} is
793 equal to \var{value}, return \code{1}, otherwise return \code{0}.
794 On error, return \code{-1}. This is equivalent to the Python
795 expression \samp{\var{value} in \var{o}}.
796\end{cfuncdesc}
797
798\begin{cfuncdesc}{int}{PySequence_Index}{PyObject *o, PyObject *value}
799 Return the first index \var{i} for which \code{\var{o}[\var{i}] ==
800 \var{value}}. On error, return \code{-1}. This is equivalent to
801 the Python expression \samp{\var{o}.index(\var{value})}.
802\end{cfuncdesc}
803
804\begin{cfuncdesc}{PyObject*}{PySequence_List}{PyObject *o}
805 Return a list object with the same contents as the arbitrary
806 sequence \var{o}. The returned list is guaranteed to be new.
807\end{cfuncdesc}
808
809\begin{cfuncdesc}{PyObject*}{PySequence_Tuple}{PyObject *o}
810 Return a tuple object with the same contents as the arbitrary
Neal Norwitz98fcaaf2005-10-12 03:58:14 +0000811 sequence \var{o} or \NULL{} on failure. If \var{o} is a tuple,
812 a new reference will be returned, otherwise a tuple will be
813 constructed with the appropriate contents. This is equivalent
814 to the Python expression \samp{tuple(\var{o})}.
815 \bifuncindex{tuple}
Fred Drake3adf79e2001-10-12 19:01:43 +0000816\end{cfuncdesc}
817
818\begin{cfuncdesc}{PyObject*}{PySequence_Fast}{PyObject *o, const char *m}
819 Returns the sequence \var{o} as a tuple, unless it is already a
820 tuple or list, in which case \var{o} is returned. Use
821 \cfunction{PySequence_Fast_GET_ITEM()} to access the members of the
822 result. Returns \NULL{} on failure. If the object is not a
823 sequence, raises \exception{TypeError} with \var{m} as the message
824 text.
825\end{cfuncdesc}
826
Martin v. Löwis29fafd82006-03-01 05:16:03 +0000827\begin{cfuncdesc}{PyObject*}{PySequence_Fast_GET_ITEM}{PyObject *o, Py_ssize_t i}
Fred Drake3adf79e2001-10-12 19:01:43 +0000828 Return the \var{i}th element of \var{o}, assuming that \var{o} was
Fred Drakec37b65e2001-11-28 07:26:15 +0000829 returned by \cfunction{PySequence_Fast()}, \var{o} is not \NULL,
Tim Peters1fc240e2001-10-26 05:06:50 +0000830 and that \var{i} is within bounds.
831\end{cfuncdesc}
832
Raymond Hettingerc1e4f9d2004-03-12 08:04:00 +0000833\begin{cfuncdesc}{PyObject**}{PySequence_Fast_ITEMS}{PyObject *o}
834 Return the underlying array of PyObject pointers. Assumes that
835 \var{o} was returned by \cfunction{PySequence_Fast()} and
836 \var{o} is not \NULL.
837 \versionadded{2.4}
838\end{cfuncdesc}
839
Martin v. Löwis29fafd82006-03-01 05:16:03 +0000840\begin{cfuncdesc}{PyObject*}{PySequence_ITEM}{PyObject *o, Py_ssize_t i}
Brett Cannon77e02122003-09-07 02:22:16 +0000841 Return the \var{i}th element of \var{o} or \NULL{} on failure.
Martin v. Löwis01f94bd2002-05-08 08:44:21 +0000842 Macro form of \cfunction{PySequence_GetItem()} but without checking
843 that \cfunction{PySequence_Check(\var{o})} is true and without
Fred Drake86228e42002-05-23 16:02:28 +0000844 adjustment for negative indices.
845 \versionadded{2.3}
Martin v. Löwis01f94bd2002-05-08 08:44:21 +0000846\end{cfuncdesc}
847
Tim Peters1fc240e2001-10-26 05:06:50 +0000848\begin{cfuncdesc}{int}{PySequence_Fast_GET_SIZE}{PyObject *o}
849 Returns the length of \var{o}, assuming that \var{o} was
850 returned by \cfunction{PySequence_Fast()} and that \var{o} is
Fred Drakec37b65e2001-11-28 07:26:15 +0000851 not \NULL. The size can also be gotten by calling
Tim Peters1fc240e2001-10-26 05:06:50 +0000852 \cfunction{PySequence_Size()} on \var{o}, but
853 \cfunction{PySequence_Fast_GET_SIZE()} is faster because it can
854 assume \var{o} is a list or tuple.
Fred Drake3adf79e2001-10-12 19:01:43 +0000855\end{cfuncdesc}
856
857
858\section{Mapping Protocol \label{mapping}}
859
860\begin{cfuncdesc}{int}{PyMapping_Check}{PyObject *o}
861 Return \code{1} if the object provides mapping protocol, and
862 \code{0} otherwise. This function always succeeds.
863\end{cfuncdesc}
864
865
Martin v. Löwis29fafd82006-03-01 05:16:03 +0000866\begin{cfuncdesc}{Py_ssize_t}{PyMapping_Length}{PyObject *o}
Fred Drake3adf79e2001-10-12 19:01:43 +0000867 Returns the number of keys in object \var{o} on success, and
868 \code{-1} on failure. For objects that do not provide mapping
869 protocol, this is equivalent to the Python expression
870 \samp{len(\var{o})}.\bifuncindex{len}
871\end{cfuncdesc}
872
873
874\begin{cfuncdesc}{int}{PyMapping_DelItemString}{PyObject *o, char *key}
875 Remove the mapping for object \var{key} from the object \var{o}.
876 Return \code{-1} on failure. This is equivalent to the Python
877 statement \samp{del \var{o}[\var{key}]}.
878\end{cfuncdesc}
879
880
881\begin{cfuncdesc}{int}{PyMapping_DelItem}{PyObject *o, PyObject *key}
882 Remove the mapping for object \var{key} from the object \var{o}.
883 Return \code{-1} on failure. This is equivalent to the Python
884 statement \samp{del \var{o}[\var{key}]}.
885\end{cfuncdesc}
886
887
888\begin{cfuncdesc}{int}{PyMapping_HasKeyString}{PyObject *o, char *key}
889 On success, return \code{1} if the mapping object has the key
890 \var{key} and \code{0} otherwise. This is equivalent to the Python
891 expression \samp{\var{o}.has_key(\var{key})}. This function always
892 succeeds.
893\end{cfuncdesc}
894
895
896\begin{cfuncdesc}{int}{PyMapping_HasKey}{PyObject *o, PyObject *key}
897 Return \code{1} if the mapping object has the key \var{key} and
898 \code{0} otherwise. This is equivalent to the Python expression
899 \samp{\var{o}.has_key(\var{key})}. This function always succeeds.
900\end{cfuncdesc}
901
902
903\begin{cfuncdesc}{PyObject*}{PyMapping_Keys}{PyObject *o}
904 On success, return a list of the keys in object \var{o}. On
905 failure, return \NULL. This is equivalent to the Python expression
906 \samp{\var{o}.keys()}.
907\end{cfuncdesc}
908
909
910\begin{cfuncdesc}{PyObject*}{PyMapping_Values}{PyObject *o}
911 On success, return a list of the values in object \var{o}. On
912 failure, return \NULL. This is equivalent to the Python expression
913 \samp{\var{o}.values()}.
914\end{cfuncdesc}
915
916
917\begin{cfuncdesc}{PyObject*}{PyMapping_Items}{PyObject *o}
918 On success, return a list of the items in object \var{o}, where each
919 item is a tuple containing a key-value pair. On failure, return
920 \NULL. This is equivalent to the Python expression
921 \samp{\var{o}.items()}.
922\end{cfuncdesc}
923
924
925\begin{cfuncdesc}{PyObject*}{PyMapping_GetItemString}{PyObject *o, char *key}
926 Return element of \var{o} corresponding to the object \var{key} or
927 \NULL{} on failure. This is the equivalent of the Python expression
928 \samp{\var{o}[\var{key}]}.
929\end{cfuncdesc}
930
931\begin{cfuncdesc}{int}{PyMapping_SetItemString}{PyObject *o, char *key,
932 PyObject *v}
933 Map the object \var{key} to the value \var{v} in object \var{o}.
934 Returns \code{-1} on failure. This is the equivalent of the Python
935 statement \samp{\var{o}[\var{key}] = \var{v}}.
936\end{cfuncdesc}
937
938
939\section{Iterator Protocol \label{iterator}}
940
941\versionadded{2.2}
942
943There are only a couple of functions specifically for working with
944iterators.
945
946\begin{cfuncdesc}{int}{PyIter_Check}{PyObject *o}
947 Return true if the object \var{o} supports the iterator protocol.
948\end{cfuncdesc}
949
950\begin{cfuncdesc}{PyObject*}{PyIter_Next}{PyObject *o}
951 Return the next value from the iteration \var{o}. If the object is
952 an iterator, this retrieves the next value from the iteration, and
953 returns \NULL{} with no exception set if there are no remaining
954 items. If the object is not an iterator, \exception{TypeError} is
955 raised, or if there is an error in retrieving the item, returns
956 \NULL{} and passes along the exception.
957\end{cfuncdesc}
958
959To write a loop which iterates over an iterator, the C code should
960look something like this:
961
962\begin{verbatim}
Fred Drake314bae52002-03-11 18:46:29 +0000963PyObject *iterator = PyObject_GetIter(obj);
Fred Drake3adf79e2001-10-12 19:01:43 +0000964PyObject *item;
965
Fred Drake314bae52002-03-11 18:46:29 +0000966if (iterator == NULL) {
967 /* propagate error */
968}
969
970while (item = PyIter_Next(iterator)) {
Fred Drake3adf79e2001-10-12 19:01:43 +0000971 /* do something with item */
Fred Drake8b8fe282001-12-26 16:53:48 +0000972 ...
973 /* release reference when done */
974 Py_DECREF(item);
Fred Drake3adf79e2001-10-12 19:01:43 +0000975}
Fred Drake314bae52002-03-11 18:46:29 +0000976
977Py_DECREF(iterator);
978
Fred Drake3adf79e2001-10-12 19:01:43 +0000979if (PyErr_Occurred()) {
Fred Drake314bae52002-03-11 18:46:29 +0000980 /* propagate error */
Fred Drake3adf79e2001-10-12 19:01:43 +0000981}
982else {
983 /* continue doing useful work */
984}
Fred Drake8b8fe282001-12-26 16:53:48 +0000985\end{verbatim}
986
Jeremy Hylton89c3a222001-11-09 21:59:42 +0000987
Fred Drake94ead572001-11-09 23:34:26 +0000988\section{Buffer Protocol \label{abstract-buffer}}
Jeremy Hylton89c3a222001-11-09 21:59:42 +0000989
990\begin{cfuncdesc}{int}{PyObject_AsCharBuffer}{PyObject *obj,
Fred Drake94ead572001-11-09 23:34:26 +0000991 const char **buffer,
Martin v. Löwis29fafd82006-03-01 05:16:03 +0000992 Py_ssize_t *buffer_len}
Jeremy Hylton89c3a222001-11-09 21:59:42 +0000993 Returns a pointer to a read-only memory location useable as character-
994 based input. The \var{obj} argument must support the single-segment
Fred Drake243ea712002-04-04 04:10:36 +0000995 character buffer interface. On success, returns \code{0}, sets
Thomas Hellerbfeeeee2001-11-12 07:46:31 +0000996 \var{buffer} to the memory location and \var{buffer_len} to the buffer
Fred Drake243ea712002-04-04 04:10:36 +0000997 length. Returns \code{-1} and sets a \exception{TypeError} on error.
Fred Drake94ead572001-11-09 23:34:26 +0000998 \versionadded{1.6}
Jeremy Hylton89c3a222001-11-09 21:59:42 +0000999\end{cfuncdesc}
1000
1001\begin{cfuncdesc}{int}{PyObject_AsReadBuffer}{PyObject *obj,
Andrew M. Kuchlingee5e4cd2004-07-07 13:07:47 +00001002 const void **buffer,
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001003 Py_ssize_t *buffer_len}
Jeremy Hylton89c3a222001-11-09 21:59:42 +00001004 Returns a pointer to a read-only memory location containing
1005 arbitrary data. The \var{obj} argument must support the
1006 single-segment readable buffer interface. On success, returns
Fred Drake243ea712002-04-04 04:10:36 +00001007 \code{0}, sets \var{buffer} to the memory location and \var{buffer_len}
1008 to the buffer length. Returns \code{-1} and sets a
Jeremy Hylton89c3a222001-11-09 21:59:42 +00001009 \exception{TypeError} on error.
Fred Drake94ead572001-11-09 23:34:26 +00001010 \versionadded{1.6}
Jeremy Hylton89c3a222001-11-09 21:59:42 +00001011\end{cfuncdesc}
1012
1013\begin{cfuncdesc}{int}{PyObject_CheckReadBuffer}{PyObject *o}
1014 Returns \code{1} if \var{o} supports the single-segment readable
1015 buffer interface. Otherwise returns \code{0}.
Fred Drake94ead572001-11-09 23:34:26 +00001016 \versionadded{2.2}
Fred Drake8b8fe282001-12-26 16:53:48 +00001017\end{cfuncdesc}
Jeremy Hylton89c3a222001-11-09 21:59:42 +00001018
1019\begin{cfuncdesc}{int}{PyObject_AsWriteBuffer}{PyObject *obj,
Andrew M. Kuchlingee5e4cd2004-07-07 13:07:47 +00001020 void **buffer,
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001021 Py_ssize_t *buffer_len}
Jeremy Hylton89c3a222001-11-09 21:59:42 +00001022 Returns a pointer to a writeable memory location. The \var{obj}
1023 argument must support the single-segment, character buffer
Fred Drake243ea712002-04-04 04:10:36 +00001024 interface. On success, returns \code{0}, sets \var{buffer} to the
Thomas Hellerbfeeeee2001-11-12 07:46:31 +00001025 memory location and \var{buffer_len} to the buffer length. Returns
Fred Drake243ea712002-04-04 04:10:36 +00001026 \code{-1} and sets a \exception{TypeError} on error.
Fred Drake94ead572001-11-09 23:34:26 +00001027 \versionadded{1.6}
Jeremy Hylton89c3a222001-11-09 21:59:42 +00001028\end{cfuncdesc}