blob: 9a539874040fcd5c52459d6b0a3e04aae9b889e5 [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
Brett Cannon7b201162007-10-16 23:26:45 +000034.. cfunction:: int PyObject_HasAttr(PyObject *o, PyObject *attr_name)
Georg Brandl8ec7f652007-08-15 14:28:01 +000035
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
Brett Cannon7b201162007-10-16 23:26:45 +000041.. cfunction:: int PyObject_HasAttrString(PyObject *o, const char *attr_name)
Georg Brandl8ec7f652007-08-15 14:28:01 +000042
43 Returns ``1`` if *o* has the attribute *attr_name*, and ``0`` otherwise. This
44 is equivalent to the Python expression ``hasattr(o, attr_name)``. This function
45 always succeeds.
46
47
48.. cfunction:: PyObject* PyObject_GetAttr(PyObject *o, PyObject *attr_name)
49
50 Retrieve an attribute named *attr_name* from object *o*. Returns the attribute
51 value on success, or *NULL* on failure. This is the equivalent of the Python
52 expression ``o.attr_name``.
53
54
Brett Cannon7b201162007-10-16 23:26:45 +000055.. cfunction:: PyObject* PyObject_GetAttrString(PyObject *o, const char *attr_name)
Georg Brandl8ec7f652007-08-15 14:28:01 +000056
Brett Cannon7b201162007-10-16 23:26:45 +000057 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``.
Georg Brandl8ec7f652007-08-15 14:28:01 +000060
61
62.. cfunction:: int PyObject_SetAttr(PyObject *o, PyObject *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
Brett Cannon7b201162007-10-16 23:26:45 +000069.. cfunction:: int PyObject_SetAttrString(PyObject *o, const char *attr_name, PyObject *v)
Georg Brandl8ec7f652007-08-15 14:28:01 +000070
Brett Cannon7b201162007-10-16 23:26:45 +000071 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``.
Georg Brandl8ec7f652007-08-15 14:28:01 +000074
75
76.. cfunction:: int PyObject_DelAttr(PyObject *o, PyObject *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
Brett Cannon7b201162007-10-16 23:26:45 +000082.. cfunction:: int PyObject_DelAttrString(PyObject *o, const char *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
Georg Brandl8ec7f652007-08-15 14:28:01 +000088.. 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
Georg Brandl814b0462007-10-08 14:12:47 +0000627.. cfunction:: int PyNumber_CoerceEx(PyObject **p1, PyObject **p2)
628
629 This function is similar to :cfunc:`PyNumber_Coerce`, except that it returns
630 ``1`` when the conversion is not possible and when no error is raised.
631 Reference counts are still not increased in this case.
632
633
Georg Brandl8ec7f652007-08-15 14:28:01 +0000634.. cfunction:: PyObject* PyNumber_Int(PyObject *o)
635
636 .. index:: builtin: int
637
638 Returns the *o* converted to an integer object on success, or *NULL* on failure.
639 If the argument is outside the integer range a long object will be returned
640 instead. This is the equivalent of the Python expression ``int(o)``.
641
642
643.. cfunction:: PyObject* PyNumber_Long(PyObject *o)
644
645 .. index:: builtin: long
646
647 Returns the *o* converted to a long integer object on success, or *NULL* on
648 failure. This is the equivalent of the Python expression ``long(o)``.
649
650
651.. cfunction:: PyObject* PyNumber_Float(PyObject *o)
652
653 .. index:: builtin: float
654
655 Returns the *o* converted to a float object on success, or *NULL* on failure.
656 This is the equivalent of the Python expression ``float(o)``.
657
658
659.. cfunction:: PyObject* PyNumber_Index(PyObject *o)
660
661 Returns the *o* converted to a Python int or long on success or *NULL* with a
662 TypeError exception raised on failure.
663
664 .. versionadded:: 2.5
665
666
667.. cfunction:: Py_ssize_t PyNumber_AsSsize_t(PyObject *o, PyObject *exc)
668
669 Returns *o* converted to a Py_ssize_t value if *o* can be interpreted as an
670 integer. If *o* can be converted to a Python int or long but the attempt to
671 convert to a Py_ssize_t value would raise an :exc:`OverflowError`, then the
672 *exc* argument is the type of exception that will be raised (usually
673 :exc:`IndexError` or :exc:`OverflowError`). If *exc* is *NULL*, then the
674 exception is cleared and the value is clipped to *PY_SSIZE_T_MIN* for a negative
675 integer or *PY_SSIZE_T_MAX* for a positive integer.
676
677 .. versionadded:: 2.5
678
679
680.. cfunction:: int PyIndex_Check(PyObject *o)
681
682 Returns True if *o* is an index integer (has the nb_index slot of the
683 tp_as_number structure filled in).
684
685 .. versionadded:: 2.5
686
687
688.. _sequence:
689
690Sequence Protocol
691=================
692
693
694.. cfunction:: int PySequence_Check(PyObject *o)
695
696 Return ``1`` if the object provides sequence protocol, and ``0`` otherwise.
697 This function always succeeds.
698
699
700.. cfunction:: Py_ssize_t PySequence_Size(PyObject *o)
701
702 .. index:: builtin: len
703
704 Returns the number of objects in sequence *o* on success, and ``-1`` on failure.
705 For objects that do not provide sequence protocol, this is equivalent to the
706 Python expression ``len(o)``.
707
708
709.. cfunction:: Py_ssize_t PySequence_Length(PyObject *o)
710
711 Alternate name for :cfunc:`PySequence_Size`.
712
713
714.. cfunction:: PyObject* PySequence_Concat(PyObject *o1, PyObject *o2)
715
716 Return the concatenation of *o1* and *o2* on success, and *NULL* on failure.
717 This is the equivalent of the Python expression ``o1 + o2``.
718
719
720.. cfunction:: PyObject* PySequence_Repeat(PyObject *o, Py_ssize_t count)
721
722 Return the result of repeating sequence object *o* *count* times, or *NULL* on
723 failure. This is the equivalent of the Python expression ``o * count``.
724
725
726.. cfunction:: PyObject* PySequence_InPlaceConcat(PyObject *o1, PyObject *o2)
727
728 Return the concatenation of *o1* and *o2* on success, and *NULL* on failure.
729 The operation is done *in-place* when *o1* supports it. This is the equivalent
730 of the Python expression ``o1 += o2``.
731
732
733.. cfunction:: PyObject* PySequence_InPlaceRepeat(PyObject *o, Py_ssize_t count)
734
735 Return the result of repeating sequence object *o* *count* times, or *NULL* on
736 failure. The operation is done *in-place* when *o* supports it. This is the
737 equivalent of the Python expression ``o *= count``.
738
739
740.. cfunction:: PyObject* PySequence_GetItem(PyObject *o, Py_ssize_t i)
741
742 Return the *i*th element of *o*, or *NULL* on failure. This is the equivalent of
743 the Python expression ``o[i]``.
744
745
746.. cfunction:: PyObject* PySequence_GetSlice(PyObject *o, Py_ssize_t i1, Py_ssize_t i2)
747
748 Return the slice of sequence object *o* between *i1* and *i2*, or *NULL* on
749 failure. This is the equivalent of the Python expression ``o[i1:i2]``.
750
751
752.. cfunction:: int PySequence_SetItem(PyObject *o, Py_ssize_t i, PyObject *v)
753
754 Assign object *v* to the *i*th element of *o*. Returns ``-1`` on failure. This
755 is the equivalent of the Python statement ``o[i] = v``. This function *does
756 not* steal a reference to *v*.
757
758
759.. cfunction:: int PySequence_DelItem(PyObject *o, Py_ssize_t i)
760
761 Delete the *i*th element of object *o*. Returns ``-1`` on failure. This is the
762 equivalent of the Python statement ``del o[i]``.
763
764
765.. cfunction:: int PySequence_SetSlice(PyObject *o, Py_ssize_t i1, Py_ssize_t i2, PyObject *v)
766
767 Assign the sequence object *v* to the slice in sequence object *o* from *i1* to
768 *i2*. This is the equivalent of the Python statement ``o[i1:i2] = v``.
769
770
771.. cfunction:: int PySequence_DelSlice(PyObject *o, Py_ssize_t i1, Py_ssize_t i2)
772
773 Delete the slice in sequence object *o* from *i1* to *i2*. Returns ``-1`` on
774 failure. This is the equivalent of the Python statement ``del o[i1:i2]``.
775
776
777.. cfunction:: Py_ssize_t PySequence_Count(PyObject *o, PyObject *value)
778
779 Return the number of occurrences of *value* in *o*, that is, return the number
780 of keys for which ``o[key] == value``. On failure, return ``-1``. This is
781 equivalent to the Python expression ``o.count(value)``.
782
783
784.. cfunction:: int PySequence_Contains(PyObject *o, PyObject *value)
785
786 Determine if *o* contains *value*. If an item in *o* is equal to *value*,
787 return ``1``, otherwise return ``0``. On error, return ``-1``. This is
788 equivalent to the Python expression ``value in o``.
789
790
791.. cfunction:: Py_ssize_t PySequence_Index(PyObject *o, PyObject *value)
792
793 Return the first index *i* for which ``o[i] == value``. On error, return
794 ``-1``. This is equivalent to the Python expression ``o.index(value)``.
795
796
797.. cfunction:: PyObject* PySequence_List(PyObject *o)
798
799 Return a list object with the same contents as the arbitrary sequence *o*. The
800 returned list is guaranteed to be new.
801
802
803.. cfunction:: PyObject* PySequence_Tuple(PyObject *o)
804
805 .. index:: builtin: tuple
806
807 Return a tuple object with the same contents as the arbitrary sequence *o* or
808 *NULL* on failure. If *o* is a tuple, a new reference will be returned,
809 otherwise a tuple will be constructed with the appropriate contents. This is
810 equivalent to the Python expression ``tuple(o)``.
811
812
813.. cfunction:: PyObject* PySequence_Fast(PyObject *o, const char *m)
814
815 Returns the sequence *o* as a tuple, unless it is already a tuple or list, in
816 which case *o* is returned. Use :cfunc:`PySequence_Fast_GET_ITEM` to access the
817 members of the result. Returns *NULL* on failure. If the object is not a
818 sequence, raises :exc:`TypeError` with *m* as the message text.
819
820
821.. cfunction:: PyObject* PySequence_Fast_GET_ITEM(PyObject *o, Py_ssize_t i)
822
823 Return the *i*th element of *o*, assuming that *o* was returned by
824 :cfunc:`PySequence_Fast`, *o* is not *NULL*, and that *i* is within bounds.
825
826
827.. cfunction:: PyObject** PySequence_Fast_ITEMS(PyObject *o)
828
829 Return the underlying array of PyObject pointers. Assumes that *o* was returned
830 by :cfunc:`PySequence_Fast` and *o* is not *NULL*.
831
832 .. versionadded:: 2.4
833
834
835.. cfunction:: PyObject* PySequence_ITEM(PyObject *o, Py_ssize_t i)
836
837 Return the *i*th element of *o* or *NULL* on failure. Macro form of
838 :cfunc:`PySequence_GetItem` but without checking that
839 :cfunc:`PySequence_Check(o)` is true and without adjustment for negative
840 indices.
841
842 .. versionadded:: 2.3
843
844
845.. cfunction:: Py_ssize_t PySequence_Fast_GET_SIZE(PyObject *o)
846
847 Returns the length of *o*, assuming that *o* was returned by
848 :cfunc:`PySequence_Fast` and that *o* is not *NULL*. The size can also be
849 gotten by calling :cfunc:`PySequence_Size` on *o*, but
850 :cfunc:`PySequence_Fast_GET_SIZE` is faster because it can assume *o* is a list
851 or tuple.
852
853
854.. _mapping:
855
856Mapping Protocol
857================
858
859
860.. cfunction:: int PyMapping_Check(PyObject *o)
861
862 Return ``1`` if the object provides mapping protocol, and ``0`` otherwise. This
863 function always succeeds.
864
865
866.. cfunction:: Py_ssize_t PyMapping_Length(PyObject *o)
867
868 .. index:: builtin: len
869
870 Returns the number of keys in object *o* on success, and ``-1`` on failure. For
871 objects that do not provide mapping protocol, this is equivalent to the Python
872 expression ``len(o)``.
873
874
875.. cfunction:: int PyMapping_DelItemString(PyObject *o, char *key)
876
877 Remove the mapping for object *key* from the object *o*. Return ``-1`` on
878 failure. This is equivalent to the Python statement ``del o[key]``.
879
880
881.. cfunction:: int PyMapping_DelItem(PyObject *o, PyObject *key)
882
883 Remove the mapping for object *key* from the object *o*. Return ``-1`` on
884 failure. This is equivalent to the Python statement ``del o[key]``.
885
886
887.. cfunction:: int PyMapping_HasKeyString(PyObject *o, char *key)
888
889 On success, return ``1`` if the mapping object has the key *key* and ``0``
890 otherwise. This is equivalent to the Python expression ``o.has_key(key)``.
891 This function always succeeds.
892
893
894.. cfunction:: int PyMapping_HasKey(PyObject *o, PyObject *key)
895
896 Return ``1`` if the mapping object has the key *key* and ``0`` otherwise. This
897 is equivalent to the Python expression ``o.has_key(key)``. This function always
898 succeeds.
899
900
901.. cfunction:: PyObject* PyMapping_Keys(PyObject *o)
902
903 On success, return a list of the keys in object *o*. On failure, return *NULL*.
904 This is equivalent to the Python expression ``o.keys()``.
905
906
907.. cfunction:: PyObject* PyMapping_Values(PyObject *o)
908
909 On success, return a list of the values in object *o*. On failure, return
910 *NULL*. This is equivalent to the Python expression ``o.values()``.
911
912
913.. cfunction:: PyObject* PyMapping_Items(PyObject *o)
914
915 On success, return a list of the items in object *o*, where each item is a tuple
916 containing a key-value pair. On failure, return *NULL*. This is equivalent to
917 the Python expression ``o.items()``.
918
919
920.. cfunction:: PyObject* PyMapping_GetItemString(PyObject *o, char *key)
921
922 Return element of *o* corresponding to the object *key* or *NULL* on failure.
923 This is the equivalent of the Python expression ``o[key]``.
924
925
926.. cfunction:: int PyMapping_SetItemString(PyObject *o, char *key, PyObject *v)
927
928 Map the object *key* to the value *v* in object *o*. Returns ``-1`` on failure.
929 This is the equivalent of the Python statement ``o[key] = v``.
930
931
932.. _iterator:
933
934Iterator Protocol
935=================
936
937.. versionadded:: 2.2
938
939There are only a couple of functions specifically for working with iterators.
940
941
942.. cfunction:: int PyIter_Check(PyObject *o)
943
944 Return true if the object *o* supports the iterator protocol.
945
946
947.. cfunction:: PyObject* PyIter_Next(PyObject *o)
948
949 Return the next value from the iteration *o*. If the object is an iterator,
950 this retrieves the next value from the iteration, and returns *NULL* with no
951 exception set if there are no remaining items. If the object is not an
952 iterator, :exc:`TypeError` is raised, or if there is an error in retrieving the
953 item, returns *NULL* and passes along the exception.
954
955To write a loop which iterates over an iterator, the C code should look
956something like this::
957
958 PyObject *iterator = PyObject_GetIter(obj);
959 PyObject *item;
960
961 if (iterator == NULL) {
962 /* propagate error */
963 }
964
965 while (item = PyIter_Next(iterator)) {
966 /* do something with item */
967 ...
968 /* release reference when done */
969 Py_DECREF(item);
970 }
971
972 Py_DECREF(iterator);
973
974 if (PyErr_Occurred()) {
975 /* propagate error */
976 }
977 else {
978 /* continue doing useful work */
979 }
980
981
982.. _abstract-buffer:
983
984Buffer Protocol
985===============
986
987
988.. cfunction:: int PyObject_AsCharBuffer(PyObject *obj, const char **buffer, Py_ssize_t *buffer_len)
989
990 Returns a pointer to a read-only memory location useable as character- based
991 input. The *obj* argument must support the single-segment character buffer
992 interface. On success, returns ``0``, sets *buffer* to the memory location and
993 *buffer_len* to the buffer length. Returns ``-1`` and sets a :exc:`TypeError`
994 on error.
995
996 .. versionadded:: 1.6
997
998
999.. cfunction:: int PyObject_AsReadBuffer(PyObject *obj, const void **buffer, Py_ssize_t *buffer_len)
1000
1001 Returns a pointer to a read-only memory location containing arbitrary data. The
1002 *obj* argument must support the single-segment readable buffer interface. On
1003 success, returns ``0``, sets *buffer* to the memory location and *buffer_len* to
1004 the buffer length. Returns ``-1`` and sets a :exc:`TypeError` on error.
1005
1006 .. versionadded:: 1.6
1007
1008
1009.. cfunction:: int PyObject_CheckReadBuffer(PyObject *o)
1010
1011 Returns ``1`` if *o* supports the single-segment readable buffer interface.
1012 Otherwise returns ``0``.
1013
1014 .. versionadded:: 2.2
1015
1016
1017.. cfunction:: int PyObject_AsWriteBuffer(PyObject *obj, void **buffer, Py_ssize_t *buffer_len)
1018
1019 Returns a pointer to a writeable memory location. The *obj* argument must
1020 support the single-segment, character buffer interface. On success, returns
1021 ``0``, sets *buffer* to the memory location and *buffer_len* to the buffer
1022 length. Returns ``-1`` and sets a :exc:`TypeError` on error.
1023
1024 .. versionadded:: 1.6
1025