blob: c5939b2b2f35ff0348df5385804be66b705f2418 [file] [log] [blame]
Fred Drake3adf79e2001-10-12 19:01:43 +00001\chapter{Abstract Objects Layer \label{abstract}}
2
3The functions in this chapter interact with Python objects regardless
4of their type, or with wide classes of object types (e.g. all
5numerical types, or all sequence types). When used on object types
6for which they do not apply, they will raise a Python exception.
7
8
9\section{Object Protocol \label{object}}
10
11\begin{cfuncdesc}{int}{PyObject_Print}{PyObject *o, FILE *fp, int flags}
12 Print an object \var{o}, on file \var{fp}. Returns \code{-1} on
13 error. The flags argument is used to enable certain printing
14 options. The only option currently supported is
15 \constant{Py_PRINT_RAW}; if given, the \function{str()} of the
16 object is written instead of the \function{repr()}.
17\end{cfuncdesc}
18
19\begin{cfuncdesc}{int}{PyObject_HasAttrString}{PyObject *o, char *attr_name}
20 Returns \code{1} if \var{o} has the attribute \var{attr_name}, and
21 \code{0} otherwise. This is equivalent to the Python expression
22 \samp{hasattr(\var{o}, \var{attr_name})}. This function always
23 succeeds.
24\end{cfuncdesc}
25
26\begin{cfuncdesc}{PyObject*}{PyObject_GetAttrString}{PyObject *o,
27 char *attr_name}
28 Retrieve an attribute named \var{attr_name} from object \var{o}.
29 Returns the attribute value on success, or \NULL{} on failure.
30 This is the equivalent of the Python expression
31 \samp{\var{o}.\var{attr_name}}.
32\end{cfuncdesc}
33
34
35\begin{cfuncdesc}{int}{PyObject_HasAttr}{PyObject *o, PyObject *attr_name}
36 Returns \code{1} if \var{o} has the attribute \var{attr_name}, and
37 \code{0} otherwise. This is equivalent to the Python expression
38 \samp{hasattr(\var{o}, \var{attr_name})}. This function always
39 succeeds.
40\end{cfuncdesc}
41
42
43\begin{cfuncdesc}{PyObject*}{PyObject_GetAttr}{PyObject *o,
44 PyObject *attr_name}
45 Retrieve an attribute named \var{attr_name} from object \var{o}.
46 Returns the attribute value on success, or \NULL{} on failure. This
47 is the equivalent of the Python expression
48 \samp{\var{o}.\var{attr_name}}.
49\end{cfuncdesc}
50
51
52\begin{cfuncdesc}{int}{PyObject_SetAttrString}{PyObject *o,
53 char *attr_name, PyObject *v}
54 Set the value of the attribute named \var{attr_name}, for object
55 \var{o}, to the value \var{v}. Returns \code{-1} on failure. This
56 is the equivalent of the Python statement
57 \samp{\var{o}.\var{attr_name} = \var{v}}.
58\end{cfuncdesc}
59
60
61\begin{cfuncdesc}{int}{PyObject_SetAttr}{PyObject *o,
62 PyObject *attr_name, PyObject *v}
63 Set the value of the attribute named \var{attr_name}, for object
64 \var{o}, to the value \var{v}. Returns \code{-1} on failure. This
65 is the equivalent of the Python statement
66 \samp{\var{o}.\var{attr_name} = \var{v}}.
67\end{cfuncdesc}
68
69
70\begin{cfuncdesc}{int}{PyObject_DelAttrString}{PyObject *o, char *attr_name}
71 Delete attribute named \var{attr_name}, for object \var{o}. Returns
72 \code{-1} on failure. This is the equivalent of the Python
73 statement: \samp{del \var{o}.\var{attr_name}}.
74\end{cfuncdesc}
75
76
77\begin{cfuncdesc}{int}{PyObject_DelAttr}{PyObject *o, PyObject *attr_name}
78 Delete attribute named \var{attr_name}, for object \var{o}. Returns
79 \code{-1} on failure. This is the equivalent of the Python
80 statement \samp{del \var{o}.\var{attr_name}}.
81\end{cfuncdesc}
82
83
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()}
178 returns \code{1} if \var{inst} is of type \var{cls}. If \var{inst}
179 is not a class instance and \var{cls} is neither a type object or
180 class object, \var{inst} must have a \member{__class__} attribute
181 --- the class relationship of the value of that attribute with
182 \var{cls} will be used to determine the result of this function.
Fred Drake3adf79e2001-10-12 19:01:43 +0000183 \versionadded{2.1}
184\end{cfuncdesc}
185
186Subclass determination is done in a fairly straightforward way, but
187includes a wrinkle that implementors of extensions to the class system
188may want to be aware of. If \class{A} and \class{B} are class
189objects, \class{B} is a subclass of \class{A} if it inherits from
190\class{A} either directly or indirectly. If either is not a class
191object, a more general mechanism is used to determine the class
192relationship of the two objects. When testing if \var{B} is a
193subclass of \var{A}, if \var{A} is \var{B},
194\cfunction{PyObject_IsSubclass()} returns true. If \var{A} and
195\var{B} are different objects, \var{B}'s \member{__bases__} attribute
196is searched in a depth-first fashion for \var{A} --- the presence of
197the \member{__bases__} attribute is considered sufficient for this
198determination.
199
200\begin{cfuncdesc}{int}{PyObject_IsSubclass}{PyObject *derived,
201 PyObject *cls}
202 Returns \code{1} if the class \var{derived} is identical to or
203 derived from the class \var{cls}, otherwise returns \code{0}. In
204 case of an error, returns \code{-1}. If either \var{derived} or
205 \var{cls} is not an actual class object, this function uses the
206 generic algorithm described above.
207 \versionadded{2.1}
208\end{cfuncdesc}
209
210
211\begin{cfuncdesc}{int}{PyCallable_Check}{PyObject *o}
212 Determine if the object \var{o} is callable. Return \code{1} if the
213 object is callable and \code{0} otherwise. This function always
214 succeeds.
215\end{cfuncdesc}
216
217
Fred Drake0e0b6182002-04-15 20:51:19 +0000218\begin{cfuncdesc}{PyObject*}{PyObject_Call}{PyObject *callable_object,
219 PyObject *args,
220 PyObject *kw}
221 Call a callable Python object \var{callable_object}, with arguments
222 given by the tuple \var{args}, and named arguments given by the
223 dictionary \var{kw}. If no named arguments are needed, \var{kw} may
224 be \NULL{}. \var{args} must not be \NULL{}, use an empty tuple if
225 no arguments are needed. Returns the result of the call on success,
226 or \NULL{} on failure. This is the equivalent of the Python
227 expression \samp{apply(\var{callable_object}, \var{args}, \var{kw})}
228 or \samp{\var{callable_object}(*\var{args}, **\var{kw})}.
229 \bifuncindex{apply}
230\end{cfuncdesc}
231
232
Fred Drake3adf79e2001-10-12 19:01:43 +0000233\begin{cfuncdesc}{PyObject*}{PyObject_CallObject}{PyObject *callable_object,
234 PyObject *args}
235 Call a callable Python object \var{callable_object}, with arguments
236 given by the tuple \var{args}. If no arguments are needed, then
237 \var{args} may be \NULL. Returns the result of the call on
238 success, or \NULL{} on failure. This is the equivalent of the
239 Python expression \samp{apply(\var{callable_object}, \var{args})} or
240 \samp{\var{callable_object}(*\var{args})}.
241 \bifuncindex{apply}
242\end{cfuncdesc}
243
Fred Drakec44e9ec2001-10-26 16:38:38 +0000244\begin{cfuncdesc}{PyObject*}{PyObject_CallFunction}{PyObject *callable,
245 char *format, \moreargs}
246 Call a callable Python object \var{callable}, with a variable
Fred Drake3adf79e2001-10-12 19:01:43 +0000247 number of C arguments. The C arguments are described using a
248 \cfunction{Py_BuildValue()} style format string. The format may be
249 \NULL, indicating that no arguments are provided. Returns the
250 result of the call on success, or \NULL{} on failure. This is the
Fred Drakec44e9ec2001-10-26 16:38:38 +0000251 equivalent of the Python expression \samp{apply(\var{callable},
252 \var{args})} or \samp{\var{callable}(*\var{args})}.
Fred Drake3adf79e2001-10-12 19:01:43 +0000253 \bifuncindex{apply}
254\end{cfuncdesc}
255
256
257\begin{cfuncdesc}{PyObject*}{PyObject_CallMethod}{PyObject *o,
Fred Drakec44e9ec2001-10-26 16:38:38 +0000258 char *method, char *format,
259 \moreargs}
Fred Drake3adf79e2001-10-12 19:01:43 +0000260 Call the method named \var{m} of object \var{o} with a variable
261 number of C arguments. The C arguments are described by a
262 \cfunction{Py_BuildValue()} format string. The format may be \NULL,
263 indicating that no arguments are provided. Returns the result of the
264 call on success, or \NULL{} on failure. This is the equivalent of
Fred Drakec44e9ec2001-10-26 16:38:38 +0000265 the Python expression \samp{\var{o}.\var{method}(\var{args})}.
266\end{cfuncdesc}
267
268
Fred Drakeb0c079e2001-10-28 02:39:03 +0000269\begin{cfuncdesc}{PyObject*}{PyObject_CallFunctionObjArgs}{PyObject *callable,
270 \moreargs,
271 \code{NULL}}
Fred Drakec44e9ec2001-10-26 16:38:38 +0000272 Call a callable Python object \var{callable}, with a variable
273 number of \ctype{PyObject*} arguments. The arguments are provided
274 as a variable number of parameters followed by \NULL.
275 Returns the result of the call on success, or \NULL{} on failure.
276 \versionadded{2.2}
277\end{cfuncdesc}
278
279
Fred Drakeb0c079e2001-10-28 02:39:03 +0000280\begin{cfuncdesc}{PyObject*}{PyObject_CallMethodObjArgs}{PyObject *o,
281 PyObject *name,
282 \moreargs,
283 \code{NULL}}
Fred Drakec44e9ec2001-10-26 16:38:38 +0000284 Calls a method of the object \var{o}, where the name of the method
285 is given as a Python string object in \var{name}. It is called with
286 a variable number of \ctype{PyObject*} arguments. The arguments are
287 provided 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}
Fred Drake3adf79e2001-10-12 19:01:43 +0000290\end{cfuncdesc}
291
292
293\begin{cfuncdesc}{int}{PyObject_Hash}{PyObject *o}
294 Compute and return the hash value of an object \var{o}. On failure,
295 return \code{-1}. This is the equivalent of the Python expression
296 \samp{hash(\var{o})}.\bifuncindex{hash}
297\end{cfuncdesc}
298
299
300\begin{cfuncdesc}{int}{PyObject_IsTrue}{PyObject *o}
301 Returns \code{1} if the object \var{o} is considered to be true, and
302 \code{0} otherwise. This is equivalent to the Python expression
303 \samp{not not \var{o}}. This function always succeeds.
304\end{cfuncdesc}
305
306
307\begin{cfuncdesc}{PyObject*}{PyObject_Type}{PyObject *o}
308 When \var{o} is non-\NULL, returns a type object corresponding to
309 the object type of object \var{o}. On failure, raises
310 \exception{SystemError} and returns \NULL. This is equivalent to
311 the Python expression \code{type(\var{o})}.
312 \bifuncindex{type}
313\end{cfuncdesc}
314
315\begin{cfuncdesc}{int}{PyObject_TypeCheck}{PyObject *o, PyTypeObject *type}
316 Return true if the object \var{o} is of type \var{type} or a subtype
317 of \var{type}. Both parameters must be non-\NULL.
318 \versionadded{2.2}
319\end{cfuncdesc}
320
321\begin{cfuncdesc}{int}{PyObject_Length}{PyObject *o}
Fred Drake0e0b6182002-04-15 20:51:19 +0000322\cfuncline{int}{PyObject_Size}{PyObject *o}
Fred Drake3adf79e2001-10-12 19:01:43 +0000323 Return the length of object \var{o}. If the object \var{o} provides
324 both sequence and mapping protocols, the sequence length is
325 returned. On error, \code{-1} is returned. This is the equivalent
326 to the Python expression \samp{len(\var{o})}.\bifuncindex{len}
327\end{cfuncdesc}
328
329
330\begin{cfuncdesc}{PyObject*}{PyObject_GetItem}{PyObject *o, PyObject *key}
331 Return element of \var{o} corresponding to the object \var{key} or
332 \NULL{} on failure. This is the equivalent of the Python expression
333 \samp{\var{o}[\var{key}]}.
334\end{cfuncdesc}
335
336
337\begin{cfuncdesc}{int}{PyObject_SetItem}{PyObject *o,
338 PyObject *key, PyObject *v}
339 Map the object \var{key} to the value \var{v}. Returns \code{-1} on
340 failure. This is the equivalent of the Python statement
341 \samp{\var{o}[\var{key}] = \var{v}}.
342\end{cfuncdesc}
343
344
345\begin{cfuncdesc}{int}{PyObject_DelItem}{PyObject *o, PyObject *key}
346 Delete the mapping for \var{key} from \var{o}. Returns \code{-1} on
347 failure. This is the equivalent of the Python statement \samp{del
348 \var{o}[\var{key}]}.
349\end{cfuncdesc}
350
351\begin{cfuncdesc}{int}{PyObject_AsFileDescriptor}{PyObject *o}
352 Derives a file-descriptor from a Python object. If the object is an
353 integer or long integer, its value is returned. If not, the
354 object's \method{fileno()} method is called if it exists; the method
355 must return an integer or long integer, which is returned as the
356 file descriptor value. Returns \code{-1} on failure.
357\end{cfuncdesc}
358
359\begin{cfuncdesc}{PyObject*}{PyObject_Dir}{PyObject *o}
360 This is equivalent to the Python expression \samp{dir(\var{o})},
361 returning a (possibly empty) list of strings appropriate for the
362 object argument, or \NULL{} if there was an error. If the argument
363 is \NULL, this is like the Python \samp{dir()}, returning the names
364 of the current locals; in this case, if no execution frame is active
365 then \NULL{} is returned but \cfunction{PyErr_Occurred()} will
366 return false.
367\end{cfuncdesc}
368
Fred Drake314bae52002-03-11 18:46:29 +0000369\begin{cfuncdesc}{PyObject*}{PyObject_GetIter}{PyObject *o}
370 This is equivalent to the Python expression \samp{iter(\var{o})}.
371 It returns a new iterator for the object argument, or the object
372 itself if the object is already an iterator. Raises
373 \exception{TypeError} and returns \NULL{} if the object cannot be
374 iterated.
375\end{cfuncdesc}
376
Fred Drake3adf79e2001-10-12 19:01:43 +0000377
378\section{Number Protocol \label{number}}
379
380\begin{cfuncdesc}{int}{PyNumber_Check}{PyObject *o}
381 Returns \code{1} if the object \var{o} provides numeric protocols,
382 and false otherwise. This function always succeeds.
383\end{cfuncdesc}
384
385
386\begin{cfuncdesc}{PyObject*}{PyNumber_Add}{PyObject *o1, PyObject *o2}
387 Returns the result of adding \var{o1} and \var{o2}, or \NULL{} on
388 failure. This is the equivalent of the Python expression
389 \samp{\var{o1} + \var{o2}}.
390\end{cfuncdesc}
391
392
393\begin{cfuncdesc}{PyObject*}{PyNumber_Subtract}{PyObject *o1, PyObject *o2}
394 Returns the result of subtracting \var{o2} from \var{o1}, or \NULL{}
395 on failure. This is the equivalent of the Python expression
396 \samp{\var{o1} - \var{o2}}.
397\end{cfuncdesc}
398
399
400\begin{cfuncdesc}{PyObject*}{PyNumber_Multiply}{PyObject *o1, PyObject *o2}
401 Returns the result of multiplying \var{o1} and \var{o2}, or \NULL{}
402 on failure. This is the equivalent of the Python expression
403 \samp{\var{o1} * \var{o2}}.
404\end{cfuncdesc}
405
406
407\begin{cfuncdesc}{PyObject*}{PyNumber_Divide}{PyObject *o1, PyObject *o2}
408 Returns the result of dividing \var{o1} by \var{o2}, or \NULL{} on
409 failure. This is the equivalent of the Python expression
410 \samp{\var{o1} / \var{o2}}.
411\end{cfuncdesc}
412
413
414\begin{cfuncdesc}{PyObject*}{PyNumber_FloorDivide}{PyObject *o1, PyObject *o2}
415 Return the floor of \var{o1} divided by \var{o2}, or \NULL{} on
416 failure. This is equivalent to the ``classic'' division of
417 integers.
418 \versionadded{2.2}
419\end{cfuncdesc}
420
421
422\begin{cfuncdesc}{PyObject*}{PyNumber_TrueDivide}{PyObject *o1, PyObject *o2}
423 Return a reasonable approximation for the mathematical value of
424 \var{o1} divided by \var{o2}, or \NULL{} on failure. The return
425 value is ``approximate'' because binary floating point numbers are
426 approximate; it is not possible to represent all real numbers in
427 base two. This function can return a floating point value when
428 passed two integers.
429 \versionadded{2.2}
430\end{cfuncdesc}
431
432
433\begin{cfuncdesc}{PyObject*}{PyNumber_Remainder}{PyObject *o1, PyObject *o2}
434 Returns the remainder of dividing \var{o1} by \var{o2}, or \NULL{}
435 on failure. This is the equivalent of the Python expression
436 \samp{\var{o1} \%\ \var{o2}}.
437\end{cfuncdesc}
438
439
440\begin{cfuncdesc}{PyObject*}{PyNumber_Divmod}{PyObject *o1, PyObject *o2}
441 See the built-in function \function{divmod()}\bifuncindex{divmod}.
442 Returns \NULL{} on failure. This is the equivalent of the Python
443 expression \samp{divmod(\var{o1}, \var{o2})}.
444\end{cfuncdesc}
445
446
447\begin{cfuncdesc}{PyObject*}{PyNumber_Power}{PyObject *o1,
448 PyObject *o2, PyObject *o3}
449 See the built-in function \function{pow()}\bifuncindex{pow}.
450 Returns \NULL{} on failure. This is the equivalent of the Python
451 expression \samp{pow(\var{o1}, \var{o2}, \var{o3})}, where \var{o3}
452 is optional. If \var{o3} is to be ignored, pass \cdata{Py_None} in
453 its place (passing \NULL{} for \var{o3} would cause an illegal
454 memory access).
455\end{cfuncdesc}
456
457
458\begin{cfuncdesc}{PyObject*}{PyNumber_Negative}{PyObject *o}
459 Returns the negation of \var{o} on success, or \NULL{} on failure.
460 This is the equivalent of the Python expression \samp{-\var{o}}.
461\end{cfuncdesc}
462
463
464\begin{cfuncdesc}{PyObject*}{PyNumber_Positive}{PyObject *o}
465 Returns \var{o} on success, or \NULL{} on failure. This is the
466 equivalent of the Python expression \samp{+\var{o}}.
467\end{cfuncdesc}
468
469
470\begin{cfuncdesc}{PyObject*}{PyNumber_Absolute}{PyObject *o}
471 Returns the absolute value of \var{o}, or \NULL{} on failure. This
472 is the equivalent of the Python expression \samp{abs(\var{o})}.
473 \bifuncindex{abs}
474\end{cfuncdesc}
475
476
477\begin{cfuncdesc}{PyObject*}{PyNumber_Invert}{PyObject *o}
478 Returns the bitwise negation of \var{o} on success, or \NULL{} on
479 failure. This is the equivalent of the Python expression
480 \samp{\~\var{o}}.
481\end{cfuncdesc}
482
483
484\begin{cfuncdesc}{PyObject*}{PyNumber_Lshift}{PyObject *o1, PyObject *o2}
485 Returns the result of left shifting \var{o1} by \var{o2} on success,
486 or \NULL{} on failure. This is the equivalent of the Python
487 expression \samp{\var{o1} <\code{<} \var{o2}}.
488\end{cfuncdesc}
489
490
491\begin{cfuncdesc}{PyObject*}{PyNumber_Rshift}{PyObject *o1, PyObject *o2}
492 Returns the result of right shifting \var{o1} by \var{o2} on
493 success, or \NULL{} on failure. This is the equivalent of the
494 Python expression \samp{\var{o1} >\code{>} \var{o2}}.
495\end{cfuncdesc}
496
497
498\begin{cfuncdesc}{PyObject*}{PyNumber_And}{PyObject *o1, PyObject *o2}
499 Returns the ``bitwise and'' of \var{o2} and \var{o2} on success and
500 \NULL{} on failure. This is the equivalent of the Python expression
501 \samp{\var{o1} \&\ \var{o2}}.
502\end{cfuncdesc}
503
504
505\begin{cfuncdesc}{PyObject*}{PyNumber_Xor}{PyObject *o1, PyObject *o2}
506 Returns the ``bitwise exclusive or'' of \var{o1} by \var{o2} on
507 success, or \NULL{} on failure. This is the equivalent of the
508 Python expression \samp{\var{o1} \textasciicircum{} \var{o2}}.
509\end{cfuncdesc}
510
511\begin{cfuncdesc}{PyObject*}{PyNumber_Or}{PyObject *o1, PyObject *o2}
512 Returns the ``bitwise or'' of \var{o1} and \var{o2} on success, or
513 \NULL{} on failure. This is the equivalent of the Python expression
514 \samp{\var{o1} | \var{o2}}.
515\end{cfuncdesc}
516
517
518\begin{cfuncdesc}{PyObject*}{PyNumber_InPlaceAdd}{PyObject *o1, PyObject *o2}
519 Returns the result of adding \var{o1} and \var{o2}, or \NULL{} on
520 failure. The operation is done \emph{in-place} when \var{o1}
521 supports it. This is the equivalent of the Python statement
522 \samp{\var{o1} += \var{o2}}.
523\end{cfuncdesc}
524
525
526\begin{cfuncdesc}{PyObject*}{PyNumber_InPlaceSubtract}{PyObject *o1,
527 PyObject *o2}
528 Returns the result of subtracting \var{o2} from \var{o1}, or \NULL{}
529 on failure. The operation is done \emph{in-place} when \var{o1}
530 supports it. This is the equivalent of the Python statement
531 \samp{\var{o1} -= \var{o2}}.
532\end{cfuncdesc}
533
534
535\begin{cfuncdesc}{PyObject*}{PyNumber_InPlaceMultiply}{PyObject *o1,
536 PyObject *o2}
537 Returns the result of multiplying \var{o1} and \var{o2}, or \NULL{}
538 on failure. The operation is done \emph{in-place} when \var{o1}
539 supports it. This is the equivalent of the Python statement
540 \samp{\var{o1} *= \var{o2}}.
541\end{cfuncdesc}
542
543
544\begin{cfuncdesc}{PyObject*}{PyNumber_InPlaceDivide}{PyObject *o1,
545 PyObject *o2}
546 Returns the result of dividing \var{o1} by \var{o2}, or \NULL{} on
547 failure. The operation is done \emph{in-place} when \var{o1}
548 supports it. This is the equivalent of the Python statement
549 \samp{\var{o1} /= \var{o2}}.
550\end{cfuncdesc}
551
552
553\begin{cfuncdesc}{PyObject*}{PyNumber_InPlaceFloorDivide}{PyObject *o1,
554 PyObject *o2}
555 Returns the mathematical of dividing \var{o1} by \var{o2}, or
556 \NULL{} on failure. The operation is done \emph{in-place} when
557 \var{o1} supports it. This is the equivalent of the Python
558 statement \samp{\var{o1} //= \var{o2}}.
559 \versionadded{2.2}
560\end{cfuncdesc}
561
562
563\begin{cfuncdesc}{PyObject*}{PyNumber_InPlaceTrueDivide}{PyObject *o1,
564 PyObject *o2}
565 Return a reasonable approximation for the mathematical value of
566 \var{o1} divided by \var{o2}, or \NULL{} on failure. The return
567 value is ``approximate'' because binary floating point numbers are
568 approximate; it is not possible to represent all real numbers in
569 base two. This function can return a floating point value when
570 passed two integers. The operation is done \emph{in-place} when
571 \var{o1} supports it.
572 \versionadded{2.2}
573\end{cfuncdesc}
574
575
576\begin{cfuncdesc}{PyObject*}{PyNumber_InPlaceRemainder}{PyObject *o1,
577 PyObject *o2}
578 Returns the remainder of dividing \var{o1} by \var{o2}, or \NULL{}
579 on failure. The operation is done \emph{in-place} when \var{o1}
580 supports it. This is the equivalent of the Python statement
581 \samp{\var{o1} \%= \var{o2}}.
582\end{cfuncdesc}
583
584
585\begin{cfuncdesc}{PyObject*}{PyNumber_InPlacePower}{PyObject *o1,
586 PyObject *o2, PyObject *o3}
587 See the built-in function \function{pow()}.\bifuncindex{pow}
588 Returns \NULL{} on failure. The operation is done \emph{in-place}
589 when \var{o1} supports it. This is the equivalent of the Python
590 statement \samp{\var{o1} **= \var{o2}} when o3 is \cdata{Py_None},
591 or an in-place variant of \samp{pow(\var{o1}, \var{o2}, \var{o3})}
592 otherwise. If \var{o3} is to be ignored, pass \cdata{Py_None} in its
593 place (passing \NULL{} for \var{o3} would cause an illegal memory
594 access).
595\end{cfuncdesc}
596
597\begin{cfuncdesc}{PyObject*}{PyNumber_InPlaceLshift}{PyObject *o1,
598 PyObject *o2}
599 Returns the result of left shifting \var{o1} by \var{o2} on success,
600 or \NULL{} on failure. The operation is done \emph{in-place} when
601 \var{o1} supports it. This is the equivalent of the Python
602 statement \samp{\var{o1} <\code{<=} \var{o2}}.
603\end{cfuncdesc}
604
605
606\begin{cfuncdesc}{PyObject*}{PyNumber_InPlaceRshift}{PyObject *o1,
607 PyObject *o2}
608 Returns the result of right shifting \var{o1} by \var{o2} on
609 success, or \NULL{} on failure. The operation is done
610 \emph{in-place} when \var{o1} supports it. This is the equivalent
611 of the Python statement \samp{\var{o1} >\code{>=} \var{o2}}.
612\end{cfuncdesc}
613
614
615\begin{cfuncdesc}{PyObject*}{PyNumber_InPlaceAnd}{PyObject *o1, PyObject *o2}
616 Returns the ``bitwise and'' of \var{o1} and \var{o2} on success and
617 \NULL{} on failure. The operation is done \emph{in-place} when
618 \var{o1} supports it. This is the equivalent of the Python
619 statement \samp{\var{o1} \&= \var{o2}}.
620\end{cfuncdesc}
621
622
623\begin{cfuncdesc}{PyObject*}{PyNumber_InPlaceXor}{PyObject *o1, PyObject *o2}
624 Returns the ``bitwise exclusive or'' of \var{o1} by \var{o2} on
625 success, or \NULL{} on failure. The operation is done
626 \emph{in-place} when \var{o1} supports it. This is the equivalent
627 of the Python statement \samp{\var{o1} \textasciicircum= \var{o2}}.
628\end{cfuncdesc}
629
630\begin{cfuncdesc}{PyObject*}{PyNumber_InPlaceOr}{PyObject *o1, PyObject *o2}
631 Returns the ``bitwise or'' of \var{o1} and \var{o2} on success, or
632 \NULL{} on failure. The operation is done \emph{in-place} when
633 \var{o1} supports it. This is the equivalent of the Python
634 statement \samp{\var{o1} |= \var{o2}}.
635\end{cfuncdesc}
636
637\begin{cfuncdesc}{int}{PyNumber_Coerce}{PyObject **p1, PyObject **p2}
638 This function takes the addresses of two variables of type
639 \ctype{PyObject*}. If the objects pointed to by \code{*\var{p1}}
640 and \code{*\var{p2}} have the same type, increment their reference
641 count and return \code{0} (success). If the objects can be converted
642 to a common numeric type, replace \code{*p1} and \code{*p2} by their
643 converted value (with 'new' reference counts), and return \code{0}.
644 If no conversion is possible, or if some other error occurs, return
645 \code{-1} (failure) and don't increment the reference counts. The
646 call \code{PyNumber_Coerce(\&o1, \&o2)} is equivalent to the Python
647 statement \samp{\var{o1}, \var{o2} = coerce(\var{o1}, \var{o2})}.
648 \bifuncindex{coerce}
649\end{cfuncdesc}
650
651\begin{cfuncdesc}{PyObject*}{PyNumber_Int}{PyObject *o}
652 Returns the \var{o} converted to an integer object on success, or
Walter Dörwaldf1715402002-11-19 20:49:15 +0000653 \NULL{} on failure. If the argument is outside the integer range
654 a long object will be returned instead. This is the equivalent
655 of the Python expression \samp{int(\var{o})}.\bifuncindex{int}
Fred Drake3adf79e2001-10-12 19:01:43 +0000656\end{cfuncdesc}
657
658\begin{cfuncdesc}{PyObject*}{PyNumber_Long}{PyObject *o}
659 Returns the \var{o} converted to a long integer object on success,
660 or \NULL{} on failure. This is the equivalent of the Python
661 expression \samp{long(\var{o})}.\bifuncindex{long}
662\end{cfuncdesc}
663
664\begin{cfuncdesc}{PyObject*}{PyNumber_Float}{PyObject *o}
665 Returns the \var{o} converted to a float object on success, or
666 \NULL{} on failure. This is the equivalent of the Python expression
667 \samp{float(\var{o})}.\bifuncindex{float}
668\end{cfuncdesc}
669
670
671\section{Sequence Protocol \label{sequence}}
672
673\begin{cfuncdesc}{int}{PySequence_Check}{PyObject *o}
674 Return \code{1} if the object provides sequence protocol, and
675 \code{0} otherwise. This function always succeeds.
676\end{cfuncdesc}
677
678\begin{cfuncdesc}{int}{PySequence_Size}{PyObject *o}
679 Returns the number of objects in sequence \var{o} on success, and
680 \code{-1} on failure. For objects that do not provide sequence
681 protocol, this is equivalent to the Python expression
682 \samp{len(\var{o})}.\bifuncindex{len}
683\end{cfuncdesc}
684
685\begin{cfuncdesc}{int}{PySequence_Length}{PyObject *o}
686 Alternate name for \cfunction{PySequence_Size()}.
687\end{cfuncdesc}
688
689\begin{cfuncdesc}{PyObject*}{PySequence_Concat}{PyObject *o1, PyObject *o2}
690 Return the concatenation of \var{o1} and \var{o2} on success, and
691 \NULL{} on failure. This is the equivalent of the Python
692 expression \samp{\var{o1} + \var{o2}}.
693\end{cfuncdesc}
694
695
696\begin{cfuncdesc}{PyObject*}{PySequence_Repeat}{PyObject *o, int count}
697 Return the result of repeating sequence object \var{o} \var{count}
698 times, or \NULL{} on failure. This is the equivalent of the Python
699 expression \samp{\var{o} * \var{count}}.
700\end{cfuncdesc}
701
702\begin{cfuncdesc}{PyObject*}{PySequence_InPlaceConcat}{PyObject *o1,
703 PyObject *o2}
704 Return the concatenation of \var{o1} and \var{o2} on success, and
705 \NULL{} on failure. The operation is done \emph{in-place} when
706 \var{o1} supports it. This is the equivalent of the Python
707 expression \samp{\var{o1} += \var{o2}}.
708\end{cfuncdesc}
709
710
711\begin{cfuncdesc}{PyObject*}{PySequence_InPlaceRepeat}{PyObject *o, int count}
712 Return the result of repeating sequence object \var{o} \var{count}
713 times, or \NULL{} on failure. The operation is done \emph{in-place}
714 when \var{o} supports it. This is the equivalent of the Python
715 expression \samp{\var{o} *= \var{count}}.
716\end{cfuncdesc}
717
718
719\begin{cfuncdesc}{PyObject*}{PySequence_GetItem}{PyObject *o, int i}
720 Return the \var{i}th element of \var{o}, or \NULL{} on failure.
721 This is the equivalent of the Python expression
722 \samp{\var{o}[\var{i}]}.
723\end{cfuncdesc}
724
725
726\begin{cfuncdesc}{PyObject*}{PySequence_GetSlice}{PyObject *o, int i1, int i2}
727 Return the slice of sequence object \var{o} between \var{i1} and
728 \var{i2}, or \NULL{} on failure. This is the equivalent of the
729 Python expression \samp{\var{o}[\var{i1}:\var{i2}]}.
730\end{cfuncdesc}
731
732
733\begin{cfuncdesc}{int}{PySequence_SetItem}{PyObject *o, int i, PyObject *v}
734 Assign object \var{v} to the \var{i}th element of \var{o}. Returns
735 \code{-1} on failure. This is the equivalent of the Python
736 statement \samp{\var{o}[\var{i}] = \var{v}}.
737\end{cfuncdesc}
738
739\begin{cfuncdesc}{int}{PySequence_DelItem}{PyObject *o, int i}
740 Delete the \var{i}th element of object \var{o}. Returns \code{-1}
741 on failure. This is the equivalent of the Python statement
742 \samp{del \var{o}[\var{i}]}.
743\end{cfuncdesc}
744
745\begin{cfuncdesc}{int}{PySequence_SetSlice}{PyObject *o, int i1,
746 int i2, PyObject *v}
747 Assign the sequence object \var{v} to the slice in sequence object
748 \var{o} from \var{i1} to \var{i2}. This is the equivalent of the
749 Python statement \samp{\var{o}[\var{i1}:\var{i2}] = \var{v}}.
750\end{cfuncdesc}
751
752\begin{cfuncdesc}{int}{PySequence_DelSlice}{PyObject *o, int i1, int i2}
753 Delete the slice in sequence object \var{o} from \var{i1} to
754 \var{i2}. Returns \code{-1} on failure. This is the equivalent of
755 the Python statement \samp{del \var{o}[\var{i1}:\var{i2}]}.
756\end{cfuncdesc}
757
758\begin{cfuncdesc}{PyObject*}{PySequence_Tuple}{PyObject *o}
759 Returns the \var{o} as a tuple on success, and \NULL{} on failure.
760 This is equivalent to the Python expression \samp{tuple(\var{o})}.
761 \bifuncindex{tuple}
762\end{cfuncdesc}
763
764\begin{cfuncdesc}{int}{PySequence_Count}{PyObject *o, PyObject *value}
765 Return the number of occurrences of \var{value} in \var{o}, that is,
766 return the number of keys for which \code{\var{o}[\var{key}] ==
767 \var{value}}. On failure, return \code{-1}. This is equivalent to
768 the Python expression \samp{\var{o}.count(\var{value})}.
769\end{cfuncdesc}
770
771\begin{cfuncdesc}{int}{PySequence_Contains}{PyObject *o, PyObject *value}
772 Determine if \var{o} contains \var{value}. If an item in \var{o} is
773 equal to \var{value}, return \code{1}, otherwise return \code{0}.
774 On error, return \code{-1}. This is equivalent to the Python
775 expression \samp{\var{value} in \var{o}}.
776\end{cfuncdesc}
777
778\begin{cfuncdesc}{int}{PySequence_Index}{PyObject *o, PyObject *value}
779 Return the first index \var{i} for which \code{\var{o}[\var{i}] ==
780 \var{value}}. On error, return \code{-1}. This is equivalent to
781 the Python expression \samp{\var{o}.index(\var{value})}.
782\end{cfuncdesc}
783
784\begin{cfuncdesc}{PyObject*}{PySequence_List}{PyObject *o}
785 Return a list object with the same contents as the arbitrary
786 sequence \var{o}. The returned list is guaranteed to be new.
787\end{cfuncdesc}
788
789\begin{cfuncdesc}{PyObject*}{PySequence_Tuple}{PyObject *o}
790 Return a tuple object with the same contents as the arbitrary
791 sequence \var{o}. If \var{o} is a tuple, a new reference will be
792 returned, otherwise a tuple will be constructed with the appropriate
793 contents.
794\end{cfuncdesc}
795
796\begin{cfuncdesc}{PyObject*}{PySequence_Fast}{PyObject *o, const char *m}
797 Returns the sequence \var{o} as a tuple, unless it is already a
798 tuple or list, in which case \var{o} is returned. Use
799 \cfunction{PySequence_Fast_GET_ITEM()} to access the members of the
800 result. Returns \NULL{} on failure. If the object is not a
801 sequence, raises \exception{TypeError} with \var{m} as the message
802 text.
803\end{cfuncdesc}
804
805\begin{cfuncdesc}{PyObject*}{PySequence_Fast_GET_ITEM}{PyObject *o, int i}
806 Return the \var{i}th element of \var{o}, assuming that \var{o} was
Fred Drakec37b65e2001-11-28 07:26:15 +0000807 returned by \cfunction{PySequence_Fast()}, \var{o} is not \NULL,
Tim Peters1fc240e2001-10-26 05:06:50 +0000808 and that \var{i} is within bounds.
809\end{cfuncdesc}
810
Martin v. Löwis01f94bd2002-05-08 08:44:21 +0000811\begin{cfuncdesc}{PyObject*}{PySequence_ITEM}{PyObject *o, int i}
812 Return the \var{i}th element of \var{o} or \NULL on failure.
813 Macro form of \cfunction{PySequence_GetItem()} but without checking
814 that \cfunction{PySequence_Check(\var{o})} is true and without
Fred Drake86228e42002-05-23 16:02:28 +0000815 adjustment for negative indices.
816 \versionadded{2.3}
Martin v. Löwis01f94bd2002-05-08 08:44:21 +0000817\end{cfuncdesc}
818
Tim Peters1fc240e2001-10-26 05:06:50 +0000819\begin{cfuncdesc}{int}{PySequence_Fast_GET_SIZE}{PyObject *o}
820 Returns the length of \var{o}, assuming that \var{o} was
821 returned by \cfunction{PySequence_Fast()} and that \var{o} is
Fred Drakec37b65e2001-11-28 07:26:15 +0000822 not \NULL. The size can also be gotten by calling
Tim Peters1fc240e2001-10-26 05:06:50 +0000823 \cfunction{PySequence_Size()} on \var{o}, but
824 \cfunction{PySequence_Fast_GET_SIZE()} is faster because it can
825 assume \var{o} is a list or tuple.
Fred Drake3adf79e2001-10-12 19:01:43 +0000826\end{cfuncdesc}
827
828
829\section{Mapping Protocol \label{mapping}}
830
831\begin{cfuncdesc}{int}{PyMapping_Check}{PyObject *o}
832 Return \code{1} if the object provides mapping protocol, and
833 \code{0} otherwise. This function always succeeds.
834\end{cfuncdesc}
835
836
837\begin{cfuncdesc}{int}{PyMapping_Length}{PyObject *o}
838 Returns the number of keys in object \var{o} on success, and
839 \code{-1} on failure. For objects that do not provide mapping
840 protocol, this is equivalent to the Python expression
841 \samp{len(\var{o})}.\bifuncindex{len}
842\end{cfuncdesc}
843
844
845\begin{cfuncdesc}{int}{PyMapping_DelItemString}{PyObject *o, char *key}
846 Remove the mapping for object \var{key} from the object \var{o}.
847 Return \code{-1} on failure. This is equivalent to the Python
848 statement \samp{del \var{o}[\var{key}]}.
849\end{cfuncdesc}
850
851
852\begin{cfuncdesc}{int}{PyMapping_DelItem}{PyObject *o, PyObject *key}
853 Remove the mapping for object \var{key} from the object \var{o}.
854 Return \code{-1} on failure. This is equivalent to the Python
855 statement \samp{del \var{o}[\var{key}]}.
856\end{cfuncdesc}
857
858
859\begin{cfuncdesc}{int}{PyMapping_HasKeyString}{PyObject *o, char *key}
860 On success, return \code{1} if the mapping object has the key
861 \var{key} and \code{0} otherwise. This is equivalent to the Python
862 expression \samp{\var{o}.has_key(\var{key})}. This function always
863 succeeds.
864\end{cfuncdesc}
865
866
867\begin{cfuncdesc}{int}{PyMapping_HasKey}{PyObject *o, PyObject *key}
868 Return \code{1} if the mapping object has the key \var{key} and
869 \code{0} otherwise. This is equivalent to the Python expression
870 \samp{\var{o}.has_key(\var{key})}. This function always succeeds.
871\end{cfuncdesc}
872
873
874\begin{cfuncdesc}{PyObject*}{PyMapping_Keys}{PyObject *o}
875 On success, return a list of the keys in object \var{o}. On
876 failure, return \NULL. This is equivalent to the Python expression
877 \samp{\var{o}.keys()}.
878\end{cfuncdesc}
879
880
881\begin{cfuncdesc}{PyObject*}{PyMapping_Values}{PyObject *o}
882 On success, return a list of the values in object \var{o}. On
883 failure, return \NULL. This is equivalent to the Python expression
884 \samp{\var{o}.values()}.
885\end{cfuncdesc}
886
887
888\begin{cfuncdesc}{PyObject*}{PyMapping_Items}{PyObject *o}
889 On success, return a list of the items in object \var{o}, where each
890 item is a tuple containing a key-value pair. On failure, return
891 \NULL. This is equivalent to the Python expression
892 \samp{\var{o}.items()}.
893\end{cfuncdesc}
894
895
896\begin{cfuncdesc}{PyObject*}{PyMapping_GetItemString}{PyObject *o, char *key}
897 Return element of \var{o} corresponding to the object \var{key} or
898 \NULL{} on failure. This is the equivalent of the Python expression
899 \samp{\var{o}[\var{key}]}.
900\end{cfuncdesc}
901
902\begin{cfuncdesc}{int}{PyMapping_SetItemString}{PyObject *o, char *key,
903 PyObject *v}
904 Map the object \var{key} to the value \var{v} in object \var{o}.
905 Returns \code{-1} on failure. This is the equivalent of the Python
906 statement \samp{\var{o}[\var{key}] = \var{v}}.
907\end{cfuncdesc}
908
909
910\section{Iterator Protocol \label{iterator}}
911
912\versionadded{2.2}
913
914There are only a couple of functions specifically for working with
915iterators.
916
917\begin{cfuncdesc}{int}{PyIter_Check}{PyObject *o}
918 Return true if the object \var{o} supports the iterator protocol.
919\end{cfuncdesc}
920
921\begin{cfuncdesc}{PyObject*}{PyIter_Next}{PyObject *o}
922 Return the next value from the iteration \var{o}. If the object is
923 an iterator, this retrieves the next value from the iteration, and
924 returns \NULL{} with no exception set if there are no remaining
925 items. If the object is not an iterator, \exception{TypeError} is
926 raised, or if there is an error in retrieving the item, returns
927 \NULL{} and passes along the exception.
928\end{cfuncdesc}
929
930To write a loop which iterates over an iterator, the C code should
931look something like this:
932
933\begin{verbatim}
Fred Drake314bae52002-03-11 18:46:29 +0000934PyObject *iterator = PyObject_GetIter(obj);
Fred Drake3adf79e2001-10-12 19:01:43 +0000935PyObject *item;
936
Fred Drake314bae52002-03-11 18:46:29 +0000937if (iterator == NULL) {
938 /* propagate error */
939}
940
941while (item = PyIter_Next(iterator)) {
Fred Drake3adf79e2001-10-12 19:01:43 +0000942 /* do something with item */
Fred Drake8b8fe282001-12-26 16:53:48 +0000943 ...
944 /* release reference when done */
945 Py_DECREF(item);
Fred Drake3adf79e2001-10-12 19:01:43 +0000946}
Fred Drake314bae52002-03-11 18:46:29 +0000947
948Py_DECREF(iterator);
949
Fred Drake3adf79e2001-10-12 19:01:43 +0000950if (PyErr_Occurred()) {
Fred Drake314bae52002-03-11 18:46:29 +0000951 /* propagate error */
Fred Drake3adf79e2001-10-12 19:01:43 +0000952}
953else {
954 /* continue doing useful work */
955}
Fred Drake8b8fe282001-12-26 16:53:48 +0000956\end{verbatim}
957
Jeremy Hylton89c3a222001-11-09 21:59:42 +0000958
Fred Drake94ead572001-11-09 23:34:26 +0000959\section{Buffer Protocol \label{abstract-buffer}}
Jeremy Hylton89c3a222001-11-09 21:59:42 +0000960
961\begin{cfuncdesc}{int}{PyObject_AsCharBuffer}{PyObject *obj,
Fred Drake94ead572001-11-09 23:34:26 +0000962 const char **buffer,
963 int *buffer_len}
Jeremy Hylton89c3a222001-11-09 21:59:42 +0000964 Returns a pointer to a read-only memory location useable as character-
965 based input. The \var{obj} argument must support the single-segment
Fred Drake243ea712002-04-04 04:10:36 +0000966 character buffer interface. On success, returns \code{0}, sets
Thomas Hellerbfeeeee2001-11-12 07:46:31 +0000967 \var{buffer} to the memory location and \var{buffer_len} to the buffer
Fred Drake243ea712002-04-04 04:10:36 +0000968 length. Returns \code{-1} and sets a \exception{TypeError} on error.
Fred Drake94ead572001-11-09 23:34:26 +0000969 \versionadded{1.6}
Jeremy Hylton89c3a222001-11-09 21:59:42 +0000970\end{cfuncdesc}
971
972\begin{cfuncdesc}{int}{PyObject_AsReadBuffer}{PyObject *obj,
Fred Drake94ead572001-11-09 23:34:26 +0000973 const char **buffer,
974 int *buffer_len}
Jeremy Hylton89c3a222001-11-09 21:59:42 +0000975 Returns a pointer to a read-only memory location containing
976 arbitrary data. The \var{obj} argument must support the
977 single-segment readable buffer interface. On success, returns
Fred Drake243ea712002-04-04 04:10:36 +0000978 \code{0}, sets \var{buffer} to the memory location and \var{buffer_len}
979 to the buffer length. Returns \code{-1} and sets a
Jeremy Hylton89c3a222001-11-09 21:59:42 +0000980 \exception{TypeError} on error.
Fred Drake94ead572001-11-09 23:34:26 +0000981 \versionadded{1.6}
Jeremy Hylton89c3a222001-11-09 21:59:42 +0000982\end{cfuncdesc}
983
984\begin{cfuncdesc}{int}{PyObject_CheckReadBuffer}{PyObject *o}
985 Returns \code{1} if \var{o} supports the single-segment readable
986 buffer interface. Otherwise returns \code{0}.
Fred Drake94ead572001-11-09 23:34:26 +0000987 \versionadded{2.2}
Fred Drake8b8fe282001-12-26 16:53:48 +0000988\end{cfuncdesc}
Jeremy Hylton89c3a222001-11-09 21:59:42 +0000989
990\begin{cfuncdesc}{int}{PyObject_AsWriteBuffer}{PyObject *obj,
Raymond Hettinger51306902002-09-08 04:39:28 +0000991 char **buffer,
Fred Drake94ead572001-11-09 23:34:26 +0000992 int *buffer_len}
Jeremy Hylton89c3a222001-11-09 21:59:42 +0000993 Returns a pointer to a writeable memory location. The \var{obj}
994 argument must support the single-segment, character buffer
Fred Drake243ea712002-04-04 04:10:36 +0000995 interface. On success, returns \code{0}, sets \var{buffer} to the
Thomas Hellerbfeeeee2001-11-12 07:46:31 +0000996 memory location and \var{buffer_len} to the buffer length. Returns
Fred Drake243ea712002-04-04 04:10:36 +0000997 \code{-1} and sets a \exception{TypeError} on error.
Fred Drake94ead572001-11-09 23:34:26 +0000998 \versionadded{1.6}
Jeremy Hylton89c3a222001-11-09 21:59:42 +0000999\end{cfuncdesc}