blob: 1a1ff13deab9486b6d539e26b72dae8dfbb09b6d [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
146 Python expression ``str(o)``. Called by the :func:`str` built-in function and
147 by the :keyword:`print` statement.
148
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
173 .. versionadded:: 2.1
174
175 .. versionchanged:: 2.2
176 Support for a tuple as the second argument added.
177
178Subclass determination is done in a fairly straightforward way, but includes a
179wrinkle that implementors of extensions to the class system may want to be aware
180of. If :class:`A` and :class:`B` are class objects, :class:`B` is a subclass of
181:class:`A` if it inherits from :class:`A` either directly or indirectly. If
182either is not a class object, a more general mechanism is used to determine the
183class relationship of the two objects. When testing if *B* is a subclass of
184*A*, if *A* is *B*, :cfunc:`PyObject_IsSubclass` returns true. If *A* and *B*
185are different objects, *B*'s :attr:`__bases__` attribute is searched in a
186depth-first fashion for *A* --- the presence of the :attr:`__bases__` attribute
187is considered sufficient for this determination.
188
189
190.. cfunction:: int PyObject_IsSubclass(PyObject *derived, PyObject *cls)
191
192 Returns ``1`` if the class *derived* is identical to or derived from the class
193 *cls*, otherwise returns ``0``. In case of an error, returns ``-1``. If *cls*
194 is a tuple, the check will be done against every entry in *cls*. The result will
195 be ``1`` when at least one of the checks returns ``1``, otherwise it will be
196 ``0``. If either *derived* or *cls* is not an actual class object (or tuple),
197 this function uses the generic algorithm described above.
198
199 .. versionadded:: 2.1
200
201 .. versionchanged:: 2.3
202 Older versions of Python did not support a tuple as the second argument.
203
204
205.. cfunction:: int PyCallable_Check(PyObject *o)
206
207 Determine if the object *o* is callable. Return ``1`` if the object is callable
208 and ``0`` otherwise. This function always succeeds.
209
210
211.. cfunction:: PyObject* PyObject_Call(PyObject *callable_object, PyObject *args, PyObject *kw)
212
213 Call a callable Python object *callable_object*, with arguments given by the
214 tuple *args*, and named arguments given by the dictionary *kw*. If no named
215 arguments are needed, *kw* may be *NULL*. *args* must not be *NULL*, use an
216 empty tuple if no arguments are needed. Returns the result of the call on
217 success, or *NULL* on failure. This is the equivalent of the Python expression
218 ``callable_object(*args, **kw)``.
219
220 .. versionadded:: 2.2
221
222
223.. cfunction:: PyObject* PyObject_CallObject(PyObject *callable_object, PyObject *args)
224
225 Call a callable Python object *callable_object*, with arguments given by the
226 tuple *args*. If no arguments are needed, then *args* may be *NULL*. Returns
227 the result of the call on success, or *NULL* on failure. This is the equivalent
228 of the Python expression ``callable_object(*args)``.
229
230
231.. cfunction:: PyObject* PyObject_CallFunction(PyObject *callable, char *format, ...)
232
233 Call a callable Python object *callable*, with a variable number of C arguments.
234 The C arguments are described using a :cfunc:`Py_BuildValue` style format
235 string. The format may be *NULL*, indicating that no arguments are provided.
236 Returns the result of the call on success, or *NULL* on failure. This is the
237 equivalent of the Python expression ``callable(*args)``. Note that if you only
238 pass :ctype:`PyObject \*` args, :cfunc:`PyObject_CallFunctionObjArgs` is a
239 faster alternative.
240
241
242.. cfunction:: PyObject* PyObject_CallMethod(PyObject *o, char *method, char *format, ...)
243
244 Call the method named *method* of object *o* with a variable number of C
245 arguments. The C arguments are described by a :cfunc:`Py_BuildValue` format
246 string that should produce a tuple. The format may be *NULL*, indicating that
247 no arguments are provided. Returns the result of the call on success, or *NULL*
248 on failure. This is the equivalent of the Python expression ``o.method(args)``.
249 Note that if you only pass :ctype:`PyObject \*` args,
250 :cfunc:`PyObject_CallMethodObjArgs` is a faster alternative.
251
252
253.. cfunction:: PyObject* PyObject_CallFunctionObjArgs(PyObject *callable, ..., NULL)
254
255 Call a callable Python object *callable*, with a variable number of
256 :ctype:`PyObject\*` arguments. The arguments are provided as a variable number
257 of parameters followed by *NULL*. Returns the result of the call on success, or
258 *NULL* on failure.
259
260 .. versionadded:: 2.2
261
262
263.. cfunction:: PyObject* PyObject_CallMethodObjArgs(PyObject *o, PyObject *name, ..., NULL)
264
265 Calls a method of the object *o*, where the name of the method is given as a
266 Python string object in *name*. It is called with a variable number of
267 :ctype:`PyObject\*` arguments. The arguments are provided as a variable number
268 of parameters followed by *NULL*. Returns the result of the call on success, or
269 *NULL* on failure.
270
271 .. versionadded:: 2.2
272
273
274.. cfunction:: long PyObject_Hash(PyObject *o)
275
276 .. index:: builtin: hash
277
278 Compute and return the hash value of an object *o*. On failure, return ``-1``.
279 This is the equivalent of the Python expression ``hash(o)``.
280
281
282.. cfunction:: int PyObject_IsTrue(PyObject *o)
283
284 Returns ``1`` if the object *o* is considered to be true, and ``0`` otherwise.
285 This is equivalent to the Python expression ``not not o``. On failure, return
286 ``-1``.
287
288
289.. cfunction:: int PyObject_Not(PyObject *o)
290
291 Returns ``0`` if the object *o* is considered to be true, and ``1`` otherwise.
292 This is equivalent to the Python expression ``not o``. On failure, return
293 ``-1``.
294
295
296.. cfunction:: PyObject* PyObject_Type(PyObject *o)
297
298 .. index:: builtin: type
299
300 When *o* is non-*NULL*, returns a type object corresponding to the object type
301 of object *o*. On failure, raises :exc:`SystemError` and returns *NULL*. This
302 is equivalent to the Python expression ``type(o)``. This function increments the
303 reference count of the return value. There's really no reason to use this
304 function instead of the common expression ``o->ob_type``, which returns a
305 pointer of type :ctype:`PyTypeObject\*`, except when the incremented reference
306 count is needed.
307
308
309.. cfunction:: int PyObject_TypeCheck(PyObject *o, PyTypeObject *type)
310
311 Return true if the object *o* is of type *type* or a subtype of *type*. Both
312 parameters must be non-*NULL*.
313
314 .. versionadded:: 2.2
315
316
317.. cfunction:: Py_ssize_t PyObject_Length(PyObject *o)
318 Py_ssize_t PyObject_Size(PyObject *o)
319
320 .. index:: builtin: len
321
322 Return the length of object *o*. If the object *o* provides either the sequence
323 and mapping protocols, the sequence length is returned. On error, ``-1`` is
324 returned. This is the equivalent to the Python expression ``len(o)``.
325
326
327.. cfunction:: PyObject* PyObject_GetItem(PyObject *o, PyObject *key)
328
329 Return element of *o* corresponding to the object *key* or *NULL* on failure.
330 This is the equivalent of the Python expression ``o[key]``.
331
332
333.. cfunction:: int PyObject_SetItem(PyObject *o, PyObject *key, PyObject *v)
334
335 Map the object *key* to the value *v*. Returns ``-1`` on failure. This is the
336 equivalent of the Python statement ``o[key] = v``.
337
338
339.. cfunction:: int PyObject_DelItem(PyObject *o, PyObject *key)
340
341 Delete the mapping for *key* from *o*. Returns ``-1`` on failure. This is the
342 equivalent of the Python statement ``del o[key]``.
343
344
345.. cfunction:: int PyObject_AsFileDescriptor(PyObject *o)
346
347 Derives a file-descriptor from a Python object. If the object is an integer or
348 long integer, its value is returned. If not, the object's :meth:`fileno` method
349 is called if it exists; the method must return an integer or long integer, which
350 is returned as the file descriptor value. Returns ``-1`` on failure.
351
352
353.. cfunction:: PyObject* PyObject_Dir(PyObject *o)
354
355 This is equivalent to the Python expression ``dir(o)``, returning a (possibly
356 empty) list of strings appropriate for the object argument, or *NULL* if there
357 was an error. If the argument is *NULL*, this is like the Python ``dir()``,
358 returning the names of the current locals; in this case, if no execution frame
359 is active then *NULL* is returned but :cfunc:`PyErr_Occurred` will return false.
360
361
362.. cfunction:: PyObject* PyObject_GetIter(PyObject *o)
363
364 This is equivalent to the Python expression ``iter(o)``. It returns a new
365 iterator for the object argument, or the object itself if the object is already
366 an iterator. Raises :exc:`TypeError` and returns *NULL* if the object cannot be
367 iterated.
368
369
370.. _number:
371
372Number Protocol
373===============
374
375
376.. cfunction:: int PyNumber_Check(PyObject *o)
377
378 Returns ``1`` if the object *o* provides numeric protocols, and false otherwise.
379 This function always succeeds.
380
381
382.. cfunction:: PyObject* PyNumber_Add(PyObject *o1, PyObject *o2)
383
384 Returns the result of adding *o1* and *o2*, or *NULL* on failure. This is the
385 equivalent of the Python expression ``o1 + o2``.
386
387
388.. cfunction:: PyObject* PyNumber_Subtract(PyObject *o1, PyObject *o2)
389
390 Returns the result of subtracting *o2* from *o1*, or *NULL* on failure. This is
391 the equivalent of the Python expression ``o1 - o2``.
392
393
394.. cfunction:: PyObject* PyNumber_Multiply(PyObject *o1, PyObject *o2)
395
396 Returns the result of multiplying *o1* and *o2*, or *NULL* on failure. This is
397 the equivalent of the Python expression ``o1 * o2``.
398
399
400.. cfunction:: PyObject* PyNumber_Divide(PyObject *o1, PyObject *o2)
401
402 Returns the result of dividing *o1* by *o2*, or *NULL* on failure. This is the
403 equivalent of the Python expression ``o1 / o2``.
404
405
406.. cfunction:: PyObject* PyNumber_FloorDivide(PyObject *o1, PyObject *o2)
407
408 Return the floor of *o1* divided by *o2*, or *NULL* on failure. This is
409 equivalent to the "classic" division of integers.
410
411 .. versionadded:: 2.2
412
413
414.. cfunction:: PyObject* PyNumber_TrueDivide(PyObject *o1, PyObject *o2)
415
416 Return a reasonable approximation for the mathematical value of *o1* divided by
417 *o2*, or *NULL* on failure. The return value is "approximate" because binary
418 floating point numbers are approximate; it is not possible to represent all real
419 numbers in base two. This function can return a floating point value when
420 passed two integers.
421
422 .. versionadded:: 2.2
423
424
425.. cfunction:: PyObject* PyNumber_Remainder(PyObject *o1, PyObject *o2)
426
427 Returns the remainder of dividing *o1* by *o2*, or *NULL* on failure. This is
428 the equivalent of the Python expression ``o1 % o2``.
429
430
431.. cfunction:: PyObject* PyNumber_Divmod(PyObject *o1, PyObject *o2)
432
433 .. index:: builtin: divmod
434
435 See the built-in function :func:`divmod`. Returns *NULL* on failure. This is
436 the equivalent of the Python expression ``divmod(o1, o2)``.
437
438
439.. cfunction:: PyObject* PyNumber_Power(PyObject *o1, PyObject *o2, PyObject *o3)
440
441 .. index:: builtin: pow
442
443 See the built-in function :func:`pow`. Returns *NULL* on failure. This is the
444 equivalent of the Python expression ``pow(o1, o2, o3)``, where *o3* is optional.
445 If *o3* is to be ignored, pass :cdata:`Py_None` in its place (passing *NULL* for
446 *o3* would cause an illegal memory access).
447
448
449.. cfunction:: PyObject* PyNumber_Negative(PyObject *o)
450
451 Returns the negation of *o* on success, or *NULL* on failure. This is the
452 equivalent of the Python expression ``-o``.
453
454
455.. cfunction:: PyObject* PyNumber_Positive(PyObject *o)
456
457 Returns *o* on success, or *NULL* on failure. This is the equivalent of the
458 Python expression ``+o``.
459
460
461.. cfunction:: PyObject* PyNumber_Absolute(PyObject *o)
462
463 .. index:: builtin: abs
464
465 Returns the absolute value of *o*, or *NULL* on failure. This is the equivalent
466 of the Python expression ``abs(o)``.
467
468
469.. cfunction:: PyObject* PyNumber_Invert(PyObject *o)
470
471 Returns the bitwise negation of *o* on success, or *NULL* on failure. This is
472 the equivalent of the Python expression ``~o``.
473
474
475.. cfunction:: PyObject* PyNumber_Lshift(PyObject *o1, PyObject *o2)
476
477 Returns the result of left shifting *o1* by *o2* on success, or *NULL* on
478 failure. This is the equivalent of the Python expression ``o1 << o2``.
479
480
481.. cfunction:: PyObject* PyNumber_Rshift(PyObject *o1, PyObject *o2)
482
483 Returns the result of right shifting *o1* by *o2* on success, or *NULL* on
484 failure. This is the equivalent of the Python expression ``o1 >> o2``.
485
486
487.. cfunction:: PyObject* PyNumber_And(PyObject *o1, PyObject *o2)
488
489 Returns the "bitwise and" of *o1* and *o2* on success and *NULL* on failure.
490 This is the equivalent of the Python expression ``o1 & o2``.
491
492
493.. cfunction:: PyObject* PyNumber_Xor(PyObject *o1, PyObject *o2)
494
495 Returns the "bitwise exclusive or" of *o1* by *o2* on success, or *NULL* on
496 failure. This is the equivalent of the Python expression ``o1 ^ o2``.
497
498
499.. cfunction:: PyObject* PyNumber_Or(PyObject *o1, PyObject *o2)
500
501 Returns the "bitwise or" of *o1* and *o2* on success, or *NULL* on failure.
502 This is the equivalent of the Python expression ``o1 | o2``.
503
504
505.. cfunction:: PyObject* PyNumber_InPlaceAdd(PyObject *o1, PyObject *o2)
506
507 Returns the result of adding *o1* and *o2*, or *NULL* on failure. The operation
508 is done *in-place* when *o1* supports it. This is the equivalent of the Python
509 statement ``o1 += o2``.
510
511
512.. cfunction:: PyObject* PyNumber_InPlaceSubtract(PyObject *o1, PyObject *o2)
513
514 Returns the result of subtracting *o2* from *o1*, or *NULL* on failure. The
515 operation is done *in-place* when *o1* supports it. This is the equivalent of
516 the Python statement ``o1 -= o2``.
517
518
519.. cfunction:: PyObject* PyNumber_InPlaceMultiply(PyObject *o1, PyObject *o2)
520
521 Returns the result of multiplying *o1* and *o2*, or *NULL* on failure. The
522 operation is done *in-place* when *o1* supports it. This is the equivalent of
523 the Python statement ``o1 *= o2``.
524
525
526.. cfunction:: PyObject* PyNumber_InPlaceDivide(PyObject *o1, PyObject *o2)
527
528 Returns the result of dividing *o1* by *o2*, or *NULL* on failure. The
529 operation is done *in-place* when *o1* supports it. This is the equivalent of
530 the Python statement ``o1 /= o2``.
531
532
533.. cfunction:: PyObject* PyNumber_InPlaceFloorDivide(PyObject *o1, PyObject *o2)
534
535 Returns the mathematical floor of dividing *o1* by *o2*, or *NULL* on failure.
536 The operation is done *in-place* when *o1* supports it. This is the equivalent
537 of the Python statement ``o1 //= o2``.
538
539 .. versionadded:: 2.2
540
541
542.. cfunction:: PyObject* PyNumber_InPlaceTrueDivide(PyObject *o1, PyObject *o2)
543
544 Return a reasonable approximation for the mathematical value of *o1* divided by
545 *o2*, or *NULL* on failure. The return value is "approximate" because binary
546 floating point numbers are approximate; it is not possible to represent all real
547 numbers in base two. This function can return a floating point value when
548 passed two integers. The operation is done *in-place* when *o1* supports it.
549
550 .. versionadded:: 2.2
551
552
553.. cfunction:: PyObject* PyNumber_InPlaceRemainder(PyObject *o1, PyObject *o2)
554
555 Returns the remainder of dividing *o1* by *o2*, or *NULL* on failure. The
556 operation is done *in-place* when *o1* supports it. This is the equivalent of
557 the Python statement ``o1 %= o2``.
558
559
560.. cfunction:: PyObject* PyNumber_InPlacePower(PyObject *o1, PyObject *o2, PyObject *o3)
561
562 .. index:: builtin: pow
563
564 See the built-in function :func:`pow`. Returns *NULL* on failure. The operation
565 is done *in-place* when *o1* supports it. This is the equivalent of the Python
566 statement ``o1 **= o2`` when o3 is :cdata:`Py_None`, or an in-place variant of
567 ``pow(o1, o2, o3)`` otherwise. If *o3* is to be ignored, pass :cdata:`Py_None`
568 in its place (passing *NULL* for *o3* would cause an illegal memory access).
569
570
571.. cfunction:: PyObject* PyNumber_InPlaceLshift(PyObject *o1, PyObject *o2)
572
573 Returns the result of left shifting *o1* by *o2* on success, or *NULL* on
574 failure. The operation is done *in-place* when *o1* supports it. This is the
575 equivalent of the Python statement ``o1 <<= o2``.
576
577
578.. cfunction:: PyObject* PyNumber_InPlaceRshift(PyObject *o1, PyObject *o2)
579
580 Returns the result of right shifting *o1* by *o2* on success, or *NULL* on
581 failure. The operation is done *in-place* when *o1* supports it. This is the
582 equivalent of the Python statement ``o1 >>= o2``.
583
584
585.. cfunction:: PyObject* PyNumber_InPlaceAnd(PyObject *o1, PyObject *o2)
586
587 Returns the "bitwise and" of *o1* and *o2* on success and *NULL* on failure. The
588 operation is done *in-place* when *o1* supports it. This is the equivalent of
589 the Python statement ``o1 &= o2``.
590
591
592.. cfunction:: PyObject* PyNumber_InPlaceXor(PyObject *o1, PyObject *o2)
593
594 Returns the "bitwise exclusive or" of *o1* by *o2* on success, or *NULL* on
595 failure. The operation is done *in-place* when *o1* supports it. This is the
596 equivalent of the Python statement ``o1 ^= o2``.
597
598
599.. cfunction:: PyObject* PyNumber_InPlaceOr(PyObject *o1, PyObject *o2)
600
601 Returns the "bitwise or" of *o1* and *o2* on success, or *NULL* on failure. The
602 operation is done *in-place* when *o1* supports it. This is the equivalent of
603 the Python statement ``o1 |= o2``.
604
605
606.. cfunction:: PyObject* PyNumber_Int(PyObject *o)
607
608 .. index:: builtin: int
609
610 Returns the *o* converted to an integer object on success, or *NULL* on failure.
611 If the argument is outside the integer range a long object will be returned
612 instead. This is the equivalent of the Python expression ``int(o)``.
613
614
615.. cfunction:: PyObject* PyNumber_Long(PyObject *o)
616
617 .. index:: builtin: long
618
619 Returns the *o* converted to a long integer object on success, or *NULL* on
620 failure. This is the equivalent of the Python expression ``long(o)``.
621
622
623.. cfunction:: PyObject* PyNumber_Float(PyObject *o)
624
625 .. index:: builtin: float
626
627 Returns the *o* converted to a float object on success, or *NULL* on failure.
628 This is the equivalent of the Python expression ``float(o)``.
629
630
631.. cfunction:: PyObject* PyNumber_Index(PyObject *o)
632
633 Returns the *o* converted to a Python int or long on success or *NULL* with a
634 TypeError exception raised on failure.
635
636 .. versionadded:: 2.5
637
638
639.. cfunction:: Py_ssize_t PyNumber_AsSsize_t(PyObject *o, PyObject *exc)
640
641 Returns *o* converted to a Py_ssize_t value if *o* can be interpreted as an
642 integer. If *o* can be converted to a Python int or long but the attempt to
643 convert to a Py_ssize_t value would raise an :exc:`OverflowError`, then the
644 *exc* argument is the type of exception that will be raised (usually
645 :exc:`IndexError` or :exc:`OverflowError`). If *exc* is *NULL*, then the
646 exception is cleared and the value is clipped to *PY_SSIZE_T_MIN* for a negative
647 integer or *PY_SSIZE_T_MAX* for a positive integer.
648
649 .. versionadded:: 2.5
650
651
652.. cfunction:: int PyIndex_Check(PyObject *o)
653
654 Returns True if *o* is an index integer (has the nb_index slot of the
655 tp_as_number structure filled in).
656
657 .. versionadded:: 2.5
658
659
660.. _sequence:
661
662Sequence Protocol
663=================
664
665
666.. cfunction:: int PySequence_Check(PyObject *o)
667
668 Return ``1`` if the object provides sequence protocol, and ``0`` otherwise.
669 This function always succeeds.
670
671
672.. cfunction:: Py_ssize_t PySequence_Size(PyObject *o)
673
674 .. index:: builtin: len
675
676 Returns the number of objects in sequence *o* on success, and ``-1`` on failure.
677 For objects that do not provide sequence protocol, this is equivalent to the
678 Python expression ``len(o)``.
679
680
681.. cfunction:: Py_ssize_t PySequence_Length(PyObject *o)
682
683 Alternate name for :cfunc:`PySequence_Size`.
684
685
686.. cfunction:: PyObject* PySequence_Concat(PyObject *o1, PyObject *o2)
687
688 Return the concatenation of *o1* and *o2* on success, and *NULL* on failure.
689 This is the equivalent of the Python expression ``o1 + o2``.
690
691
692.. cfunction:: PyObject* PySequence_Repeat(PyObject *o, Py_ssize_t count)
693
694 Return the result of repeating sequence object *o* *count* times, or *NULL* on
695 failure. This is the equivalent of the Python expression ``o * count``.
696
697
698.. cfunction:: PyObject* PySequence_InPlaceConcat(PyObject *o1, PyObject *o2)
699
700 Return the concatenation of *o1* and *o2* on success, and *NULL* on failure.
701 The operation is done *in-place* when *o1* supports it. This is the equivalent
702 of the Python expression ``o1 += o2``.
703
704
705.. cfunction:: PyObject* PySequence_InPlaceRepeat(PyObject *o, Py_ssize_t count)
706
707 Return the result of repeating sequence object *o* *count* times, or *NULL* on
708 failure. The operation is done *in-place* when *o* supports it. This is the
709 equivalent of the Python expression ``o *= count``.
710
711
712.. cfunction:: PyObject* PySequence_GetItem(PyObject *o, Py_ssize_t i)
713
714 Return the *i*th element of *o*, or *NULL* on failure. This is the equivalent of
715 the Python expression ``o[i]``.
716
717
718.. cfunction:: PyObject* PySequence_GetSlice(PyObject *o, Py_ssize_t i1, Py_ssize_t i2)
719
720 Return the slice of sequence object *o* between *i1* and *i2*, or *NULL* on
721 failure. This is the equivalent of the Python expression ``o[i1:i2]``.
722
723
724.. cfunction:: int PySequence_SetItem(PyObject *o, Py_ssize_t i, PyObject *v)
725
726 Assign object *v* to the *i*th element of *o*. Returns ``-1`` on failure. This
727 is the equivalent of the Python statement ``o[i] = v``. This function *does
728 not* steal a reference to *v*.
729
730
731.. cfunction:: int PySequence_DelItem(PyObject *o, Py_ssize_t i)
732
733 Delete the *i*th element of object *o*. Returns ``-1`` on failure. This is the
734 equivalent of the Python statement ``del o[i]``.
735
736
737.. cfunction:: int PySequence_SetSlice(PyObject *o, Py_ssize_t i1, Py_ssize_t i2, PyObject *v)
738
739 Assign the sequence object *v* to the slice in sequence object *o* from *i1* to
740 *i2*. This is the equivalent of the Python statement ``o[i1:i2] = v``.
741
742
743.. cfunction:: int PySequence_DelSlice(PyObject *o, Py_ssize_t i1, Py_ssize_t i2)
744
745 Delete the slice in sequence object *o* from *i1* to *i2*. Returns ``-1`` on
746 failure. This is the equivalent of the Python statement ``del o[i1:i2]``.
747
748
749.. cfunction:: Py_ssize_t PySequence_Count(PyObject *o, PyObject *value)
750
751 Return the number of occurrences of *value* in *o*, that is, return the number
752 of keys for which ``o[key] == value``. On failure, return ``-1``. This is
753 equivalent to the Python expression ``o.count(value)``.
754
755
756.. cfunction:: int PySequence_Contains(PyObject *o, PyObject *value)
757
758 Determine if *o* contains *value*. If an item in *o* is equal to *value*,
759 return ``1``, otherwise return ``0``. On error, return ``-1``. This is
760 equivalent to the Python expression ``value in o``.
761
762
763.. cfunction:: Py_ssize_t PySequence_Index(PyObject *o, PyObject *value)
764
765 Return the first index *i* for which ``o[i] == value``. On error, return
766 ``-1``. This is equivalent to the Python expression ``o.index(value)``.
767
768
769.. cfunction:: PyObject* PySequence_List(PyObject *o)
770
771 Return a list object with the same contents as the arbitrary sequence *o*. The
772 returned list is guaranteed to be new.
773
774
775.. cfunction:: PyObject* PySequence_Tuple(PyObject *o)
776
777 .. index:: builtin: tuple
778
779 Return a tuple object with the same contents as the arbitrary sequence *o* or
780 *NULL* on failure. If *o* is a tuple, a new reference will be returned,
781 otherwise a tuple will be constructed with the appropriate contents. This is
782 equivalent to the Python expression ``tuple(o)``.
783
784
785.. cfunction:: PyObject* PySequence_Fast(PyObject *o, const char *m)
786
787 Returns the sequence *o* as a tuple, unless it is already a tuple or list, in
788 which case *o* is returned. Use :cfunc:`PySequence_Fast_GET_ITEM` to access the
789 members of the result. Returns *NULL* on failure. If the object is not a
790 sequence, raises :exc:`TypeError` with *m* as the message text.
791
792
793.. cfunction:: PyObject* PySequence_Fast_GET_ITEM(PyObject *o, Py_ssize_t i)
794
795 Return the *i*th element of *o*, assuming that *o* was returned by
796 :cfunc:`PySequence_Fast`, *o* is not *NULL*, and that *i* is within bounds.
797
798
799.. cfunction:: PyObject** PySequence_Fast_ITEMS(PyObject *o)
800
801 Return the underlying array of PyObject pointers. Assumes that *o* was returned
802 by :cfunc:`PySequence_Fast` and *o* is not *NULL*.
803
804 .. versionadded:: 2.4
805
806
807.. cfunction:: PyObject* PySequence_ITEM(PyObject *o, Py_ssize_t i)
808
809 Return the *i*th element of *o* or *NULL* on failure. Macro form of
810 :cfunc:`PySequence_GetItem` but without checking that
811 :cfunc:`PySequence_Check(o)` is true and without adjustment for negative
812 indices.
813
814 .. versionadded:: 2.3
815
816
817.. cfunction:: Py_ssize_t PySequence_Fast_GET_SIZE(PyObject *o)
818
819 Returns the length of *o*, assuming that *o* was returned by
820 :cfunc:`PySequence_Fast` and that *o* is not *NULL*. The size can also be
821 gotten by calling :cfunc:`PySequence_Size` on *o*, but
822 :cfunc:`PySequence_Fast_GET_SIZE` is faster because it can assume *o* is a list
823 or tuple.
824
825
826.. _mapping:
827
828Mapping Protocol
829================
830
831
832.. cfunction:: int PyMapping_Check(PyObject *o)
833
834 Return ``1`` if the object provides mapping protocol, and ``0`` otherwise. This
835 function always succeeds.
836
837
838.. cfunction:: Py_ssize_t PyMapping_Length(PyObject *o)
839
840 .. index:: builtin: len
841
842 Returns the number of keys in object *o* on success, and ``-1`` on failure. For
843 objects that do not provide mapping protocol, this is equivalent to the Python
844 expression ``len(o)``.
845
846
847.. cfunction:: int PyMapping_DelItemString(PyObject *o, char *key)
848
849 Remove the mapping for object *key* from the object *o*. Return ``-1`` on
850 failure. This is equivalent to the Python statement ``del o[key]``.
851
852
853.. cfunction:: int PyMapping_DelItem(PyObject *o, PyObject *key)
854
855 Remove the mapping for object *key* from the object *o*. Return ``-1`` on
856 failure. This is equivalent to the Python statement ``del o[key]``.
857
858
859.. cfunction:: int PyMapping_HasKeyString(PyObject *o, char *key)
860
861 On success, return ``1`` if the mapping object has the key *key* and ``0``
862 otherwise. This is equivalent to the Python expression ``o.has_key(key)``.
863 This function always succeeds.
864
865
866.. cfunction:: int PyMapping_HasKey(PyObject *o, PyObject *key)
867
868 Return ``1`` if the mapping object has the key *key* and ``0`` otherwise. This
869 is equivalent to the Python expression ``o.has_key(key)``. This function always
870 succeeds.
871
872
873.. cfunction:: PyObject* PyMapping_Keys(PyObject *o)
874
875 On success, return a list of the keys in object *o*. On failure, return *NULL*.
876 This is equivalent to the Python expression ``o.keys()``.
877
878
879.. cfunction:: PyObject* PyMapping_Values(PyObject *o)
880
881 On success, return a list of the values in object *o*. On failure, return
882 *NULL*. This is equivalent to the Python expression ``o.values()``.
883
884
885.. cfunction:: PyObject* PyMapping_Items(PyObject *o)
886
887 On success, return a list of the items in object *o*, where each item is a tuple
888 containing a key-value pair. On failure, return *NULL*. This is equivalent to
889 the Python expression ``o.items()``.
890
891
892.. cfunction:: PyObject* PyMapping_GetItemString(PyObject *o, char *key)
893
894 Return element of *o* corresponding to the object *key* or *NULL* on failure.
895 This is the equivalent of the Python expression ``o[key]``.
896
897
898.. cfunction:: int PyMapping_SetItemString(PyObject *o, char *key, PyObject *v)
899
900 Map the object *key* to the value *v* in object *o*. Returns ``-1`` on failure.
901 This is the equivalent of the Python statement ``o[key] = v``.
902
903
904.. _iterator:
905
906Iterator Protocol
907=================
908
909.. versionadded:: 2.2
910
911There are only a couple of functions specifically for working with iterators.
912
913
914.. cfunction:: int PyIter_Check(PyObject *o)
915
916 Return true if the object *o* supports the iterator protocol.
917
918
919.. cfunction:: PyObject* PyIter_Next(PyObject *o)
920
921 Return the next value from the iteration *o*. If the object is an iterator,
922 this retrieves the next value from the iteration, and returns *NULL* with no
923 exception set if there are no remaining items. If the object is not an
924 iterator, :exc:`TypeError` is raised, or if there is an error in retrieving the
925 item, returns *NULL* and passes along the exception.
926
927To write a loop which iterates over an iterator, the C code should look
928something like this::
929
930 PyObject *iterator = PyObject_GetIter(obj);
931 PyObject *item;
932
933 if (iterator == NULL) {
934 /* propagate error */
935 }
936
937 while (item = PyIter_Next(iterator)) {
938 /* do something with item */
939 ...
940 /* release reference when done */
941 Py_DECREF(item);
942 }
943
944 Py_DECREF(iterator);
945
946 if (PyErr_Occurred()) {
947 /* propagate error */
948 }
949 else {
950 /* continue doing useful work */
951 }
952
953
954.. _abstract-buffer:
955
956Buffer Protocol
957===============
958
959
960.. cfunction:: int PyObject_AsCharBuffer(PyObject *obj, const char **buffer, Py_ssize_t *buffer_len)
961
962 Returns a pointer to a read-only memory location useable as character- based
963 input. The *obj* argument must support the single-segment character buffer
964 interface. On success, returns ``0``, sets *buffer* to the memory location and
965 *buffer_len* to the buffer length. Returns ``-1`` and sets a :exc:`TypeError`
966 on error.
967
968 .. versionadded:: 1.6
969
970
971.. cfunction:: int PyObject_AsReadBuffer(PyObject *obj, const void **buffer, Py_ssize_t *buffer_len)
972
973 Returns a pointer to a read-only memory location containing arbitrary data. The
974 *obj* argument must support the single-segment readable buffer interface. On
975 success, returns ``0``, sets *buffer* to the memory location and *buffer_len* to
976 the buffer length. Returns ``-1`` and sets a :exc:`TypeError` on error.
977
978 .. versionadded:: 1.6
979
980
981.. cfunction:: int PyObject_CheckReadBuffer(PyObject *o)
982
983 Returns ``1`` if *o* supports the single-segment readable buffer interface.
984 Otherwise returns ``0``.
985
986 .. versionadded:: 2.2
987
988
989.. cfunction:: int PyObject_AsWriteBuffer(PyObject *obj, void **buffer, Py_ssize_t *buffer_len)
990
991 Returns a pointer to a writeable memory location. The *obj* argument must
992 support the single-segment, character buffer interface. On success, returns
993 ``0``, sets *buffer* to the memory location and *buffer_len* to the buffer
994 length. Returns ``-1`` and sets a :exc:`TypeError` on error.
995
996 .. versionadded:: 1.6
997