blob: 24de2f103cfb92dc97b68b041a4268caf59f7fde [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,
85 PyObject *o2, int op}
86 Compare the values of \var{o1} and \var{o2} using the operation
87 specified by \var{op}, which must be one of
88 \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
100 \samp{\var{o1} \emph{op} \var{o2}}, where \emph{op} is the operator
101 corresponding to \var{op}. Returns the value of the comparison on
102 success, or \NULL{} on failure.
103\end{cfuncdesc}
104
105\begin{cfuncdesc}{int}{PyObject_RichCompareBool}{PyObject *o1,
106 PyObject *o2, int op}
107 Compare the values of \var{o1} and \var{o2} using the operation
108 specified by \var{op}, which must be one of
109 \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
122 Python expression \samp{\var{o1} \emph{op} \var{o2}}, where
123 \emph{op} is the operator corresponding to \var{op}.
124\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
169 \samp{unistr(\var{o})}. Called by the
Tim Peters1fc240e2001-10-26 05:06:50 +0000170 \function{unistr()}\bifuncindex{unistr} 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
653 \NULL{} on failure. This is the equivalent of the Python expression
654 \samp{int(\var{o})}.\bifuncindex{int}
655\end{cfuncdesc}
656
657\begin{cfuncdesc}{PyObject*}{PyNumber_Long}{PyObject *o}
658 Returns the \var{o} converted to a long integer object on success,
659 or \NULL{} on failure. This is the equivalent of the Python
660 expression \samp{long(\var{o})}.\bifuncindex{long}
661\end{cfuncdesc}
662
663\begin{cfuncdesc}{PyObject*}{PyNumber_Float}{PyObject *o}
664 Returns the \var{o} converted to a float object on success, or
665 \NULL{} on failure. This is the equivalent of the Python expression
666 \samp{float(\var{o})}.\bifuncindex{float}
667\end{cfuncdesc}
668
669
670\section{Sequence Protocol \label{sequence}}
671
672\begin{cfuncdesc}{int}{PySequence_Check}{PyObject *o}
673 Return \code{1} if the object provides sequence protocol, and
674 \code{0} otherwise. This function always succeeds.
675\end{cfuncdesc}
676
677\begin{cfuncdesc}{int}{PySequence_Size}{PyObject *o}
678 Returns the number of objects in sequence \var{o} on success, and
679 \code{-1} on failure. For objects that do not provide sequence
680 protocol, this is equivalent to the Python expression
681 \samp{len(\var{o})}.\bifuncindex{len}
682\end{cfuncdesc}
683
684\begin{cfuncdesc}{int}{PySequence_Length}{PyObject *o}
685 Alternate name for \cfunction{PySequence_Size()}.
686\end{cfuncdesc}
687
688\begin{cfuncdesc}{PyObject*}{PySequence_Concat}{PyObject *o1, PyObject *o2}
689 Return the concatenation of \var{o1} and \var{o2} on success, and
690 \NULL{} on failure. This is the equivalent of the Python
691 expression \samp{\var{o1} + \var{o2}}.
692\end{cfuncdesc}
693
694
695\begin{cfuncdesc}{PyObject*}{PySequence_Repeat}{PyObject *o, int count}
696 Return the result of repeating sequence object \var{o} \var{count}
697 times, or \NULL{} on failure. This is the equivalent of the Python
698 expression \samp{\var{o} * \var{count}}.
699\end{cfuncdesc}
700
701\begin{cfuncdesc}{PyObject*}{PySequence_InPlaceConcat}{PyObject *o1,
702 PyObject *o2}
703 Return the concatenation of \var{o1} and \var{o2} on success, and
704 \NULL{} on failure. The operation is done \emph{in-place} when
705 \var{o1} supports it. This is the equivalent of the Python
706 expression \samp{\var{o1} += \var{o2}}.
707\end{cfuncdesc}
708
709
710\begin{cfuncdesc}{PyObject*}{PySequence_InPlaceRepeat}{PyObject *o, int count}
711 Return the result of repeating sequence object \var{o} \var{count}
712 times, or \NULL{} on failure. The operation is done \emph{in-place}
713 when \var{o} supports it. This is the equivalent of the Python
714 expression \samp{\var{o} *= \var{count}}.
715\end{cfuncdesc}
716
717
718\begin{cfuncdesc}{PyObject*}{PySequence_GetItem}{PyObject *o, int i}
719 Return the \var{i}th element of \var{o}, or \NULL{} on failure.
720 This is the equivalent of the Python expression
721 \samp{\var{o}[\var{i}]}.
722\end{cfuncdesc}
723
724
725\begin{cfuncdesc}{PyObject*}{PySequence_GetSlice}{PyObject *o, int i1, int i2}
726 Return the slice of sequence object \var{o} between \var{i1} and
727 \var{i2}, or \NULL{} on failure. This is the equivalent of the
728 Python expression \samp{\var{o}[\var{i1}:\var{i2}]}.
729\end{cfuncdesc}
730
731
732\begin{cfuncdesc}{int}{PySequence_SetItem}{PyObject *o, int i, PyObject *v}
733 Assign object \var{v} to the \var{i}th element of \var{o}. Returns
734 \code{-1} on failure. This is the equivalent of the Python
735 statement \samp{\var{o}[\var{i}] = \var{v}}.
736\end{cfuncdesc}
737
738\begin{cfuncdesc}{int}{PySequence_DelItem}{PyObject *o, int i}
739 Delete the \var{i}th element of object \var{o}. Returns \code{-1}
740 on failure. This is the equivalent of the Python statement
741 \samp{del \var{o}[\var{i}]}.
742\end{cfuncdesc}
743
744\begin{cfuncdesc}{int}{PySequence_SetSlice}{PyObject *o, int i1,
745 int i2, PyObject *v}
746 Assign the sequence object \var{v} to the slice in sequence object
747 \var{o} from \var{i1} to \var{i2}. This is the equivalent of the
748 Python statement \samp{\var{o}[\var{i1}:\var{i2}] = \var{v}}.
749\end{cfuncdesc}
750
751\begin{cfuncdesc}{int}{PySequence_DelSlice}{PyObject *o, int i1, int i2}
752 Delete the slice in sequence object \var{o} from \var{i1} to
753 \var{i2}. Returns \code{-1} on failure. This is the equivalent of
754 the Python statement \samp{del \var{o}[\var{i1}:\var{i2}]}.
755\end{cfuncdesc}
756
757\begin{cfuncdesc}{PyObject*}{PySequence_Tuple}{PyObject *o}
758 Returns the \var{o} as a tuple on success, and \NULL{} on failure.
759 This is equivalent to the Python expression \samp{tuple(\var{o})}.
760 \bifuncindex{tuple}
761\end{cfuncdesc}
762
763\begin{cfuncdesc}{int}{PySequence_Count}{PyObject *o, PyObject *value}
764 Return the number of occurrences of \var{value} in \var{o}, that is,
765 return the number of keys for which \code{\var{o}[\var{key}] ==
766 \var{value}}. On failure, return \code{-1}. This is equivalent to
767 the Python expression \samp{\var{o}.count(\var{value})}.
768\end{cfuncdesc}
769
770\begin{cfuncdesc}{int}{PySequence_Contains}{PyObject *o, PyObject *value}
771 Determine if \var{o} contains \var{value}. If an item in \var{o} is
772 equal to \var{value}, return \code{1}, otherwise return \code{0}.
773 On error, return \code{-1}. This is equivalent to the Python
774 expression \samp{\var{value} in \var{o}}.
775\end{cfuncdesc}
776
777\begin{cfuncdesc}{int}{PySequence_Index}{PyObject *o, PyObject *value}
778 Return the first index \var{i} for which \code{\var{o}[\var{i}] ==
779 \var{value}}. On error, return \code{-1}. This is equivalent to
780 the Python expression \samp{\var{o}.index(\var{value})}.
781\end{cfuncdesc}
782
783\begin{cfuncdesc}{PyObject*}{PySequence_List}{PyObject *o}
784 Return a list object with the same contents as the arbitrary
785 sequence \var{o}. The returned list is guaranteed to be new.
786\end{cfuncdesc}
787
788\begin{cfuncdesc}{PyObject*}{PySequence_Tuple}{PyObject *o}
789 Return a tuple object with the same contents as the arbitrary
790 sequence \var{o}. If \var{o} is a tuple, a new reference will be
791 returned, otherwise a tuple will be constructed with the appropriate
792 contents.
793\end{cfuncdesc}
794
795\begin{cfuncdesc}{PyObject*}{PySequence_Fast}{PyObject *o, const char *m}
796 Returns the sequence \var{o} as a tuple, unless it is already a
797 tuple or list, in which case \var{o} is returned. Use
798 \cfunction{PySequence_Fast_GET_ITEM()} to access the members of the
799 result. Returns \NULL{} on failure. If the object is not a
800 sequence, raises \exception{TypeError} with \var{m} as the message
801 text.
802\end{cfuncdesc}
803
804\begin{cfuncdesc}{PyObject*}{PySequence_Fast_GET_ITEM}{PyObject *o, int i}
805 Return the \var{i}th element of \var{o}, assuming that \var{o} was
Fred Drakec37b65e2001-11-28 07:26:15 +0000806 returned by \cfunction{PySequence_Fast()}, \var{o} is not \NULL,
Tim Peters1fc240e2001-10-26 05:06:50 +0000807 and that \var{i} is within bounds.
808\end{cfuncdesc}
809
Martin v. Löwis01f94bd2002-05-08 08:44:21 +0000810\begin{cfuncdesc}{PyObject*}{PySequence_ITEM}{PyObject *o, int i}
811 Return the \var{i}th element of \var{o} or \NULL on failure.
812 Macro form of \cfunction{PySequence_GetItem()} but without checking
813 that \cfunction{PySequence_Check(\var{o})} is true and without
Fred Drake86228e42002-05-23 16:02:28 +0000814 adjustment for negative indices.
815 \versionadded{2.3}
Martin v. Löwis01f94bd2002-05-08 08:44:21 +0000816\end{cfuncdesc}
817
Tim Peters1fc240e2001-10-26 05:06:50 +0000818\begin{cfuncdesc}{int}{PySequence_Fast_GET_SIZE}{PyObject *o}
819 Returns the length of \var{o}, assuming that \var{o} was
820 returned by \cfunction{PySequence_Fast()} and that \var{o} is
Fred Drakec37b65e2001-11-28 07:26:15 +0000821 not \NULL. The size can also be gotten by calling
Tim Peters1fc240e2001-10-26 05:06:50 +0000822 \cfunction{PySequence_Size()} on \var{o}, but
823 \cfunction{PySequence_Fast_GET_SIZE()} is faster because it can
824 assume \var{o} is a list or tuple.
Fred Drake3adf79e2001-10-12 19:01:43 +0000825\end{cfuncdesc}
826
827
828\section{Mapping Protocol \label{mapping}}
829
830\begin{cfuncdesc}{int}{PyMapping_Check}{PyObject *o}
831 Return \code{1} if the object provides mapping protocol, and
832 \code{0} otherwise. This function always succeeds.
833\end{cfuncdesc}
834
835
836\begin{cfuncdesc}{int}{PyMapping_Length}{PyObject *o}
837 Returns the number of keys in object \var{o} on success, and
838 \code{-1} on failure. For objects that do not provide mapping
839 protocol, this is equivalent to the Python expression
840 \samp{len(\var{o})}.\bifuncindex{len}
841\end{cfuncdesc}
842
843
844\begin{cfuncdesc}{int}{PyMapping_DelItemString}{PyObject *o, char *key}
845 Remove the mapping for object \var{key} from the object \var{o}.
846 Return \code{-1} on failure. This is equivalent to the Python
847 statement \samp{del \var{o}[\var{key}]}.
848\end{cfuncdesc}
849
850
851\begin{cfuncdesc}{int}{PyMapping_DelItem}{PyObject *o, PyObject *key}
852 Remove the mapping for object \var{key} from the object \var{o}.
853 Return \code{-1} on failure. This is equivalent to the Python
854 statement \samp{del \var{o}[\var{key}]}.
855\end{cfuncdesc}
856
857
858\begin{cfuncdesc}{int}{PyMapping_HasKeyString}{PyObject *o, char *key}
859 On success, return \code{1} if the mapping object has the key
860 \var{key} and \code{0} otherwise. This is equivalent to the Python
861 expression \samp{\var{o}.has_key(\var{key})}. This function always
862 succeeds.
863\end{cfuncdesc}
864
865
866\begin{cfuncdesc}{int}{PyMapping_HasKey}{PyObject *o, PyObject *key}
867 Return \code{1} if the mapping object has the key \var{key} and
868 \code{0} otherwise. This is equivalent to the Python expression
869 \samp{\var{o}.has_key(\var{key})}. This function always succeeds.
870\end{cfuncdesc}
871
872
873\begin{cfuncdesc}{PyObject*}{PyMapping_Keys}{PyObject *o}
874 On success, return a list of the keys in object \var{o}. On
875 failure, return \NULL. This is equivalent to the Python expression
876 \samp{\var{o}.keys()}.
877\end{cfuncdesc}
878
879
880\begin{cfuncdesc}{PyObject*}{PyMapping_Values}{PyObject *o}
881 On success, return a list of the values in object \var{o}. On
882 failure, return \NULL. This is equivalent to the Python expression
883 \samp{\var{o}.values()}.
884\end{cfuncdesc}
885
886
887\begin{cfuncdesc}{PyObject*}{PyMapping_Items}{PyObject *o}
888 On success, return a list of the items in object \var{o}, where each
889 item is a tuple containing a key-value pair. On failure, return
890 \NULL. This is equivalent to the Python expression
891 \samp{\var{o}.items()}.
892\end{cfuncdesc}
893
894
895\begin{cfuncdesc}{PyObject*}{PyMapping_GetItemString}{PyObject *o, char *key}
896 Return element of \var{o} corresponding to the object \var{key} or
897 \NULL{} on failure. This is the equivalent of the Python expression
898 \samp{\var{o}[\var{key}]}.
899\end{cfuncdesc}
900
901\begin{cfuncdesc}{int}{PyMapping_SetItemString}{PyObject *o, char *key,
902 PyObject *v}
903 Map the object \var{key} to the value \var{v} in object \var{o}.
904 Returns \code{-1} on failure. This is the equivalent of the Python
905 statement \samp{\var{o}[\var{key}] = \var{v}}.
906\end{cfuncdesc}
907
908
909\section{Iterator Protocol \label{iterator}}
910
911\versionadded{2.2}
912
913There are only a couple of functions specifically for working with
914iterators.
915
916\begin{cfuncdesc}{int}{PyIter_Check}{PyObject *o}
917 Return true if the object \var{o} supports the iterator protocol.
918\end{cfuncdesc}
919
920\begin{cfuncdesc}{PyObject*}{PyIter_Next}{PyObject *o}
921 Return the next value from the iteration \var{o}. If the object is
922 an iterator, this retrieves the next value from the iteration, and
923 returns \NULL{} with no exception set if there are no remaining
924 items. If the object is not an iterator, \exception{TypeError} is
925 raised, or if there is an error in retrieving the item, returns
926 \NULL{} and passes along the exception.
927\end{cfuncdesc}
928
929To write a loop which iterates over an iterator, the C code should
930look something like this:
931
932\begin{verbatim}
Fred Drake314bae52002-03-11 18:46:29 +0000933PyObject *iterator = PyObject_GetIter(obj);
Fred Drake3adf79e2001-10-12 19:01:43 +0000934PyObject *item;
935
Fred Drake314bae52002-03-11 18:46:29 +0000936if (iterator == NULL) {
937 /* propagate error */
938}
939
940while (item = PyIter_Next(iterator)) {
Fred Drake3adf79e2001-10-12 19:01:43 +0000941 /* do something with item */
Fred Drake8b8fe282001-12-26 16:53:48 +0000942 ...
943 /* release reference when done */
944 Py_DECREF(item);
Fred Drake3adf79e2001-10-12 19:01:43 +0000945}
Fred Drake314bae52002-03-11 18:46:29 +0000946
947Py_DECREF(iterator);
948
Fred Drake3adf79e2001-10-12 19:01:43 +0000949if (PyErr_Occurred()) {
Fred Drake314bae52002-03-11 18:46:29 +0000950 /* propagate error */
Fred Drake3adf79e2001-10-12 19:01:43 +0000951}
952else {
953 /* continue doing useful work */
954}
Fred Drake8b8fe282001-12-26 16:53:48 +0000955\end{verbatim}
956
Jeremy Hylton89c3a222001-11-09 21:59:42 +0000957
Fred Drake94ead572001-11-09 23:34:26 +0000958\section{Buffer Protocol \label{abstract-buffer}}
Jeremy Hylton89c3a222001-11-09 21:59:42 +0000959
960\begin{cfuncdesc}{int}{PyObject_AsCharBuffer}{PyObject *obj,
Fred Drake94ead572001-11-09 23:34:26 +0000961 const char **buffer,
962 int *buffer_len}
Jeremy Hylton89c3a222001-11-09 21:59:42 +0000963 Returns a pointer to a read-only memory location useable as character-
964 based input. The \var{obj} argument must support the single-segment
Fred Drake243ea712002-04-04 04:10:36 +0000965 character buffer interface. On success, returns \code{0}, sets
Thomas Hellerbfeeeee2001-11-12 07:46:31 +0000966 \var{buffer} to the memory location and \var{buffer_len} to the buffer
Fred Drake243ea712002-04-04 04:10:36 +0000967 length. Returns \code{-1} and sets a \exception{TypeError} on error.
Fred Drake94ead572001-11-09 23:34:26 +0000968 \versionadded{1.6}
Jeremy Hylton89c3a222001-11-09 21:59:42 +0000969\end{cfuncdesc}
970
971\begin{cfuncdesc}{int}{PyObject_AsReadBuffer}{PyObject *obj,
Fred Drake94ead572001-11-09 23:34:26 +0000972 const char **buffer,
973 int *buffer_len}
Jeremy Hylton89c3a222001-11-09 21:59:42 +0000974 Returns a pointer to a read-only memory location containing
975 arbitrary data. The \var{obj} argument must support the
976 single-segment readable buffer interface. On success, returns
Fred Drake243ea712002-04-04 04:10:36 +0000977 \code{0}, sets \var{buffer} to the memory location and \var{buffer_len}
978 to the buffer length. Returns \code{-1} and sets a
Jeremy Hylton89c3a222001-11-09 21:59:42 +0000979 \exception{TypeError} on error.
Fred Drake94ead572001-11-09 23:34:26 +0000980 \versionadded{1.6}
Jeremy Hylton89c3a222001-11-09 21:59:42 +0000981\end{cfuncdesc}
982
983\begin{cfuncdesc}{int}{PyObject_CheckReadBuffer}{PyObject *o}
984 Returns \code{1} if \var{o} supports the single-segment readable
985 buffer interface. Otherwise returns \code{0}.
Fred Drake94ead572001-11-09 23:34:26 +0000986 \versionadded{2.2}
Fred Drake8b8fe282001-12-26 16:53:48 +0000987\end{cfuncdesc}
Jeremy Hylton89c3a222001-11-09 21:59:42 +0000988
989\begin{cfuncdesc}{int}{PyObject_AsWriteBuffer}{PyObject *obj,
Fred Drake94ead572001-11-09 23:34:26 +0000990 const char **buffer,
991 int *buffer_len}
Jeremy Hylton89c3a222001-11-09 21:59:42 +0000992 Returns a pointer to a writeable memory location. The \var{obj}
993 argument must support the single-segment, character buffer
Fred Drake243ea712002-04-04 04:10:36 +0000994 interface. On success, returns \code{0}, sets \var{buffer} to the
Thomas Hellerbfeeeee2001-11-12 07:46:31 +0000995 memory location and \var{buffer_len} to the buffer length. Returns
Fred Drake243ea712002-04-04 04:10:36 +0000996 \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}