Issue #7205: Fix a possible deadlock when using a BZ2File object from several threads...
[python.git] / Doc / c-api / number.rst
blobffc2585525fe96aea3910a27ca48ad9353f2da32
1 .. highlightlang:: c
3 .. _number:
5 Number Protocol
6 ===============
9 .. cfunction:: int PyNumber_Check(PyObject *o)
11    Returns ``1`` if the object *o* provides numeric protocols, and false otherwise.
12    This function always succeeds.
15 .. cfunction:: PyObject* PyNumber_Add(PyObject *o1, PyObject *o2)
17    Returns the result of adding *o1* and *o2*, or *NULL* on failure.  This is the
18    equivalent of the Python expression ``o1 + o2``.
21 .. cfunction:: PyObject* PyNumber_Subtract(PyObject *o1, PyObject *o2)
23    Returns the result of subtracting *o2* from *o1*, or *NULL* on failure.  This is
24    the equivalent of the Python expression ``o1 - o2``.
27 .. cfunction:: PyObject* PyNumber_Multiply(PyObject *o1, PyObject *o2)
29    Returns the result of multiplying *o1* and *o2*, or *NULL* on failure.  This is
30    the equivalent of the Python expression ``o1 * o2``.
33 .. cfunction:: PyObject* PyNumber_Divide(PyObject *o1, PyObject *o2)
35    Returns the result of dividing *o1* by *o2*, or *NULL* on failure.  This is the
36    equivalent of the Python expression ``o1 / o2``.
39 .. cfunction:: PyObject* PyNumber_FloorDivide(PyObject *o1, PyObject *o2)
41    Return the floor of *o1* divided by *o2*, or *NULL* on failure.  This is
42    equivalent to the "classic" division of integers.
44    .. versionadded:: 2.2
47 .. cfunction:: PyObject* PyNumber_TrueDivide(PyObject *o1, PyObject *o2)
49    Return a reasonable approximation for the mathematical value of *o1* divided by
50    *o2*, or *NULL* on failure.  The return value is "approximate" because binary
51    floating point numbers are approximate; it is not possible to represent all real
52    numbers in base two.  This function can return a floating point value when
53    passed two integers.
55    .. versionadded:: 2.2
58 .. cfunction:: PyObject* PyNumber_Remainder(PyObject *o1, PyObject *o2)
60    Returns the remainder of dividing *o1* by *o2*, or *NULL* on failure.  This is
61    the equivalent of the Python expression ``o1 % o2``.
64 .. cfunction:: PyObject* PyNumber_Divmod(PyObject *o1, PyObject *o2)
66    .. index:: builtin: divmod
68    See the built-in function :func:`divmod`. Returns *NULL* on failure.  This is
69    the equivalent of the Python expression ``divmod(o1, o2)``.
72 .. cfunction:: PyObject* PyNumber_Power(PyObject *o1, PyObject *o2, PyObject *o3)
74    .. index:: builtin: pow
76    See the built-in function :func:`pow`. Returns *NULL* on failure.  This is the
77    equivalent of the Python expression ``pow(o1, o2, o3)``, where *o3* is optional.
78    If *o3* is to be ignored, pass :cdata:`Py_None` in its place (passing *NULL* for
79    *o3* would cause an illegal memory access).
82 .. cfunction:: PyObject* PyNumber_Negative(PyObject *o)
84    Returns the negation of *o* on success, or *NULL* on failure. This is the
85    equivalent of the Python expression ``-o``.
88 .. cfunction:: PyObject* PyNumber_Positive(PyObject *o)
90    Returns *o* on success, or *NULL* on failure.  This is the equivalent of the
91    Python expression ``+o``.
94 .. cfunction:: PyObject* PyNumber_Absolute(PyObject *o)
96    .. index:: builtin: abs
98    Returns the absolute value of *o*, or *NULL* on failure.  This is the equivalent
99    of the Python expression ``abs(o)``.
102 .. cfunction:: PyObject* PyNumber_Invert(PyObject *o)
104    Returns the bitwise negation of *o* on success, or *NULL* on failure.  This is
105    the equivalent of the Python expression ``~o``.
108 .. cfunction:: PyObject* PyNumber_Lshift(PyObject *o1, PyObject *o2)
110    Returns the result of left shifting *o1* by *o2* on success, or *NULL* on
111    failure.  This is the equivalent of the Python expression ``o1 << o2``.
114 .. cfunction:: PyObject* PyNumber_Rshift(PyObject *o1, PyObject *o2)
116    Returns the result of right shifting *o1* by *o2* on success, or *NULL* on
117    failure.  This is the equivalent of the Python expression ``o1 >> o2``.
120 .. cfunction:: PyObject* PyNumber_And(PyObject *o1, PyObject *o2)
122    Returns the "bitwise and" of *o1* and *o2* on success and *NULL* on failure.
123    This is the equivalent of the Python expression ``o1 & o2``.
126 .. cfunction:: PyObject* PyNumber_Xor(PyObject *o1, PyObject *o2)
128    Returns the "bitwise exclusive or" of *o1* by *o2* on success, or *NULL* on
129    failure.  This is the equivalent of the Python expression ``o1 ^ o2``.
132 .. cfunction:: PyObject* PyNumber_Or(PyObject *o1, PyObject *o2)
134    Returns the "bitwise or" of *o1* and *o2* on success, or *NULL* on failure.
135    This is the equivalent of the Python expression ``o1 | o2``.
138 .. cfunction:: PyObject* PyNumber_InPlaceAdd(PyObject *o1, PyObject *o2)
140    Returns the result of adding *o1* and *o2*, or *NULL* on failure.  The operation
141    is done *in-place* when *o1* supports it.  This is the equivalent of the Python
142    statement ``o1 += o2``.
145 .. cfunction:: PyObject* PyNumber_InPlaceSubtract(PyObject *o1, PyObject *o2)
147    Returns the result of subtracting *o2* from *o1*, or *NULL* on failure.  The
148    operation is done *in-place* when *o1* supports it.  This is the equivalent of
149    the Python statement ``o1 -= o2``.
152 .. cfunction:: PyObject* PyNumber_InPlaceMultiply(PyObject *o1, PyObject *o2)
154    Returns the result of multiplying *o1* and *o2*, or *NULL* on failure.  The
155    operation is done *in-place* when *o1* supports it.  This is the equivalent of
156    the Python statement ``o1 *= o2``.
159 .. cfunction:: PyObject* PyNumber_InPlaceDivide(PyObject *o1, PyObject *o2)
161    Returns the result of dividing *o1* by *o2*, or *NULL* on failure.  The
162    operation is done *in-place* when *o1* supports it. This is the equivalent of
163    the Python statement ``o1 /= o2``.
166 .. cfunction:: PyObject* PyNumber_InPlaceFloorDivide(PyObject *o1, PyObject *o2)
168    Returns the mathematical floor of dividing *o1* by *o2*, or *NULL* on failure.
169    The operation is done *in-place* when *o1* supports it.  This is the equivalent
170    of the Python statement ``o1 //= o2``.
172    .. versionadded:: 2.2
175 .. cfunction:: PyObject* PyNumber_InPlaceTrueDivide(PyObject *o1, PyObject *o2)
177    Return a reasonable approximation for the mathematical value of *o1* divided by
178    *o2*, or *NULL* on failure.  The return value is "approximate" because binary
179    floating point numbers are approximate; it is not possible to represent all real
180    numbers in base two.  This function can return a floating point value when
181    passed two integers.  The operation is done *in-place* when *o1* supports it.
183    .. versionadded:: 2.2
186 .. cfunction:: PyObject* PyNumber_InPlaceRemainder(PyObject *o1, PyObject *o2)
188    Returns the remainder of dividing *o1* by *o2*, or *NULL* on failure.  The
189    operation is done *in-place* when *o1* supports it.  This is the equivalent of
190    the Python statement ``o1 %= o2``.
193 .. cfunction:: PyObject* PyNumber_InPlacePower(PyObject *o1, PyObject *o2, PyObject *o3)
195    .. index:: builtin: pow
197    See the built-in function :func:`pow`. Returns *NULL* on failure.  The operation
198    is done *in-place* when *o1* supports it.  This is the equivalent of the Python
199    statement ``o1 **= o2`` when o3 is :cdata:`Py_None`, or an in-place variant of
200    ``pow(o1, o2, o3)`` otherwise. If *o3* is to be ignored, pass :cdata:`Py_None`
201    in its place (passing *NULL* for *o3* would cause an illegal memory access).
204 .. cfunction:: PyObject* PyNumber_InPlaceLshift(PyObject *o1, PyObject *o2)
206    Returns the result of left shifting *o1* by *o2* on success, or *NULL* on
207    failure.  The operation is done *in-place* when *o1* supports it.  This is the
208    equivalent of the Python statement ``o1 <<= o2``.
211 .. cfunction:: PyObject* PyNumber_InPlaceRshift(PyObject *o1, PyObject *o2)
213    Returns the result of right shifting *o1* by *o2* on success, or *NULL* on
214    failure.  The operation is done *in-place* when *o1* supports it.  This is the
215    equivalent of the Python statement ``o1 >>= o2``.
218 .. cfunction:: PyObject* PyNumber_InPlaceAnd(PyObject *o1, PyObject *o2)
220    Returns the "bitwise and" of *o1* and *o2* on success and *NULL* on failure. The
221    operation is done *in-place* when *o1* supports it.  This is the equivalent of
222    the Python statement ``o1 &= o2``.
225 .. cfunction:: PyObject* PyNumber_InPlaceXor(PyObject *o1, PyObject *o2)
227    Returns the "bitwise exclusive or" of *o1* by *o2* on success, or *NULL* on
228    failure.  The operation is done *in-place* when *o1* supports it.  This is the
229    equivalent of the Python statement ``o1 ^= o2``.
232 .. cfunction:: PyObject* PyNumber_InPlaceOr(PyObject *o1, PyObject *o2)
234    Returns the "bitwise or" of *o1* and *o2* on success, or *NULL* on failure.  The
235    operation is done *in-place* when *o1* supports it.  This is the equivalent of
236    the Python statement ``o1 |= o2``.
239 .. cfunction:: int PyNumber_Coerce(PyObject **p1, PyObject **p2)
241    .. index:: builtin: coerce
243    This function takes the addresses of two variables of type :ctype:`PyObject\*`.
244    If the objects pointed to by ``*p1`` and ``*p2`` have the same type, increment
245    their reference count and return ``0`` (success). If the objects can be
246    converted to a common numeric type, replace ``*p1`` and ``*p2`` by their
247    converted value (with 'new' reference counts), and return ``0``. If no
248    conversion is possible, or if some other error occurs, return ``-1`` (failure)
249    and don't increment the reference counts.  The call ``PyNumber_Coerce(&o1,
250    &o2)`` is equivalent to the Python statement ``o1, o2 = coerce(o1, o2)``.
253 .. cfunction:: int PyNumber_CoerceEx(PyObject **p1, PyObject **p2)
255    This function is similar to :cfunc:`PyNumber_Coerce`, except that it returns
256    ``1`` when the conversion is not possible and when no error is raised.
257    Reference counts are still not increased in this case.
260 .. cfunction:: PyObject* PyNumber_Int(PyObject *o)
262    .. index:: builtin: int
264    Returns the *o* converted to an integer object on success, or *NULL* on failure.
265    If the argument is outside the integer range a long object will be returned
266    instead. This is the equivalent of the Python expression ``int(o)``.
269 .. cfunction:: PyObject* PyNumber_Long(PyObject *o)
271    .. index:: builtin: long
273    Returns the *o* converted to a long integer object on success, or *NULL* on
274    failure.  This is the equivalent of the Python expression ``long(o)``.
277 .. cfunction:: PyObject* PyNumber_Float(PyObject *o)
279    .. index:: builtin: float
281    Returns the *o* converted to a float object on success, or *NULL* on failure.
282    This is the equivalent of the Python expression ``float(o)``.
285 .. cfunction:: PyObject* PyNumber_Index(PyObject *o)
287    Returns the *o* converted to a Python int or long on success or *NULL* with a
288    :exc:`TypeError` exception raised on failure.
290    .. versionadded:: 2.5
293 .. cfunction:: PyObject* PyNumber_ToBase(PyObject *n, int base)
295    Returns the integer *n* converted to *base* as a string with a base
296    marker of ``'0b'``, ``'0o'``, or ``'0x'`` if applicable.  When
297    *base* is not 2, 8, 10, or 16, the format is ``'x#num'`` where x is the
298    base. If *n* is not an int object, it is converted with
299    :cfunc:`PyNumber_Index` first.
301    .. versionadded:: 2.6
304 .. cfunction:: Py_ssize_t PyNumber_AsSsize_t(PyObject *o, PyObject *exc)
306    Returns *o* converted to a Py_ssize_t value if *o* can be interpreted as an
307    integer. If *o* can be converted to a Python int or long but the attempt to
308    convert to a Py_ssize_t value would raise an :exc:`OverflowError`, then the
309    *exc* argument is the type of exception that will be raised (usually
310    :exc:`IndexError` or :exc:`OverflowError`).  If *exc* is *NULL*, then the
311    exception is cleared and the value is clipped to *PY_SSIZE_T_MIN* for a negative
312    integer or *PY_SSIZE_T_MAX* for a positive integer.
314    .. versionadded:: 2.5
317 .. cfunction:: int PyIndex_Check(PyObject *o)
319    Returns True if *o* is an index integer (has the nb_index slot of  the
320    tp_as_number structure filled in).
322    .. versionadded:: 2.5