blob: 39f0e78de0087ebc429d0ae2bbb52f0c7e350de9 [file] [log] [blame]
Georg Brandlf6842722008-01-19 22:08:21 +00001.. highlightlang:: c
2
3.. _number:
4
5Number Protocol
6===============
7
8
Sandro Tosi98ed08f2012-01-14 16:42:02 +01009.. c:function:: int PyNumber_Check(PyObject *o)
Georg Brandlf6842722008-01-19 22:08:21 +000010
11 Returns ``1`` if the object *o* provides numeric protocols, and false otherwise.
12 This function always succeeds.
13
14
Sandro Tosi98ed08f2012-01-14 16:42:02 +010015.. c:function:: PyObject* PyNumber_Add(PyObject *o1, PyObject *o2)
Georg Brandlf6842722008-01-19 22:08:21 +000016
17 Returns the result of adding *o1* and *o2*, or *NULL* on failure. This is the
18 equivalent of the Python expression ``o1 + o2``.
19
20
Sandro Tosi98ed08f2012-01-14 16:42:02 +010021.. c:function:: PyObject* PyNumber_Subtract(PyObject *o1, PyObject *o2)
Georg Brandlf6842722008-01-19 22:08:21 +000022
23 Returns the result of subtracting *o2* from *o1*, or *NULL* on failure. This is
24 the equivalent of the Python expression ``o1 - o2``.
25
26
Sandro Tosi98ed08f2012-01-14 16:42:02 +010027.. c:function:: PyObject* PyNumber_Multiply(PyObject *o1, PyObject *o2)
Georg Brandlf6842722008-01-19 22:08:21 +000028
29 Returns the result of multiplying *o1* and *o2*, or *NULL* on failure. This is
30 the equivalent of the Python expression ``o1 * o2``.
31
32
Sandro Tosi98ed08f2012-01-14 16:42:02 +010033.. c:function:: PyObject* PyNumber_Divide(PyObject *o1, PyObject *o2)
Georg Brandlf6842722008-01-19 22:08:21 +000034
35 Returns the result of dividing *o1* by *o2*, or *NULL* on failure. This is the
36 equivalent of the Python expression ``o1 / o2``.
37
38
Sandro Tosi98ed08f2012-01-14 16:42:02 +010039.. c:function:: PyObject* PyNumber_FloorDivide(PyObject *o1, PyObject *o2)
Georg Brandlf6842722008-01-19 22:08:21 +000040
41 Return the floor of *o1* divided by *o2*, or *NULL* on failure. This is
42 equivalent to the "classic" division of integers.
43
44 .. versionadded:: 2.2
45
46
Sandro Tosi98ed08f2012-01-14 16:42:02 +010047.. c:function:: PyObject* PyNumber_TrueDivide(PyObject *o1, PyObject *o2)
Georg Brandlf6842722008-01-19 22:08:21 +000048
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.
54
55 .. versionadded:: 2.2
56
57
Sandro Tosi98ed08f2012-01-14 16:42:02 +010058.. c:function:: PyObject* PyNumber_Remainder(PyObject *o1, PyObject *o2)
Georg Brandlf6842722008-01-19 22:08:21 +000059
60 Returns the remainder of dividing *o1* by *o2*, or *NULL* on failure. This is
61 the equivalent of the Python expression ``o1 % o2``.
62
63
Sandro Tosi98ed08f2012-01-14 16:42:02 +010064.. c:function:: PyObject* PyNumber_Divmod(PyObject *o1, PyObject *o2)
Georg Brandlf6842722008-01-19 22:08:21 +000065
66 .. index:: builtin: divmod
67
68 See the built-in function :func:`divmod`. Returns *NULL* on failure. This is
69 the equivalent of the Python expression ``divmod(o1, o2)``.
70
71
Sandro Tosi98ed08f2012-01-14 16:42:02 +010072.. c:function:: PyObject* PyNumber_Power(PyObject *o1, PyObject *o2, PyObject *o3)
Georg Brandlf6842722008-01-19 22:08:21 +000073
74 .. index:: builtin: pow
75
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.
Sandro Tosi98ed08f2012-01-14 16:42:02 +010078 If *o3* is to be ignored, pass :c:data:`Py_None` in its place (passing *NULL* for
Georg Brandlf6842722008-01-19 22:08:21 +000079 *o3* would cause an illegal memory access).
80
81
Sandro Tosi98ed08f2012-01-14 16:42:02 +010082.. c:function:: PyObject* PyNumber_Negative(PyObject *o)
Georg Brandlf6842722008-01-19 22:08:21 +000083
84 Returns the negation of *o* on success, or *NULL* on failure. This is the
85 equivalent of the Python expression ``-o``.
86
87
Sandro Tosi98ed08f2012-01-14 16:42:02 +010088.. c:function:: PyObject* PyNumber_Positive(PyObject *o)
Georg Brandlf6842722008-01-19 22:08:21 +000089
90 Returns *o* on success, or *NULL* on failure. This is the equivalent of the
91 Python expression ``+o``.
92
93
Sandro Tosi98ed08f2012-01-14 16:42:02 +010094.. c:function:: PyObject* PyNumber_Absolute(PyObject *o)
Georg Brandlf6842722008-01-19 22:08:21 +000095
96 .. index:: builtin: abs
97
98 Returns the absolute value of *o*, or *NULL* on failure. This is the equivalent
99 of the Python expression ``abs(o)``.
100
101
Sandro Tosi98ed08f2012-01-14 16:42:02 +0100102.. c:function:: PyObject* PyNumber_Invert(PyObject *o)
Georg Brandlf6842722008-01-19 22:08:21 +0000103
104 Returns the bitwise negation of *o* on success, or *NULL* on failure. This is
105 the equivalent of the Python expression ``~o``.
106
107
Sandro Tosi98ed08f2012-01-14 16:42:02 +0100108.. c:function:: PyObject* PyNumber_Lshift(PyObject *o1, PyObject *o2)
Georg Brandlf6842722008-01-19 22:08:21 +0000109
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``.
112
113
Sandro Tosi98ed08f2012-01-14 16:42:02 +0100114.. c:function:: PyObject* PyNumber_Rshift(PyObject *o1, PyObject *o2)
Georg Brandlf6842722008-01-19 22:08:21 +0000115
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``.
118
119
Sandro Tosi98ed08f2012-01-14 16:42:02 +0100120.. c:function:: PyObject* PyNumber_And(PyObject *o1, PyObject *o2)
Georg Brandlf6842722008-01-19 22:08:21 +0000121
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``.
124
125
Sandro Tosi98ed08f2012-01-14 16:42:02 +0100126.. c:function:: PyObject* PyNumber_Xor(PyObject *o1, PyObject *o2)
Georg Brandlf6842722008-01-19 22:08:21 +0000127
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``.
130
131
Sandro Tosi98ed08f2012-01-14 16:42:02 +0100132.. c:function:: PyObject* PyNumber_Or(PyObject *o1, PyObject *o2)
Georg Brandlf6842722008-01-19 22:08:21 +0000133
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``.
136
137
Sandro Tosi98ed08f2012-01-14 16:42:02 +0100138.. c:function:: PyObject* PyNumber_InPlaceAdd(PyObject *o1, PyObject *o2)
Georg Brandlf6842722008-01-19 22:08:21 +0000139
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``.
143
144
Sandro Tosi98ed08f2012-01-14 16:42:02 +0100145.. c:function:: PyObject* PyNumber_InPlaceSubtract(PyObject *o1, PyObject *o2)
Georg Brandlf6842722008-01-19 22:08:21 +0000146
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``.
150
151
Sandro Tosi98ed08f2012-01-14 16:42:02 +0100152.. c:function:: PyObject* PyNumber_InPlaceMultiply(PyObject *o1, PyObject *o2)
Georg Brandlf6842722008-01-19 22:08:21 +0000153
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``.
157
158
Sandro Tosi98ed08f2012-01-14 16:42:02 +0100159.. c:function:: PyObject* PyNumber_InPlaceDivide(PyObject *o1, PyObject *o2)
Georg Brandlf6842722008-01-19 22:08:21 +0000160
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``.
164
165
Sandro Tosi98ed08f2012-01-14 16:42:02 +0100166.. c:function:: PyObject* PyNumber_InPlaceFloorDivide(PyObject *o1, PyObject *o2)
Georg Brandlf6842722008-01-19 22:08:21 +0000167
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``.
171
172 .. versionadded:: 2.2
173
174
Sandro Tosi98ed08f2012-01-14 16:42:02 +0100175.. c:function:: PyObject* PyNumber_InPlaceTrueDivide(PyObject *o1, PyObject *o2)
Georg Brandlf6842722008-01-19 22:08:21 +0000176
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.
182
183 .. versionadded:: 2.2
184
185
Sandro Tosi98ed08f2012-01-14 16:42:02 +0100186.. c:function:: PyObject* PyNumber_InPlaceRemainder(PyObject *o1, PyObject *o2)
Georg Brandlf6842722008-01-19 22:08:21 +0000187
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``.
191
192
Sandro Tosi98ed08f2012-01-14 16:42:02 +0100193.. c:function:: PyObject* PyNumber_InPlacePower(PyObject *o1, PyObject *o2, PyObject *o3)
Georg Brandlf6842722008-01-19 22:08:21 +0000194
195 .. index:: builtin: pow
196
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
Sandro Tosi98ed08f2012-01-14 16:42:02 +0100199 statement ``o1 **= o2`` when o3 is :c:data:`Py_None`, or an in-place variant of
200 ``pow(o1, o2, o3)`` otherwise. If *o3* is to be ignored, pass :c:data:`Py_None`
Georg Brandlf6842722008-01-19 22:08:21 +0000201 in its place (passing *NULL* for *o3* would cause an illegal memory access).
202
203
Sandro Tosi98ed08f2012-01-14 16:42:02 +0100204.. c:function:: PyObject* PyNumber_InPlaceLshift(PyObject *o1, PyObject *o2)
Georg Brandlf6842722008-01-19 22:08:21 +0000205
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``.
209
210
Sandro Tosi98ed08f2012-01-14 16:42:02 +0100211.. c:function:: PyObject* PyNumber_InPlaceRshift(PyObject *o1, PyObject *o2)
Georg Brandlf6842722008-01-19 22:08:21 +0000212
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``.
216
217
Sandro Tosi98ed08f2012-01-14 16:42:02 +0100218.. c:function:: PyObject* PyNumber_InPlaceAnd(PyObject *o1, PyObject *o2)
Georg Brandlf6842722008-01-19 22:08:21 +0000219
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``.
223
224
Sandro Tosi98ed08f2012-01-14 16:42:02 +0100225.. c:function:: PyObject* PyNumber_InPlaceXor(PyObject *o1, PyObject *o2)
Georg Brandlf6842722008-01-19 22:08:21 +0000226
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``.
230
231
Sandro Tosi98ed08f2012-01-14 16:42:02 +0100232.. c:function:: PyObject* PyNumber_InPlaceOr(PyObject *o1, PyObject *o2)
Georg Brandlf6842722008-01-19 22:08:21 +0000233
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``.
237
238
Sandro Tosi98ed08f2012-01-14 16:42:02 +0100239.. c:function:: int PyNumber_Coerce(PyObject **p1, PyObject **p2)
Georg Brandlf6842722008-01-19 22:08:21 +0000240
241 .. index:: builtin: coerce
242
Sandro Tosi98ed08f2012-01-14 16:42:02 +0100243 This function takes the addresses of two variables of type :c:type:`PyObject\*`.
Georg Brandlf6842722008-01-19 22:08:21 +0000244 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)``.
251
252
Sandro Tosi98ed08f2012-01-14 16:42:02 +0100253.. c:function:: int PyNumber_CoerceEx(PyObject **p1, PyObject **p2)
Georg Brandlf6842722008-01-19 22:08:21 +0000254
Sandro Tosi98ed08f2012-01-14 16:42:02 +0100255 This function is similar to :c:func:`PyNumber_Coerce`, except that it returns
Georg Brandlf6842722008-01-19 22:08:21 +0000256 ``1`` when the conversion is not possible and when no error is raised.
257 Reference counts are still not increased in this case.
258
259
Sandro Tosi98ed08f2012-01-14 16:42:02 +0100260.. c:function:: PyObject* PyNumber_Int(PyObject *o)
Georg Brandlf6842722008-01-19 22:08:21 +0000261
262 .. index:: builtin: int
263
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)``.
267
268
Sandro Tosi98ed08f2012-01-14 16:42:02 +0100269.. c:function:: PyObject* PyNumber_Long(PyObject *o)
Georg Brandlf6842722008-01-19 22:08:21 +0000270
271 .. index:: builtin: long
272
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)``.
275
276
Sandro Tosi98ed08f2012-01-14 16:42:02 +0100277.. c:function:: PyObject* PyNumber_Float(PyObject *o)
Georg Brandlf6842722008-01-19 22:08:21 +0000278
279 .. index:: builtin: float
280
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)``.
283
284
Sandro Tosi98ed08f2012-01-14 16:42:02 +0100285.. c:function:: PyObject* PyNumber_Index(PyObject *o)
Georg Brandlf6842722008-01-19 22:08:21 +0000286
287 Returns the *o* converted to a Python int or long on success or *NULL* with a
Andrew M. Kuchling17ff29d2008-09-30 13:00:34 +0000288 :exc:`TypeError` exception raised on failure.
Georg Brandlf6842722008-01-19 22:08:21 +0000289
290 .. versionadded:: 2.5
291
292
Sandro Tosi98ed08f2012-01-14 16:42:02 +0100293.. c:function:: PyObject* PyNumber_ToBase(PyObject *n, int base)
Benjamin Petersonfe4948b2008-04-27 18:14:39 +0000294
Andrew M. Kuchlingaa34f5a2009-04-03 21:44:49 +0000295 Returns the integer *n* converted to *base* as a string with a base
296 marker of ``'0b'``, ``'0o'``, or ``'0x'`` if applicable. When
Benjamin Petersonfe4948b2008-04-27 18:14:39 +0000297 *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
Sandro Tosi98ed08f2012-01-14 16:42:02 +0100299 :c:func:`PyNumber_Index` first.
Benjamin Petersonfe4948b2008-04-27 18:14:39 +0000300
301 .. versionadded:: 2.6
302
303
Sandro Tosi98ed08f2012-01-14 16:42:02 +0100304.. c:function:: Py_ssize_t PyNumber_AsSsize_t(PyObject *o, PyObject *exc)
Georg Brandlf6842722008-01-19 22:08:21 +0000305
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.
313
314 .. versionadded:: 2.5
315
316
Sandro Tosi98ed08f2012-01-14 16:42:02 +0100317.. c:function:: int PyIndex_Check(PyObject *o)
Georg Brandlf6842722008-01-19 22:08:21 +0000318
Serhiy Storchakadc0e3a82016-10-19 18:30:16 +0300319 Returns ``1`` if *o* is an index integer (has the nb_index slot of the
320 tp_as_number structure filled in), and ``0`` otherwise.
Georg Brandlf6842722008-01-19 22:08:21 +0000321
322 .. versionadded:: 2.5