blob: 4f16b578eb5999aca1173c002fa126ebe8863552 [file] [log] [blame]
Georg Brandl54a3faa2008-01-20 09:30:57 +00001.. highlightlang:: c
2
3.. _longobjects:
4
5Integer Objects
6---------------
7
8.. index:: object: long integer
9 object: integer
10
11All integers are implemented as "long" integer objects of arbitrary size.
12
Gregory P. Smithf5b04a32018-01-28 17:48:31 -080013On error, most ``PyLong_As*`` APIs return ``(return type)-1`` which cannot be
14distinguished from a number. Use :c:func:`PyErr_Occurred` to disambiguate.
15
Georg Brandl60203b42010-10-06 10:11:56 +000016.. c:type:: PyLongObject
Georg Brandl54a3faa2008-01-20 09:30:57 +000017
Georg Brandl60203b42010-10-06 10:11:56 +000018 This subtype of :c:type:`PyObject` represents a Python integer object.
Georg Brandl54a3faa2008-01-20 09:30:57 +000019
20
Georg Brandl60203b42010-10-06 10:11:56 +000021.. c:var:: PyTypeObject PyLong_Type
Georg Brandl54a3faa2008-01-20 09:30:57 +000022
Georg Brandl60203b42010-10-06 10:11:56 +000023 This instance of :c:type:`PyTypeObject` represents the Python integer type.
Georg Brandl2aff3352010-10-17 10:59:41 +000024 This is the same object as :class:`int` in the Python layer.
Georg Brandl54a3faa2008-01-20 09:30:57 +000025
26
Georg Brandl60203b42010-10-06 10:11:56 +000027.. c:function:: int PyLong_Check(PyObject *p)
Georg Brandl54a3faa2008-01-20 09:30:57 +000028
Georg Brandl60203b42010-10-06 10:11:56 +000029 Return true if its argument is a :c:type:`PyLongObject` or a subtype of
30 :c:type:`PyLongObject`.
Georg Brandl54a3faa2008-01-20 09:30:57 +000031
32
Georg Brandl60203b42010-10-06 10:11:56 +000033.. c:function:: int PyLong_CheckExact(PyObject *p)
Georg Brandl54a3faa2008-01-20 09:30:57 +000034
Georg Brandl60203b42010-10-06 10:11:56 +000035 Return true if its argument is a :c:type:`PyLongObject`, but not a subtype of
36 :c:type:`PyLongObject`.
Georg Brandl54a3faa2008-01-20 09:30:57 +000037
38
Georg Brandl60203b42010-10-06 10:11:56 +000039.. c:function:: PyObject* PyLong_FromLong(long v)
Georg Brandl54a3faa2008-01-20 09:30:57 +000040
Georg Brandl60203b42010-10-06 10:11:56 +000041 Return a new :c:type:`PyLongObject` object from *v*, or *NULL* on failure.
Georg Brandl54a3faa2008-01-20 09:30:57 +000042
43 The current implementation keeps an array of integer objects for all integers
44 between ``-5`` and ``256``, when you create an int in that range you actually
45 just get back a reference to the existing object. So it should be possible to
46 change the value of ``1``. I suspect the behaviour of Python in this case is
47 undefined. :-)
48
49
Georg Brandl60203b42010-10-06 10:11:56 +000050.. c:function:: PyObject* PyLong_FromUnsignedLong(unsigned long v)
Georg Brandl54a3faa2008-01-20 09:30:57 +000051
Georg Brandl60203b42010-10-06 10:11:56 +000052 Return a new :c:type:`PyLongObject` object from a C :c:type:`unsigned long`, or
Georg Brandl54a3faa2008-01-20 09:30:57 +000053 *NULL* on failure.
54
55
Georg Brandl60203b42010-10-06 10:11:56 +000056.. c:function:: PyObject* PyLong_FromSsize_t(Py_ssize_t v)
Georg Brandl54a3faa2008-01-20 09:30:57 +000057
Georg Brandl60203b42010-10-06 10:11:56 +000058 Return a new :c:type:`PyLongObject` object from a C :c:type:`Py_ssize_t`, or
Christian Heimes81ee3ef2008-05-04 22:42:01 +000059 *NULL* on failure.
Georg Brandl54a3faa2008-01-20 09:30:57 +000060
61
Georg Brandl60203b42010-10-06 10:11:56 +000062.. c:function:: PyObject* PyLong_FromSize_t(size_t v)
Georg Brandl54a3faa2008-01-20 09:30:57 +000063
Georg Brandl60203b42010-10-06 10:11:56 +000064 Return a new :c:type:`PyLongObject` object from a C :c:type:`size_t`, or
Christian Heimes81ee3ef2008-05-04 22:42:01 +000065 *NULL* on failure.
Georg Brandl54a3faa2008-01-20 09:30:57 +000066
67
Benjamin Peterson47ff0732016-09-08 09:15:54 -070068.. c:function:: PyObject* PyLong_FromLongLong(long long v)
Georg Brandl54a3faa2008-01-20 09:30:57 +000069
Georg Brandl60203b42010-10-06 10:11:56 +000070 Return a new :c:type:`PyLongObject` object from a C :c:type:`long long`, or *NULL*
Georg Brandl54a3faa2008-01-20 09:30:57 +000071 on failure.
72
73
Benjamin Peterson47ff0732016-09-08 09:15:54 -070074.. c:function:: PyObject* PyLong_FromUnsignedLongLong(unsigned long long v)
Georg Brandl54a3faa2008-01-20 09:30:57 +000075
Georg Brandl60203b42010-10-06 10:11:56 +000076 Return a new :c:type:`PyLongObject` object from a C :c:type:`unsigned long long`,
Georg Brandl54a3faa2008-01-20 09:30:57 +000077 or *NULL* on failure.
78
79
Georg Brandl60203b42010-10-06 10:11:56 +000080.. c:function:: PyObject* PyLong_FromDouble(double v)
Georg Brandl54a3faa2008-01-20 09:30:57 +000081
Georg Brandl60203b42010-10-06 10:11:56 +000082 Return a new :c:type:`PyLongObject` object from the integer part of *v*, or
Georg Brandl54a3faa2008-01-20 09:30:57 +000083 *NULL* on failure.
84
85
Serhiy Storchakac6792272013-10-19 21:03:34 +030086.. c:function:: PyObject* PyLong_FromString(const char *str, char **pend, int base)
Georg Brandl54a3faa2008-01-20 09:30:57 +000087
Georg Brandl60203b42010-10-06 10:11:56 +000088 Return a new :c:type:`PyLongObject` based on the string value in *str*, which
Georg Brandl54a3faa2008-01-20 09:30:57 +000089 is interpreted according to the radix in *base*. If *pend* is non-*NULL*,
Ezio Melottiea30c6f2010-01-30 13:32:14 +000090 *\*pend* will point to the first character in *str* which follows the
csabella26896f22017-04-23 23:54:08 -040091 representation of the number. If *base* is ``0``, *str* is interpreted using
92 the :ref:`integers` definition; in this case, leading zeros in a
93 non-zero decimal number raises a :exc:`ValueError`. If *base* is not ``0``,
94 it must be between ``2`` and ``36``, inclusive. Leading spaces and single
95 underscores after a base specifier and between digits are ignored. If there
96 are no digits, :exc:`ValueError` will be raised.
Georg Brandl54a3faa2008-01-20 09:30:57 +000097
98
Georg Brandl60203b42010-10-06 10:11:56 +000099.. c:function:: PyObject* PyLong_FromUnicode(Py_UNICODE *u, Py_ssize_t length, int base)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000100
101 Convert a sequence of Unicode digits to a Python integer value. The Unicode
Georg Brandl60203b42010-10-06 10:11:56 +0000102 string is first encoded to a byte string using :c:func:`PyUnicode_EncodeDecimal`
103 and then converted using :c:func:`PyLong_FromString`.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000104
Georg Brandldb6c7f52011-10-07 11:19:11 +0200105 .. deprecated-removed:: 3.3 4.0
106 Part of the old-style :c:type:`Py_UNICODE` API; please migrate to using
107 :c:func:`PyLong_FromUnicodeObject`.
108
109
110.. c:function:: PyObject* PyLong_FromUnicodeObject(PyObject *u, int base)
111
112 Convert a sequence of Unicode digits in the string *u* to a Python integer
113 value. The Unicode string is first encoded to a byte string using
114 :c:func:`PyUnicode_EncodeDecimal` and then converted using
115 :c:func:`PyLong_FromString`.
116
117 .. versionadded:: 3.3
118
Georg Brandl54a3faa2008-01-20 09:30:57 +0000119
Georg Brandl60203b42010-10-06 10:11:56 +0000120.. c:function:: PyObject* PyLong_FromVoidPtr(void *p)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000121
122 Create a Python integer from the pointer *p*. The pointer value can be
Georg Brandl60203b42010-10-06 10:11:56 +0000123 retrieved from the resulting value using :c:func:`PyLong_AsVoidPtr`.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000124
125
Georg Brandl48310cd2009-01-03 21:18:54 +0000126.. XXX alias PyLong_AS_LONG (for now)
Mark Dickinson0a229242012-06-23 10:49:12 +0100127.. c:function:: long PyLong_AsLong(PyObject *obj)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000128
129 .. index::
130 single: LONG_MAX
131 single: OverflowError (built-in exception)
132
Mark Dickinson0a229242012-06-23 10:49:12 +0100133 Return a C :c:type:`long` representation of *obj*. If *obj* is not an
134 instance of :c:type:`PyLongObject`, first call its :meth:`__int__` method
135 (if present) to convert it to a :c:type:`PyLongObject`.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000136
Mark Dickinson0a229242012-06-23 10:49:12 +0100137 Raise :exc:`OverflowError` if the value of *obj* is out of range for a
138 :c:type:`long`.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000139
Gregory P. Smithf5b04a32018-01-28 17:48:31 -0800140 Returns -1 on error. Use :c:func:`PyErr_Occurred` to disambiguate.
141
Mark Dickinsonf0acfee2012-06-23 11:14:22 +0100142
Mark Dickinson0a229242012-06-23 10:49:12 +0100143.. c:function:: long PyLong_AsLongAndOverflow(PyObject *obj, int *overflow)
144
145 Return a C :c:type:`long` representation of *obj*. If *obj* is not an
146 instance of :c:type:`PyLongObject`, first call its :meth:`__int__` method
147 (if present) to convert it to a :c:type:`PyLongObject`.
148
149 If the value of *obj* is greater than :const:`LONG_MAX` or less than
150 :const:`LONG_MIN`, set *\*overflow* to ``1`` or ``-1``, respectively, and
151 return ``-1``; otherwise, set *\*overflow* to ``0``. If any other exception
152 occurs set *\*overflow* to ``0`` and return ``-1`` as usual.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000153
Gregory P. Smithf5b04a32018-01-28 17:48:31 -0800154 Returns -1 on error. Use :c:func:`PyErr_Occurred` to disambiguate.
155
Georg Brandl54a3faa2008-01-20 09:30:57 +0000156
Benjamin Peterson47ff0732016-09-08 09:15:54 -0700157.. c:function:: long long PyLong_AsLongLong(PyObject *obj)
Mark Dickinson93f562c2010-01-30 10:30:15 +0000158
Mark Dickinsonf0acfee2012-06-23 11:14:22 +0100159 .. index::
160 single: OverflowError (built-in exception)
161
162 Return a C :c:type:`long long` representation of *obj*. If *obj* is not an
163 instance of :c:type:`PyLongObject`, first call its :meth:`__int__` method
164 (if present) to convert it to a :c:type:`PyLongObject`.
165
166 Raise :exc:`OverflowError` if the value of *obj* is out of range for a
167 :c:type:`long`.
168
Gregory P. Smithf5b04a32018-01-28 17:48:31 -0800169 Returns -1 on error. Use :c:func:`PyErr_Occurred` to disambiguate.
170
Mark Dickinsonf0acfee2012-06-23 11:14:22 +0100171
Benjamin Peterson47ff0732016-09-08 09:15:54 -0700172.. c:function:: long long PyLong_AsLongLongAndOverflow(PyObject *obj, int *overflow)
Mark Dickinsonf0acfee2012-06-23 11:14:22 +0100173
174 Return a C :c:type:`long long` representation of *obj*. If *obj* is not an
175 instance of :c:type:`PyLongObject`, first call its :meth:`__int__` method
176 (if present) to convert it to a :c:type:`PyLongObject`.
177
178 If the value of *obj* is greater than :const:`PY_LLONG_MAX` or less than
179 :const:`PY_LLONG_MIN`, set *\*overflow* to ``1`` or ``-1``, respectively,
180 and return ``-1``; otherwise, set *\*overflow* to ``0``. If any other
181 exception occurs set *\*overflow* to ``0`` and return ``-1`` as usual.
Mark Dickinson93f562c2010-01-30 10:30:15 +0000182
Gregory P. Smithf5b04a32018-01-28 17:48:31 -0800183 Returns -1 on error. Use :c:func:`PyErr_Occurred` to disambiguate.
184
Mark Dickinson93f562c2010-01-30 10:30:15 +0000185 .. versionadded:: 3.2
186
187
Georg Brandl60203b42010-10-06 10:11:56 +0000188.. c:function:: Py_ssize_t PyLong_AsSsize_t(PyObject *pylong)
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000189
190 .. index::
191 single: PY_SSIZE_T_MAX
192 single: OverflowError (built-in exception)
193
Mark Dickinsonb8dc3ab2012-06-23 12:12:52 +0100194 Return a C :c:type:`Py_ssize_t` representation of *pylong*. *pylong* must
195 be an instance of :c:type:`PyLongObject`.
196
197 Raise :exc:`OverflowError` if the value of *pylong* is out of range for a
198 :c:type:`Py_ssize_t`.
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000199
Gregory P. Smithf5b04a32018-01-28 17:48:31 -0800200 Returns -1 on error. Use :c:func:`PyErr_Occurred` to disambiguate.
201
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000202
Georg Brandl60203b42010-10-06 10:11:56 +0000203.. c:function:: unsigned long PyLong_AsUnsignedLong(PyObject *pylong)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000204
205 .. index::
206 single: ULONG_MAX
207 single: OverflowError (built-in exception)
208
Mark Dickinsonb8dc3ab2012-06-23 12:12:52 +0100209 Return a C :c:type:`unsigned long` representation of *pylong*. *pylong*
210 must be an instance of :c:type:`PyLongObject`.
211
212 Raise :exc:`OverflowError` if the value of *pylong* is out of range for a
213 :c:type:`unsigned long`.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000214
Gregory P. Smithf5b04a32018-01-28 17:48:31 -0800215 Returns ``(unsigned long)-1`` on error.
216 Use :c:func:`PyErr_Occurred` to disambiguate.
217
Georg Brandl54a3faa2008-01-20 09:30:57 +0000218
Georg Brandl60203b42010-10-06 10:11:56 +0000219.. c:function:: size_t PyLong_AsSize_t(PyObject *pylong)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000220
Gregory P. Smithf5b04a32018-01-28 17:48:31 -0800221 .. index::
222 single: SIZE_MAX
223 single: OverflowError (built-in exception)
224
Terry Jan Reedy65e69b32013-03-11 17:23:46 -0400225 Return a C :c:type:`size_t` representation of *pylong*. *pylong* must be
Mark Dickinsonb8dc3ab2012-06-23 12:12:52 +0100226 an instance of :c:type:`PyLongObject`.
227
228 Raise :exc:`OverflowError` if the value of *pylong* is out of range for a
229 :c:type:`size_t`.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000230
Gregory P. Smithf5b04a32018-01-28 17:48:31 -0800231 Returns ``(size_t)-1`` on error.
232 Use :c:func:`PyErr_Occurred` to disambiguate.
233
Georg Brandl54a3faa2008-01-20 09:30:57 +0000234
Benjamin Peterson47ff0732016-09-08 09:15:54 -0700235.. c:function:: unsigned long long PyLong_AsUnsignedLongLong(PyObject *pylong)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000236
Mark Dickinson21776072009-02-10 16:13:25 +0000237 .. index::
238 single: OverflowError (built-in exception)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000239
Benjamin Peterson47ff0732016-09-08 09:15:54 -0700240 Return a C :c:type:`unsigned long long` representation of *pylong*. *pylong*
241 must be an instance of :c:type:`PyLongObject`.
Mark Dickinsonb8dc3ab2012-06-23 12:12:52 +0100242
243 Raise :exc:`OverflowError` if the value of *pylong* is out of range for an
Benjamin Peterson47ff0732016-09-08 09:15:54 -0700244 :c:type:`unsigned long long`.
Mark Dickinson21776072009-02-10 16:13:25 +0000245
Gregory P. Smithf5b04a32018-01-28 17:48:31 -0800246 Returns ``(unsigned long long)-1`` on error.
247 Use :c:func:`PyErr_Occurred` to disambiguate.
248
Mark Dickinson21776072009-02-10 16:13:25 +0000249 .. versionchanged:: 3.1
Georg Brandl67b21b72010-08-17 15:07:14 +0000250 A negative *pylong* now raises :exc:`OverflowError`, not :exc:`TypeError`.
251
Georg Brandl54a3faa2008-01-20 09:30:57 +0000252
Mark Dickinsonb8dc3ab2012-06-23 12:12:52 +0100253.. c:function:: unsigned long PyLong_AsUnsignedLongMask(PyObject *obj)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000254
Mark Dickinsonb8dc3ab2012-06-23 12:12:52 +0100255 Return a C :c:type:`unsigned long` representation of *obj*. If *obj*
256 is not an instance of :c:type:`PyLongObject`, first call its :meth:`__int__`
257 method (if present) to convert it to a :c:type:`PyLongObject`.
258
259 If the value of *obj* is out of range for an :c:type:`unsigned long`,
Serhiy Storchaka1ecf7d22016-10-27 21:41:19 +0300260 return the reduction of that value modulo ``ULONG_MAX + 1``.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000261
Gregory P. Smithf5b04a32018-01-28 17:48:31 -0800262 Returns -1 on error. Use :c:func:`PyErr_Occurred` to disambiguate.
263
Georg Brandl54a3faa2008-01-20 09:30:57 +0000264
Benjamin Peterson47ff0732016-09-08 09:15:54 -0700265.. c:function:: unsigned long long PyLong_AsUnsignedLongLongMask(PyObject *obj)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000266
Mark Dickinsonb8dc3ab2012-06-23 12:12:52 +0100267 Return a C :c:type:`unsigned long long` representation of *obj*. If *obj*
268 is not an instance of :c:type:`PyLongObject`, first call its :meth:`__int__`
269 method (if present) to convert it to a :c:type:`PyLongObject`.
270
271 If the value of *obj* is out of range for an :c:type:`unsigned long long`,
Serhiy Storchaka1ecf7d22016-10-27 21:41:19 +0300272 return the reduction of that value modulo ``PY_ULLONG_MAX + 1``.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000273
Gregory P. Smithf5b04a32018-01-28 17:48:31 -0800274 Returns -1 on error. Use :c:func:`PyErr_Occurred` to disambiguate.
275
Georg Brandl54a3faa2008-01-20 09:30:57 +0000276
Georg Brandl60203b42010-10-06 10:11:56 +0000277.. c:function:: double PyLong_AsDouble(PyObject *pylong)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000278
Mark Dickinsonb8dc3ab2012-06-23 12:12:52 +0100279 Return a C :c:type:`double` representation of *pylong*. *pylong* must be
280 an instance of :c:type:`PyLongObject`.
281
282 Raise :exc:`OverflowError` if the value of *pylong* is out of range for a
283 :c:type:`double`.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000284
Gregory P. Smithf5b04a32018-01-28 17:48:31 -0800285 Returns -1.0 on error. Use :c:func:`PyErr_Occurred` to disambiguate.
286
Georg Brandl54a3faa2008-01-20 09:30:57 +0000287
Georg Brandl60203b42010-10-06 10:11:56 +0000288.. c:function:: void* PyLong_AsVoidPtr(PyObject *pylong)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000289
Georg Brandl60203b42010-10-06 10:11:56 +0000290 Convert a Python integer *pylong* to a C :c:type:`void` pointer.
Christian Heimesc3f30c42008-02-22 16:37:40 +0000291 If *pylong* cannot be converted, an :exc:`OverflowError` will be raised. This
Georg Brandl60203b42010-10-06 10:11:56 +0000292 is only assured to produce a usable :c:type:`void` pointer for values created
293 with :c:func:`PyLong_FromVoidPtr`.
Gregory P. Smithf5b04a32018-01-28 17:48:31 -0800294
295 Returns NULL on error. Use :c:func:`PyErr_Occurred` to disambiguate.