blob: e1c3901d65094b5b2c37e4eb70d41b5c9d61df03 [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
236 expression \samp{apply(\var{callable_object}, \var{args}, \var{kw})}
237 or \samp{\var{callable_object}(*\var{args}, **\var{kw})}.
Fred Drakef5368272003-01-25 07:48:13 +0000238 \versionadded{2.2}
Fred Drake0e0b6182002-04-15 20:51:19 +0000239\end{cfuncdesc}
240
241
Fred Drake3adf79e2001-10-12 19:01:43 +0000242\begin{cfuncdesc}{PyObject*}{PyObject_CallObject}{PyObject *callable_object,
243 PyObject *args}
244 Call a callable Python object \var{callable_object}, with arguments
245 given by the tuple \var{args}. If no arguments are needed, then
246 \var{args} may be \NULL. Returns the result of the call on
247 success, or \NULL{} on failure. This is the equivalent of the
248 Python expression \samp{apply(\var{callable_object}, \var{args})} or
249 \samp{\var{callable_object}(*\var{args})}.
Fred Drake3adf79e2001-10-12 19:01:43 +0000250\end{cfuncdesc}
251
Fred Drakec44e9ec2001-10-26 16:38:38 +0000252\begin{cfuncdesc}{PyObject*}{PyObject_CallFunction}{PyObject *callable,
253 char *format, \moreargs}
254 Call a callable Python object \var{callable}, with a variable
Fred Drake3adf79e2001-10-12 19:01:43 +0000255 number of C arguments. The C arguments are described using a
256 \cfunction{Py_BuildValue()} style format string. The format may be
257 \NULL, indicating that no arguments are provided. Returns the
258 result of the call on success, or \NULL{} on failure. This is the
Fred Drakec44e9ec2001-10-26 16:38:38 +0000259 equivalent of the Python expression \samp{apply(\var{callable},
260 \var{args})} or \samp{\var{callable}(*\var{args})}.
Fred Drake3adf79e2001-10-12 19:01:43 +0000261\end{cfuncdesc}
262
263
264\begin{cfuncdesc}{PyObject*}{PyObject_CallMethod}{PyObject *o,
Fred Drakec44e9ec2001-10-26 16:38:38 +0000265 char *method, char *format,
266 \moreargs}
Raymond Hettinger2619c9e2003-12-07 11:40:17 +0000267 Call the method named \var{method} of object \var{o} with a variable
Fred Drake3adf79e2001-10-12 19:01:43 +0000268 number of C arguments. The C arguments are described by a
Andrew M. Kuchlingfe80b632004-08-07 17:53:05 +0000269 \cfunction{Py_BuildValue()} format string that should
270 produce a tuple. The format may be \NULL,
Fred Drake3adf79e2001-10-12 19:01:43 +0000271 indicating that no arguments are provided. Returns the result of the
272 call on success, or \NULL{} on failure. This is the equivalent of
Fred Drakec44e9ec2001-10-26 16:38:38 +0000273 the Python expression \samp{\var{o}.\var{method}(\var{args})}.
274\end{cfuncdesc}
275
276
Fred Drakeb0c079e2001-10-28 02:39:03 +0000277\begin{cfuncdesc}{PyObject*}{PyObject_CallFunctionObjArgs}{PyObject *callable,
278 \moreargs,
279 \code{NULL}}
Fred Drakec44e9ec2001-10-26 16:38:38 +0000280 Call a callable Python object \var{callable}, with a variable
281 number of \ctype{PyObject*} arguments. The arguments are provided
282 as a variable number of parameters followed by \NULL.
283 Returns the result of the call on success, or \NULL{} on failure.
284 \versionadded{2.2}
285\end{cfuncdesc}
286
287
Fred Drakeb0c079e2001-10-28 02:39:03 +0000288\begin{cfuncdesc}{PyObject*}{PyObject_CallMethodObjArgs}{PyObject *o,
289 PyObject *name,
290 \moreargs,
291 \code{NULL}}
Fred Drakec44e9ec2001-10-26 16:38:38 +0000292 Calls a method of the object \var{o}, where the name of the method
293 is given as a Python string object in \var{name}. It is called with
294 a variable number of \ctype{PyObject*} arguments. The arguments are
295 provided as a variable number of parameters followed by \NULL.
296 Returns the result of the call on success, or \NULL{} on failure.
297 \versionadded{2.2}
Fred Drake3adf79e2001-10-12 19:01:43 +0000298\end{cfuncdesc}
299
300
Martin v. Löwis29fafd82006-03-01 05:16:03 +0000301\begin{cfuncdesc}{long}{PyObject_Hash}{PyObject *o}
Fred Drake3adf79e2001-10-12 19:01:43 +0000302 Compute and return the hash value of an object \var{o}. On failure,
303 return \code{-1}. This is the equivalent of the Python expression
304 \samp{hash(\var{o})}.\bifuncindex{hash}
305\end{cfuncdesc}
306
307
308\begin{cfuncdesc}{int}{PyObject_IsTrue}{PyObject *o}
309 Returns \code{1} if the object \var{o} is considered to be true, and
310 \code{0} otherwise. This is equivalent to the Python expression
Raymond Hettinger2ed6dff2003-04-16 17:28:12 +0000311 \samp{not not \var{o}}. On failure, return \code{-1}.
312\end{cfuncdesc}
313
314
315\begin{cfuncdesc}{int}{PyObject_Not}{PyObject *o}
316 Returns \code{0} if the object \var{o} is considered to be true, and
317 \code{1} otherwise. This is equivalent to the Python expression
318 \samp{not \var{o}}. On failure, return \code{-1}.
Fred Drake3adf79e2001-10-12 19:01:43 +0000319\end{cfuncdesc}
320
321
322\begin{cfuncdesc}{PyObject*}{PyObject_Type}{PyObject *o}
323 When \var{o} is non-\NULL, returns a type object corresponding to
324 the object type of object \var{o}. On failure, raises
325 \exception{SystemError} and returns \NULL. This is equivalent to
Fred Drake12dd7b12003-04-09 18:15:57 +0000326 the Python expression \code{type(\var{o})}.\bifuncindex{type}
Guido van Rossum6db77182003-04-09 18:02:23 +0000327 This function increments the reference count of the return value.
328 There's really no reason to use this function instead of the
329 common expression \code{\var{o}->ob_type}, which returns a pointer
Fred Drake12dd7b12003-04-09 18:15:57 +0000330 of type \ctype{PyTypeObject*}, except when the incremented reference
Guido van Rossum6db77182003-04-09 18:02:23 +0000331 count is needed.
Fred Drake3adf79e2001-10-12 19:01:43 +0000332\end{cfuncdesc}
333
334\begin{cfuncdesc}{int}{PyObject_TypeCheck}{PyObject *o, PyTypeObject *type}
335 Return true if the object \var{o} is of type \var{type} or a subtype
336 of \var{type}. Both parameters must be non-\NULL.
337 \versionadded{2.2}
338\end{cfuncdesc}
339
Martin v. Löwis29fafd82006-03-01 05:16:03 +0000340\begin{cfuncdesc}{Py_ssize_t}{PyObject_Length}{PyObject *o}
341\cfuncline{Py_ssize_t}{PyObject_Size}{PyObject *o}
Fred Drake3adf79e2001-10-12 19:01:43 +0000342 Return the length of object \var{o}. If the object \var{o} provides
Raymond Hettinger4cd5a082004-01-04 03:11:45 +0000343 either the sequence and mapping protocols, the sequence length is
Fred Drake3adf79e2001-10-12 19:01:43 +0000344 returned. On error, \code{-1} is returned. This is the equivalent
345 to the Python expression \samp{len(\var{o})}.\bifuncindex{len}
346\end{cfuncdesc}
347
348
349\begin{cfuncdesc}{PyObject*}{PyObject_GetItem}{PyObject *o, PyObject *key}
350 Return element of \var{o} corresponding to the object \var{key} or
351 \NULL{} on failure. This is the equivalent of the Python expression
352 \samp{\var{o}[\var{key}]}.
353\end{cfuncdesc}
354
355
356\begin{cfuncdesc}{int}{PyObject_SetItem}{PyObject *o,
357 PyObject *key, PyObject *v}
358 Map the object \var{key} to the value \var{v}. Returns \code{-1} on
359 failure. This is the equivalent of the Python statement
360 \samp{\var{o}[\var{key}] = \var{v}}.
361\end{cfuncdesc}
362
363
364\begin{cfuncdesc}{int}{PyObject_DelItem}{PyObject *o, PyObject *key}
365 Delete the mapping for \var{key} from \var{o}. Returns \code{-1} on
366 failure. This is the equivalent of the Python statement \samp{del
367 \var{o}[\var{key}]}.
368\end{cfuncdesc}
369
370\begin{cfuncdesc}{int}{PyObject_AsFileDescriptor}{PyObject *o}
371 Derives a file-descriptor from a Python object. If the object is an
372 integer or long integer, its value is returned. If not, the
373 object's \method{fileno()} method is called if it exists; the method
374 must return an integer or long integer, which is returned as the
375 file descriptor value. Returns \code{-1} on failure.
376\end{cfuncdesc}
377
378\begin{cfuncdesc}{PyObject*}{PyObject_Dir}{PyObject *o}
379 This is equivalent to the Python expression \samp{dir(\var{o})},
380 returning a (possibly empty) list of strings appropriate for the
381 object argument, or \NULL{} if there was an error. If the argument
382 is \NULL, this is like the Python \samp{dir()}, returning the names
383 of the current locals; in this case, if no execution frame is active
384 then \NULL{} is returned but \cfunction{PyErr_Occurred()} will
385 return false.
386\end{cfuncdesc}
387
Fred Drake314bae52002-03-11 18:46:29 +0000388\begin{cfuncdesc}{PyObject*}{PyObject_GetIter}{PyObject *o}
389 This is equivalent to the Python expression \samp{iter(\var{o})}.
390 It returns a new iterator for the object argument, or the object
391 itself if the object is already an iterator. Raises
392 \exception{TypeError} and returns \NULL{} if the object cannot be
393 iterated.
394\end{cfuncdesc}
395
Fred Drake3adf79e2001-10-12 19:01:43 +0000396
397\section{Number Protocol \label{number}}
398
399\begin{cfuncdesc}{int}{PyNumber_Check}{PyObject *o}
400 Returns \code{1} if the object \var{o} provides numeric protocols,
401 and false otherwise. This function always succeeds.
402\end{cfuncdesc}
403
404
405\begin{cfuncdesc}{PyObject*}{PyNumber_Add}{PyObject *o1, PyObject *o2}
406 Returns the result of adding \var{o1} and \var{o2}, or \NULL{} on
407 failure. This is the equivalent of the Python expression
408 \samp{\var{o1} + \var{o2}}.
409\end{cfuncdesc}
410
411
412\begin{cfuncdesc}{PyObject*}{PyNumber_Subtract}{PyObject *o1, PyObject *o2}
413 Returns the result of subtracting \var{o2} from \var{o1}, or \NULL{}
414 on failure. This is the equivalent of the Python expression
415 \samp{\var{o1} - \var{o2}}.
416\end{cfuncdesc}
417
418
419\begin{cfuncdesc}{PyObject*}{PyNumber_Multiply}{PyObject *o1, PyObject *o2}
420 Returns the result of multiplying \var{o1} and \var{o2}, or \NULL{}
421 on failure. This is the equivalent of the Python expression
422 \samp{\var{o1} * \var{o2}}.
423\end{cfuncdesc}
424
425
426\begin{cfuncdesc}{PyObject*}{PyNumber_Divide}{PyObject *o1, PyObject *o2}
427 Returns the result of dividing \var{o1} by \var{o2}, or \NULL{} on
428 failure. This is the equivalent of the Python expression
429 \samp{\var{o1} / \var{o2}}.
430\end{cfuncdesc}
431
432
433\begin{cfuncdesc}{PyObject*}{PyNumber_FloorDivide}{PyObject *o1, PyObject *o2}
434 Return the floor of \var{o1} divided by \var{o2}, or \NULL{} on
435 failure. This is equivalent to the ``classic'' division of
436 integers.
437 \versionadded{2.2}
438\end{cfuncdesc}
439
440
441\begin{cfuncdesc}{PyObject*}{PyNumber_TrueDivide}{PyObject *o1, PyObject *o2}
442 Return a reasonable approximation for the mathematical value of
443 \var{o1} divided by \var{o2}, or \NULL{} on failure. The return
444 value is ``approximate'' because binary floating point numbers are
445 approximate; it is not possible to represent all real numbers in
446 base two. This function can return a floating point value when
447 passed two integers.
448 \versionadded{2.2}
449\end{cfuncdesc}
450
451
452\begin{cfuncdesc}{PyObject*}{PyNumber_Remainder}{PyObject *o1, PyObject *o2}
453 Returns the remainder of dividing \var{o1} by \var{o2}, or \NULL{}
454 on failure. This is the equivalent of the Python expression
455 \samp{\var{o1} \%\ \var{o2}}.
456\end{cfuncdesc}
457
458
459\begin{cfuncdesc}{PyObject*}{PyNumber_Divmod}{PyObject *o1, PyObject *o2}
460 See the built-in function \function{divmod()}\bifuncindex{divmod}.
461 Returns \NULL{} on failure. This is the equivalent of the Python
462 expression \samp{divmod(\var{o1}, \var{o2})}.
463\end{cfuncdesc}
464
465
466\begin{cfuncdesc}{PyObject*}{PyNumber_Power}{PyObject *o1,
467 PyObject *o2, PyObject *o3}
468 See the built-in function \function{pow()}\bifuncindex{pow}.
469 Returns \NULL{} on failure. This is the equivalent of the Python
470 expression \samp{pow(\var{o1}, \var{o2}, \var{o3})}, where \var{o3}
471 is optional. If \var{o3} is to be ignored, pass \cdata{Py_None} in
472 its place (passing \NULL{} for \var{o3} would cause an illegal
473 memory access).
474\end{cfuncdesc}
475
476
477\begin{cfuncdesc}{PyObject*}{PyNumber_Negative}{PyObject *o}
478 Returns the negation of \var{o} on success, or \NULL{} on failure.
479 This is the equivalent of the Python expression \samp{-\var{o}}.
480\end{cfuncdesc}
481
482
483\begin{cfuncdesc}{PyObject*}{PyNumber_Positive}{PyObject *o}
484 Returns \var{o} on success, or \NULL{} on failure. This is the
485 equivalent of the Python expression \samp{+\var{o}}.
486\end{cfuncdesc}
487
488
489\begin{cfuncdesc}{PyObject*}{PyNumber_Absolute}{PyObject *o}
490 Returns the absolute value of \var{o}, or \NULL{} on failure. This
491 is the equivalent of the Python expression \samp{abs(\var{o})}.
492 \bifuncindex{abs}
493\end{cfuncdesc}
494
495
496\begin{cfuncdesc}{PyObject*}{PyNumber_Invert}{PyObject *o}
497 Returns the bitwise negation of \var{o} on success, or \NULL{} on
498 failure. This is the equivalent of the Python expression
499 \samp{\~\var{o}}.
500\end{cfuncdesc}
501
502
503\begin{cfuncdesc}{PyObject*}{PyNumber_Lshift}{PyObject *o1, PyObject *o2}
504 Returns the result of left shifting \var{o1} by \var{o2} on success,
505 or \NULL{} on failure. This is the equivalent of the Python
506 expression \samp{\var{o1} <\code{<} \var{o2}}.
507\end{cfuncdesc}
508
509
510\begin{cfuncdesc}{PyObject*}{PyNumber_Rshift}{PyObject *o1, PyObject *o2}
511 Returns the result of right shifting \var{o1} by \var{o2} on
512 success, or \NULL{} on failure. This is the equivalent of the
513 Python expression \samp{\var{o1} >\code{>} \var{o2}}.
514\end{cfuncdesc}
515
516
517\begin{cfuncdesc}{PyObject*}{PyNumber_And}{PyObject *o1, PyObject *o2}
Raymond Hettingere5fced72004-04-17 11:57:40 +0000518 Returns the ``bitwise and'' of \var{o1} and \var{o2} on success and
Fred Drake3adf79e2001-10-12 19:01:43 +0000519 \NULL{} on failure. This is the equivalent of the Python expression
520 \samp{\var{o1} \&\ \var{o2}}.
521\end{cfuncdesc}
522
523
524\begin{cfuncdesc}{PyObject*}{PyNumber_Xor}{PyObject *o1, PyObject *o2}
525 Returns the ``bitwise exclusive or'' of \var{o1} by \var{o2} on
526 success, or \NULL{} on failure. This is the equivalent of the
527 Python expression \samp{\var{o1} \textasciicircum{} \var{o2}}.
528\end{cfuncdesc}
529
530\begin{cfuncdesc}{PyObject*}{PyNumber_Or}{PyObject *o1, PyObject *o2}
531 Returns the ``bitwise or'' of \var{o1} and \var{o2} on success, or
532 \NULL{} on failure. This is the equivalent of the Python expression
533 \samp{\var{o1} | \var{o2}}.
534\end{cfuncdesc}
535
536
537\begin{cfuncdesc}{PyObject*}{PyNumber_InPlaceAdd}{PyObject *o1, PyObject *o2}
538 Returns the result of adding \var{o1} and \var{o2}, or \NULL{} on
539 failure. The operation is done \emph{in-place} when \var{o1}
540 supports it. This is the equivalent of the Python statement
541 \samp{\var{o1} += \var{o2}}.
542\end{cfuncdesc}
543
544
545\begin{cfuncdesc}{PyObject*}{PyNumber_InPlaceSubtract}{PyObject *o1,
546 PyObject *o2}
547 Returns the result of subtracting \var{o2} from \var{o1}, or \NULL{}
548 on failure. The operation is done \emph{in-place} when \var{o1}
549 supports it. This is the equivalent of the Python statement
550 \samp{\var{o1} -= \var{o2}}.
551\end{cfuncdesc}
552
553
554\begin{cfuncdesc}{PyObject*}{PyNumber_InPlaceMultiply}{PyObject *o1,
555 PyObject *o2}
556 Returns the result of multiplying \var{o1} and \var{o2}, or \NULL{}
557 on failure. The operation is done \emph{in-place} when \var{o1}
558 supports it. This is the equivalent of the Python statement
559 \samp{\var{o1} *= \var{o2}}.
560\end{cfuncdesc}
561
562
563\begin{cfuncdesc}{PyObject*}{PyNumber_InPlaceDivide}{PyObject *o1,
564 PyObject *o2}
565 Returns the result of dividing \var{o1} by \var{o2}, or \NULL{} on
566 failure. The operation is done \emph{in-place} when \var{o1}
567 supports it. This is the equivalent of the Python statement
568 \samp{\var{o1} /= \var{o2}}.
569\end{cfuncdesc}
570
571
572\begin{cfuncdesc}{PyObject*}{PyNumber_InPlaceFloorDivide}{PyObject *o1,
573 PyObject *o2}
Andrew M. Kuchling1b50b432004-06-05 19:00:55 +0000574 Returns the mathematical floor of dividing \var{o1} by \var{o2}, or
Fred Drake3adf79e2001-10-12 19:01:43 +0000575 \NULL{} on failure. The operation is done \emph{in-place} when
576 \var{o1} supports it. This is the equivalent of the Python
577 statement \samp{\var{o1} //= \var{o2}}.
578 \versionadded{2.2}
579\end{cfuncdesc}
580
581
582\begin{cfuncdesc}{PyObject*}{PyNumber_InPlaceTrueDivide}{PyObject *o1,
583 PyObject *o2}
584 Return a reasonable approximation for the mathematical value of
585 \var{o1} divided by \var{o2}, or \NULL{} on failure. The return
586 value is ``approximate'' because binary floating point numbers are
587 approximate; it is not possible to represent all real numbers in
588 base two. This function can return a floating point value when
589 passed two integers. The operation is done \emph{in-place} when
590 \var{o1} supports it.
591 \versionadded{2.2}
592\end{cfuncdesc}
593
594
595\begin{cfuncdesc}{PyObject*}{PyNumber_InPlaceRemainder}{PyObject *o1,
596 PyObject *o2}
597 Returns the remainder of dividing \var{o1} by \var{o2}, or \NULL{}
598 on failure. The operation is done \emph{in-place} when \var{o1}
599 supports it. This is the equivalent of the Python statement
600 \samp{\var{o1} \%= \var{o2}}.
601\end{cfuncdesc}
602
603
604\begin{cfuncdesc}{PyObject*}{PyNumber_InPlacePower}{PyObject *o1,
605 PyObject *o2, PyObject *o3}
606 See the built-in function \function{pow()}.\bifuncindex{pow}
607 Returns \NULL{} on failure. The operation is done \emph{in-place}
608 when \var{o1} supports it. This is the equivalent of the Python
609 statement \samp{\var{o1} **= \var{o2}} when o3 is \cdata{Py_None},
610 or an in-place variant of \samp{pow(\var{o1}, \var{o2}, \var{o3})}
611 otherwise. If \var{o3} is to be ignored, pass \cdata{Py_None} in its
612 place (passing \NULL{} for \var{o3} would cause an illegal memory
613 access).
614\end{cfuncdesc}
615
616\begin{cfuncdesc}{PyObject*}{PyNumber_InPlaceLshift}{PyObject *o1,
617 PyObject *o2}
618 Returns the result of left shifting \var{o1} by \var{o2} on success,
619 or \NULL{} on failure. The operation is done \emph{in-place} when
620 \var{o1} supports it. This is the equivalent of the Python
621 statement \samp{\var{o1} <\code{<=} \var{o2}}.
622\end{cfuncdesc}
623
624
625\begin{cfuncdesc}{PyObject*}{PyNumber_InPlaceRshift}{PyObject *o1,
626 PyObject *o2}
627 Returns the result of right shifting \var{o1} by \var{o2} on
628 success, or \NULL{} on failure. The operation is done
629 \emph{in-place} when \var{o1} supports it. This is the equivalent
630 of the Python statement \samp{\var{o1} >\code{>=} \var{o2}}.
631\end{cfuncdesc}
632
633
634\begin{cfuncdesc}{PyObject*}{PyNumber_InPlaceAnd}{PyObject *o1, PyObject *o2}
635 Returns the ``bitwise and'' of \var{o1} and \var{o2} on success and
636 \NULL{} on failure. The operation is done \emph{in-place} when
637 \var{o1} supports it. This is the equivalent of the Python
638 statement \samp{\var{o1} \&= \var{o2}}.
639\end{cfuncdesc}
640
641
642\begin{cfuncdesc}{PyObject*}{PyNumber_InPlaceXor}{PyObject *o1, PyObject *o2}
643 Returns the ``bitwise exclusive or'' of \var{o1} by \var{o2} on
644 success, or \NULL{} on failure. The operation is done
645 \emph{in-place} when \var{o1} supports it. This is the equivalent
646 of the Python statement \samp{\var{o1} \textasciicircum= \var{o2}}.
647\end{cfuncdesc}
648
649\begin{cfuncdesc}{PyObject*}{PyNumber_InPlaceOr}{PyObject *o1, PyObject *o2}
650 Returns the ``bitwise or'' of \var{o1} and \var{o2} on success, or
651 \NULL{} on failure. The operation is done \emph{in-place} when
652 \var{o1} supports it. This is the equivalent of the Python
653 statement \samp{\var{o1} |= \var{o2}}.
654\end{cfuncdesc}
655
656\begin{cfuncdesc}{int}{PyNumber_Coerce}{PyObject **p1, PyObject **p2}
657 This function takes the addresses of two variables of type
658 \ctype{PyObject*}. If the objects pointed to by \code{*\var{p1}}
659 and \code{*\var{p2}} have the same type, increment their reference
660 count and return \code{0} (success). If the objects can be converted
661 to a common numeric type, replace \code{*p1} and \code{*p2} by their
662 converted value (with 'new' reference counts), and return \code{0}.
663 If no conversion is possible, or if some other error occurs, return
664 \code{-1} (failure) and don't increment the reference counts. The
665 call \code{PyNumber_Coerce(\&o1, \&o2)} is equivalent to the Python
666 statement \samp{\var{o1}, \var{o2} = coerce(\var{o1}, \var{o2})}.
667 \bifuncindex{coerce}
668\end{cfuncdesc}
669
670\begin{cfuncdesc}{PyObject*}{PyNumber_Int}{PyObject *o}
671 Returns the \var{o} converted to an integer object on success, or
Walter Dörwaldf1715402002-11-19 20:49:15 +0000672 \NULL{} on failure. If the argument is outside the integer range
673 a long object will be returned instead. This is the equivalent
674 of the Python expression \samp{int(\var{o})}.\bifuncindex{int}
Fred Drake3adf79e2001-10-12 19:01:43 +0000675\end{cfuncdesc}
676
677\begin{cfuncdesc}{PyObject*}{PyNumber_Long}{PyObject *o}
678 Returns the \var{o} converted to a long integer object on success,
679 or \NULL{} on failure. This is the equivalent of the Python
680 expression \samp{long(\var{o})}.\bifuncindex{long}
681\end{cfuncdesc}
682
683\begin{cfuncdesc}{PyObject*}{PyNumber_Float}{PyObject *o}
684 Returns the \var{o} converted to a float object on success, or
685 \NULL{} on failure. This is the equivalent of the Python expression
686 \samp{float(\var{o})}.\bifuncindex{float}
687\end{cfuncdesc}
688
Guido van Rossum38fff8c2006-03-07 18:50:55 +0000689\begin{cfuncdesc}{Py_ssize_t}{PyNumber_Index}{PyObject *o}
690 Returns the \var{o} converted to a Py_ssize_t integer on success, or
691 -1 with an exception raised on failure.
Neal Norwitz025f14b2006-03-08 05:29:18 +0000692 \versionadded{2.5}
Guido van Rossum38fff8c2006-03-07 18:50:55 +0000693\end{cfuncdesc}
Fred Drake3adf79e2001-10-12 19:01:43 +0000694
695\section{Sequence Protocol \label{sequence}}
696
697\begin{cfuncdesc}{int}{PySequence_Check}{PyObject *o}
698 Return \code{1} if the object provides sequence protocol, and
699 \code{0} otherwise. This function always succeeds.
700\end{cfuncdesc}
701
Martin v. Löwis29fafd82006-03-01 05:16:03 +0000702\begin{cfuncdesc}{Py_ssize_t}{PySequence_Size}{PyObject *o}
Fred Drake3adf79e2001-10-12 19:01:43 +0000703 Returns the number of objects in sequence \var{o} on success, and
704 \code{-1} on failure. For objects that do not provide sequence
705 protocol, this is equivalent to the Python expression
706 \samp{len(\var{o})}.\bifuncindex{len}
707\end{cfuncdesc}
708
Martin v. Löwis29fafd82006-03-01 05:16:03 +0000709\begin{cfuncdesc}{Py_ssize_t}{PySequence_Length}{PyObject *o}
Fred Drake3adf79e2001-10-12 19:01:43 +0000710 Alternate name for \cfunction{PySequence_Size()}.
711\end{cfuncdesc}
712
713\begin{cfuncdesc}{PyObject*}{PySequence_Concat}{PyObject *o1, PyObject *o2}
714 Return the concatenation of \var{o1} and \var{o2} on success, and
715 \NULL{} on failure. This is the equivalent of the Python
716 expression \samp{\var{o1} + \var{o2}}.
717\end{cfuncdesc}
718
719
Martin v. Löwis29fafd82006-03-01 05:16:03 +0000720\begin{cfuncdesc}{PyObject*}{PySequence_Repeat}{PyObject *o, Py_ssize_t count}
Fred Drake3adf79e2001-10-12 19:01:43 +0000721 Return the result of repeating sequence object \var{o} \var{count}
722 times, or \NULL{} on failure. This is the equivalent of the Python
723 expression \samp{\var{o} * \var{count}}.
724\end{cfuncdesc}
725
726\begin{cfuncdesc}{PyObject*}{PySequence_InPlaceConcat}{PyObject *o1,
727 PyObject *o2}
728 Return the concatenation of \var{o1} and \var{o2} on success, and
729 \NULL{} on failure. The operation is done \emph{in-place} when
730 \var{o1} supports it. This is the equivalent of the Python
731 expression \samp{\var{o1} += \var{o2}}.
732\end{cfuncdesc}
733
734
Martin v. Löwis29fafd82006-03-01 05:16:03 +0000735\begin{cfuncdesc}{PyObject*}{PySequence_InPlaceRepeat}{PyObject *o, Py_ssize_t count}
Fred Drake3adf79e2001-10-12 19:01:43 +0000736 Return the result of repeating sequence object \var{o} \var{count}
737 times, or \NULL{} on failure. The operation is done \emph{in-place}
738 when \var{o} supports it. This is the equivalent of the Python
739 expression \samp{\var{o} *= \var{count}}.
740\end{cfuncdesc}
741
742
Martin v. Löwis29fafd82006-03-01 05:16:03 +0000743\begin{cfuncdesc}{PyObject*}{PySequence_GetItem}{PyObject *o, Py_ssize_t i}
Fred Drake3adf79e2001-10-12 19:01:43 +0000744 Return the \var{i}th element of \var{o}, or \NULL{} on failure.
745 This is the equivalent of the Python expression
746 \samp{\var{o}[\var{i}]}.
747\end{cfuncdesc}
748
749
Martin v. Löwis29fafd82006-03-01 05:16:03 +0000750\begin{cfuncdesc}{PyObject*}{PySequence_GetSlice}{PyObject *o, Py_ssize_t i1, Py_ssize_t i2}
Fred Drake3adf79e2001-10-12 19:01:43 +0000751 Return the slice of sequence object \var{o} between \var{i1} and
752 \var{i2}, or \NULL{} on failure. This is the equivalent of the
753 Python expression \samp{\var{o}[\var{i1}:\var{i2}]}.
754\end{cfuncdesc}
755
756
Martin v. Löwis29fafd82006-03-01 05:16:03 +0000757\begin{cfuncdesc}{int}{PySequence_SetItem}{PyObject *o, Py_ssize_t i, PyObject *v}
Fred Drake3adf79e2001-10-12 19:01:43 +0000758 Assign object \var{v} to the \var{i}th element of \var{o}. Returns
759 \code{-1} on failure. This is the equivalent of the Python
Raymond Hettinger12c484d2003-08-09 04:37:14 +0000760 statement \samp{\var{o}[\var{i}] = \var{v}}. This function \emph{does not}
761 steal a reference to \var{v}.
Fred Drake3adf79e2001-10-12 19:01:43 +0000762\end{cfuncdesc}
763
Martin v. Löwis29fafd82006-03-01 05:16:03 +0000764\begin{cfuncdesc}{int}{PySequence_DelItem}{PyObject *o, Py_ssize_t i}
Fred Drake3adf79e2001-10-12 19:01:43 +0000765 Delete the \var{i}th element of object \var{o}. Returns \code{-1}
766 on failure. This is the equivalent of the Python statement
767 \samp{del \var{o}[\var{i}]}.
768\end{cfuncdesc}
769
Martin v. Löwis29fafd82006-03-01 05:16:03 +0000770\begin{cfuncdesc}{int}{PySequence_SetSlice}{PyObject *o, Py_ssize_t i1,
771 Py_ssize_t i2, PyObject *v}
Fred Drake3adf79e2001-10-12 19:01:43 +0000772 Assign the sequence object \var{v} to the slice in sequence object
773 \var{o} from \var{i1} to \var{i2}. This is the equivalent of the
774 Python statement \samp{\var{o}[\var{i1}:\var{i2}] = \var{v}}.
775\end{cfuncdesc}
776
Martin v. Löwis29fafd82006-03-01 05:16:03 +0000777\begin{cfuncdesc}{int}{PySequence_DelSlice}{PyObject *o, Py_ssize_t i1, Py_ssize_t i2}
Fred Drake3adf79e2001-10-12 19:01:43 +0000778 Delete the slice in sequence object \var{o} from \var{i1} to
779 \var{i2}. Returns \code{-1} on failure. This is the equivalent of
780 the Python statement \samp{del \var{o}[\var{i1}:\var{i2}]}.
781\end{cfuncdesc}
782
Fred Drake3adf79e2001-10-12 19:01:43 +0000783\begin{cfuncdesc}{int}{PySequence_Count}{PyObject *o, PyObject *value}
784 Return the number of occurrences of \var{value} in \var{o}, that is,
785 return the number of keys for which \code{\var{o}[\var{key}] ==
786 \var{value}}. On failure, return \code{-1}. This is equivalent to
787 the Python expression \samp{\var{o}.count(\var{value})}.
788\end{cfuncdesc}
789
790\begin{cfuncdesc}{int}{PySequence_Contains}{PyObject *o, PyObject *value}
791 Determine if \var{o} contains \var{value}. If an item in \var{o} is
792 equal to \var{value}, return \code{1}, otherwise return \code{0}.
793 On error, return \code{-1}. This is equivalent to the Python
794 expression \samp{\var{value} in \var{o}}.
795\end{cfuncdesc}
796
797\begin{cfuncdesc}{int}{PySequence_Index}{PyObject *o, PyObject *value}
798 Return the first index \var{i} for which \code{\var{o}[\var{i}] ==
799 \var{value}}. On error, return \code{-1}. This is equivalent to
800 the Python expression \samp{\var{o}.index(\var{value})}.
801\end{cfuncdesc}
802
803\begin{cfuncdesc}{PyObject*}{PySequence_List}{PyObject *o}
804 Return a list object with the same contents as the arbitrary
805 sequence \var{o}. The returned list is guaranteed to be new.
806\end{cfuncdesc}
807
808\begin{cfuncdesc}{PyObject*}{PySequence_Tuple}{PyObject *o}
809 Return a tuple object with the same contents as the arbitrary
Neal Norwitz98fcaaf2005-10-12 03:58:14 +0000810 sequence \var{o} or \NULL{} on failure. If \var{o} is a tuple,
811 a new reference will be returned, otherwise a tuple will be
812 constructed with the appropriate contents. This is equivalent
813 to the Python expression \samp{tuple(\var{o})}.
814 \bifuncindex{tuple}
Fred Drake3adf79e2001-10-12 19:01:43 +0000815\end{cfuncdesc}
816
817\begin{cfuncdesc}{PyObject*}{PySequence_Fast}{PyObject *o, const char *m}
818 Returns the sequence \var{o} as a tuple, unless it is already a
819 tuple or list, in which case \var{o} is returned. Use
820 \cfunction{PySequence_Fast_GET_ITEM()} to access the members of the
821 result. Returns \NULL{} on failure. If the object is not a
822 sequence, raises \exception{TypeError} with \var{m} as the message
823 text.
824\end{cfuncdesc}
825
Martin v. Löwis29fafd82006-03-01 05:16:03 +0000826\begin{cfuncdesc}{PyObject*}{PySequence_Fast_GET_ITEM}{PyObject *o, Py_ssize_t i}
Fred Drake3adf79e2001-10-12 19:01:43 +0000827 Return the \var{i}th element of \var{o}, assuming that \var{o} was
Fred Drakec37b65e2001-11-28 07:26:15 +0000828 returned by \cfunction{PySequence_Fast()}, \var{o} is not \NULL,
Tim Peters1fc240e2001-10-26 05:06:50 +0000829 and that \var{i} is within bounds.
830\end{cfuncdesc}
831
Raymond Hettingerc1e4f9d2004-03-12 08:04:00 +0000832\begin{cfuncdesc}{PyObject**}{PySequence_Fast_ITEMS}{PyObject *o}
833 Return the underlying array of PyObject pointers. Assumes that
834 \var{o} was returned by \cfunction{PySequence_Fast()} and
835 \var{o} is not \NULL.
836 \versionadded{2.4}
837\end{cfuncdesc}
838
Martin v. Löwis29fafd82006-03-01 05:16:03 +0000839\begin{cfuncdesc}{PyObject*}{PySequence_ITEM}{PyObject *o, Py_ssize_t i}
Brett Cannon77e02122003-09-07 02:22:16 +0000840 Return the \var{i}th element of \var{o} or \NULL{} on failure.
Martin v. Löwis01f94bd2002-05-08 08:44:21 +0000841 Macro form of \cfunction{PySequence_GetItem()} but without checking
842 that \cfunction{PySequence_Check(\var{o})} is true and without
Fred Drake86228e42002-05-23 16:02:28 +0000843 adjustment for negative indices.
844 \versionadded{2.3}
Martin v. Löwis01f94bd2002-05-08 08:44:21 +0000845\end{cfuncdesc}
846
Tim Peters1fc240e2001-10-26 05:06:50 +0000847\begin{cfuncdesc}{int}{PySequence_Fast_GET_SIZE}{PyObject *o}
848 Returns the length of \var{o}, assuming that \var{o} was
849 returned by \cfunction{PySequence_Fast()} and that \var{o} is
Fred Drakec37b65e2001-11-28 07:26:15 +0000850 not \NULL. The size can also be gotten by calling
Tim Peters1fc240e2001-10-26 05:06:50 +0000851 \cfunction{PySequence_Size()} on \var{o}, but
852 \cfunction{PySequence_Fast_GET_SIZE()} is faster because it can
853 assume \var{o} is a list or tuple.
Fred Drake3adf79e2001-10-12 19:01:43 +0000854\end{cfuncdesc}
855
856
857\section{Mapping Protocol \label{mapping}}
858
859\begin{cfuncdesc}{int}{PyMapping_Check}{PyObject *o}
860 Return \code{1} if the object provides mapping protocol, and
861 \code{0} otherwise. This function always succeeds.
862\end{cfuncdesc}
863
864
Martin v. Löwis29fafd82006-03-01 05:16:03 +0000865\begin{cfuncdesc}{Py_ssize_t}{PyMapping_Length}{PyObject *o}
Fred Drake3adf79e2001-10-12 19:01:43 +0000866 Returns the number of keys in object \var{o} on success, and
867 \code{-1} on failure. For objects that do not provide mapping
868 protocol, this is equivalent to the Python expression
869 \samp{len(\var{o})}.\bifuncindex{len}
870\end{cfuncdesc}
871
872
873\begin{cfuncdesc}{int}{PyMapping_DelItemString}{PyObject *o, char *key}
874 Remove the mapping for object \var{key} from the object \var{o}.
875 Return \code{-1} on failure. This is equivalent to the Python
876 statement \samp{del \var{o}[\var{key}]}.
877\end{cfuncdesc}
878
879
880\begin{cfuncdesc}{int}{PyMapping_DelItem}{PyObject *o, PyObject *key}
881 Remove the mapping for object \var{key} from the object \var{o}.
882 Return \code{-1} on failure. This is equivalent to the Python
883 statement \samp{del \var{o}[\var{key}]}.
884\end{cfuncdesc}
885
886
887\begin{cfuncdesc}{int}{PyMapping_HasKeyString}{PyObject *o, char *key}
888 On success, return \code{1} if the mapping object has the key
889 \var{key} and \code{0} otherwise. This is equivalent to the Python
890 expression \samp{\var{o}.has_key(\var{key})}. This function always
891 succeeds.
892\end{cfuncdesc}
893
894
895\begin{cfuncdesc}{int}{PyMapping_HasKey}{PyObject *o, PyObject *key}
896 Return \code{1} if the mapping object has the key \var{key} and
897 \code{0} otherwise. This is equivalent to the Python expression
898 \samp{\var{o}.has_key(\var{key})}. This function always succeeds.
899\end{cfuncdesc}
900
901
902\begin{cfuncdesc}{PyObject*}{PyMapping_Keys}{PyObject *o}
903 On success, return a list of the keys in object \var{o}. On
904 failure, return \NULL. This is equivalent to the Python expression
905 \samp{\var{o}.keys()}.
906\end{cfuncdesc}
907
908
909\begin{cfuncdesc}{PyObject*}{PyMapping_Values}{PyObject *o}
910 On success, return a list of the values in object \var{o}. On
911 failure, return \NULL. This is equivalent to the Python expression
912 \samp{\var{o}.values()}.
913\end{cfuncdesc}
914
915
916\begin{cfuncdesc}{PyObject*}{PyMapping_Items}{PyObject *o}
917 On success, return a list of the items in object \var{o}, where each
918 item is a tuple containing a key-value pair. On failure, return
919 \NULL. This is equivalent to the Python expression
920 \samp{\var{o}.items()}.
921\end{cfuncdesc}
922
923
924\begin{cfuncdesc}{PyObject*}{PyMapping_GetItemString}{PyObject *o, char *key}
925 Return element of \var{o} corresponding to the object \var{key} or
926 \NULL{} on failure. This is the equivalent of the Python expression
927 \samp{\var{o}[\var{key}]}.
928\end{cfuncdesc}
929
930\begin{cfuncdesc}{int}{PyMapping_SetItemString}{PyObject *o, char *key,
931 PyObject *v}
932 Map the object \var{key} to the value \var{v} in object \var{o}.
933 Returns \code{-1} on failure. This is the equivalent of the Python
934 statement \samp{\var{o}[\var{key}] = \var{v}}.
935\end{cfuncdesc}
936
937
938\section{Iterator Protocol \label{iterator}}
939
940\versionadded{2.2}
941
942There are only a couple of functions specifically for working with
943iterators.
944
945\begin{cfuncdesc}{int}{PyIter_Check}{PyObject *o}
946 Return true if the object \var{o} supports the iterator protocol.
947\end{cfuncdesc}
948
949\begin{cfuncdesc}{PyObject*}{PyIter_Next}{PyObject *o}
950 Return the next value from the iteration \var{o}. If the object is
951 an iterator, this retrieves the next value from the iteration, and
952 returns \NULL{} with no exception set if there are no remaining
953 items. If the object is not an iterator, \exception{TypeError} is
954 raised, or if there is an error in retrieving the item, returns
955 \NULL{} and passes along the exception.
956\end{cfuncdesc}
957
958To write a loop which iterates over an iterator, the C code should
959look something like this:
960
961\begin{verbatim}
Fred Drake314bae52002-03-11 18:46:29 +0000962PyObject *iterator = PyObject_GetIter(obj);
Fred Drake3adf79e2001-10-12 19:01:43 +0000963PyObject *item;
964
Fred Drake314bae52002-03-11 18:46:29 +0000965if (iterator == NULL) {
966 /* propagate error */
967}
968
969while (item = PyIter_Next(iterator)) {
Fred Drake3adf79e2001-10-12 19:01:43 +0000970 /* do something with item */
Fred Drake8b8fe282001-12-26 16:53:48 +0000971 ...
972 /* release reference when done */
973 Py_DECREF(item);
Fred Drake3adf79e2001-10-12 19:01:43 +0000974}
Fred Drake314bae52002-03-11 18:46:29 +0000975
976Py_DECREF(iterator);
977
Fred Drake3adf79e2001-10-12 19:01:43 +0000978if (PyErr_Occurred()) {
Fred Drake314bae52002-03-11 18:46:29 +0000979 /* propagate error */
Fred Drake3adf79e2001-10-12 19:01:43 +0000980}
981else {
982 /* continue doing useful work */
983}
Fred Drake8b8fe282001-12-26 16:53:48 +0000984\end{verbatim}
985
Jeremy Hylton89c3a222001-11-09 21:59:42 +0000986
Fred Drake94ead572001-11-09 23:34:26 +0000987\section{Buffer Protocol \label{abstract-buffer}}
Jeremy Hylton89c3a222001-11-09 21:59:42 +0000988
989\begin{cfuncdesc}{int}{PyObject_AsCharBuffer}{PyObject *obj,
Fred Drake94ead572001-11-09 23:34:26 +0000990 const char **buffer,
Martin v. Löwis29fafd82006-03-01 05:16:03 +0000991 Py_ssize_t *buffer_len}
Jeremy Hylton89c3a222001-11-09 21:59:42 +0000992 Returns a pointer to a read-only memory location useable as character-
993 based input. The \var{obj} argument must support the single-segment
Fred Drake243ea712002-04-04 04:10:36 +0000994 character buffer interface. On success, returns \code{0}, sets
Thomas Hellerbfeeeee2001-11-12 07:46:31 +0000995 \var{buffer} to the memory location and \var{buffer_len} to the buffer
Fred Drake243ea712002-04-04 04:10:36 +0000996 length. Returns \code{-1} and sets a \exception{TypeError} on error.
Fred Drake94ead572001-11-09 23:34:26 +0000997 \versionadded{1.6}
Jeremy Hylton89c3a222001-11-09 21:59:42 +0000998\end{cfuncdesc}
999
1000\begin{cfuncdesc}{int}{PyObject_AsReadBuffer}{PyObject *obj,
Andrew M. Kuchlingee5e4cd2004-07-07 13:07:47 +00001001 const void **buffer,
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001002 Py_ssize_t *buffer_len}
Jeremy Hylton89c3a222001-11-09 21:59:42 +00001003 Returns a pointer to a read-only memory location containing
1004 arbitrary data. The \var{obj} argument must support the
1005 single-segment readable buffer interface. On success, returns
Fred Drake243ea712002-04-04 04:10:36 +00001006 \code{0}, sets \var{buffer} to the memory location and \var{buffer_len}
1007 to the buffer length. Returns \code{-1} and sets a
Jeremy Hylton89c3a222001-11-09 21:59:42 +00001008 \exception{TypeError} on error.
Fred Drake94ead572001-11-09 23:34:26 +00001009 \versionadded{1.6}
Jeremy Hylton89c3a222001-11-09 21:59:42 +00001010\end{cfuncdesc}
1011
1012\begin{cfuncdesc}{int}{PyObject_CheckReadBuffer}{PyObject *o}
1013 Returns \code{1} if \var{o} supports the single-segment readable
1014 buffer interface. Otherwise returns \code{0}.
Fred Drake94ead572001-11-09 23:34:26 +00001015 \versionadded{2.2}
Fred Drake8b8fe282001-12-26 16:53:48 +00001016\end{cfuncdesc}
Jeremy Hylton89c3a222001-11-09 21:59:42 +00001017
1018\begin{cfuncdesc}{int}{PyObject_AsWriteBuffer}{PyObject *obj,
Andrew M. Kuchlingee5e4cd2004-07-07 13:07:47 +00001019 void **buffer,
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001020 Py_ssize_t *buffer_len}
Jeremy Hylton89c3a222001-11-09 21:59:42 +00001021 Returns a pointer to a writeable memory location. The \var{obj}
1022 argument must support the single-segment, character buffer
Fred Drake243ea712002-04-04 04:10:36 +00001023 interface. On success, returns \code{0}, sets \var{buffer} to the
Thomas Hellerbfeeeee2001-11-12 07:46:31 +00001024 memory location and \var{buffer_len} to the buffer length. Returns
Fred Drake243ea712002-04-04 04:10:36 +00001025 \code{-1} and sets a \exception{TypeError} on error.
Fred Drake94ead572001-11-09 23:34:26 +00001026 \versionadded{1.6}
Jeremy Hylton89c3a222001-11-09 21:59:42 +00001027\end{cfuncdesc}