blob: e4f299d0fbacb5c27d1bdaeb174a6b36cc6e0bbb [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
84\begin{cfuncdesc}{int}{PyObject_Cmp}{PyObject *o1, PyObject *o2, int *result}
85 Compare the values of \var{o1} and \var{o2} using a routine provided
86 by \var{o1}, if one exists, otherwise with a routine provided by
87 \var{o2}. The result of the comparison is returned in
88 \var{result}. Returns \code{-1} on failure. This is the equivalent
89 of the Python statement\bifuncindex{cmp} \samp{\var{result} =
90 cmp(\var{o1}, \var{o2})}.
91\end{cfuncdesc}
92
93
94\begin{cfuncdesc}{int}{PyObject_Compare}{PyObject *o1, PyObject *o2}
95 Compare the values of \var{o1} and \var{o2} using a routine provided
96 by \var{o1}, if one exists, otherwise with a routine provided by
97 \var{o2}. Returns the result of the comparison on success. On
98 error, the value returned is undefined; use
99 \cfunction{PyErr_Occurred()} to detect an error. This is equivalent
100 to the Python expression\bifuncindex{cmp} \samp{cmp(\var{o1},
101 \var{o2})}.
102\end{cfuncdesc}
103
104
105\begin{cfuncdesc}{PyObject*}{PyObject_Repr}{PyObject *o}
106 Compute a string representation of object \var{o}. Returns the
107 string representation on success, \NULL{} on failure. This is the
108 equivalent of the Python expression \samp{repr(\var{o})}. Called by
109 the \function{repr()}\bifuncindex{repr} built-in function and by
110 reverse quotes.
111\end{cfuncdesc}
112
113
114\begin{cfuncdesc}{PyObject*}{PyObject_Str}{PyObject *o}
115 Compute a string representation of object \var{o}. Returns the
116 string representation on success, \NULL{} on failure. This is the
117 equivalent of the Python expression \samp{str(\var{o})}. Called by
118 the \function{str()}\bifuncindex{str} built-in function and by the
119 \keyword{print} statement.
120\end{cfuncdesc}
121
122
123\begin{cfuncdesc}{PyObject*}{PyObject_Unicode}{PyObject *o}
124 Compute a Unicode string representation of object \var{o}. Returns
125 the Unicode string representation on success, \NULL{} on failure.
126 This is the equivalent of the Python expression
127 \samp{unistr(\var{o})}. Called by the
Tim Peters1fc240e2001-10-26 05:06:50 +0000128 \function{unistr()}\bifuncindex{unistr} built-in function.
Fred Drake3adf79e2001-10-12 19:01:43 +0000129\end{cfuncdesc}
130
131\begin{cfuncdesc}{int}{PyObject_IsInstance}{PyObject *inst, PyObject *cls}
Fred Drakeb957bc32002-04-23 18:15:44 +0000132 Returns \code{1} if \var{inst} is an instance of the class \var{cls}
133 or a subclass of \var{cls}, or \code{0} if not. On error, returns
134 \code{-1} and sets an exception. If \var{cls} is a type object
135 rather than a class object, \cfunction{PyObject_IsInstance()}
136 returns \code{1} if \var{inst} is of type \var{cls}. If \var{inst}
137 is not a class instance and \var{cls} is neither a type object or
138 class object, \var{inst} must have a \member{__class__} attribute
139 --- the class relationship of the value of that attribute with
140 \var{cls} will be used to determine the result of this function.
Fred Drake3adf79e2001-10-12 19:01:43 +0000141 \versionadded{2.1}
142\end{cfuncdesc}
143
144Subclass determination is done in a fairly straightforward way, but
145includes a wrinkle that implementors of extensions to the class system
146may want to be aware of. If \class{A} and \class{B} are class
147objects, \class{B} is a subclass of \class{A} if it inherits from
148\class{A} either directly or indirectly. If either is not a class
149object, a more general mechanism is used to determine the class
150relationship of the two objects. When testing if \var{B} is a
151subclass of \var{A}, if \var{A} is \var{B},
152\cfunction{PyObject_IsSubclass()} returns true. If \var{A} and
153\var{B} are different objects, \var{B}'s \member{__bases__} attribute
154is searched in a depth-first fashion for \var{A} --- the presence of
155the \member{__bases__} attribute is considered sufficient for this
156determination.
157
158\begin{cfuncdesc}{int}{PyObject_IsSubclass}{PyObject *derived,
159 PyObject *cls}
160 Returns \code{1} if the class \var{derived} is identical to or
161 derived from the class \var{cls}, otherwise returns \code{0}. In
162 case of an error, returns \code{-1}. If either \var{derived} or
163 \var{cls} is not an actual class object, this function uses the
164 generic algorithm described above.
165 \versionadded{2.1}
166\end{cfuncdesc}
167
168
169\begin{cfuncdesc}{int}{PyCallable_Check}{PyObject *o}
170 Determine if the object \var{o} is callable. Return \code{1} if the
171 object is callable and \code{0} otherwise. This function always
172 succeeds.
173\end{cfuncdesc}
174
175
Fred Drake0e0b6182002-04-15 20:51:19 +0000176\begin{cfuncdesc}{PyObject*}{PyObject_Call}{PyObject *callable_object,
177 PyObject *args,
178 PyObject *kw}
179 Call a callable Python object \var{callable_object}, with arguments
180 given by the tuple \var{args}, and named arguments given by the
181 dictionary \var{kw}. If no named arguments are needed, \var{kw} may
182 be \NULL{}. \var{args} must not be \NULL{}, use an empty tuple if
183 no arguments are needed. Returns the result of the call on success,
184 or \NULL{} on failure. This is the equivalent of the Python
185 expression \samp{apply(\var{callable_object}, \var{args}, \var{kw})}
186 or \samp{\var{callable_object}(*\var{args}, **\var{kw})}.
187 \bifuncindex{apply}
188\end{cfuncdesc}
189
190
Fred Drake3adf79e2001-10-12 19:01:43 +0000191\begin{cfuncdesc}{PyObject*}{PyObject_CallObject}{PyObject *callable_object,
192 PyObject *args}
193 Call a callable Python object \var{callable_object}, with arguments
194 given by the tuple \var{args}. If no arguments are needed, then
195 \var{args} may be \NULL. Returns the result of the call on
196 success, or \NULL{} on failure. This is the equivalent of the
197 Python expression \samp{apply(\var{callable_object}, \var{args})} or
198 \samp{\var{callable_object}(*\var{args})}.
199 \bifuncindex{apply}
200\end{cfuncdesc}
201
Fred Drakec44e9ec2001-10-26 16:38:38 +0000202\begin{cfuncdesc}{PyObject*}{PyObject_CallFunction}{PyObject *callable,
203 char *format, \moreargs}
204 Call a callable Python object \var{callable}, with a variable
Fred Drake3adf79e2001-10-12 19:01:43 +0000205 number of C arguments. The C arguments are described using a
206 \cfunction{Py_BuildValue()} style format string. The format may be
207 \NULL, indicating that no arguments are provided. Returns the
208 result of the call on success, or \NULL{} on failure. This is the
Fred Drakec44e9ec2001-10-26 16:38:38 +0000209 equivalent of the Python expression \samp{apply(\var{callable},
210 \var{args})} or \samp{\var{callable}(*\var{args})}.
Fred Drake3adf79e2001-10-12 19:01:43 +0000211 \bifuncindex{apply}
212\end{cfuncdesc}
213
214
215\begin{cfuncdesc}{PyObject*}{PyObject_CallMethod}{PyObject *o,
Fred Drakec44e9ec2001-10-26 16:38:38 +0000216 char *method, char *format,
217 \moreargs}
Fred Drake3adf79e2001-10-12 19:01:43 +0000218 Call the method named \var{m} of object \var{o} with a variable
219 number of C arguments. The C arguments are described by a
220 \cfunction{Py_BuildValue()} format string. The format may be \NULL,
221 indicating that no arguments are provided. Returns the result of the
222 call on success, or \NULL{} on failure. This is the equivalent of
Fred Drakec44e9ec2001-10-26 16:38:38 +0000223 the Python expression \samp{\var{o}.\var{method}(\var{args})}.
224\end{cfuncdesc}
225
226
Fred Drakeb0c079e2001-10-28 02:39:03 +0000227\begin{cfuncdesc}{PyObject*}{PyObject_CallFunctionObjArgs}{PyObject *callable,
228 \moreargs,
229 \code{NULL}}
Fred Drakec44e9ec2001-10-26 16:38:38 +0000230 Call a callable Python object \var{callable}, with a variable
231 number of \ctype{PyObject*} arguments. The arguments are provided
232 as a variable number of parameters followed by \NULL.
233 Returns the result of the call on success, or \NULL{} on failure.
234 \versionadded{2.2}
235\end{cfuncdesc}
236
237
Fred Drakeb0c079e2001-10-28 02:39:03 +0000238\begin{cfuncdesc}{PyObject*}{PyObject_CallMethodObjArgs}{PyObject *o,
239 PyObject *name,
240 \moreargs,
241 \code{NULL}}
Fred Drakec44e9ec2001-10-26 16:38:38 +0000242 Calls a method of the object \var{o}, where the name of the method
243 is given as a Python string object in \var{name}. It is called with
244 a variable number of \ctype{PyObject*} arguments. The arguments are
245 provided as a variable number of parameters followed by \NULL.
246 Returns the result of the call on success, or \NULL{} on failure.
247 \versionadded{2.2}
Fred Drake3adf79e2001-10-12 19:01:43 +0000248\end{cfuncdesc}
249
250
251\begin{cfuncdesc}{int}{PyObject_Hash}{PyObject *o}
252 Compute and return the hash value of an object \var{o}. On failure,
253 return \code{-1}. This is the equivalent of the Python expression
254 \samp{hash(\var{o})}.\bifuncindex{hash}
255\end{cfuncdesc}
256
257
258\begin{cfuncdesc}{int}{PyObject_IsTrue}{PyObject *o}
259 Returns \code{1} if the object \var{o} is considered to be true, and
260 \code{0} otherwise. This is equivalent to the Python expression
261 \samp{not not \var{o}}. This function always succeeds.
262\end{cfuncdesc}
263
264
265\begin{cfuncdesc}{PyObject*}{PyObject_Type}{PyObject *o}
266 When \var{o} is non-\NULL, returns a type object corresponding to
267 the object type of object \var{o}. On failure, raises
268 \exception{SystemError} and returns \NULL. This is equivalent to
269 the Python expression \code{type(\var{o})}.
270 \bifuncindex{type}
271\end{cfuncdesc}
272
273\begin{cfuncdesc}{int}{PyObject_TypeCheck}{PyObject *o, PyTypeObject *type}
274 Return true if the object \var{o} is of type \var{type} or a subtype
275 of \var{type}. Both parameters must be non-\NULL.
276 \versionadded{2.2}
277\end{cfuncdesc}
278
279\begin{cfuncdesc}{int}{PyObject_Length}{PyObject *o}
Fred Drake0e0b6182002-04-15 20:51:19 +0000280\cfuncline{int}{PyObject_Size}{PyObject *o}
Fred Drake3adf79e2001-10-12 19:01:43 +0000281 Return the length of object \var{o}. If the object \var{o} provides
282 both sequence and mapping protocols, the sequence length is
283 returned. On error, \code{-1} is returned. This is the equivalent
284 to the Python expression \samp{len(\var{o})}.\bifuncindex{len}
285\end{cfuncdesc}
286
287
288\begin{cfuncdesc}{PyObject*}{PyObject_GetItem}{PyObject *o, PyObject *key}
289 Return element of \var{o} corresponding to the object \var{key} or
290 \NULL{} on failure. This is the equivalent of the Python expression
291 \samp{\var{o}[\var{key}]}.
292\end{cfuncdesc}
293
294
295\begin{cfuncdesc}{int}{PyObject_SetItem}{PyObject *o,
296 PyObject *key, PyObject *v}
297 Map the object \var{key} to the value \var{v}. Returns \code{-1} on
298 failure. This is the equivalent of the Python statement
299 \samp{\var{o}[\var{key}] = \var{v}}.
300\end{cfuncdesc}
301
302
303\begin{cfuncdesc}{int}{PyObject_DelItem}{PyObject *o, PyObject *key}
304 Delete the mapping for \var{key} from \var{o}. Returns \code{-1} on
305 failure. This is the equivalent of the Python statement \samp{del
306 \var{o}[\var{key}]}.
307\end{cfuncdesc}
308
309\begin{cfuncdesc}{int}{PyObject_AsFileDescriptor}{PyObject *o}
310 Derives a file-descriptor from a Python object. If the object is an
311 integer or long integer, its value is returned. If not, the
312 object's \method{fileno()} method is called if it exists; the method
313 must return an integer or long integer, which is returned as the
314 file descriptor value. Returns \code{-1} on failure.
315\end{cfuncdesc}
316
317\begin{cfuncdesc}{PyObject*}{PyObject_Dir}{PyObject *o}
318 This is equivalent to the Python expression \samp{dir(\var{o})},
319 returning a (possibly empty) list of strings appropriate for the
320 object argument, or \NULL{} if there was an error. If the argument
321 is \NULL, this is like the Python \samp{dir()}, returning the names
322 of the current locals; in this case, if no execution frame is active
323 then \NULL{} is returned but \cfunction{PyErr_Occurred()} will
324 return false.
325\end{cfuncdesc}
326
Fred Drake314bae52002-03-11 18:46:29 +0000327\begin{cfuncdesc}{PyObject*}{PyObject_GetIter}{PyObject *o}
328 This is equivalent to the Python expression \samp{iter(\var{o})}.
329 It returns a new iterator for the object argument, or the object
330 itself if the object is already an iterator. Raises
331 \exception{TypeError} and returns \NULL{} if the object cannot be
332 iterated.
333\end{cfuncdesc}
334
Fred Drake3adf79e2001-10-12 19:01:43 +0000335
336\section{Number Protocol \label{number}}
337
338\begin{cfuncdesc}{int}{PyNumber_Check}{PyObject *o}
339 Returns \code{1} if the object \var{o} provides numeric protocols,
340 and false otherwise. This function always succeeds.
341\end{cfuncdesc}
342
343
344\begin{cfuncdesc}{PyObject*}{PyNumber_Add}{PyObject *o1, PyObject *o2}
345 Returns the result of adding \var{o1} and \var{o2}, or \NULL{} on
346 failure. This is the equivalent of the Python expression
347 \samp{\var{o1} + \var{o2}}.
348\end{cfuncdesc}
349
350
351\begin{cfuncdesc}{PyObject*}{PyNumber_Subtract}{PyObject *o1, PyObject *o2}
352 Returns the result of subtracting \var{o2} from \var{o1}, or \NULL{}
353 on failure. This is the equivalent of the Python expression
354 \samp{\var{o1} - \var{o2}}.
355\end{cfuncdesc}
356
357
358\begin{cfuncdesc}{PyObject*}{PyNumber_Multiply}{PyObject *o1, PyObject *o2}
359 Returns the result of multiplying \var{o1} and \var{o2}, or \NULL{}
360 on failure. This is the equivalent of the Python expression
361 \samp{\var{o1} * \var{o2}}.
362\end{cfuncdesc}
363
364
365\begin{cfuncdesc}{PyObject*}{PyNumber_Divide}{PyObject *o1, PyObject *o2}
366 Returns the result of dividing \var{o1} by \var{o2}, or \NULL{} on
367 failure. This is the equivalent of the Python expression
368 \samp{\var{o1} / \var{o2}}.
369\end{cfuncdesc}
370
371
372\begin{cfuncdesc}{PyObject*}{PyNumber_FloorDivide}{PyObject *o1, PyObject *o2}
373 Return the floor of \var{o1} divided by \var{o2}, or \NULL{} on
374 failure. This is equivalent to the ``classic'' division of
375 integers.
376 \versionadded{2.2}
377\end{cfuncdesc}
378
379
380\begin{cfuncdesc}{PyObject*}{PyNumber_TrueDivide}{PyObject *o1, PyObject *o2}
381 Return a reasonable approximation for the mathematical value of
382 \var{o1} divided by \var{o2}, or \NULL{} on failure. The return
383 value is ``approximate'' because binary floating point numbers are
384 approximate; it is not possible to represent all real numbers in
385 base two. This function can return a floating point value when
386 passed two integers.
387 \versionadded{2.2}
388\end{cfuncdesc}
389
390
391\begin{cfuncdesc}{PyObject*}{PyNumber_Remainder}{PyObject *o1, PyObject *o2}
392 Returns the remainder of dividing \var{o1} by \var{o2}, or \NULL{}
393 on failure. This is the equivalent of the Python expression
394 \samp{\var{o1} \%\ \var{o2}}.
395\end{cfuncdesc}
396
397
398\begin{cfuncdesc}{PyObject*}{PyNumber_Divmod}{PyObject *o1, PyObject *o2}
399 See the built-in function \function{divmod()}\bifuncindex{divmod}.
400 Returns \NULL{} on failure. This is the equivalent of the Python
401 expression \samp{divmod(\var{o1}, \var{o2})}.
402\end{cfuncdesc}
403
404
405\begin{cfuncdesc}{PyObject*}{PyNumber_Power}{PyObject *o1,
406 PyObject *o2, PyObject *o3}
407 See the built-in function \function{pow()}\bifuncindex{pow}.
408 Returns \NULL{} on failure. This is the equivalent of the Python
409 expression \samp{pow(\var{o1}, \var{o2}, \var{o3})}, where \var{o3}
410 is optional. If \var{o3} is to be ignored, pass \cdata{Py_None} in
411 its place (passing \NULL{} for \var{o3} would cause an illegal
412 memory access).
413\end{cfuncdesc}
414
415
416\begin{cfuncdesc}{PyObject*}{PyNumber_Negative}{PyObject *o}
417 Returns the negation of \var{o} on success, or \NULL{} on failure.
418 This is the equivalent of the Python expression \samp{-\var{o}}.
419\end{cfuncdesc}
420
421
422\begin{cfuncdesc}{PyObject*}{PyNumber_Positive}{PyObject *o}
423 Returns \var{o} on success, or \NULL{} on failure. This is the
424 equivalent of the Python expression \samp{+\var{o}}.
425\end{cfuncdesc}
426
427
428\begin{cfuncdesc}{PyObject*}{PyNumber_Absolute}{PyObject *o}
429 Returns the absolute value of \var{o}, or \NULL{} on failure. This
430 is the equivalent of the Python expression \samp{abs(\var{o})}.
431 \bifuncindex{abs}
432\end{cfuncdesc}
433
434
435\begin{cfuncdesc}{PyObject*}{PyNumber_Invert}{PyObject *o}
436 Returns the bitwise negation of \var{o} on success, or \NULL{} on
437 failure. This is the equivalent of the Python expression
438 \samp{\~\var{o}}.
439\end{cfuncdesc}
440
441
442\begin{cfuncdesc}{PyObject*}{PyNumber_Lshift}{PyObject *o1, PyObject *o2}
443 Returns the result of left shifting \var{o1} by \var{o2} on success,
444 or \NULL{} on failure. This is the equivalent of the Python
445 expression \samp{\var{o1} <\code{<} \var{o2}}.
446\end{cfuncdesc}
447
448
449\begin{cfuncdesc}{PyObject*}{PyNumber_Rshift}{PyObject *o1, PyObject *o2}
450 Returns the result of right shifting \var{o1} by \var{o2} on
451 success, or \NULL{} on failure. This is the equivalent of the
452 Python expression \samp{\var{o1} >\code{>} \var{o2}}.
453\end{cfuncdesc}
454
455
456\begin{cfuncdesc}{PyObject*}{PyNumber_And}{PyObject *o1, PyObject *o2}
457 Returns the ``bitwise and'' of \var{o2} and \var{o2} on success and
458 \NULL{} on failure. This is the equivalent of the Python expression
459 \samp{\var{o1} \&\ \var{o2}}.
460\end{cfuncdesc}
461
462
463\begin{cfuncdesc}{PyObject*}{PyNumber_Xor}{PyObject *o1, PyObject *o2}
464 Returns the ``bitwise exclusive or'' of \var{o1} by \var{o2} on
465 success, or \NULL{} on failure. This is the equivalent of the
466 Python expression \samp{\var{o1} \textasciicircum{} \var{o2}}.
467\end{cfuncdesc}
468
469\begin{cfuncdesc}{PyObject*}{PyNumber_Or}{PyObject *o1, PyObject *o2}
470 Returns the ``bitwise or'' of \var{o1} and \var{o2} on success, or
471 \NULL{} on failure. This is the equivalent of the Python expression
472 \samp{\var{o1} | \var{o2}}.
473\end{cfuncdesc}
474
475
476\begin{cfuncdesc}{PyObject*}{PyNumber_InPlaceAdd}{PyObject *o1, PyObject *o2}
477 Returns the result of adding \var{o1} and \var{o2}, or \NULL{} on
478 failure. The operation is done \emph{in-place} when \var{o1}
479 supports it. This is the equivalent of the Python statement
480 \samp{\var{o1} += \var{o2}}.
481\end{cfuncdesc}
482
483
484\begin{cfuncdesc}{PyObject*}{PyNumber_InPlaceSubtract}{PyObject *o1,
485 PyObject *o2}
486 Returns the result of subtracting \var{o2} from \var{o1}, or \NULL{}
487 on failure. The operation is done \emph{in-place} when \var{o1}
488 supports it. This is the equivalent of the Python statement
489 \samp{\var{o1} -= \var{o2}}.
490\end{cfuncdesc}
491
492
493\begin{cfuncdesc}{PyObject*}{PyNumber_InPlaceMultiply}{PyObject *o1,
494 PyObject *o2}
495 Returns the result of multiplying \var{o1} and \var{o2}, or \NULL{}
496 on failure. The operation is done \emph{in-place} when \var{o1}
497 supports it. This is the equivalent of the Python statement
498 \samp{\var{o1} *= \var{o2}}.
499\end{cfuncdesc}
500
501
502\begin{cfuncdesc}{PyObject*}{PyNumber_InPlaceDivide}{PyObject *o1,
503 PyObject *o2}
504 Returns the result of dividing \var{o1} by \var{o2}, or \NULL{} on
505 failure. The operation is done \emph{in-place} when \var{o1}
506 supports it. This is the equivalent of the Python statement
507 \samp{\var{o1} /= \var{o2}}.
508\end{cfuncdesc}
509
510
511\begin{cfuncdesc}{PyObject*}{PyNumber_InPlaceFloorDivide}{PyObject *o1,
512 PyObject *o2}
513 Returns the mathematical of dividing \var{o1} by \var{o2}, or
514 \NULL{} on failure. The operation is done \emph{in-place} when
515 \var{o1} supports it. This is the equivalent of the Python
516 statement \samp{\var{o1} //= \var{o2}}.
517 \versionadded{2.2}
518\end{cfuncdesc}
519
520
521\begin{cfuncdesc}{PyObject*}{PyNumber_InPlaceTrueDivide}{PyObject *o1,
522 PyObject *o2}
523 Return a reasonable approximation for the mathematical value of
524 \var{o1} divided by \var{o2}, or \NULL{} on failure. The return
525 value is ``approximate'' because binary floating point numbers are
526 approximate; it is not possible to represent all real numbers in
527 base two. This function can return a floating point value when
528 passed two integers. The operation is done \emph{in-place} when
529 \var{o1} supports it.
530 \versionadded{2.2}
531\end{cfuncdesc}
532
533
534\begin{cfuncdesc}{PyObject*}{PyNumber_InPlaceRemainder}{PyObject *o1,
535 PyObject *o2}
536 Returns the remainder of dividing \var{o1} by \var{o2}, or \NULL{}
537 on failure. The operation is done \emph{in-place} when \var{o1}
538 supports it. This is the equivalent of the Python statement
539 \samp{\var{o1} \%= \var{o2}}.
540\end{cfuncdesc}
541
542
543\begin{cfuncdesc}{PyObject*}{PyNumber_InPlacePower}{PyObject *o1,
544 PyObject *o2, PyObject *o3}
545 See the built-in function \function{pow()}.\bifuncindex{pow}
546 Returns \NULL{} on failure. The operation is done \emph{in-place}
547 when \var{o1} supports it. This is the equivalent of the Python
548 statement \samp{\var{o1} **= \var{o2}} when o3 is \cdata{Py_None},
549 or an in-place variant of \samp{pow(\var{o1}, \var{o2}, \var{o3})}
550 otherwise. If \var{o3} is to be ignored, pass \cdata{Py_None} in its
551 place (passing \NULL{} for \var{o3} would cause an illegal memory
552 access).
553\end{cfuncdesc}
554
555\begin{cfuncdesc}{PyObject*}{PyNumber_InPlaceLshift}{PyObject *o1,
556 PyObject *o2}
557 Returns the result of left shifting \var{o1} by \var{o2} on success,
558 or \NULL{} on failure. The operation is done \emph{in-place} when
559 \var{o1} supports it. This is the equivalent of the Python
560 statement \samp{\var{o1} <\code{<=} \var{o2}}.
561\end{cfuncdesc}
562
563
564\begin{cfuncdesc}{PyObject*}{PyNumber_InPlaceRshift}{PyObject *o1,
565 PyObject *o2}
566 Returns the result of right shifting \var{o1} by \var{o2} on
567 success, or \NULL{} on failure. The operation is done
568 \emph{in-place} when \var{o1} supports it. This is the equivalent
569 of the Python statement \samp{\var{o1} >\code{>=} \var{o2}}.
570\end{cfuncdesc}
571
572
573\begin{cfuncdesc}{PyObject*}{PyNumber_InPlaceAnd}{PyObject *o1, PyObject *o2}
574 Returns the ``bitwise and'' of \var{o1} and \var{o2} on success and
575 \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\end{cfuncdesc}
579
580
581\begin{cfuncdesc}{PyObject*}{PyNumber_InPlaceXor}{PyObject *o1, PyObject *o2}
582 Returns the ``bitwise exclusive or'' of \var{o1} by \var{o2} on
583 success, or \NULL{} on failure. The operation is done
584 \emph{in-place} when \var{o1} supports it. This is the equivalent
585 of the Python statement \samp{\var{o1} \textasciicircum= \var{o2}}.
586\end{cfuncdesc}
587
588\begin{cfuncdesc}{PyObject*}{PyNumber_InPlaceOr}{PyObject *o1, PyObject *o2}
589 Returns the ``bitwise or'' of \var{o1} and \var{o2} on success, or
590 \NULL{} on failure. The operation is done \emph{in-place} when
591 \var{o1} supports it. This is the equivalent of the Python
592 statement \samp{\var{o1} |= \var{o2}}.
593\end{cfuncdesc}
594
595\begin{cfuncdesc}{int}{PyNumber_Coerce}{PyObject **p1, PyObject **p2}
596 This function takes the addresses of two variables of type
597 \ctype{PyObject*}. If the objects pointed to by \code{*\var{p1}}
598 and \code{*\var{p2}} have the same type, increment their reference
599 count and return \code{0} (success). If the objects can be converted
600 to a common numeric type, replace \code{*p1} and \code{*p2} by their
601 converted value (with 'new' reference counts), and return \code{0}.
602 If no conversion is possible, or if some other error occurs, return
603 \code{-1} (failure) and don't increment the reference counts. The
604 call \code{PyNumber_Coerce(\&o1, \&o2)} is equivalent to the Python
605 statement \samp{\var{o1}, \var{o2} = coerce(\var{o1}, \var{o2})}.
606 \bifuncindex{coerce}
607\end{cfuncdesc}
608
609\begin{cfuncdesc}{PyObject*}{PyNumber_Int}{PyObject *o}
610 Returns the \var{o} converted to an integer object on success, or
611 \NULL{} on failure. This is the equivalent of the Python expression
612 \samp{int(\var{o})}.\bifuncindex{int}
613\end{cfuncdesc}
614
615\begin{cfuncdesc}{PyObject*}{PyNumber_Long}{PyObject *o}
616 Returns the \var{o} converted to a long integer object on success,
617 or \NULL{} on failure. This is the equivalent of the Python
618 expression \samp{long(\var{o})}.\bifuncindex{long}
619\end{cfuncdesc}
620
621\begin{cfuncdesc}{PyObject*}{PyNumber_Float}{PyObject *o}
622 Returns the \var{o} converted to a float object on success, or
623 \NULL{} on failure. This is the equivalent of the Python expression
624 \samp{float(\var{o})}.\bifuncindex{float}
625\end{cfuncdesc}
626
627
628\section{Sequence Protocol \label{sequence}}
629
630\begin{cfuncdesc}{int}{PySequence_Check}{PyObject *o}
631 Return \code{1} if the object provides sequence protocol, and
632 \code{0} otherwise. This function always succeeds.
633\end{cfuncdesc}
634
635\begin{cfuncdesc}{int}{PySequence_Size}{PyObject *o}
636 Returns the number of objects in sequence \var{o} on success, and
637 \code{-1} on failure. For objects that do not provide sequence
638 protocol, this is equivalent to the Python expression
639 \samp{len(\var{o})}.\bifuncindex{len}
640\end{cfuncdesc}
641
642\begin{cfuncdesc}{int}{PySequence_Length}{PyObject *o}
643 Alternate name for \cfunction{PySequence_Size()}.
644\end{cfuncdesc}
645
646\begin{cfuncdesc}{PyObject*}{PySequence_Concat}{PyObject *o1, PyObject *o2}
647 Return the concatenation of \var{o1} and \var{o2} on success, and
648 \NULL{} on failure. This is the equivalent of the Python
649 expression \samp{\var{o1} + \var{o2}}.
650\end{cfuncdesc}
651
652
653\begin{cfuncdesc}{PyObject*}{PySequence_Repeat}{PyObject *o, int count}
654 Return the result of repeating sequence object \var{o} \var{count}
655 times, or \NULL{} on failure. This is the equivalent of the Python
656 expression \samp{\var{o} * \var{count}}.
657\end{cfuncdesc}
658
659\begin{cfuncdesc}{PyObject*}{PySequence_InPlaceConcat}{PyObject *o1,
660 PyObject *o2}
661 Return the concatenation of \var{o1} and \var{o2} on success, and
662 \NULL{} on failure. The operation is done \emph{in-place} when
663 \var{o1} supports it. This is the equivalent of the Python
664 expression \samp{\var{o1} += \var{o2}}.
665\end{cfuncdesc}
666
667
668\begin{cfuncdesc}{PyObject*}{PySequence_InPlaceRepeat}{PyObject *o, int count}
669 Return the result of repeating sequence object \var{o} \var{count}
670 times, or \NULL{} on failure. The operation is done \emph{in-place}
671 when \var{o} supports it. This is the equivalent of the Python
672 expression \samp{\var{o} *= \var{count}}.
673\end{cfuncdesc}
674
675
676\begin{cfuncdesc}{PyObject*}{PySequence_GetItem}{PyObject *o, int i}
677 Return the \var{i}th element of \var{o}, or \NULL{} on failure.
678 This is the equivalent of the Python expression
679 \samp{\var{o}[\var{i}]}.
680\end{cfuncdesc}
681
682
683\begin{cfuncdesc}{PyObject*}{PySequence_GetSlice}{PyObject *o, int i1, int i2}
684 Return the slice of sequence object \var{o} between \var{i1} and
685 \var{i2}, or \NULL{} on failure. This is the equivalent of the
686 Python expression \samp{\var{o}[\var{i1}:\var{i2}]}.
687\end{cfuncdesc}
688
689
690\begin{cfuncdesc}{int}{PySequence_SetItem}{PyObject *o, int i, PyObject *v}
691 Assign object \var{v} to the \var{i}th element of \var{o}. Returns
692 \code{-1} on failure. This is the equivalent of the Python
693 statement \samp{\var{o}[\var{i}] = \var{v}}.
694\end{cfuncdesc}
695
696\begin{cfuncdesc}{int}{PySequence_DelItem}{PyObject *o, int i}
697 Delete the \var{i}th element of object \var{o}. Returns \code{-1}
698 on failure. This is the equivalent of the Python statement
699 \samp{del \var{o}[\var{i}]}.
700\end{cfuncdesc}
701
702\begin{cfuncdesc}{int}{PySequence_SetSlice}{PyObject *o, int i1,
703 int i2, PyObject *v}
704 Assign the sequence object \var{v} to the slice in sequence object
705 \var{o} from \var{i1} to \var{i2}. This is the equivalent of the
706 Python statement \samp{\var{o}[\var{i1}:\var{i2}] = \var{v}}.
707\end{cfuncdesc}
708
709\begin{cfuncdesc}{int}{PySequence_DelSlice}{PyObject *o, int i1, int i2}
710 Delete the slice in sequence object \var{o} from \var{i1} to
711 \var{i2}. Returns \code{-1} on failure. This is the equivalent of
712 the Python statement \samp{del \var{o}[\var{i1}:\var{i2}]}.
713\end{cfuncdesc}
714
715\begin{cfuncdesc}{PyObject*}{PySequence_Tuple}{PyObject *o}
716 Returns the \var{o} as a tuple on success, and \NULL{} on failure.
717 This is equivalent to the Python expression \samp{tuple(\var{o})}.
718 \bifuncindex{tuple}
719\end{cfuncdesc}
720
721\begin{cfuncdesc}{int}{PySequence_Count}{PyObject *o, PyObject *value}
722 Return the number of occurrences of \var{value} in \var{o}, that is,
723 return the number of keys for which \code{\var{o}[\var{key}] ==
724 \var{value}}. On failure, return \code{-1}. This is equivalent to
725 the Python expression \samp{\var{o}.count(\var{value})}.
726\end{cfuncdesc}
727
728\begin{cfuncdesc}{int}{PySequence_Contains}{PyObject *o, PyObject *value}
729 Determine if \var{o} contains \var{value}. If an item in \var{o} is
730 equal to \var{value}, return \code{1}, otherwise return \code{0}.
731 On error, return \code{-1}. This is equivalent to the Python
732 expression \samp{\var{value} in \var{o}}.
733\end{cfuncdesc}
734
735\begin{cfuncdesc}{int}{PySequence_Index}{PyObject *o, PyObject *value}
736 Return the first index \var{i} for which \code{\var{o}[\var{i}] ==
737 \var{value}}. On error, return \code{-1}. This is equivalent to
738 the Python expression \samp{\var{o}.index(\var{value})}.
739\end{cfuncdesc}
740
741\begin{cfuncdesc}{PyObject*}{PySequence_List}{PyObject *o}
742 Return a list object with the same contents as the arbitrary
743 sequence \var{o}. The returned list is guaranteed to be new.
744\end{cfuncdesc}
745
746\begin{cfuncdesc}{PyObject*}{PySequence_Tuple}{PyObject *o}
747 Return a tuple object with the same contents as the arbitrary
748 sequence \var{o}. If \var{o} is a tuple, a new reference will be
749 returned, otherwise a tuple will be constructed with the appropriate
750 contents.
751\end{cfuncdesc}
752
753\begin{cfuncdesc}{PyObject*}{PySequence_Fast}{PyObject *o, const char *m}
754 Returns the sequence \var{o} as a tuple, unless it is already a
755 tuple or list, in which case \var{o} is returned. Use
756 \cfunction{PySequence_Fast_GET_ITEM()} to access the members of the
757 result. Returns \NULL{} on failure. If the object is not a
758 sequence, raises \exception{TypeError} with \var{m} as the message
759 text.
760\end{cfuncdesc}
761
762\begin{cfuncdesc}{PyObject*}{PySequence_Fast_GET_ITEM}{PyObject *o, int i}
763 Return the \var{i}th element of \var{o}, assuming that \var{o} was
Fred Drakec37b65e2001-11-28 07:26:15 +0000764 returned by \cfunction{PySequence_Fast()}, \var{o} is not \NULL,
Tim Peters1fc240e2001-10-26 05:06:50 +0000765 and that \var{i} is within bounds.
766\end{cfuncdesc}
767
Martin v. Löwis01f94bd2002-05-08 08:44:21 +0000768\begin{cfuncdesc}{PyObject*}{PySequence_ITEM}{PyObject *o, int i}
769 Return the \var{i}th element of \var{o} or \NULL on failure.
770 Macro form of \cfunction{PySequence_GetItem()} but without checking
771 that \cfunction{PySequence_Check(\var{o})} is true and without
772 adjustment for negative indices.
773\end{cfuncdesc}
774
Tim Peters1fc240e2001-10-26 05:06:50 +0000775\begin{cfuncdesc}{int}{PySequence_Fast_GET_SIZE}{PyObject *o}
776 Returns the length of \var{o}, assuming that \var{o} was
777 returned by \cfunction{PySequence_Fast()} and that \var{o} is
Fred Drakec37b65e2001-11-28 07:26:15 +0000778 not \NULL. The size can also be gotten by calling
Tim Peters1fc240e2001-10-26 05:06:50 +0000779 \cfunction{PySequence_Size()} on \var{o}, but
780 \cfunction{PySequence_Fast_GET_SIZE()} is faster because it can
781 assume \var{o} is a list or tuple.
Fred Drake3adf79e2001-10-12 19:01:43 +0000782\end{cfuncdesc}
783
784
785\section{Mapping Protocol \label{mapping}}
786
787\begin{cfuncdesc}{int}{PyMapping_Check}{PyObject *o}
788 Return \code{1} if the object provides mapping protocol, and
789 \code{0} otherwise. This function always succeeds.
790\end{cfuncdesc}
791
792
793\begin{cfuncdesc}{int}{PyMapping_Length}{PyObject *o}
794 Returns the number of keys in object \var{o} on success, and
795 \code{-1} on failure. For objects that do not provide mapping
796 protocol, this is equivalent to the Python expression
797 \samp{len(\var{o})}.\bifuncindex{len}
798\end{cfuncdesc}
799
800
801\begin{cfuncdesc}{int}{PyMapping_DelItemString}{PyObject *o, char *key}
802 Remove the mapping for object \var{key} from the object \var{o}.
803 Return \code{-1} on failure. This is equivalent to the Python
804 statement \samp{del \var{o}[\var{key}]}.
805\end{cfuncdesc}
806
807
808\begin{cfuncdesc}{int}{PyMapping_DelItem}{PyObject *o, PyObject *key}
809 Remove the mapping for object \var{key} from the object \var{o}.
810 Return \code{-1} on failure. This is equivalent to the Python
811 statement \samp{del \var{o}[\var{key}]}.
812\end{cfuncdesc}
813
814
815\begin{cfuncdesc}{int}{PyMapping_HasKeyString}{PyObject *o, char *key}
816 On success, return \code{1} if the mapping object has the key
817 \var{key} and \code{0} otherwise. This is equivalent to the Python
818 expression \samp{\var{o}.has_key(\var{key})}. This function always
819 succeeds.
820\end{cfuncdesc}
821
822
823\begin{cfuncdesc}{int}{PyMapping_HasKey}{PyObject *o, PyObject *key}
824 Return \code{1} if the mapping object has the key \var{key} and
825 \code{0} otherwise. This is equivalent to the Python expression
826 \samp{\var{o}.has_key(\var{key})}. This function always succeeds.
827\end{cfuncdesc}
828
829
830\begin{cfuncdesc}{PyObject*}{PyMapping_Keys}{PyObject *o}
831 On success, return a list of the keys in object \var{o}. On
832 failure, return \NULL. This is equivalent to the Python expression
833 \samp{\var{o}.keys()}.
834\end{cfuncdesc}
835
836
837\begin{cfuncdesc}{PyObject*}{PyMapping_Values}{PyObject *o}
838 On success, return a list of the values in object \var{o}. On
839 failure, return \NULL. This is equivalent to the Python expression
840 \samp{\var{o}.values()}.
841\end{cfuncdesc}
842
843
844\begin{cfuncdesc}{PyObject*}{PyMapping_Items}{PyObject *o}
845 On success, return a list of the items in object \var{o}, where each
846 item is a tuple containing a key-value pair. On failure, return
847 \NULL. This is equivalent to the Python expression
848 \samp{\var{o}.items()}.
849\end{cfuncdesc}
850
851
852\begin{cfuncdesc}{PyObject*}{PyMapping_GetItemString}{PyObject *o, char *key}
853 Return element of \var{o} corresponding to the object \var{key} or
854 \NULL{} on failure. This is the equivalent of the Python expression
855 \samp{\var{o}[\var{key}]}.
856\end{cfuncdesc}
857
858\begin{cfuncdesc}{int}{PyMapping_SetItemString}{PyObject *o, char *key,
859 PyObject *v}
860 Map the object \var{key} to the value \var{v} in object \var{o}.
861 Returns \code{-1} on failure. This is the equivalent of the Python
862 statement \samp{\var{o}[\var{key}] = \var{v}}.
863\end{cfuncdesc}
864
865
866\section{Iterator Protocol \label{iterator}}
867
868\versionadded{2.2}
869
870There are only a couple of functions specifically for working with
871iterators.
872
873\begin{cfuncdesc}{int}{PyIter_Check}{PyObject *o}
874 Return true if the object \var{o} supports the iterator protocol.
875\end{cfuncdesc}
876
877\begin{cfuncdesc}{PyObject*}{PyIter_Next}{PyObject *o}
878 Return the next value from the iteration \var{o}. If the object is
879 an iterator, this retrieves the next value from the iteration, and
880 returns \NULL{} with no exception set if there are no remaining
881 items. If the object is not an iterator, \exception{TypeError} is
882 raised, or if there is an error in retrieving the item, returns
883 \NULL{} and passes along the exception.
884\end{cfuncdesc}
885
886To write a loop which iterates over an iterator, the C code should
887look something like this:
888
889\begin{verbatim}
Fred Drake314bae52002-03-11 18:46:29 +0000890PyObject *iterator = PyObject_GetIter(obj);
Fred Drake3adf79e2001-10-12 19:01:43 +0000891PyObject *item;
892
Fred Drake314bae52002-03-11 18:46:29 +0000893if (iterator == NULL) {
894 /* propagate error */
895}
896
897while (item = PyIter_Next(iterator)) {
Fred Drake3adf79e2001-10-12 19:01:43 +0000898 /* do something with item */
Fred Drake8b8fe282001-12-26 16:53:48 +0000899 ...
900 /* release reference when done */
901 Py_DECREF(item);
Fred Drake3adf79e2001-10-12 19:01:43 +0000902}
Fred Drake314bae52002-03-11 18:46:29 +0000903
904Py_DECREF(iterator);
905
Fred Drake3adf79e2001-10-12 19:01:43 +0000906if (PyErr_Occurred()) {
Fred Drake314bae52002-03-11 18:46:29 +0000907 /* propagate error */
Fred Drake3adf79e2001-10-12 19:01:43 +0000908}
909else {
910 /* continue doing useful work */
911}
Fred Drake8b8fe282001-12-26 16:53:48 +0000912\end{verbatim}
913
Jeremy Hylton89c3a222001-11-09 21:59:42 +0000914
Fred Drake94ead572001-11-09 23:34:26 +0000915\section{Buffer Protocol \label{abstract-buffer}}
Jeremy Hylton89c3a222001-11-09 21:59:42 +0000916
917\begin{cfuncdesc}{int}{PyObject_AsCharBuffer}{PyObject *obj,
Fred Drake94ead572001-11-09 23:34:26 +0000918 const char **buffer,
919 int *buffer_len}
Jeremy Hylton89c3a222001-11-09 21:59:42 +0000920 Returns a pointer to a read-only memory location useable as character-
921 based input. The \var{obj} argument must support the single-segment
Fred Drake243ea712002-04-04 04:10:36 +0000922 character buffer interface. On success, returns \code{0}, sets
Thomas Hellerbfeeeee2001-11-12 07:46:31 +0000923 \var{buffer} to the memory location and \var{buffer_len} to the buffer
Fred Drake243ea712002-04-04 04:10:36 +0000924 length. Returns \code{-1} and sets a \exception{TypeError} on error.
Fred Drake94ead572001-11-09 23:34:26 +0000925 \versionadded{1.6}
Jeremy Hylton89c3a222001-11-09 21:59:42 +0000926\end{cfuncdesc}
927
928\begin{cfuncdesc}{int}{PyObject_AsReadBuffer}{PyObject *obj,
Fred Drake94ead572001-11-09 23:34:26 +0000929 const char **buffer,
930 int *buffer_len}
Jeremy Hylton89c3a222001-11-09 21:59:42 +0000931 Returns a pointer to a read-only memory location containing
932 arbitrary data. The \var{obj} argument must support the
933 single-segment readable buffer interface. On success, returns
Fred Drake243ea712002-04-04 04:10:36 +0000934 \code{0}, sets \var{buffer} to the memory location and \var{buffer_len}
935 to the buffer length. Returns \code{-1} and sets a
Jeremy Hylton89c3a222001-11-09 21:59:42 +0000936 \exception{TypeError} on error.
Fred Drake94ead572001-11-09 23:34:26 +0000937 \versionadded{1.6}
Jeremy Hylton89c3a222001-11-09 21:59:42 +0000938\end{cfuncdesc}
939
940\begin{cfuncdesc}{int}{PyObject_CheckReadBuffer}{PyObject *o}
941 Returns \code{1} if \var{o} supports the single-segment readable
942 buffer interface. Otherwise returns \code{0}.
Fred Drake94ead572001-11-09 23:34:26 +0000943 \versionadded{2.2}
Fred Drake8b8fe282001-12-26 16:53:48 +0000944\end{cfuncdesc}
Jeremy Hylton89c3a222001-11-09 21:59:42 +0000945
946\begin{cfuncdesc}{int}{PyObject_AsWriteBuffer}{PyObject *obj,
Fred Drake94ead572001-11-09 23:34:26 +0000947 const char **buffer,
948 int *buffer_len}
Jeremy Hylton89c3a222001-11-09 21:59:42 +0000949 Returns a pointer to a writeable memory location. The \var{obj}
950 argument must support the single-segment, character buffer
Fred Drake243ea712002-04-04 04:10:36 +0000951 interface. On success, returns \code{0}, sets \var{buffer} to the
Thomas Hellerbfeeeee2001-11-12 07:46:31 +0000952 memory location and \var{buffer_len} to the buffer length. Returns
Fred Drake243ea712002-04-04 04:10:36 +0000953 \code{-1} and sets a \exception{TypeError} on error.
Fred Drake94ead572001-11-09 23:34:26 +0000954 \versionadded{1.6}
Jeremy Hylton89c3a222001-11-09 21:59:42 +0000955\end{cfuncdesc}