blob: 515150d4594208c9607c7fe85634cbec33ee6534 [file] [log] [blame]
Georg Brandl116aa622007-08-15 14:28:22 +00001.. highlightlang:: c
2
3
4.. _abstract:
5
6**********************
7Abstract Objects Layer
8**********************
9
10The functions in this chapter interact with Python objects regardless of their
11type, or with wide classes of object types (e.g. all numerical types, or all
12sequence types). When used on object types for which they do not apply, they
13will raise a Python exception.
14
15It is not possible to use these functions on objects that are not properly
16initialized, such as a list object that has been created by :cfunc:`PyList_New`,
17but whose items have not been set to some non-\ ``NULL`` value yet.
18
19
20.. _object:
21
22Object Protocol
23===============
24
25
26.. cfunction:: int PyObject_Print(PyObject *o, FILE *fp, int flags)
27
28 Print an object *o*, on file *fp*. Returns ``-1`` on error. The flags argument
29 is used to enable certain printing options. The only option currently supported
30 is :const:`Py_PRINT_RAW`; if given, the :func:`str` of the object is written
31 instead of the :func:`repr`.
32
33
34.. cfunction:: int PyObject_HasAttrString(PyObject *o, const char *attr_name)
35
36 Returns ``1`` if *o* has the attribute *attr_name*, and ``0`` otherwise. This
37 is equivalent to the Python expression ``hasattr(o, attr_name)``. This function
38 always succeeds.
39
40
41.. cfunction:: PyObject* PyObject_GetAttrString(PyObject *o, const char *attr_name)
42
43 Retrieve an attribute named *attr_name* from object *o*. Returns the attribute
44 value on success, or *NULL* on failure. This is the equivalent of the Python
45 expression ``o.attr_name``.
46
47
48.. cfunction:: int PyObject_HasAttr(PyObject *o, PyObject *attr_name)
49
50 Returns ``1`` if *o* has the attribute *attr_name*, and ``0`` otherwise. This
51 is equivalent to the Python expression ``hasattr(o, attr_name)``. This function
52 always succeeds.
53
54
55.. cfunction:: PyObject* PyObject_GetAttr(PyObject *o, PyObject *attr_name)
56
57 Retrieve an attribute named *attr_name* from object *o*. Returns the attribute
58 value on success, or *NULL* on failure. This is the equivalent of the Python
59 expression ``o.attr_name``.
60
61
62.. cfunction:: int PyObject_SetAttrString(PyObject *o, const char *attr_name, PyObject *v)
63
64 Set the value of the attribute named *attr_name*, for object *o*, to the value
65 *v*. Returns ``-1`` on failure. This is the equivalent of the Python statement
66 ``o.attr_name = v``.
67
68
69.. cfunction:: int PyObject_SetAttr(PyObject *o, PyObject *attr_name, PyObject *v)
70
71 Set the value of the attribute named *attr_name*, for object *o*, to the value
72 *v*. Returns ``-1`` on failure. This is the equivalent of the Python statement
73 ``o.attr_name = v``.
74
75
76.. cfunction:: int PyObject_DelAttrString(PyObject *o, const char *attr_name)
77
78 Delete attribute named *attr_name*, for object *o*. Returns ``-1`` on failure.
79 This is the equivalent of the Python statement: ``del o.attr_name``.
80
81
82.. cfunction:: int PyObject_DelAttr(PyObject *o, PyObject *attr_name)
83
84 Delete attribute named *attr_name*, for object *o*. Returns ``-1`` on failure.
85 This is the equivalent of the Python statement ``del o.attr_name``.
86
87
88.. cfunction:: PyObject* PyObject_RichCompare(PyObject *o1, PyObject *o2, int opid)
89
90 Compare the values of *o1* and *o2* using the operation specified by *opid*,
91 which must be one of :const:`Py_LT`, :const:`Py_LE`, :const:`Py_EQ`,
92 :const:`Py_NE`, :const:`Py_GT`, or :const:`Py_GE`, corresponding to ``<``,
93 ``<=``, ``==``, ``!=``, ``>``, or ``>=`` respectively. This is the equivalent of
94 the Python expression ``o1 op o2``, where ``op`` is the operator corresponding
95 to *opid*. Returns the value of the comparison on success, or *NULL* on failure.
96
97
98.. cfunction:: int PyObject_RichCompareBool(PyObject *o1, PyObject *o2, int opid)
99
100 Compare the values of *o1* and *o2* using the operation specified by *opid*,
101 which must be one of :const:`Py_LT`, :const:`Py_LE`, :const:`Py_EQ`,
102 :const:`Py_NE`, :const:`Py_GT`, or :const:`Py_GE`, corresponding to ``<``,
103 ``<=``, ``==``, ``!=``, ``>``, or ``>=`` respectively. Returns ``-1`` on error,
104 ``0`` if the result is false, ``1`` otherwise. This is the equivalent of the
105 Python expression ``o1 op o2``, where ``op`` is the operator corresponding to
106 *opid*.
107
108
109.. cfunction:: int PyObject_Cmp(PyObject *o1, PyObject *o2, int *result)
110
111 .. index:: builtin: cmp
112
113 Compare the values of *o1* and *o2* using a routine provided by *o1*, if one
114 exists, otherwise with a routine provided by *o2*. The result of the comparison
115 is returned in *result*. Returns ``-1`` on failure. This is the equivalent of
116 the Python statement ``result = cmp(o1, o2)``.
117
118
119.. cfunction:: int PyObject_Compare(PyObject *o1, PyObject *o2)
120
121 .. index:: builtin: cmp
122
123 Compare the values of *o1* and *o2* using a routine provided by *o1*, if one
124 exists, otherwise with a routine provided by *o2*. Returns the result of the
125 comparison on success. On error, the value returned is undefined; use
126 :cfunc:`PyErr_Occurred` to detect an error. This is equivalent to the Python
127 expression ``cmp(o1, o2)``.
128
129
130.. cfunction:: PyObject* PyObject_Repr(PyObject *o)
131
132 .. index:: builtin: repr
133
134 Compute a string representation of object *o*. Returns the string
135 representation on success, *NULL* on failure. This is the equivalent of the
136 Python expression ``repr(o)``. Called by the :func:`repr` built-in function and
137 by reverse quotes.
138
139
140.. cfunction:: PyObject* PyObject_Str(PyObject *o)
141
142 .. index:: builtin: str
143
144 Compute a string representation of object *o*. Returns the string
145 representation on success, *NULL* on failure. This is the equivalent of the
Georg Brandl6911e3c2007-09-04 07:15:32 +0000146 Python expression ``str(o)``. Called by the :func:`str` built-in function
147 and, therefore, by the :func:`print` function.
Georg Brandl116aa622007-08-15 14:28:22 +0000148
149
150.. cfunction:: PyObject* PyObject_Unicode(PyObject *o)
151
152 .. index:: builtin: unicode
153
154 Compute a Unicode string representation of object *o*. Returns the Unicode
155 string representation on success, *NULL* on failure. This is the equivalent of
156 the Python expression ``unicode(o)``. Called by the :func:`unicode` built-in
157 function.
158
159
160.. cfunction:: int PyObject_IsInstance(PyObject *inst, PyObject *cls)
161
162 Returns ``1`` if *inst* is an instance of the class *cls* or a subclass of
163 *cls*, or ``0`` if not. On error, returns ``-1`` and sets an exception. If
164 *cls* is a type object rather than a class object, :cfunc:`PyObject_IsInstance`
165 returns ``1`` if *inst* is of type *cls*. If *cls* is a tuple, the check will
166 be done against every entry in *cls*. The result will be ``1`` when at least one
167 of the checks returns ``1``, otherwise it will be ``0``. If *inst* is not a
168 class instance and *cls* is neither a type object, nor a class object, nor a
169 tuple, *inst* must have a :attr:`__class__` attribute --- the class relationship
170 of the value of that attribute with *cls* will be used to determine the result
171 of this function.
172
Georg Brandl116aa622007-08-15 14:28:22 +0000173
174Subclass determination is done in a fairly straightforward way, but includes a
175wrinkle that implementors of extensions to the class system may want to be aware
176of. If :class:`A` and :class:`B` are class objects, :class:`B` is a subclass of
177:class:`A` if it inherits from :class:`A` either directly or indirectly. If
178either is not a class object, a more general mechanism is used to determine the
179class relationship of the two objects. When testing if *B* is a subclass of
180*A*, if *A* is *B*, :cfunc:`PyObject_IsSubclass` returns true. If *A* and *B*
181are different objects, *B*'s :attr:`__bases__` attribute is searched in a
182depth-first fashion for *A* --- the presence of the :attr:`__bases__` attribute
183is considered sufficient for this determination.
184
185
186.. cfunction:: int PyObject_IsSubclass(PyObject *derived, PyObject *cls)
187
188 Returns ``1`` if the class *derived* is identical to or derived from the class
189 *cls*, otherwise returns ``0``. In case of an error, returns ``-1``. If *cls*
190 is a tuple, the check will be done against every entry in *cls*. The result will
191 be ``1`` when at least one of the checks returns ``1``, otherwise it will be
192 ``0``. If either *derived* or *cls* is not an actual class object (or tuple),
193 this function uses the generic algorithm described above.
194
Georg Brandl116aa622007-08-15 14:28:22 +0000195
196.. cfunction:: int PyCallable_Check(PyObject *o)
197
198 Determine if the object *o* is callable. Return ``1`` if the object is callable
199 and ``0`` otherwise. This function always succeeds.
200
201
202.. cfunction:: PyObject* PyObject_Call(PyObject *callable_object, PyObject *args, PyObject *kw)
203
204 Call a callable Python object *callable_object*, with arguments given by the
205 tuple *args*, and named arguments given by the dictionary *kw*. If no named
206 arguments are needed, *kw* may be *NULL*. *args* must not be *NULL*, use an
207 empty tuple if no arguments are needed. Returns the result of the call on
208 success, or *NULL* on failure. This is the equivalent of the Python expression
209 ``callable_object(*args, **kw)``.
210
Georg Brandl116aa622007-08-15 14:28:22 +0000211
212.. cfunction:: PyObject* PyObject_CallObject(PyObject *callable_object, PyObject *args)
213
214 Call a callable Python object *callable_object*, with arguments given by the
215 tuple *args*. If no arguments are needed, then *args* may be *NULL*. Returns
216 the result of the call on success, or *NULL* on failure. This is the equivalent
217 of the Python expression ``callable_object(*args)``.
218
219
220.. cfunction:: PyObject* PyObject_CallFunction(PyObject *callable, char *format, ...)
221
222 Call a callable Python object *callable*, with a variable number of C arguments.
223 The C arguments are described using a :cfunc:`Py_BuildValue` style format
224 string. The format may be *NULL*, indicating that no arguments are provided.
225 Returns the result of the call on success, or *NULL* on failure. This is the
226 equivalent of the Python expression ``callable(*args)``. Note that if you only
227 pass :ctype:`PyObject \*` args, :cfunc:`PyObject_CallFunctionObjArgs` is a
228 faster alternative.
229
230
231.. cfunction:: PyObject* PyObject_CallMethod(PyObject *o, char *method, char *format, ...)
232
233 Call the method named *method* of object *o* with a variable number of C
234 arguments. The C arguments are described by a :cfunc:`Py_BuildValue` format
235 string that should produce a tuple. The format may be *NULL*, indicating that
236 no arguments are provided. Returns the result of the call on success, or *NULL*
237 on failure. This is the equivalent of the Python expression ``o.method(args)``.
238 Note that if you only pass :ctype:`PyObject \*` args,
239 :cfunc:`PyObject_CallMethodObjArgs` is a faster alternative.
240
241
242.. cfunction:: PyObject* PyObject_CallFunctionObjArgs(PyObject *callable, ..., NULL)
243
244 Call a callable Python object *callable*, with a variable number of
245 :ctype:`PyObject\*` arguments. The arguments are provided as a variable number
246 of parameters followed by *NULL*. Returns the result of the call on success, or
247 *NULL* on failure.
248
Georg Brandl116aa622007-08-15 14:28:22 +0000249
250.. cfunction:: PyObject* PyObject_CallMethodObjArgs(PyObject *o, PyObject *name, ..., NULL)
251
252 Calls a method of the object *o*, where the name of the method is given as a
253 Python string object in *name*. It is called with a variable number of
254 :ctype:`PyObject\*` arguments. The arguments are provided as a variable number
255 of parameters followed by *NULL*. Returns the result of the call on success, or
256 *NULL* on failure.
257
Georg Brandl116aa622007-08-15 14:28:22 +0000258
259.. cfunction:: long PyObject_Hash(PyObject *o)
260
261 .. index:: builtin: hash
262
263 Compute and return the hash value of an object *o*. On failure, return ``-1``.
264 This is the equivalent of the Python expression ``hash(o)``.
265
266
267.. cfunction:: int PyObject_IsTrue(PyObject *o)
268
269 Returns ``1`` if the object *o* is considered to be true, and ``0`` otherwise.
270 This is equivalent to the Python expression ``not not o``. On failure, return
271 ``-1``.
272
273
274.. cfunction:: int PyObject_Not(PyObject *o)
275
276 Returns ``0`` if the object *o* is considered to be true, and ``1`` otherwise.
277 This is equivalent to the Python expression ``not o``. On failure, return
278 ``-1``.
279
280
281.. cfunction:: PyObject* PyObject_Type(PyObject *o)
282
283 .. index:: builtin: type
284
285 When *o* is non-*NULL*, returns a type object corresponding to the object type
286 of object *o*. On failure, raises :exc:`SystemError` and returns *NULL*. This
287 is equivalent to the Python expression ``type(o)``. This function increments the
288 reference count of the return value. There's really no reason to use this
289 function instead of the common expression ``o->ob_type``, which returns a
290 pointer of type :ctype:`PyTypeObject\*`, except when the incremented reference
291 count is needed.
292
293
294.. cfunction:: int PyObject_TypeCheck(PyObject *o, PyTypeObject *type)
295
296 Return true if the object *o* is of type *type* or a subtype of *type*. Both
297 parameters must be non-*NULL*.
298
Georg Brandl116aa622007-08-15 14:28:22 +0000299
300.. cfunction:: Py_ssize_t PyObject_Length(PyObject *o)
301 Py_ssize_t PyObject_Size(PyObject *o)
302
303 .. index:: builtin: len
304
305 Return the length of object *o*. If the object *o* provides either the sequence
306 and mapping protocols, the sequence length is returned. On error, ``-1`` is
307 returned. This is the equivalent to the Python expression ``len(o)``.
308
309
310.. cfunction:: PyObject* PyObject_GetItem(PyObject *o, PyObject *key)
311
312 Return element of *o* corresponding to the object *key* or *NULL* on failure.
313 This is the equivalent of the Python expression ``o[key]``.
314
315
316.. cfunction:: int PyObject_SetItem(PyObject *o, PyObject *key, PyObject *v)
317
318 Map the object *key* to the value *v*. Returns ``-1`` on failure. This is the
319 equivalent of the Python statement ``o[key] = v``.
320
321
322.. cfunction:: int PyObject_DelItem(PyObject *o, PyObject *key)
323
324 Delete the mapping for *key* from *o*. Returns ``-1`` on failure. This is the
325 equivalent of the Python statement ``del o[key]``.
326
327
328.. cfunction:: int PyObject_AsFileDescriptor(PyObject *o)
329
330 Derives a file-descriptor from a Python object. If the object is an integer or
331 long integer, its value is returned. If not, the object's :meth:`fileno` method
332 is called if it exists; the method must return an integer or long integer, which
333 is returned as the file descriptor value. Returns ``-1`` on failure.
334
335
336.. cfunction:: PyObject* PyObject_Dir(PyObject *o)
337
338 This is equivalent to the Python expression ``dir(o)``, returning a (possibly
339 empty) list of strings appropriate for the object argument, or *NULL* if there
340 was an error. If the argument is *NULL*, this is like the Python ``dir()``,
341 returning the names of the current locals; in this case, if no execution frame
342 is active then *NULL* is returned but :cfunc:`PyErr_Occurred` will return false.
343
344
345.. cfunction:: PyObject* PyObject_GetIter(PyObject *o)
346
347 This is equivalent to the Python expression ``iter(o)``. It returns a new
348 iterator for the object argument, or the object itself if the object is already
349 an iterator. Raises :exc:`TypeError` and returns *NULL* if the object cannot be
350 iterated.
351
352
353.. _number:
354
355Number Protocol
356===============
357
358
359.. cfunction:: int PyNumber_Check(PyObject *o)
360
361 Returns ``1`` if the object *o* provides numeric protocols, and false otherwise.
362 This function always succeeds.
363
364
365.. cfunction:: PyObject* PyNumber_Add(PyObject *o1, PyObject *o2)
366
367 Returns the result of adding *o1* and *o2*, or *NULL* on failure. This is the
368 equivalent of the Python expression ``o1 + o2``.
369
370
371.. cfunction:: PyObject* PyNumber_Subtract(PyObject *o1, PyObject *o2)
372
373 Returns the result of subtracting *o2* from *o1*, or *NULL* on failure. This is
374 the equivalent of the Python expression ``o1 - o2``.
375
376
377.. cfunction:: PyObject* PyNumber_Multiply(PyObject *o1, PyObject *o2)
378
379 Returns the result of multiplying *o1* and *o2*, or *NULL* on failure. This is
380 the equivalent of the Python expression ``o1 * o2``.
381
382
383.. cfunction:: PyObject* PyNumber_Divide(PyObject *o1, PyObject *o2)
384
385 Returns the result of dividing *o1* by *o2*, or *NULL* on failure. This is the
386 equivalent of the Python expression ``o1 / o2``.
387
388
389.. cfunction:: PyObject* PyNumber_FloorDivide(PyObject *o1, PyObject *o2)
390
391 Return the floor of *o1* divided by *o2*, or *NULL* on failure. This is
392 equivalent to the "classic" division of integers.
393
Georg Brandl116aa622007-08-15 14:28:22 +0000394
395.. cfunction:: PyObject* PyNumber_TrueDivide(PyObject *o1, PyObject *o2)
396
397 Return a reasonable approximation for the mathematical value of *o1* divided by
398 *o2*, or *NULL* on failure. The return value is "approximate" because binary
399 floating point numbers are approximate; it is not possible to represent all real
400 numbers in base two. This function can return a floating point value when
401 passed two integers.
402
Georg Brandl116aa622007-08-15 14:28:22 +0000403
404.. cfunction:: PyObject* PyNumber_Remainder(PyObject *o1, PyObject *o2)
405
406 Returns the remainder of dividing *o1* by *o2*, or *NULL* on failure. This is
407 the equivalent of the Python expression ``o1 % o2``.
408
409
410.. cfunction:: PyObject* PyNumber_Divmod(PyObject *o1, PyObject *o2)
411
412 .. index:: builtin: divmod
413
414 See the built-in function :func:`divmod`. Returns *NULL* on failure. This is
415 the equivalent of the Python expression ``divmod(o1, o2)``.
416
417
418.. cfunction:: PyObject* PyNumber_Power(PyObject *o1, PyObject *o2, PyObject *o3)
419
420 .. index:: builtin: pow
421
422 See the built-in function :func:`pow`. Returns *NULL* on failure. This is the
423 equivalent of the Python expression ``pow(o1, o2, o3)``, where *o3* is optional.
424 If *o3* is to be ignored, pass :cdata:`Py_None` in its place (passing *NULL* for
425 *o3* would cause an illegal memory access).
426
427
428.. cfunction:: PyObject* PyNumber_Negative(PyObject *o)
429
430 Returns the negation of *o* on success, or *NULL* on failure. This is the
431 equivalent of the Python expression ``-o``.
432
433
434.. cfunction:: PyObject* PyNumber_Positive(PyObject *o)
435
436 Returns *o* on success, or *NULL* on failure. This is the equivalent of the
437 Python expression ``+o``.
438
439
440.. cfunction:: PyObject* PyNumber_Absolute(PyObject *o)
441
442 .. index:: builtin: abs
443
444 Returns the absolute value of *o*, or *NULL* on failure. This is the equivalent
445 of the Python expression ``abs(o)``.
446
447
448.. cfunction:: PyObject* PyNumber_Invert(PyObject *o)
449
450 Returns the bitwise negation of *o* on success, or *NULL* on failure. This is
451 the equivalent of the Python expression ``~o``.
452
453
454.. cfunction:: PyObject* PyNumber_Lshift(PyObject *o1, PyObject *o2)
455
456 Returns the result of left shifting *o1* by *o2* on success, or *NULL* on
457 failure. This is the equivalent of the Python expression ``o1 << o2``.
458
459
460.. cfunction:: PyObject* PyNumber_Rshift(PyObject *o1, PyObject *o2)
461
462 Returns the result of right shifting *o1* by *o2* on success, or *NULL* on
463 failure. This is the equivalent of the Python expression ``o1 >> o2``.
464
465
466.. cfunction:: PyObject* PyNumber_And(PyObject *o1, PyObject *o2)
467
468 Returns the "bitwise and" of *o1* and *o2* on success and *NULL* on failure.
469 This is the equivalent of the Python expression ``o1 & o2``.
470
471
472.. cfunction:: PyObject* PyNumber_Xor(PyObject *o1, PyObject *o2)
473
474 Returns the "bitwise exclusive or" of *o1* by *o2* on success, or *NULL* on
475 failure. This is the equivalent of the Python expression ``o1 ^ o2``.
476
477
478.. cfunction:: PyObject* PyNumber_Or(PyObject *o1, PyObject *o2)
479
480 Returns the "bitwise or" of *o1* and *o2* on success, or *NULL* on failure.
481 This is the equivalent of the Python expression ``o1 | o2``.
482
483
484.. cfunction:: PyObject* PyNumber_InPlaceAdd(PyObject *o1, PyObject *o2)
485
486 Returns the result of adding *o1* and *o2*, or *NULL* on failure. The operation
487 is done *in-place* when *o1* supports it. This is the equivalent of the Python
488 statement ``o1 += o2``.
489
490
491.. cfunction:: PyObject* PyNumber_InPlaceSubtract(PyObject *o1, PyObject *o2)
492
493 Returns the result of subtracting *o2* from *o1*, or *NULL* on failure. The
494 operation is done *in-place* when *o1* supports it. This is the equivalent of
495 the Python statement ``o1 -= o2``.
496
497
498.. cfunction:: PyObject* PyNumber_InPlaceMultiply(PyObject *o1, PyObject *o2)
499
500 Returns the result of multiplying *o1* and *o2*, or *NULL* on failure. The
501 operation is done *in-place* when *o1* supports it. This is the equivalent of
502 the Python statement ``o1 *= o2``.
503
504
505.. cfunction:: PyObject* PyNumber_InPlaceDivide(PyObject *o1, PyObject *o2)
506
507 Returns the result of dividing *o1* by *o2*, or *NULL* on failure. The
508 operation is done *in-place* when *o1* supports it. This is the equivalent of
509 the Python statement ``o1 /= o2``.
510
511
512.. cfunction:: PyObject* PyNumber_InPlaceFloorDivide(PyObject *o1, PyObject *o2)
513
514 Returns the mathematical floor of dividing *o1* by *o2*, or *NULL* on failure.
515 The operation is done *in-place* when *o1* supports it. This is the equivalent
516 of the Python statement ``o1 //= o2``.
517
Georg Brandl116aa622007-08-15 14:28:22 +0000518
519.. cfunction:: PyObject* PyNumber_InPlaceTrueDivide(PyObject *o1, PyObject *o2)
520
521 Return a reasonable approximation for the mathematical value of *o1* divided by
522 *o2*, or *NULL* on failure. The return value is "approximate" because binary
523 floating point numbers are approximate; it is not possible to represent all real
524 numbers in base two. This function can return a floating point value when
525 passed two integers. The operation is done *in-place* when *o1* supports it.
526
Georg Brandl116aa622007-08-15 14:28:22 +0000527
528.. cfunction:: PyObject* PyNumber_InPlaceRemainder(PyObject *o1, PyObject *o2)
529
530 Returns the remainder of dividing *o1* by *o2*, or *NULL* on failure. The
531 operation is done *in-place* when *o1* supports it. This is the equivalent of
532 the Python statement ``o1 %= o2``.
533
534
535.. cfunction:: PyObject* PyNumber_InPlacePower(PyObject *o1, PyObject *o2, PyObject *o3)
536
537 .. index:: builtin: pow
538
539 See the built-in function :func:`pow`. Returns *NULL* on failure. The operation
540 is done *in-place* when *o1* supports it. This is the equivalent of the Python
541 statement ``o1 **= o2`` when o3 is :cdata:`Py_None`, or an in-place variant of
542 ``pow(o1, o2, o3)`` otherwise. If *o3* is to be ignored, pass :cdata:`Py_None`
543 in its place (passing *NULL* for *o3* would cause an illegal memory access).
544
545
546.. cfunction:: PyObject* PyNumber_InPlaceLshift(PyObject *o1, PyObject *o2)
547
548 Returns the result of left shifting *o1* by *o2* on success, or *NULL* on
549 failure. The operation is done *in-place* when *o1* supports it. This is the
550 equivalent of the Python statement ``o1 <<= o2``.
551
552
553.. cfunction:: PyObject* PyNumber_InPlaceRshift(PyObject *o1, PyObject *o2)
554
555 Returns the result of right shifting *o1* by *o2* on success, or *NULL* on
556 failure. The operation is done *in-place* when *o1* supports it. This is the
557 equivalent of the Python statement ``o1 >>= o2``.
558
559
560.. cfunction:: PyObject* PyNumber_InPlaceAnd(PyObject *o1, PyObject *o2)
561
562 Returns the "bitwise and" of *o1* and *o2* on success and *NULL* on failure. The
563 operation is done *in-place* when *o1* supports it. This is the equivalent of
564 the Python statement ``o1 &= o2``.
565
566
567.. cfunction:: PyObject* PyNumber_InPlaceXor(PyObject *o1, PyObject *o2)
568
569 Returns the "bitwise exclusive or" of *o1* by *o2* on success, or *NULL* on
570 failure. The operation is done *in-place* when *o1* supports it. This is the
571 equivalent of the Python statement ``o1 ^= o2``.
572
573
574.. cfunction:: PyObject* PyNumber_InPlaceOr(PyObject *o1, PyObject *o2)
575
576 Returns the "bitwise or" of *o1* and *o2* on success, or *NULL* on failure. The
577 operation is done *in-place* when *o1* supports it. This is the equivalent of
578 the Python statement ``o1 |= o2``.
579
580
581.. cfunction:: PyObject* PyNumber_Int(PyObject *o)
582
583 .. index:: builtin: int
584
585 Returns the *o* converted to an integer object on success, or *NULL* on failure.
586 If the argument is outside the integer range a long object will be returned
587 instead. This is the equivalent of the Python expression ``int(o)``.
588
589
590.. cfunction:: PyObject* PyNumber_Long(PyObject *o)
591
592 .. index:: builtin: long
593
594 Returns the *o* converted to a long integer object on success, or *NULL* on
595 failure. This is the equivalent of the Python expression ``long(o)``.
596
597
598.. cfunction:: PyObject* PyNumber_Float(PyObject *o)
599
600 .. index:: builtin: float
601
602 Returns the *o* converted to a float object on success, or *NULL* on failure.
603 This is the equivalent of the Python expression ``float(o)``.
604
605
606.. cfunction:: PyObject* PyNumber_Index(PyObject *o)
607
608 Returns the *o* converted to a Python int or long on success or *NULL* with a
609 TypeError exception raised on failure.
610
Georg Brandl116aa622007-08-15 14:28:22 +0000611
612.. cfunction:: Py_ssize_t PyNumber_AsSsize_t(PyObject *o, PyObject *exc)
613
614 Returns *o* converted to a Py_ssize_t value if *o* can be interpreted as an
615 integer. If *o* can be converted to a Python int or long but the attempt to
616 convert to a Py_ssize_t value would raise an :exc:`OverflowError`, then the
617 *exc* argument is the type of exception that will be raised (usually
618 :exc:`IndexError` or :exc:`OverflowError`). If *exc* is *NULL*, then the
619 exception is cleared and the value is clipped to *PY_SSIZE_T_MIN* for a negative
620 integer or *PY_SSIZE_T_MAX* for a positive integer.
621
Georg Brandl116aa622007-08-15 14:28:22 +0000622
623.. cfunction:: int PyIndex_Check(PyObject *o)
624
625 Returns True if *o* is an index integer (has the nb_index slot of the
626 tp_as_number structure filled in).
627
Georg Brandl116aa622007-08-15 14:28:22 +0000628
629.. _sequence:
630
631Sequence Protocol
632=================
633
634
635.. cfunction:: int PySequence_Check(PyObject *o)
636
637 Return ``1`` if the object provides sequence protocol, and ``0`` otherwise.
638 This function always succeeds.
639
640
641.. cfunction:: Py_ssize_t PySequence_Size(PyObject *o)
642
643 .. index:: builtin: len
644
645 Returns the number of objects in sequence *o* on success, and ``-1`` on failure.
646 For objects that do not provide sequence protocol, this is equivalent to the
647 Python expression ``len(o)``.
648
649
650.. cfunction:: Py_ssize_t PySequence_Length(PyObject *o)
651
652 Alternate name for :cfunc:`PySequence_Size`.
653
654
655.. cfunction:: PyObject* PySequence_Concat(PyObject *o1, PyObject *o2)
656
657 Return the concatenation of *o1* and *o2* on success, and *NULL* on failure.
658 This is the equivalent of the Python expression ``o1 + o2``.
659
660
661.. cfunction:: PyObject* PySequence_Repeat(PyObject *o, Py_ssize_t count)
662
663 Return the result of repeating sequence object *o* *count* times, or *NULL* on
664 failure. This is the equivalent of the Python expression ``o * count``.
665
666
667.. cfunction:: PyObject* PySequence_InPlaceConcat(PyObject *o1, PyObject *o2)
668
669 Return the concatenation of *o1* and *o2* on success, and *NULL* on failure.
670 The operation is done *in-place* when *o1* supports it. This is the equivalent
671 of the Python expression ``o1 += o2``.
672
673
674.. cfunction:: PyObject* PySequence_InPlaceRepeat(PyObject *o, Py_ssize_t count)
675
676 Return the result of repeating sequence object *o* *count* times, or *NULL* on
677 failure. The operation is done *in-place* when *o* supports it. This is the
678 equivalent of the Python expression ``o *= count``.
679
680
681.. cfunction:: PyObject* PySequence_GetItem(PyObject *o, Py_ssize_t i)
682
683 Return the *i*th element of *o*, or *NULL* on failure. This is the equivalent of
684 the Python expression ``o[i]``.
685
686
687.. cfunction:: PyObject* PySequence_GetSlice(PyObject *o, Py_ssize_t i1, Py_ssize_t i2)
688
689 Return the slice of sequence object *o* between *i1* and *i2*, or *NULL* on
690 failure. This is the equivalent of the Python expression ``o[i1:i2]``.
691
692
693.. cfunction:: int PySequence_SetItem(PyObject *o, Py_ssize_t i, PyObject *v)
694
695 Assign object *v* to the *i*th element of *o*. Returns ``-1`` on failure. This
696 is the equivalent of the Python statement ``o[i] = v``. This function *does
697 not* steal a reference to *v*.
698
699
700.. cfunction:: int PySequence_DelItem(PyObject *o, Py_ssize_t i)
701
702 Delete the *i*th element of object *o*. Returns ``-1`` on failure. This is the
703 equivalent of the Python statement ``del o[i]``.
704
705
706.. cfunction:: int PySequence_SetSlice(PyObject *o, Py_ssize_t i1, Py_ssize_t i2, PyObject *v)
707
708 Assign the sequence object *v* to the slice in sequence object *o* from *i1* to
709 *i2*. This is the equivalent of the Python statement ``o[i1:i2] = v``.
710
711
712.. cfunction:: int PySequence_DelSlice(PyObject *o, Py_ssize_t i1, Py_ssize_t i2)
713
714 Delete the slice in sequence object *o* from *i1* to *i2*. Returns ``-1`` on
715 failure. This is the equivalent of the Python statement ``del o[i1:i2]``.
716
717
718.. cfunction:: Py_ssize_t PySequence_Count(PyObject *o, PyObject *value)
719
720 Return the number of occurrences of *value* in *o*, that is, return the number
721 of keys for which ``o[key] == value``. On failure, return ``-1``. This is
722 equivalent to the Python expression ``o.count(value)``.
723
724
725.. cfunction:: int PySequence_Contains(PyObject *o, PyObject *value)
726
727 Determine if *o* contains *value*. If an item in *o* is equal to *value*,
728 return ``1``, otherwise return ``0``. On error, return ``-1``. This is
729 equivalent to the Python expression ``value in o``.
730
731
732.. cfunction:: Py_ssize_t PySequence_Index(PyObject *o, PyObject *value)
733
734 Return the first index *i* for which ``o[i] == value``. On error, return
735 ``-1``. This is equivalent to the Python expression ``o.index(value)``.
736
737
738.. cfunction:: PyObject* PySequence_List(PyObject *o)
739
740 Return a list object with the same contents as the arbitrary sequence *o*. The
741 returned list is guaranteed to be new.
742
743
744.. cfunction:: PyObject* PySequence_Tuple(PyObject *o)
745
746 .. index:: builtin: tuple
747
748 Return a tuple object with the same contents as the arbitrary sequence *o* or
749 *NULL* on failure. If *o* is a tuple, a new reference will be returned,
750 otherwise a tuple will be constructed with the appropriate contents. This is
751 equivalent to the Python expression ``tuple(o)``.
752
753
754.. cfunction:: PyObject* PySequence_Fast(PyObject *o, const char *m)
755
756 Returns the sequence *o* as a tuple, unless it is already a tuple or list, in
757 which case *o* is returned. Use :cfunc:`PySequence_Fast_GET_ITEM` to access the
758 members of the result. Returns *NULL* on failure. If the object is not a
759 sequence, raises :exc:`TypeError` with *m* as the message text.
760
761
762.. cfunction:: PyObject* PySequence_Fast_GET_ITEM(PyObject *o, Py_ssize_t i)
763
764 Return the *i*th element of *o*, assuming that *o* was returned by
765 :cfunc:`PySequence_Fast`, *o* is not *NULL*, and that *i* is within bounds.
766
767
768.. cfunction:: PyObject** PySequence_Fast_ITEMS(PyObject *o)
769
770 Return the underlying array of PyObject pointers. Assumes that *o* was returned
771 by :cfunc:`PySequence_Fast` and *o* is not *NULL*.
772
Georg Brandl116aa622007-08-15 14:28:22 +0000773
774.. cfunction:: PyObject* PySequence_ITEM(PyObject *o, Py_ssize_t i)
775
776 Return the *i*th element of *o* or *NULL* on failure. Macro form of
777 :cfunc:`PySequence_GetItem` but without checking that
778 :cfunc:`PySequence_Check(o)` is true and without adjustment for negative
779 indices.
780
Georg Brandl116aa622007-08-15 14:28:22 +0000781
782.. cfunction:: Py_ssize_t PySequence_Fast_GET_SIZE(PyObject *o)
783
784 Returns the length of *o*, assuming that *o* was returned by
785 :cfunc:`PySequence_Fast` and that *o* is not *NULL*. The size can also be
786 gotten by calling :cfunc:`PySequence_Size` on *o*, but
787 :cfunc:`PySequence_Fast_GET_SIZE` is faster because it can assume *o* is a list
788 or tuple.
789
790
791.. _mapping:
792
793Mapping Protocol
794================
795
796
797.. cfunction:: int PyMapping_Check(PyObject *o)
798
799 Return ``1`` if the object provides mapping protocol, and ``0`` otherwise. This
800 function always succeeds.
801
802
803.. cfunction:: Py_ssize_t PyMapping_Length(PyObject *o)
804
805 .. index:: builtin: len
806
807 Returns the number of keys in object *o* on success, and ``-1`` on failure. For
808 objects that do not provide mapping protocol, this is equivalent to the Python
809 expression ``len(o)``.
810
811
812.. cfunction:: int PyMapping_DelItemString(PyObject *o, char *key)
813
814 Remove the mapping for object *key* from the object *o*. Return ``-1`` on
815 failure. This is equivalent to the Python statement ``del o[key]``.
816
817
818.. cfunction:: int PyMapping_DelItem(PyObject *o, PyObject *key)
819
820 Remove the mapping for object *key* from the object *o*. Return ``-1`` on
821 failure. This is equivalent to the Python statement ``del o[key]``.
822
823
824.. cfunction:: int PyMapping_HasKeyString(PyObject *o, char *key)
825
826 On success, return ``1`` if the mapping object has the key *key* and ``0``
Collin Winter19ab2bd2007-09-10 00:20:46 +0000827 otherwise. This is equivalent to the Python expression ``key in o``.
Georg Brandl116aa622007-08-15 14:28:22 +0000828 This function always succeeds.
829
830
831.. cfunction:: int PyMapping_HasKey(PyObject *o, PyObject *key)
832
833 Return ``1`` if the mapping object has the key *key* and ``0`` otherwise. This
Collin Winter19ab2bd2007-09-10 00:20:46 +0000834 is equivalent to the Python expression ``key in o``. This function always
Georg Brandl116aa622007-08-15 14:28:22 +0000835 succeeds.
836
837
838.. cfunction:: PyObject* PyMapping_Keys(PyObject *o)
839
840 On success, return a list of the keys in object *o*. On failure, return *NULL*.
841 This is equivalent to the Python expression ``o.keys()``.
842
843
844.. cfunction:: PyObject* PyMapping_Values(PyObject *o)
845
846 On success, return a list of the values in object *o*. On failure, return
847 *NULL*. This is equivalent to the Python expression ``o.values()``.
848
849
850.. cfunction:: PyObject* PyMapping_Items(PyObject *o)
851
852 On success, return a list of the items in object *o*, where each item is a tuple
853 containing a key-value pair. On failure, return *NULL*. This is equivalent to
854 the Python expression ``o.items()``.
855
856
857.. cfunction:: PyObject* PyMapping_GetItemString(PyObject *o, char *key)
858
859 Return element of *o* corresponding to the object *key* or *NULL* on failure.
860 This is the equivalent of the Python expression ``o[key]``.
861
862
863.. cfunction:: int PyMapping_SetItemString(PyObject *o, char *key, PyObject *v)
864
865 Map the object *key* to the value *v* in object *o*. Returns ``-1`` on failure.
866 This is the equivalent of the Python statement ``o[key] = v``.
867
868
869.. _iterator:
870
871Iterator Protocol
872=================
873
Georg Brandl116aa622007-08-15 14:28:22 +0000874There are only a couple of functions specifically for working with iterators.
875
Georg Brandl116aa622007-08-15 14:28:22 +0000876.. cfunction:: int PyIter_Check(PyObject *o)
877
878 Return true if the object *o* supports the iterator protocol.
879
880
881.. cfunction:: PyObject* PyIter_Next(PyObject *o)
882
883 Return the next value from the iteration *o*. If the object is an iterator,
884 this retrieves the next value from the iteration, and returns *NULL* with no
885 exception set if there are no remaining items. If the object is not an
886 iterator, :exc:`TypeError` is raised, or if there is an error in retrieving the
887 item, returns *NULL* and passes along the exception.
888
889To write a loop which iterates over an iterator, the C code should look
890something like this::
891
892 PyObject *iterator = PyObject_GetIter(obj);
893 PyObject *item;
894
895 if (iterator == NULL) {
896 /* propagate error */
897 }
898
899 while (item = PyIter_Next(iterator)) {
900 /* do something with item */
901 ...
902 /* release reference when done */
903 Py_DECREF(item);
904 }
905
906 Py_DECREF(iterator);
907
908 if (PyErr_Occurred()) {
909 /* propagate error */
910 }
911 else {
912 /* continue doing useful work */
913 }
914
915
916.. _abstract-buffer:
917
918Buffer Protocol
919===============
920
921
922.. cfunction:: int PyObject_AsCharBuffer(PyObject *obj, const char **buffer, Py_ssize_t *buffer_len)
923
924 Returns a pointer to a read-only memory location useable as character- based
925 input. The *obj* argument must support the single-segment character buffer
926 interface. On success, returns ``0``, sets *buffer* to the memory location and
927 *buffer_len* to the buffer length. Returns ``-1`` and sets a :exc:`TypeError`
928 on error.
929
Georg Brandl116aa622007-08-15 14:28:22 +0000930
931.. cfunction:: int PyObject_AsReadBuffer(PyObject *obj, const void **buffer, Py_ssize_t *buffer_len)
932
933 Returns a pointer to a read-only memory location containing arbitrary data. The
934 *obj* argument must support the single-segment readable buffer interface. On
935 success, returns ``0``, sets *buffer* to the memory location and *buffer_len* to
936 the buffer length. Returns ``-1`` and sets a :exc:`TypeError` on error.
937
Georg Brandl116aa622007-08-15 14:28:22 +0000938
939.. cfunction:: int PyObject_CheckReadBuffer(PyObject *o)
940
941 Returns ``1`` if *o* supports the single-segment readable buffer interface.
942 Otherwise returns ``0``.
943
Georg Brandl116aa622007-08-15 14:28:22 +0000944
945.. cfunction:: int PyObject_AsWriteBuffer(PyObject *obj, void **buffer, Py_ssize_t *buffer_len)
946
Sean Reifscheider54cf12b2007-09-17 17:55:36 +0000947 Returns a pointer to a writable memory location. The *obj* argument must
Georg Brandl116aa622007-08-15 14:28:22 +0000948 support the single-segment, character buffer interface. On success, returns
949 ``0``, sets *buffer* to the memory location and *buffer_len* to the buffer
950 length. Returns ``-1`` and sets a :exc:`TypeError` on error.
951