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