blob: c5b53d7705672857eb7b86576cb2a89558dc05a0 [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
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00008It is not possible to use these functions on objects that are not properly
9initialized, such as a list object that has been created by
10\cfunction{PyList_New()}, but whose items have not been set to some
11non-\code{NULL} value yet.
Fred Drake3adf79e2001-10-12 19:01:43 +000012
13\section{Object Protocol \label{object}}
14
15\begin{cfuncdesc}{int}{PyObject_Print}{PyObject *o, FILE *fp, int flags}
16 Print an object \var{o}, on file \var{fp}. Returns \code{-1} on
17 error. The flags argument is used to enable certain printing
18 options. The only option currently supported is
19 \constant{Py_PRINT_RAW}; if given, the \function{str()} of the
20 object is written instead of the \function{repr()}.
21\end{cfuncdesc}
22
Martin v. Löwis29fafd82006-03-01 05:16:03 +000023\begin{cfuncdesc}{int}{PyObject_HasAttrString}{PyObject *o, const char *attr_name}
Fred Drake3adf79e2001-10-12 19:01:43 +000024 Returns \code{1} if \var{o} has the attribute \var{attr_name}, and
25 \code{0} otherwise. This is equivalent to the Python expression
26 \samp{hasattr(\var{o}, \var{attr_name})}. This function always
27 succeeds.
28\end{cfuncdesc}
29
30\begin{cfuncdesc}{PyObject*}{PyObject_GetAttrString}{PyObject *o,
Martin v. Löwis29fafd82006-03-01 05:16:03 +000031 const char *attr_name}
Fred Drake3adf79e2001-10-12 19:01:43 +000032 Retrieve an attribute named \var{attr_name} from object \var{o}.
33 Returns the attribute value on success, or \NULL{} on failure.
34 This is the equivalent of the Python expression
35 \samp{\var{o}.\var{attr_name}}.
36\end{cfuncdesc}
37
38
39\begin{cfuncdesc}{int}{PyObject_HasAttr}{PyObject *o, PyObject *attr_name}
40 Returns \code{1} if \var{o} has the attribute \var{attr_name}, and
41 \code{0} otherwise. This is equivalent to the Python expression
42 \samp{hasattr(\var{o}, \var{attr_name})}. This function always
43 succeeds.
44\end{cfuncdesc}
45
46
47\begin{cfuncdesc}{PyObject*}{PyObject_GetAttr}{PyObject *o,
48 PyObject *attr_name}
49 Retrieve an attribute named \var{attr_name} from object \var{o}.
50 Returns the attribute value on success, or \NULL{} on failure. This
51 is the equivalent of the Python expression
52 \samp{\var{o}.\var{attr_name}}.
53\end{cfuncdesc}
54
55
56\begin{cfuncdesc}{int}{PyObject_SetAttrString}{PyObject *o,
Martin v. Löwis29fafd82006-03-01 05:16:03 +000057 const char *attr_name, PyObject *v}
Fred Drake3adf79e2001-10-12 19:01:43 +000058 Set the value of the attribute named \var{attr_name}, for object
59 \var{o}, to the value \var{v}. Returns \code{-1} on failure. This
60 is the equivalent of the Python statement
61 \samp{\var{o}.\var{attr_name} = \var{v}}.
62\end{cfuncdesc}
63
64
65\begin{cfuncdesc}{int}{PyObject_SetAttr}{PyObject *o,
66 PyObject *attr_name, PyObject *v}
67 Set the value of the attribute named \var{attr_name}, for object
68 \var{o}, to the value \var{v}. Returns \code{-1} on failure. This
69 is the equivalent of the Python statement
70 \samp{\var{o}.\var{attr_name} = \var{v}}.
71\end{cfuncdesc}
72
73
Martin v. Löwis29fafd82006-03-01 05:16:03 +000074\begin{cfuncdesc}{int}{PyObject_DelAttrString}{PyObject *o, const char *attr_name}
Fred Drake3adf79e2001-10-12 19:01:43 +000075 Delete attribute named \var{attr_name}, for object \var{o}. Returns
76 \code{-1} on failure. This is the equivalent of the Python
77 statement: \samp{del \var{o}.\var{attr_name}}.
78\end{cfuncdesc}
79
80
81\begin{cfuncdesc}{int}{PyObject_DelAttr}{PyObject *o, PyObject *attr_name}
82 Delete attribute named \var{attr_name}, for object \var{o}. Returns
83 \code{-1} on failure. This is the equivalent of the Python
84 statement \samp{del \var{o}.\var{attr_name}}.
85\end{cfuncdesc}
86
87
Fred Drake178153f2002-06-13 11:51:48 +000088\begin{cfuncdesc}{PyObject*}{PyObject_RichCompare}{PyObject *o1,
Fred Drakea0c5e9f2002-06-14 14:35:56 +000089 PyObject *o2, int opid}
Fred Drake178153f2002-06-13 11:51:48 +000090 Compare the values of \var{o1} and \var{o2} using the operation
Fred Drakea0c5e9f2002-06-14 14:35:56 +000091 specified by \var{opid}, which must be one of
Fred Drake178153f2002-06-13 11:51:48 +000092 \constant{Py_LT},
93 \constant{Py_LE},
94 \constant{Py_EQ},
95 \constant{Py_NE},
96 \constant{Py_GT}, or
97 \constant{Py_GE}, corresponding to
98 \code{<},
99 \code{<=},
100 \code{==},
101 \code{!=},
102 \code{>}, or
103 \code{>=} respectively. This is the equivalent of the Python expression
Fred Drakea0c5e9f2002-06-14 14:35:56 +0000104 \samp{\var{o1} op \var{o2}}, where \code{op} is the operator
105 corresponding to \var{opid}. Returns the value of the comparison on
Fred Drake178153f2002-06-13 11:51:48 +0000106 success, or \NULL{} on failure.
107\end{cfuncdesc}
108
109\begin{cfuncdesc}{int}{PyObject_RichCompareBool}{PyObject *o1,
Fred Drakea0c5e9f2002-06-14 14:35:56 +0000110 PyObject *o2, int opid}
Fred Drake178153f2002-06-13 11:51:48 +0000111 Compare the values of \var{o1} and \var{o2} using the operation
Fred Drakea0c5e9f2002-06-14 14:35:56 +0000112 specified by \var{opid}, which must be one of
Fred Drake178153f2002-06-13 11:51:48 +0000113 \constant{Py_LT},
114 \constant{Py_LE},
115 \constant{Py_EQ},
116 \constant{Py_NE},
117 \constant{Py_GT}, or
118 \constant{Py_GE}, corresponding to
119 \code{<},
120 \code{<=},
121 \code{==},
122 \code{!=},
123 \code{>}, or
124 \code{>=} respectively. Returns \code{-1} on error, \code{0} if the
125 result is false, \code{1} otherwise. This is the equivalent of the
Fred Drakea0c5e9f2002-06-14 14:35:56 +0000126 Python expression \samp{\var{o1} op \var{o2}}, where
127 \code{op} is the operator corresponding to \var{opid}.
Fred Drake178153f2002-06-13 11:51:48 +0000128\end{cfuncdesc}
129
Fred Drake3adf79e2001-10-12 19:01:43 +0000130\begin{cfuncdesc}{int}{PyObject_Cmp}{PyObject *o1, PyObject *o2, int *result}
131 Compare the values of \var{o1} and \var{o2} using a routine provided
132 by \var{o1}, if one exists, otherwise with a routine provided by
133 \var{o2}. The result of the comparison is returned in
134 \var{result}. Returns \code{-1} on failure. This is the equivalent
135 of the Python statement\bifuncindex{cmp} \samp{\var{result} =
136 cmp(\var{o1}, \var{o2})}.
137\end{cfuncdesc}
138
139
140\begin{cfuncdesc}{int}{PyObject_Compare}{PyObject *o1, PyObject *o2}
141 Compare the values of \var{o1} and \var{o2} using a routine provided
142 by \var{o1}, if one exists, otherwise with a routine provided by
143 \var{o2}. Returns the result of the comparison on success. On
144 error, the value returned is undefined; use
145 \cfunction{PyErr_Occurred()} to detect an error. This is equivalent
146 to the Python expression\bifuncindex{cmp} \samp{cmp(\var{o1},
147 \var{o2})}.
148\end{cfuncdesc}
149
150
151\begin{cfuncdesc}{PyObject*}{PyObject_Repr}{PyObject *o}
152 Compute a string representation of object \var{o}. Returns the
153 string representation on success, \NULL{} on failure. This is the
154 equivalent of the Python expression \samp{repr(\var{o})}. Called by
155 the \function{repr()}\bifuncindex{repr} built-in function and by
156 reverse quotes.
157\end{cfuncdesc}
158
159
160\begin{cfuncdesc}{PyObject*}{PyObject_Str}{PyObject *o}
161 Compute a string representation of object \var{o}. Returns the
162 string representation on success, \NULL{} on failure. This is the
163 equivalent of the Python expression \samp{str(\var{o})}. Called by
164 the \function{str()}\bifuncindex{str} built-in function and by the
165 \keyword{print} statement.
166\end{cfuncdesc}
167
168
169\begin{cfuncdesc}{PyObject*}{PyObject_Unicode}{PyObject *o}
170 Compute a Unicode string representation of object \var{o}. Returns
171 the Unicode string representation on success, \NULL{} on failure.
172 This is the equivalent of the Python expression
Neal Norwitz4ddfd502002-07-28 13:55:20 +0000173 \samp{unicode(\var{o})}. Called by the
174 \function{unicode()}\bifuncindex{unicode} built-in function.
Fred Drake3adf79e2001-10-12 19:01:43 +0000175\end{cfuncdesc}
176
177\begin{cfuncdesc}{int}{PyObject_IsInstance}{PyObject *inst, PyObject *cls}
Fred Drakeb957bc32002-04-23 18:15:44 +0000178 Returns \code{1} if \var{inst} is an instance of the class \var{cls}
179 or a subclass of \var{cls}, or \code{0} if not. On error, returns
180 \code{-1} and sets an exception. If \var{cls} is a type object
181 rather than a class object, \cfunction{PyObject_IsInstance()}
Walter Dörwald6d5f30e2002-12-06 10:09:16 +0000182 returns \code{1} if \var{inst} is of type \var{cls}. If \var{cls}
183 is a tuple, the check will be done against every entry in \var{cls}.
184 The result will be \code{1} when at least one of the checks returns
185 \code{1}, otherwise it will be \code{0}. If \var{inst} is not a class
186 instance and \var{cls} is neither a type object, nor a class object,
187 nor a tuple, \var{inst} must have a \member{__class__} attribute
Fred Drakeb957bc32002-04-23 18:15:44 +0000188 --- the class relationship of the value of that attribute with
189 \var{cls} will be used to determine the result of this function.
Fred Drake3adf79e2001-10-12 19:01:43 +0000190 \versionadded{2.1}
Walter Dörwald6d5f30e2002-12-06 10:09:16 +0000191 \versionchanged[Support for a tuple as the second argument added]{2.2}
Fred Drake3adf79e2001-10-12 19:01:43 +0000192\end{cfuncdesc}
193
194Subclass determination is done in a fairly straightforward way, but
195includes a wrinkle that implementors of extensions to the class system
196may want to be aware of. If \class{A} and \class{B} are class
197objects, \class{B} is a subclass of \class{A} if it inherits from
198\class{A} either directly or indirectly. If either is not a class
199object, a more general mechanism is used to determine the class
200relationship of the two objects. When testing if \var{B} is a
201subclass of \var{A}, if \var{A} is \var{B},
202\cfunction{PyObject_IsSubclass()} returns true. If \var{A} and
203\var{B} are different objects, \var{B}'s \member{__bases__} attribute
204is searched in a depth-first fashion for \var{A} --- the presence of
205the \member{__bases__} attribute is considered sufficient for this
206determination.
207
208\begin{cfuncdesc}{int}{PyObject_IsSubclass}{PyObject *derived,
209 PyObject *cls}
210 Returns \code{1} if the class \var{derived} is identical to or
211 derived from the class \var{cls}, otherwise returns \code{0}. In
Walter Dörwaldd9a6ad32002-12-12 16:41:44 +0000212 case of an error, returns \code{-1}. If \var{cls}
213 is a tuple, the check will be done against every entry in \var{cls}.
214 The result will be \code{1} when at least one of the checks returns
215 \code{1}, otherwise it will be \code{0}. If either \var{derived} or
216 \var{cls} is not an actual class object (or tuple), this function
217 uses the generic algorithm described above.
Fred Drake3adf79e2001-10-12 19:01:43 +0000218 \versionadded{2.1}
Walter Dörwaldd9a6ad32002-12-12 16:41:44 +0000219 \versionchanged[Older versions of Python did not support a tuple
220 as the second argument]{2.3}
Fred Drake3adf79e2001-10-12 19:01:43 +0000221\end{cfuncdesc}
222
223
224\begin{cfuncdesc}{int}{PyCallable_Check}{PyObject *o}
225 Determine if the object \var{o} is callable. Return \code{1} if the
226 object is callable and \code{0} otherwise. This function always
227 succeeds.
228\end{cfuncdesc}
229
230
Fred Drake0e0b6182002-04-15 20:51:19 +0000231\begin{cfuncdesc}{PyObject*}{PyObject_Call}{PyObject *callable_object,
232 PyObject *args,
233 PyObject *kw}
234 Call a callable Python object \var{callable_object}, with arguments
235 given by the tuple \var{args}, and named arguments given by the
236 dictionary \var{kw}. If no named arguments are needed, \var{kw} may
237 be \NULL{}. \var{args} must not be \NULL{}, use an empty tuple if
238 no arguments are needed. Returns the result of the call on success,
239 or \NULL{} on failure. This is the equivalent of the Python
Neal Norwitz28ad48e2006-03-17 08:04:59 +0000240 expression \samp{\var{callable_object}(*\var{args}, **\var{kw})}.
Fred Drakef5368272003-01-25 07:48:13 +0000241 \versionadded{2.2}
Fred Drake0e0b6182002-04-15 20:51:19 +0000242\end{cfuncdesc}
243
244
Fred Drake3adf79e2001-10-12 19:01:43 +0000245\begin{cfuncdesc}{PyObject*}{PyObject_CallObject}{PyObject *callable_object,
246 PyObject *args}
247 Call a callable Python object \var{callable_object}, with arguments
248 given by the tuple \var{args}. If no arguments are needed, then
249 \var{args} may be \NULL. Returns the result of the call on
250 success, or \NULL{} on failure. This is the equivalent of the
Neal Norwitz28ad48e2006-03-17 08:04:59 +0000251 Python expression \samp{\var{callable_object}(*\var{args})}.
Fred Drake3adf79e2001-10-12 19:01:43 +0000252\end{cfuncdesc}
253
Fred Drakec44e9ec2001-10-26 16:38:38 +0000254\begin{cfuncdesc}{PyObject*}{PyObject_CallFunction}{PyObject *callable,
255 char *format, \moreargs}
256 Call a callable Python object \var{callable}, with a variable
Fred Drake3adf79e2001-10-12 19:01:43 +0000257 number of C arguments. The C arguments are described using a
258 \cfunction{Py_BuildValue()} style format string. The format may be
259 \NULL, indicating that no arguments are provided. Returns the
260 result of the call on success, or \NULL{} on failure. This is the
Neal Norwitz28ad48e2006-03-17 08:04:59 +0000261 equivalent of the Python expression \samp{\var{callable}(*\var{args})}.
Thomas Wouters477c8d52006-05-27 19:21:47 +0000262 Note that if you only pass \ctype{PyObject *} args,
263 \cfunction{PyObject_CallFunctionObjArgs} is a faster alternative.
Fred Drake3adf79e2001-10-12 19:01:43 +0000264\end{cfuncdesc}
265
266
267\begin{cfuncdesc}{PyObject*}{PyObject_CallMethod}{PyObject *o,
Fred Drakec44e9ec2001-10-26 16:38:38 +0000268 char *method, char *format,
269 \moreargs}
Raymond Hettinger2619c9e2003-12-07 11:40:17 +0000270 Call the method named \var{method} of object \var{o} with a variable
Fred Drake3adf79e2001-10-12 19:01:43 +0000271 number of C arguments. The C arguments are described by a
Andrew M. Kuchlingfe80b632004-08-07 17:53:05 +0000272 \cfunction{Py_BuildValue()} format string that should
273 produce a tuple. The format may be \NULL,
Fred Drake3adf79e2001-10-12 19:01:43 +0000274 indicating that no arguments are provided. Returns the result of the
275 call on success, or \NULL{} on failure. This is the equivalent of
Fred Drakec44e9ec2001-10-26 16:38:38 +0000276 the Python expression \samp{\var{o}.\var{method}(\var{args})}.
Thomas Wouters477c8d52006-05-27 19:21:47 +0000277 Note that if you only pass \ctype{PyObject *} args,
278 \cfunction{PyObject_CallMethodObjArgs} is a faster alternative.
Fred Drakec44e9ec2001-10-26 16:38:38 +0000279\end{cfuncdesc}
280
281
Fred Drakeb0c079e2001-10-28 02:39:03 +0000282\begin{cfuncdesc}{PyObject*}{PyObject_CallFunctionObjArgs}{PyObject *callable,
283 \moreargs,
284 \code{NULL}}
Fred Drakec44e9ec2001-10-26 16:38:38 +0000285 Call a callable Python object \var{callable}, with a variable
286 number of \ctype{PyObject*} arguments. The arguments are provided
287 as a variable number of parameters followed by \NULL.
288 Returns the result of the call on success, or \NULL{} on failure.
289 \versionadded{2.2}
290\end{cfuncdesc}
291
292
Fred Drakeb0c079e2001-10-28 02:39:03 +0000293\begin{cfuncdesc}{PyObject*}{PyObject_CallMethodObjArgs}{PyObject *o,
294 PyObject *name,
295 \moreargs,
296 \code{NULL}}
Fred Drakec44e9ec2001-10-26 16:38:38 +0000297 Calls a method of the object \var{o}, where the name of the method
298 is given as a Python string object in \var{name}. It is called with
299 a variable number of \ctype{PyObject*} arguments. The arguments are
300 provided as a variable number of parameters followed by \NULL.
301 Returns the result of the call on success, or \NULL{} on failure.
302 \versionadded{2.2}
Fred Drake3adf79e2001-10-12 19:01:43 +0000303\end{cfuncdesc}
304
305
Martin v. Löwis29fafd82006-03-01 05:16:03 +0000306\begin{cfuncdesc}{long}{PyObject_Hash}{PyObject *o}
Fred Drake3adf79e2001-10-12 19:01:43 +0000307 Compute and return the hash value of an object \var{o}. On failure,
308 return \code{-1}. This is the equivalent of the Python expression
309 \samp{hash(\var{o})}.\bifuncindex{hash}
310\end{cfuncdesc}
311
312
313\begin{cfuncdesc}{int}{PyObject_IsTrue}{PyObject *o}
314 Returns \code{1} if the object \var{o} is considered to be true, and
315 \code{0} otherwise. This is equivalent to the Python expression
Raymond Hettinger2ed6dff2003-04-16 17:28:12 +0000316 \samp{not not \var{o}}. On failure, return \code{-1}.
317\end{cfuncdesc}
318
319
320\begin{cfuncdesc}{int}{PyObject_Not}{PyObject *o}
321 Returns \code{0} if the object \var{o} is considered to be true, and
322 \code{1} otherwise. This is equivalent to the Python expression
323 \samp{not \var{o}}. On failure, return \code{-1}.
Fred Drake3adf79e2001-10-12 19:01:43 +0000324\end{cfuncdesc}
325
326
327\begin{cfuncdesc}{PyObject*}{PyObject_Type}{PyObject *o}
328 When \var{o} is non-\NULL, returns a type object corresponding to
329 the object type of object \var{o}. On failure, raises
330 \exception{SystemError} and returns \NULL. This is equivalent to
Fred Drake12dd7b12003-04-09 18:15:57 +0000331 the Python expression \code{type(\var{o})}.\bifuncindex{type}
Guido van Rossum6db77182003-04-09 18:02:23 +0000332 This function increments the reference count of the return value.
333 There's really no reason to use this function instead of the
334 common expression \code{\var{o}->ob_type}, which returns a pointer
Fred Drake12dd7b12003-04-09 18:15:57 +0000335 of type \ctype{PyTypeObject*}, except when the incremented reference
Guido van Rossum6db77182003-04-09 18:02:23 +0000336 count is needed.
Fred Drake3adf79e2001-10-12 19:01:43 +0000337\end{cfuncdesc}
338
339\begin{cfuncdesc}{int}{PyObject_TypeCheck}{PyObject *o, PyTypeObject *type}
340 Return true if the object \var{o} is of type \var{type} or a subtype
341 of \var{type}. Both parameters must be non-\NULL.
342 \versionadded{2.2}
343\end{cfuncdesc}
344
Martin v. Löwis29fafd82006-03-01 05:16:03 +0000345\begin{cfuncdesc}{Py_ssize_t}{PyObject_Length}{PyObject *o}
346\cfuncline{Py_ssize_t}{PyObject_Size}{PyObject *o}
Fred Drake3adf79e2001-10-12 19:01:43 +0000347 Return the length of object \var{o}. If the object \var{o} provides
Raymond Hettinger4cd5a082004-01-04 03:11:45 +0000348 either the sequence and mapping protocols, the sequence length is
Fred Drake3adf79e2001-10-12 19:01:43 +0000349 returned. On error, \code{-1} is returned. This is the equivalent
350 to the Python expression \samp{len(\var{o})}.\bifuncindex{len}
351\end{cfuncdesc}
352
353
354\begin{cfuncdesc}{PyObject*}{PyObject_GetItem}{PyObject *o, PyObject *key}
355 Return element of \var{o} corresponding to the object \var{key} or
356 \NULL{} on failure. This is the equivalent of the Python expression
357 \samp{\var{o}[\var{key}]}.
358\end{cfuncdesc}
359
360
361\begin{cfuncdesc}{int}{PyObject_SetItem}{PyObject *o,
362 PyObject *key, PyObject *v}
363 Map the object \var{key} to the value \var{v}. Returns \code{-1} on
364 failure. This is the equivalent of the Python statement
365 \samp{\var{o}[\var{key}] = \var{v}}.
366\end{cfuncdesc}
367
368
369\begin{cfuncdesc}{int}{PyObject_DelItem}{PyObject *o, PyObject *key}
370 Delete the mapping for \var{key} from \var{o}. Returns \code{-1} on
371 failure. This is the equivalent of the Python statement \samp{del
372 \var{o}[\var{key}]}.
373\end{cfuncdesc}
374
375\begin{cfuncdesc}{int}{PyObject_AsFileDescriptor}{PyObject *o}
376 Derives a file-descriptor from a Python object. If the object is an
377 integer or long integer, its value is returned. If not, the
378 object's \method{fileno()} method is called if it exists; the method
379 must return an integer or long integer, which is returned as the
380 file descriptor value. Returns \code{-1} on failure.
381\end{cfuncdesc}
382
383\begin{cfuncdesc}{PyObject*}{PyObject_Dir}{PyObject *o}
384 This is equivalent to the Python expression \samp{dir(\var{o})},
385 returning a (possibly empty) list of strings appropriate for the
386 object argument, or \NULL{} if there was an error. If the argument
387 is \NULL, this is like the Python \samp{dir()}, returning the names
388 of the current locals; in this case, if no execution frame is active
389 then \NULL{} is returned but \cfunction{PyErr_Occurred()} will
390 return false.
391\end{cfuncdesc}
392
Fred Drake314bae52002-03-11 18:46:29 +0000393\begin{cfuncdesc}{PyObject*}{PyObject_GetIter}{PyObject *o}
394 This is equivalent to the Python expression \samp{iter(\var{o})}.
395 It returns a new iterator for the object argument, or the object
396 itself if the object is already an iterator. Raises
397 \exception{TypeError} and returns \NULL{} if the object cannot be
398 iterated.
399\end{cfuncdesc}
400
Fred Drake3adf79e2001-10-12 19:01:43 +0000401
402\section{Number Protocol \label{number}}
403
404\begin{cfuncdesc}{int}{PyNumber_Check}{PyObject *o}
405 Returns \code{1} if the object \var{o} provides numeric protocols,
406 and false otherwise. This function always succeeds.
407\end{cfuncdesc}
408
409
410\begin{cfuncdesc}{PyObject*}{PyNumber_Add}{PyObject *o1, PyObject *o2}
411 Returns the result of adding \var{o1} and \var{o2}, or \NULL{} on
412 failure. This is the equivalent of the Python expression
413 \samp{\var{o1} + \var{o2}}.
414\end{cfuncdesc}
415
416
417\begin{cfuncdesc}{PyObject*}{PyNumber_Subtract}{PyObject *o1, PyObject *o2}
418 Returns the result of subtracting \var{o2} from \var{o1}, or \NULL{}
419 on failure. This is the equivalent of the Python expression
420 \samp{\var{o1} - \var{o2}}.
421\end{cfuncdesc}
422
423
424\begin{cfuncdesc}{PyObject*}{PyNumber_Multiply}{PyObject *o1, PyObject *o2}
425 Returns the result of multiplying \var{o1} and \var{o2}, or \NULL{}
426 on failure. This is the equivalent of the Python expression
427 \samp{\var{o1} * \var{o2}}.
428\end{cfuncdesc}
429
430
431\begin{cfuncdesc}{PyObject*}{PyNumber_Divide}{PyObject *o1, PyObject *o2}
432 Returns the result of dividing \var{o1} by \var{o2}, or \NULL{} on
433 failure. This is the equivalent of the Python expression
434 \samp{\var{o1} / \var{o2}}.
435\end{cfuncdesc}
436
437
438\begin{cfuncdesc}{PyObject*}{PyNumber_FloorDivide}{PyObject *o1, PyObject *o2}
439 Return the floor of \var{o1} divided by \var{o2}, or \NULL{} on
440 failure. This is equivalent to the ``classic'' division of
441 integers.
442 \versionadded{2.2}
443\end{cfuncdesc}
444
445
446\begin{cfuncdesc}{PyObject*}{PyNumber_TrueDivide}{PyObject *o1, PyObject *o2}
447 Return a reasonable approximation for the mathematical value of
448 \var{o1} divided by \var{o2}, or \NULL{} on failure. The return
449 value is ``approximate'' because binary floating point numbers are
450 approximate; it is not possible to represent all real numbers in
451 base two. This function can return a floating point value when
452 passed two integers.
453 \versionadded{2.2}
454\end{cfuncdesc}
455
456
457\begin{cfuncdesc}{PyObject*}{PyNumber_Remainder}{PyObject *o1, PyObject *o2}
458 Returns the remainder of dividing \var{o1} by \var{o2}, or \NULL{}
459 on failure. This is the equivalent of the Python expression
460 \samp{\var{o1} \%\ \var{o2}}.
461\end{cfuncdesc}
462
463
464\begin{cfuncdesc}{PyObject*}{PyNumber_Divmod}{PyObject *o1, PyObject *o2}
465 See the built-in function \function{divmod()}\bifuncindex{divmod}.
466 Returns \NULL{} on failure. This is the equivalent of the Python
467 expression \samp{divmod(\var{o1}, \var{o2})}.
468\end{cfuncdesc}
469
470
471\begin{cfuncdesc}{PyObject*}{PyNumber_Power}{PyObject *o1,
472 PyObject *o2, PyObject *o3}
473 See the built-in function \function{pow()}\bifuncindex{pow}.
474 Returns \NULL{} on failure. This is the equivalent of the Python
475 expression \samp{pow(\var{o1}, \var{o2}, \var{o3})}, where \var{o3}
476 is optional. If \var{o3} is to be ignored, pass \cdata{Py_None} in
477 its place (passing \NULL{} for \var{o3} would cause an illegal
478 memory access).
479\end{cfuncdesc}
480
481
482\begin{cfuncdesc}{PyObject*}{PyNumber_Negative}{PyObject *o}
483 Returns the negation of \var{o} on success, or \NULL{} on failure.
484 This is the equivalent of the Python expression \samp{-\var{o}}.
485\end{cfuncdesc}
486
487
488\begin{cfuncdesc}{PyObject*}{PyNumber_Positive}{PyObject *o}
489 Returns \var{o} on success, or \NULL{} on failure. This is the
490 equivalent of the Python expression \samp{+\var{o}}.
491\end{cfuncdesc}
492
493
494\begin{cfuncdesc}{PyObject*}{PyNumber_Absolute}{PyObject *o}
495 Returns the absolute value of \var{o}, or \NULL{} on failure. This
496 is the equivalent of the Python expression \samp{abs(\var{o})}.
497 \bifuncindex{abs}
498\end{cfuncdesc}
499
500
501\begin{cfuncdesc}{PyObject*}{PyNumber_Invert}{PyObject *o}
502 Returns the bitwise negation of \var{o} on success, or \NULL{} on
503 failure. This is the equivalent of the Python expression
504 \samp{\~\var{o}}.
505\end{cfuncdesc}
506
507
508\begin{cfuncdesc}{PyObject*}{PyNumber_Lshift}{PyObject *o1, PyObject *o2}
509 Returns the result of left shifting \var{o1} by \var{o2} on success,
510 or \NULL{} on failure. This is the equivalent of the Python
511 expression \samp{\var{o1} <\code{<} \var{o2}}.
512\end{cfuncdesc}
513
514
515\begin{cfuncdesc}{PyObject*}{PyNumber_Rshift}{PyObject *o1, PyObject *o2}
516 Returns the result of right shifting \var{o1} by \var{o2} on
517 success, or \NULL{} on failure. This is the equivalent of the
518 Python expression \samp{\var{o1} >\code{>} \var{o2}}.
519\end{cfuncdesc}
520
521
522\begin{cfuncdesc}{PyObject*}{PyNumber_And}{PyObject *o1, PyObject *o2}
Raymond Hettingere5fced72004-04-17 11:57:40 +0000523 Returns the ``bitwise and'' of \var{o1} and \var{o2} on success and
Fred Drake3adf79e2001-10-12 19:01:43 +0000524 \NULL{} on failure. This is the equivalent of the Python expression
525 \samp{\var{o1} \&\ \var{o2}}.
526\end{cfuncdesc}
527
528
529\begin{cfuncdesc}{PyObject*}{PyNumber_Xor}{PyObject *o1, PyObject *o2}
530 Returns the ``bitwise exclusive or'' of \var{o1} by \var{o2} on
531 success, or \NULL{} on failure. This is the equivalent of the
532 Python expression \samp{\var{o1} \textasciicircum{} \var{o2}}.
533\end{cfuncdesc}
534
535\begin{cfuncdesc}{PyObject*}{PyNumber_Or}{PyObject *o1, PyObject *o2}
536 Returns the ``bitwise or'' of \var{o1} and \var{o2} on success, or
537 \NULL{} on failure. This is the equivalent of the Python expression
538 \samp{\var{o1} | \var{o2}}.
539\end{cfuncdesc}
540
541
542\begin{cfuncdesc}{PyObject*}{PyNumber_InPlaceAdd}{PyObject *o1, PyObject *o2}
543 Returns the result of adding \var{o1} and \var{o2}, or \NULL{} on
544 failure. The operation is done \emph{in-place} when \var{o1}
545 supports it. This is the equivalent of the Python statement
546 \samp{\var{o1} += \var{o2}}.
547\end{cfuncdesc}
548
549
550\begin{cfuncdesc}{PyObject*}{PyNumber_InPlaceSubtract}{PyObject *o1,
551 PyObject *o2}
552 Returns the result of subtracting \var{o2} from \var{o1}, or \NULL{}
553 on failure. The operation is done \emph{in-place} when \var{o1}
554 supports it. This is the equivalent of the Python statement
555 \samp{\var{o1} -= \var{o2}}.
556\end{cfuncdesc}
557
558
559\begin{cfuncdesc}{PyObject*}{PyNumber_InPlaceMultiply}{PyObject *o1,
560 PyObject *o2}
561 Returns the result of multiplying \var{o1} and \var{o2}, or \NULL{}
562 on failure. The operation is done \emph{in-place} when \var{o1}
563 supports it. This is the equivalent of the Python statement
564 \samp{\var{o1} *= \var{o2}}.
565\end{cfuncdesc}
566
567
568\begin{cfuncdesc}{PyObject*}{PyNumber_InPlaceDivide}{PyObject *o1,
569 PyObject *o2}
570 Returns the result of dividing \var{o1} by \var{o2}, or \NULL{} on
571 failure. The operation is done \emph{in-place} when \var{o1}
572 supports it. This is the equivalent of the Python statement
573 \samp{\var{o1} /= \var{o2}}.
574\end{cfuncdesc}
575
576
577\begin{cfuncdesc}{PyObject*}{PyNumber_InPlaceFloorDivide}{PyObject *o1,
578 PyObject *o2}
Andrew M. Kuchling1b50b432004-06-05 19:00:55 +0000579 Returns the mathematical floor of dividing \var{o1} by \var{o2}, or
Fred Drake3adf79e2001-10-12 19:01:43 +0000580 \NULL{} on failure. The operation is done \emph{in-place} when
581 \var{o1} supports it. This is the equivalent of the Python
582 statement \samp{\var{o1} //= \var{o2}}.
583 \versionadded{2.2}
584\end{cfuncdesc}
585
586
587\begin{cfuncdesc}{PyObject*}{PyNumber_InPlaceTrueDivide}{PyObject *o1,
588 PyObject *o2}
589 Return a reasonable approximation for the mathematical value of
590 \var{o1} divided by \var{o2}, or \NULL{} on failure. The return
591 value is ``approximate'' because binary floating point numbers are
592 approximate; it is not possible to represent all real numbers in
593 base two. This function can return a floating point value when
594 passed two integers. The operation is done \emph{in-place} when
595 \var{o1} supports it.
596 \versionadded{2.2}
597\end{cfuncdesc}
598
599
600\begin{cfuncdesc}{PyObject*}{PyNumber_InPlaceRemainder}{PyObject *o1,
601 PyObject *o2}
602 Returns the remainder of dividing \var{o1} by \var{o2}, or \NULL{}
603 on failure. The operation is done \emph{in-place} when \var{o1}
604 supports it. This is the equivalent of the Python statement
605 \samp{\var{o1} \%= \var{o2}}.
606\end{cfuncdesc}
607
608
609\begin{cfuncdesc}{PyObject*}{PyNumber_InPlacePower}{PyObject *o1,
610 PyObject *o2, PyObject *o3}
611 See the built-in function \function{pow()}.\bifuncindex{pow}
612 Returns \NULL{} on failure. The operation is done \emph{in-place}
613 when \var{o1} supports it. This is the equivalent of the Python
614 statement \samp{\var{o1} **= \var{o2}} when o3 is \cdata{Py_None},
615 or an in-place variant of \samp{pow(\var{o1}, \var{o2}, \var{o3})}
616 otherwise. If \var{o3} is to be ignored, pass \cdata{Py_None} in its
617 place (passing \NULL{} for \var{o3} would cause an illegal memory
618 access).
619\end{cfuncdesc}
620
621\begin{cfuncdesc}{PyObject*}{PyNumber_InPlaceLshift}{PyObject *o1,
622 PyObject *o2}
623 Returns the result of left shifting \var{o1} by \var{o2} on success,
624 or \NULL{} on failure. The operation is done \emph{in-place} when
625 \var{o1} supports it. This is the equivalent of the Python
626 statement \samp{\var{o1} <\code{<=} \var{o2}}.
627\end{cfuncdesc}
628
629
630\begin{cfuncdesc}{PyObject*}{PyNumber_InPlaceRshift}{PyObject *o1,
631 PyObject *o2}
632 Returns the result of right shifting \var{o1} by \var{o2} on
633 success, or \NULL{} on failure. The operation is done
634 \emph{in-place} when \var{o1} supports it. This is the equivalent
Thomas Wouters477c8d52006-05-27 19:21:47 +0000635 of the Python statement \samp{\var{o1} >>= \var{o2}}.
Fred Drake3adf79e2001-10-12 19:01:43 +0000636\end{cfuncdesc}
637
638
639\begin{cfuncdesc}{PyObject*}{PyNumber_InPlaceAnd}{PyObject *o1, PyObject *o2}
640 Returns the ``bitwise and'' of \var{o1} and \var{o2} on success and
641 \NULL{} on failure. The operation is done \emph{in-place} when
642 \var{o1} supports it. This is the equivalent of the Python
643 statement \samp{\var{o1} \&= \var{o2}}.
644\end{cfuncdesc}
645
646
647\begin{cfuncdesc}{PyObject*}{PyNumber_InPlaceXor}{PyObject *o1, PyObject *o2}
648 Returns the ``bitwise exclusive or'' of \var{o1} by \var{o2} on
649 success, or \NULL{} on failure. The operation is done
650 \emph{in-place} when \var{o1} supports it. This is the equivalent
651 of the Python statement \samp{\var{o1} \textasciicircum= \var{o2}}.
652\end{cfuncdesc}
653
654\begin{cfuncdesc}{PyObject*}{PyNumber_InPlaceOr}{PyObject *o1, PyObject *o2}
655 Returns the ``bitwise or'' of \var{o1} and \var{o2} on success, or
656 \NULL{} on failure. The operation is done \emph{in-place} when
657 \var{o1} supports it. This is the equivalent of the Python
658 statement \samp{\var{o1} |= \var{o2}}.
659\end{cfuncdesc}
660
Fred Drake3adf79e2001-10-12 19:01:43 +0000661\begin{cfuncdesc}{PyObject*}{PyNumber_Int}{PyObject *o}
662 Returns the \var{o} converted to an integer object on success, or
Walter Dörwaldf1715402002-11-19 20:49:15 +0000663 \NULL{} on failure. If the argument is outside the integer range
664 a long object will be returned instead. This is the equivalent
665 of the Python expression \samp{int(\var{o})}.\bifuncindex{int}
Fred Drake3adf79e2001-10-12 19:01:43 +0000666\end{cfuncdesc}
667
668\begin{cfuncdesc}{PyObject*}{PyNumber_Long}{PyObject *o}
669 Returns the \var{o} converted to a long integer object on success,
670 or \NULL{} on failure. This is the equivalent of the Python
671 expression \samp{long(\var{o})}.\bifuncindex{long}
672\end{cfuncdesc}
673
674\begin{cfuncdesc}{PyObject*}{PyNumber_Float}{PyObject *o}
675 Returns the \var{o} converted to a float object on success, or
676 \NULL{} on failure. This is the equivalent of the Python expression
677 \samp{float(\var{o})}.\bifuncindex{float}
678\end{cfuncdesc}
679
Thomas Wouters00ee7ba2006-08-21 19:07:27 +0000680\begin{cfuncdesc}{PyObject*}{PyNumber_Index}{PyObject *o}
681 Returns the \var{o} converted to a Python int or long on success or \NULL{}
682 with a TypeError exception raised on failure.
Neal Norwitz025f14b2006-03-08 05:29:18 +0000683 \versionadded{2.5}
Guido van Rossum38fff8c2006-03-07 18:50:55 +0000684\end{cfuncdesc}
Fred Drake3adf79e2001-10-12 19:01:43 +0000685
Thomas Wouters00ee7ba2006-08-21 19:07:27 +0000686\begin{cfuncdesc}{Py_ssize_t}{PyNumber_AsSsize_t}{PyObject *o, PyObject *exc}
687 Returns \var{o} converted to a Py_ssize_t value if \var{o}
688 can be interpreted as an integer. If \var{o} can be converted to a Python
689 int or long but the attempt to convert to a Py_ssize_t value
690 would raise an \exception{OverflowError}, then the \var{exc} argument
691 is the type of exception that will be raised (usually \exception{IndexError}
692 or \exception{OverflowError}). If \var{exc} is \NULL{}, then the exception
693 is cleared and the value is clipped to \var{PY_SSIZE_T_MIN}
694 for a negative integer or \var{PY_SSIZE_T_MAX} for a positive integer.
695 \versionadded{2.5}
696\end{cfuncdesc}
697
698\begin{cfuncdesc}{int}{PyIndex_Check}{PyObject *o}
699 Returns True if \var{o} is an index integer (has the nb_index slot of
700 the tp_as_number structure filled in).
701 \versionadded{2.5}
702\end{cfuncdesc}
703
704
Fred Drake3adf79e2001-10-12 19:01:43 +0000705\section{Sequence Protocol \label{sequence}}
706
707\begin{cfuncdesc}{int}{PySequence_Check}{PyObject *o}
708 Return \code{1} if the object provides sequence protocol, and
709 \code{0} otherwise. This function always succeeds.
710\end{cfuncdesc}
711
Martin v. Löwis29fafd82006-03-01 05:16:03 +0000712\begin{cfuncdesc}{Py_ssize_t}{PySequence_Size}{PyObject *o}
Fred Drake3adf79e2001-10-12 19:01:43 +0000713 Returns the number of objects in sequence \var{o} on success, and
714 \code{-1} on failure. For objects that do not provide sequence
715 protocol, this is equivalent to the Python expression
716 \samp{len(\var{o})}.\bifuncindex{len}
717\end{cfuncdesc}
718
Martin v. Löwis29fafd82006-03-01 05:16:03 +0000719\begin{cfuncdesc}{Py_ssize_t}{PySequence_Length}{PyObject *o}
Fred Drake3adf79e2001-10-12 19:01:43 +0000720 Alternate name for \cfunction{PySequence_Size()}.
721\end{cfuncdesc}
722
723\begin{cfuncdesc}{PyObject*}{PySequence_Concat}{PyObject *o1, PyObject *o2}
724 Return the concatenation of \var{o1} and \var{o2} on success, and
725 \NULL{} on failure. This is the equivalent of the Python
726 expression \samp{\var{o1} + \var{o2}}.
727\end{cfuncdesc}
728
729
Martin v. Löwis29fafd82006-03-01 05:16:03 +0000730\begin{cfuncdesc}{PyObject*}{PySequence_Repeat}{PyObject *o, Py_ssize_t count}
Fred Drake3adf79e2001-10-12 19:01:43 +0000731 Return the result of repeating sequence object \var{o} \var{count}
732 times, or \NULL{} on failure. This is the equivalent of the Python
733 expression \samp{\var{o} * \var{count}}.
734\end{cfuncdesc}
735
736\begin{cfuncdesc}{PyObject*}{PySequence_InPlaceConcat}{PyObject *o1,
737 PyObject *o2}
738 Return the concatenation of \var{o1} and \var{o2} on success, and
739 \NULL{} on failure. The operation is done \emph{in-place} when
740 \var{o1} supports it. This is the equivalent of the Python
741 expression \samp{\var{o1} += \var{o2}}.
742\end{cfuncdesc}
743
744
Martin v. Löwis29fafd82006-03-01 05:16:03 +0000745\begin{cfuncdesc}{PyObject*}{PySequence_InPlaceRepeat}{PyObject *o, Py_ssize_t count}
Fred Drake3adf79e2001-10-12 19:01:43 +0000746 Return the result of repeating sequence object \var{o} \var{count}
747 times, or \NULL{} on failure. The operation is done \emph{in-place}
748 when \var{o} supports it. This is the equivalent of the Python
749 expression \samp{\var{o} *= \var{count}}.
750\end{cfuncdesc}
751
752
Martin v. Löwis29fafd82006-03-01 05:16:03 +0000753\begin{cfuncdesc}{PyObject*}{PySequence_GetItem}{PyObject *o, Py_ssize_t i}
Fred Drake3adf79e2001-10-12 19:01:43 +0000754 Return the \var{i}th element of \var{o}, or \NULL{} on failure.
755 This is the equivalent of the Python expression
756 \samp{\var{o}[\var{i}]}.
757\end{cfuncdesc}
758
759
Martin v. Löwis29fafd82006-03-01 05:16:03 +0000760\begin{cfuncdesc}{PyObject*}{PySequence_GetSlice}{PyObject *o, Py_ssize_t i1, Py_ssize_t i2}
Fred Drake3adf79e2001-10-12 19:01:43 +0000761 Return the slice of sequence object \var{o} between \var{i1} and
762 \var{i2}, or \NULL{} on failure. This is the equivalent of the
763 Python expression \samp{\var{o}[\var{i1}:\var{i2}]}.
764\end{cfuncdesc}
765
766
Martin v. Löwis29fafd82006-03-01 05:16:03 +0000767\begin{cfuncdesc}{int}{PySequence_SetItem}{PyObject *o, Py_ssize_t i, PyObject *v}
Fred Drake3adf79e2001-10-12 19:01:43 +0000768 Assign object \var{v} to the \var{i}th element of \var{o}. Returns
769 \code{-1} on failure. This is the equivalent of the Python
Raymond Hettinger12c484d2003-08-09 04:37:14 +0000770 statement \samp{\var{o}[\var{i}] = \var{v}}. This function \emph{does not}
771 steal a reference to \var{v}.
Fred Drake3adf79e2001-10-12 19:01:43 +0000772\end{cfuncdesc}
773
Martin v. Löwis29fafd82006-03-01 05:16:03 +0000774\begin{cfuncdesc}{int}{PySequence_DelItem}{PyObject *o, Py_ssize_t i}
Fred Drake3adf79e2001-10-12 19:01:43 +0000775 Delete the \var{i}th element of object \var{o}. Returns \code{-1}
776 on failure. This is the equivalent of the Python statement
777 \samp{del \var{o}[\var{i}]}.
778\end{cfuncdesc}
779
Martin v. Löwis29fafd82006-03-01 05:16:03 +0000780\begin{cfuncdesc}{int}{PySequence_SetSlice}{PyObject *o, Py_ssize_t i1,
781 Py_ssize_t i2, PyObject *v}
Fred Drake3adf79e2001-10-12 19:01:43 +0000782 Assign the sequence object \var{v} to the slice in sequence object
783 \var{o} from \var{i1} to \var{i2}. This is the equivalent of the
784 Python statement \samp{\var{o}[\var{i1}:\var{i2}] = \var{v}}.
785\end{cfuncdesc}
786
Martin v. Löwis29fafd82006-03-01 05:16:03 +0000787\begin{cfuncdesc}{int}{PySequence_DelSlice}{PyObject *o, Py_ssize_t i1, Py_ssize_t i2}
Fred Drake3adf79e2001-10-12 19:01:43 +0000788 Delete the slice in sequence object \var{o} from \var{i1} to
789 \var{i2}. Returns \code{-1} on failure. This is the equivalent of
790 the Python statement \samp{del \var{o}[\var{i1}:\var{i2}]}.
791\end{cfuncdesc}
792
Guido van Rossumd8faa362007-04-27 19:54:29 +0000793\begin{cfuncdesc}{Py_ssize_t}{PySequence_Count}{PyObject *o, PyObject *value}
Fred Drake3adf79e2001-10-12 19:01:43 +0000794 Return the number of occurrences of \var{value} in \var{o}, that is,
795 return the number of keys for which \code{\var{o}[\var{key}] ==
796 \var{value}}. On failure, return \code{-1}. This is equivalent to
797 the Python expression \samp{\var{o}.count(\var{value})}.
798\end{cfuncdesc}
799
800\begin{cfuncdesc}{int}{PySequence_Contains}{PyObject *o, PyObject *value}
801 Determine if \var{o} contains \var{value}. If an item in \var{o} is
802 equal to \var{value}, return \code{1}, otherwise return \code{0}.
803 On error, return \code{-1}. This is equivalent to the Python
804 expression \samp{\var{value} in \var{o}}.
805\end{cfuncdesc}
806
Guido van Rossumd8faa362007-04-27 19:54:29 +0000807\begin{cfuncdesc}{Py_ssize_t}{PySequence_Index}{PyObject *o, PyObject *value}
Fred Drake3adf79e2001-10-12 19:01:43 +0000808 Return the first index \var{i} for which \code{\var{o}[\var{i}] ==
809 \var{value}}. On error, return \code{-1}. This is equivalent to
810 the Python expression \samp{\var{o}.index(\var{value})}.
811\end{cfuncdesc}
812
813\begin{cfuncdesc}{PyObject*}{PySequence_List}{PyObject *o}
814 Return a list object with the same contents as the arbitrary
815 sequence \var{o}. The returned list is guaranteed to be new.
816\end{cfuncdesc}
817
818\begin{cfuncdesc}{PyObject*}{PySequence_Tuple}{PyObject *o}
819 Return a tuple object with the same contents as the arbitrary
Neal Norwitz98fcaaf2005-10-12 03:58:14 +0000820 sequence \var{o} or \NULL{} on failure. If \var{o} is a tuple,
821 a new reference will be returned, otherwise a tuple will be
822 constructed with the appropriate contents. This is equivalent
823 to the Python expression \samp{tuple(\var{o})}.
824 \bifuncindex{tuple}
Fred Drake3adf79e2001-10-12 19:01:43 +0000825\end{cfuncdesc}
826
827\begin{cfuncdesc}{PyObject*}{PySequence_Fast}{PyObject *o, const char *m}
828 Returns the sequence \var{o} as a tuple, unless it is already a
829 tuple or list, in which case \var{o} is returned. Use
830 \cfunction{PySequence_Fast_GET_ITEM()} to access the members of the
831 result. Returns \NULL{} on failure. If the object is not a
832 sequence, raises \exception{TypeError} with \var{m} as the message
833 text.
834\end{cfuncdesc}
835
Martin v. Löwis29fafd82006-03-01 05:16:03 +0000836\begin{cfuncdesc}{PyObject*}{PySequence_Fast_GET_ITEM}{PyObject *o, Py_ssize_t i}
Fred Drake3adf79e2001-10-12 19:01:43 +0000837 Return the \var{i}th element of \var{o}, assuming that \var{o} was
Fred Drakec37b65e2001-11-28 07:26:15 +0000838 returned by \cfunction{PySequence_Fast()}, \var{o} is not \NULL,
Tim Peters1fc240e2001-10-26 05:06:50 +0000839 and that \var{i} is within bounds.
840\end{cfuncdesc}
841
Raymond Hettingerc1e4f9d2004-03-12 08:04:00 +0000842\begin{cfuncdesc}{PyObject**}{PySequence_Fast_ITEMS}{PyObject *o}
843 Return the underlying array of PyObject pointers. Assumes that
844 \var{o} was returned by \cfunction{PySequence_Fast()} and
845 \var{o} is not \NULL.
846 \versionadded{2.4}
847\end{cfuncdesc}
848
Martin v. Löwis29fafd82006-03-01 05:16:03 +0000849\begin{cfuncdesc}{PyObject*}{PySequence_ITEM}{PyObject *o, Py_ssize_t i}
Brett Cannon77e02122003-09-07 02:22:16 +0000850 Return the \var{i}th element of \var{o} or \NULL{} on failure.
Martin v. Löwis01f94bd2002-05-08 08:44:21 +0000851 Macro form of \cfunction{PySequence_GetItem()} but without checking
852 that \cfunction{PySequence_Check(\var{o})} is true and without
Fred Drake86228e42002-05-23 16:02:28 +0000853 adjustment for negative indices.
854 \versionadded{2.3}
Martin v. Löwis01f94bd2002-05-08 08:44:21 +0000855\end{cfuncdesc}
856
Guido van Rossumd8faa362007-04-27 19:54:29 +0000857\begin{cfuncdesc}{Py_ssize_t}{PySequence_Fast_GET_SIZE}{PyObject *o}
Tim Peters1fc240e2001-10-26 05:06:50 +0000858 Returns the length of \var{o}, assuming that \var{o} was
859 returned by \cfunction{PySequence_Fast()} and that \var{o} is
Fred Drakec37b65e2001-11-28 07:26:15 +0000860 not \NULL. The size can also be gotten by calling
Tim Peters1fc240e2001-10-26 05:06:50 +0000861 \cfunction{PySequence_Size()} on \var{o}, but
862 \cfunction{PySequence_Fast_GET_SIZE()} is faster because it can
863 assume \var{o} is a list or tuple.
Fred Drake3adf79e2001-10-12 19:01:43 +0000864\end{cfuncdesc}
865
866
867\section{Mapping Protocol \label{mapping}}
868
869\begin{cfuncdesc}{int}{PyMapping_Check}{PyObject *o}
870 Return \code{1} if the object provides mapping protocol, and
871 \code{0} otherwise. This function always succeeds.
872\end{cfuncdesc}
873
874
Martin v. Löwis29fafd82006-03-01 05:16:03 +0000875\begin{cfuncdesc}{Py_ssize_t}{PyMapping_Length}{PyObject *o}
Fred Drake3adf79e2001-10-12 19:01:43 +0000876 Returns the number of keys in object \var{o} on success, and
877 \code{-1} on failure. For objects that do not provide mapping
878 protocol, this is equivalent to the Python expression
879 \samp{len(\var{o})}.\bifuncindex{len}
880\end{cfuncdesc}
881
882
883\begin{cfuncdesc}{int}{PyMapping_DelItemString}{PyObject *o, char *key}
884 Remove the mapping for object \var{key} from the object \var{o}.
885 Return \code{-1} on failure. This is equivalent to the Python
886 statement \samp{del \var{o}[\var{key}]}.
887\end{cfuncdesc}
888
889
890\begin{cfuncdesc}{int}{PyMapping_DelItem}{PyObject *o, PyObject *key}
891 Remove the mapping for object \var{key} from the object \var{o}.
892 Return \code{-1} on failure. This is equivalent to the Python
893 statement \samp{del \var{o}[\var{key}]}.
894\end{cfuncdesc}
895
896
897\begin{cfuncdesc}{int}{PyMapping_HasKeyString}{PyObject *o, char *key}
898 On success, return \code{1} if the mapping object has the key
899 \var{key} and \code{0} otherwise. This is equivalent to the Python
900 expression \samp{\var{o}.has_key(\var{key})}. This function always
901 succeeds.
902\end{cfuncdesc}
903
904
905\begin{cfuncdesc}{int}{PyMapping_HasKey}{PyObject *o, PyObject *key}
906 Return \code{1} if the mapping object has the key \var{key} and
907 \code{0} otherwise. This is equivalent to the Python expression
908 \samp{\var{o}.has_key(\var{key})}. This function always succeeds.
909\end{cfuncdesc}
910
911
912\begin{cfuncdesc}{PyObject*}{PyMapping_Keys}{PyObject *o}
913 On success, return a list of the keys in object \var{o}. On
914 failure, return \NULL. This is equivalent to the Python expression
915 \samp{\var{o}.keys()}.
916\end{cfuncdesc}
917
918
919\begin{cfuncdesc}{PyObject*}{PyMapping_Values}{PyObject *o}
920 On success, return a list of the values in object \var{o}. On
921 failure, return \NULL. This is equivalent to the Python expression
922 \samp{\var{o}.values()}.
923\end{cfuncdesc}
924
925
926\begin{cfuncdesc}{PyObject*}{PyMapping_Items}{PyObject *o}
927 On success, return a list of the items in object \var{o}, where each
928 item is a tuple containing a key-value pair. On failure, return
929 \NULL. This is equivalent to the Python expression
930 \samp{\var{o}.items()}.
931\end{cfuncdesc}
932
933
934\begin{cfuncdesc}{PyObject*}{PyMapping_GetItemString}{PyObject *o, char *key}
935 Return element of \var{o} corresponding to the object \var{key} or
936 \NULL{} on failure. This is the equivalent of the Python expression
937 \samp{\var{o}[\var{key}]}.
938\end{cfuncdesc}
939
940\begin{cfuncdesc}{int}{PyMapping_SetItemString}{PyObject *o, char *key,
941 PyObject *v}
942 Map the object \var{key} to the value \var{v} in object \var{o}.
943 Returns \code{-1} on failure. This is the equivalent of the Python
944 statement \samp{\var{o}[\var{key}] = \var{v}}.
945\end{cfuncdesc}
946
947
948\section{Iterator Protocol \label{iterator}}
949
950\versionadded{2.2}
951
952There are only a couple of functions specifically for working with
953iterators.
954
955\begin{cfuncdesc}{int}{PyIter_Check}{PyObject *o}
956 Return true if the object \var{o} supports the iterator protocol.
957\end{cfuncdesc}
958
959\begin{cfuncdesc}{PyObject*}{PyIter_Next}{PyObject *o}
960 Return the next value from the iteration \var{o}. If the object is
961 an iterator, this retrieves the next value from the iteration, and
962 returns \NULL{} with no exception set if there are no remaining
963 items. If the object is not an iterator, \exception{TypeError} is
964 raised, or if there is an error in retrieving the item, returns
965 \NULL{} and passes along the exception.
966\end{cfuncdesc}
967
968To write a loop which iterates over an iterator, the C code should
969look something like this:
970
971\begin{verbatim}
Fred Drake314bae52002-03-11 18:46:29 +0000972PyObject *iterator = PyObject_GetIter(obj);
Fred Drake3adf79e2001-10-12 19:01:43 +0000973PyObject *item;
974
Fred Drake314bae52002-03-11 18:46:29 +0000975if (iterator == NULL) {
976 /* propagate error */
977}
978
979while (item = PyIter_Next(iterator)) {
Fred Drake3adf79e2001-10-12 19:01:43 +0000980 /* do something with item */
Fred Drake8b8fe282001-12-26 16:53:48 +0000981 ...
982 /* release reference when done */
983 Py_DECREF(item);
Fred Drake3adf79e2001-10-12 19:01:43 +0000984}
Fred Drake314bae52002-03-11 18:46:29 +0000985
986Py_DECREF(iterator);
987
Fred Drake3adf79e2001-10-12 19:01:43 +0000988if (PyErr_Occurred()) {
Fred Drake314bae52002-03-11 18:46:29 +0000989 /* propagate error */
Fred Drake3adf79e2001-10-12 19:01:43 +0000990}
991else {
992 /* continue doing useful work */
993}
Fred Drake8b8fe282001-12-26 16:53:48 +0000994\end{verbatim}
995
Jeremy Hylton89c3a222001-11-09 21:59:42 +0000996
Fred Drake94ead572001-11-09 23:34:26 +0000997\section{Buffer Protocol \label{abstract-buffer}}
Jeremy Hylton89c3a222001-11-09 21:59:42 +0000998
999\begin{cfuncdesc}{int}{PyObject_AsCharBuffer}{PyObject *obj,
Fred Drake94ead572001-11-09 23:34:26 +00001000 const char **buffer,
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001001 Py_ssize_t *buffer_len}
Jeremy Hylton89c3a222001-11-09 21:59:42 +00001002 Returns a pointer to a read-only memory location useable as character-
1003 based input. The \var{obj} argument must support the single-segment
Fred Drake243ea712002-04-04 04:10:36 +00001004 character buffer interface. On success, returns \code{0}, sets
Thomas Hellerbfeeeee2001-11-12 07:46:31 +00001005 \var{buffer} to the memory location and \var{buffer_len} to the buffer
Fred Drake243ea712002-04-04 04:10:36 +00001006 length. Returns \code{-1} and sets a \exception{TypeError} on error.
Fred Drake94ead572001-11-09 23:34:26 +00001007 \versionadded{1.6}
Jeremy Hylton89c3a222001-11-09 21:59:42 +00001008\end{cfuncdesc}
1009
1010\begin{cfuncdesc}{int}{PyObject_AsReadBuffer}{PyObject *obj,
Andrew M. Kuchlingee5e4cd2004-07-07 13:07:47 +00001011 const void **buffer,
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001012 Py_ssize_t *buffer_len}
Jeremy Hylton89c3a222001-11-09 21:59:42 +00001013 Returns a pointer to a read-only memory location containing
1014 arbitrary data. The \var{obj} argument must support the
1015 single-segment readable buffer interface. On success, returns
Fred Drake243ea712002-04-04 04:10:36 +00001016 \code{0}, sets \var{buffer} to the memory location and \var{buffer_len}
1017 to the buffer length. Returns \code{-1} and sets a
Jeremy Hylton89c3a222001-11-09 21:59:42 +00001018 \exception{TypeError} on error.
Fred Drake94ead572001-11-09 23:34:26 +00001019 \versionadded{1.6}
Jeremy Hylton89c3a222001-11-09 21:59:42 +00001020\end{cfuncdesc}
1021
1022\begin{cfuncdesc}{int}{PyObject_CheckReadBuffer}{PyObject *o}
1023 Returns \code{1} if \var{o} supports the single-segment readable
1024 buffer interface. Otherwise returns \code{0}.
Fred Drake94ead572001-11-09 23:34:26 +00001025 \versionadded{2.2}
Fred Drake8b8fe282001-12-26 16:53:48 +00001026\end{cfuncdesc}
Jeremy Hylton89c3a222001-11-09 21:59:42 +00001027
1028\begin{cfuncdesc}{int}{PyObject_AsWriteBuffer}{PyObject *obj,
Andrew M. Kuchlingee5e4cd2004-07-07 13:07:47 +00001029 void **buffer,
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001030 Py_ssize_t *buffer_len}
Jeremy Hylton89c3a222001-11-09 21:59:42 +00001031 Returns a pointer to a writeable memory location. The \var{obj}
1032 argument must support the single-segment, character buffer
Fred Drake243ea712002-04-04 04:10:36 +00001033 interface. On success, returns \code{0}, sets \var{buffer} to the
Thomas Hellerbfeeeee2001-11-12 07:46:31 +00001034 memory location and \var{buffer_len} to the buffer length. Returns
Fred Drake243ea712002-04-04 04:10:36 +00001035 \code{-1} and sets a \exception{TypeError} on error.
Fred Drake94ead572001-11-09 23:34:26 +00001036 \versionadded{1.6}
Jeremy Hylton89c3a222001-11-09 21:59:42 +00001037\end{cfuncdesc}