blob: a7bd43df90689d753ea6c2a7e5caec524b52ebbc [file] [log] [blame]
Stéphane Wirtelcbb64842019-05-17 11:55:34 +02001.. highlight:: c
Georg Brandl54a3faa2008-01-20 09:30:57 +00002
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
Serhiy Storchaka25fc0882019-10-30 12:03:20 +020041 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
sgal1d8b04e2019-07-16 08:15:17 -070045 just get back a reference to the existing object.
Georg Brandl54a3faa2008-01-20 09:30:57 +000046
47
Georg Brandl60203b42010-10-06 10:11:56 +000048.. c:function:: PyObject* PyLong_FromUnsignedLong(unsigned long v)
Georg Brandl54a3faa2008-01-20 09:30:57 +000049
Georg Brandl60203b42010-10-06 10:11:56 +000050 Return a new :c:type:`PyLongObject` object from a C :c:type:`unsigned long`, or
Serhiy Storchaka25fc0882019-10-30 12:03:20 +020051 ``NULL`` on failure.
Georg Brandl54a3faa2008-01-20 09:30:57 +000052
53
Georg Brandl60203b42010-10-06 10:11:56 +000054.. c:function:: PyObject* PyLong_FromSsize_t(Py_ssize_t v)
Georg Brandl54a3faa2008-01-20 09:30:57 +000055
Georg Brandl60203b42010-10-06 10:11:56 +000056 Return a new :c:type:`PyLongObject` object from a C :c:type:`Py_ssize_t`, or
Serhiy Storchaka25fc0882019-10-30 12:03:20 +020057 ``NULL`` on failure.
Georg Brandl54a3faa2008-01-20 09:30:57 +000058
59
Georg Brandl60203b42010-10-06 10:11:56 +000060.. c:function:: PyObject* PyLong_FromSize_t(size_t v)
Georg Brandl54a3faa2008-01-20 09:30:57 +000061
Georg Brandl60203b42010-10-06 10:11:56 +000062 Return a new :c:type:`PyLongObject` object from a C :c:type:`size_t`, or
Serhiy Storchaka25fc0882019-10-30 12:03:20 +020063 ``NULL`` on failure.
Georg Brandl54a3faa2008-01-20 09:30:57 +000064
65
Benjamin Peterson47ff0732016-09-08 09:15:54 -070066.. c:function:: PyObject* PyLong_FromLongLong(long long v)
Georg Brandl54a3faa2008-01-20 09:30:57 +000067
Serhiy Storchaka25fc0882019-10-30 12:03:20 +020068 Return a new :c:type:`PyLongObject` object from a C :c:type:`long long`, or ``NULL``
Georg Brandl54a3faa2008-01-20 09:30:57 +000069 on failure.
70
71
Benjamin Peterson47ff0732016-09-08 09:15:54 -070072.. c:function:: PyObject* PyLong_FromUnsignedLongLong(unsigned long long v)
Georg Brandl54a3faa2008-01-20 09:30:57 +000073
Georg Brandl60203b42010-10-06 10:11:56 +000074 Return a new :c:type:`PyLongObject` object from a C :c:type:`unsigned long long`,
Serhiy Storchaka25fc0882019-10-30 12:03:20 +020075 or ``NULL`` on failure.
Georg Brandl54a3faa2008-01-20 09:30:57 +000076
77
Georg Brandl60203b42010-10-06 10:11:56 +000078.. c:function:: PyObject* PyLong_FromDouble(double v)
Georg Brandl54a3faa2008-01-20 09:30:57 +000079
Georg Brandl60203b42010-10-06 10:11:56 +000080 Return a new :c:type:`PyLongObject` object from the integer part of *v*, or
Serhiy Storchaka25fc0882019-10-30 12:03:20 +020081 ``NULL`` on failure.
Georg Brandl54a3faa2008-01-20 09:30:57 +000082
83
Serhiy Storchakac6792272013-10-19 21:03:34 +030084.. c:function:: PyObject* PyLong_FromString(const char *str, char **pend, int base)
Georg Brandl54a3faa2008-01-20 09:30:57 +000085
Georg Brandl60203b42010-10-06 10:11:56 +000086 Return a new :c:type:`PyLongObject` based on the string value in *str*, which
Serhiy Storchaka25fc0882019-10-30 12:03:20 +020087 is interpreted according to the radix in *base*. If *pend* is non-``NULL``,
Ezio Melottiea30c6f2010-01-30 13:32:14 +000088 *\*pend* will point to the first character in *str* which follows the
csabella26896f22017-04-23 23:54:08 -040089 representation of the number. If *base* is ``0``, *str* is interpreted using
90 the :ref:`integers` definition; in this case, leading zeros in a
91 non-zero decimal number raises a :exc:`ValueError`. If *base* is not ``0``,
92 it must be between ``2`` and ``36``, inclusive. Leading spaces and single
93 underscores after a base specifier and between digits are ignored. If there
94 are no digits, :exc:`ValueError` will be raised.
Georg Brandl54a3faa2008-01-20 09:30:57 +000095
96
Georg Brandl60203b42010-10-06 10:11:56 +000097.. c:function:: PyObject* PyLong_FromUnicode(Py_UNICODE *u, Py_ssize_t length, int base)
Georg Brandl54a3faa2008-01-20 09:30:57 +000098
99 Convert a sequence of Unicode digits to a Python integer value. The Unicode
Georg Brandl60203b42010-10-06 10:11:56 +0000100 string is first encoded to a byte string using :c:func:`PyUnicode_EncodeDecimal`
101 and then converted using :c:func:`PyLong_FromString`.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000102
Georg Brandldb6c7f52011-10-07 11:19:11 +0200103 .. deprecated-removed:: 3.3 4.0
104 Part of the old-style :c:type:`Py_UNICODE` API; please migrate to using
105 :c:func:`PyLong_FromUnicodeObject`.
106
107
108.. c:function:: PyObject* PyLong_FromUnicodeObject(PyObject *u, int base)
109
110 Convert a sequence of Unicode digits in the string *u* to a Python integer
111 value. The Unicode string is first encoded to a byte string using
112 :c:func:`PyUnicode_EncodeDecimal` and then converted using
113 :c:func:`PyLong_FromString`.
114
115 .. versionadded:: 3.3
116
Georg Brandl54a3faa2008-01-20 09:30:57 +0000117
Georg Brandl60203b42010-10-06 10:11:56 +0000118.. c:function:: PyObject* PyLong_FromVoidPtr(void *p)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000119
120 Create a Python integer from the pointer *p*. The pointer value can be
Georg Brandl60203b42010-10-06 10:11:56 +0000121 retrieved from the resulting value using :c:func:`PyLong_AsVoidPtr`.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000122
123
Georg Brandl48310cd2009-01-03 21:18:54 +0000124.. XXX alias PyLong_AS_LONG (for now)
Mark Dickinson0a229242012-06-23 10:49:12 +0100125.. c:function:: long PyLong_AsLong(PyObject *obj)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000126
127 .. index::
128 single: LONG_MAX
129 single: OverflowError (built-in exception)
130
Mark Dickinson0a229242012-06-23 10:49:12 +0100131 Return a C :c:type:`long` representation of *obj*. If *obj* is not an
Mark Dickinson20941de2020-05-27 13:43:17 +0100132 instance of :c:type:`PyLongObject`, first call its :meth:`__index__` method
133 (if present) to convert it to a :c:type:`PyLongObject`.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000134
Mark Dickinson0a229242012-06-23 10:49:12 +0100135 Raise :exc:`OverflowError` if the value of *obj* is out of range for a
136 :c:type:`long`.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000137
Serhiy Storchaka5bb00052018-02-09 13:31:19 +0200138 Returns ``-1`` on error. Use :c:func:`PyErr_Occurred` to disambiguate.
Gregory P. Smithf5b04a32018-01-28 17:48:31 -0800139
Serhiy Storchaka6a44f6e2019-02-25 17:57:58 +0200140 .. versionchanged:: 3.8
141 Use :meth:`__index__` if available.
142
Mark Dickinson20941de2020-05-27 13:43:17 +0100143 .. versionchanged:: 3.10
144 This function will no longer use :meth:`__int__`.
Serhiy Storchaka6a44f6e2019-02-25 17:57:58 +0200145
Mark Dickinsonf0acfee2012-06-23 11:14:22 +0100146
Mark Dickinson0a229242012-06-23 10:49:12 +0100147.. c:function:: long PyLong_AsLongAndOverflow(PyObject *obj, int *overflow)
148
149 Return a C :c:type:`long` representation of *obj*. If *obj* is not an
Mark Dickinson20941de2020-05-27 13:43:17 +0100150 instance of :c:type:`PyLongObject`, first call its :meth:`__index__`
151 method (if present) to convert it to a :c:type:`PyLongObject`.
Mark Dickinson0a229242012-06-23 10:49:12 +0100152
153 If the value of *obj* is greater than :const:`LONG_MAX` or less than
154 :const:`LONG_MIN`, set *\*overflow* to ``1`` or ``-1``, respectively, and
155 return ``-1``; otherwise, set *\*overflow* to ``0``. If any other exception
156 occurs set *\*overflow* to ``0`` and return ``-1`` as usual.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000157
Serhiy Storchaka5bb00052018-02-09 13:31:19 +0200158 Returns ``-1`` on error. Use :c:func:`PyErr_Occurred` to disambiguate.
Gregory P. Smithf5b04a32018-01-28 17:48:31 -0800159
Serhiy Storchaka6a44f6e2019-02-25 17:57:58 +0200160 .. versionchanged:: 3.8
161 Use :meth:`__index__` if available.
162
Mark Dickinson20941de2020-05-27 13:43:17 +0100163 .. versionchanged:: 3.10
164 This function will no longer use :meth:`__int__`.
Serhiy Storchaka6a44f6e2019-02-25 17:57:58 +0200165
Georg Brandl54a3faa2008-01-20 09:30:57 +0000166
Benjamin Peterson47ff0732016-09-08 09:15:54 -0700167.. c:function:: long long PyLong_AsLongLong(PyObject *obj)
Mark Dickinson93f562c2010-01-30 10:30:15 +0000168
Mark Dickinsonf0acfee2012-06-23 11:14:22 +0100169 .. index::
170 single: OverflowError (built-in exception)
171
172 Return a C :c:type:`long long` representation of *obj*. If *obj* is not an
Mark Dickinson20941de2020-05-27 13:43:17 +0100173 instance of :c:type:`PyLongObject`, first call its :meth:`__index__` method
174 (if present) to convert it to a :c:type:`PyLongObject`.
Mark Dickinsonf0acfee2012-06-23 11:14:22 +0100175
176 Raise :exc:`OverflowError` if the value of *obj* is out of range for a
Keith Erskine47be7d02020-01-21 13:14:13 -0600177 :c:type:`long long`.
Mark Dickinsonf0acfee2012-06-23 11:14:22 +0100178
Serhiy Storchaka5bb00052018-02-09 13:31:19 +0200179 Returns ``-1`` on error. Use :c:func:`PyErr_Occurred` to disambiguate.
Gregory P. Smithf5b04a32018-01-28 17:48:31 -0800180
Serhiy Storchaka6a44f6e2019-02-25 17:57:58 +0200181 .. versionchanged:: 3.8
182 Use :meth:`__index__` if available.
183
Mark Dickinson20941de2020-05-27 13:43:17 +0100184 .. versionchanged:: 3.10
185 This function will no longer use :meth:`__int__`.
Serhiy Storchaka6a44f6e2019-02-25 17:57:58 +0200186
Mark Dickinsonf0acfee2012-06-23 11:14:22 +0100187
Benjamin Peterson47ff0732016-09-08 09:15:54 -0700188.. c:function:: long long PyLong_AsLongLongAndOverflow(PyObject *obj, int *overflow)
Mark Dickinsonf0acfee2012-06-23 11:14:22 +0100189
190 Return a C :c:type:`long long` representation of *obj*. If *obj* is not an
Mark Dickinson20941de2020-05-27 13:43:17 +0100191 instance of :c:type:`PyLongObject`, first call its :meth:`__index__` method
192 (if present) to convert it to a :c:type:`PyLongObject`.
Mark Dickinsonf0acfee2012-06-23 11:14:22 +0100193
Sergey Fedoseev1f9f69d2019-12-05 19:55:28 +0500194 If the value of *obj* is greater than :const:`LLONG_MAX` or less than
195 :const:`LLONG_MIN`, set *\*overflow* to ``1`` or ``-1``, respectively,
Mark Dickinsonf0acfee2012-06-23 11:14:22 +0100196 and return ``-1``; otherwise, set *\*overflow* to ``0``. If any other
197 exception occurs set *\*overflow* to ``0`` and return ``-1`` as usual.
Mark Dickinson93f562c2010-01-30 10:30:15 +0000198
Serhiy Storchaka5bb00052018-02-09 13:31:19 +0200199 Returns ``-1`` on error. Use :c:func:`PyErr_Occurred` to disambiguate.
Gregory P. Smithf5b04a32018-01-28 17:48:31 -0800200
Mark Dickinson93f562c2010-01-30 10:30:15 +0000201 .. versionadded:: 3.2
202
Serhiy Storchaka6a44f6e2019-02-25 17:57:58 +0200203 .. versionchanged:: 3.8
204 Use :meth:`__index__` if available.
205
Mark Dickinson20941de2020-05-27 13:43:17 +0100206 .. versionchanged:: 3.10
207 This function will no longer use :meth:`__int__`.
Serhiy Storchaka6a44f6e2019-02-25 17:57:58 +0200208
Mark Dickinson93f562c2010-01-30 10:30:15 +0000209
Georg Brandl60203b42010-10-06 10:11:56 +0000210.. c:function:: Py_ssize_t PyLong_AsSsize_t(PyObject *pylong)
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000211
212 .. index::
213 single: PY_SSIZE_T_MAX
214 single: OverflowError (built-in exception)
215
Mark Dickinsonb8dc3ab2012-06-23 12:12:52 +0100216 Return a C :c:type:`Py_ssize_t` representation of *pylong*. *pylong* must
217 be an instance of :c:type:`PyLongObject`.
218
219 Raise :exc:`OverflowError` if the value of *pylong* is out of range for a
220 :c:type:`Py_ssize_t`.
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000221
Serhiy Storchaka5bb00052018-02-09 13:31:19 +0200222 Returns ``-1`` on error. Use :c:func:`PyErr_Occurred` to disambiguate.
Gregory P. Smithf5b04a32018-01-28 17:48:31 -0800223
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000224
Georg Brandl60203b42010-10-06 10:11:56 +0000225.. c:function:: unsigned long PyLong_AsUnsignedLong(PyObject *pylong)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000226
227 .. index::
228 single: ULONG_MAX
229 single: OverflowError (built-in exception)
230
Mark Dickinsonb8dc3ab2012-06-23 12:12:52 +0100231 Return a C :c:type:`unsigned long` representation of *pylong*. *pylong*
232 must be an instance of :c:type:`PyLongObject`.
233
234 Raise :exc:`OverflowError` if the value of *pylong* is out of range for a
235 :c:type:`unsigned long`.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000236
Gregory P. Smithf5b04a32018-01-28 17:48:31 -0800237 Returns ``(unsigned long)-1`` on error.
238 Use :c:func:`PyErr_Occurred` to disambiguate.
239
Georg Brandl54a3faa2008-01-20 09:30:57 +0000240
Georg Brandl60203b42010-10-06 10:11:56 +0000241.. c:function:: size_t PyLong_AsSize_t(PyObject *pylong)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000242
Gregory P. Smithf5b04a32018-01-28 17:48:31 -0800243 .. index::
244 single: SIZE_MAX
245 single: OverflowError (built-in exception)
246
Terry Jan Reedy65e69b32013-03-11 17:23:46 -0400247 Return a C :c:type:`size_t` representation of *pylong*. *pylong* must be
Mark Dickinsonb8dc3ab2012-06-23 12:12:52 +0100248 an instance of :c:type:`PyLongObject`.
249
250 Raise :exc:`OverflowError` if the value of *pylong* is out of range for a
251 :c:type:`size_t`.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000252
Gregory P. Smithf5b04a32018-01-28 17:48:31 -0800253 Returns ``(size_t)-1`` on error.
254 Use :c:func:`PyErr_Occurred` to disambiguate.
255
Georg Brandl54a3faa2008-01-20 09:30:57 +0000256
Benjamin Peterson47ff0732016-09-08 09:15:54 -0700257.. c:function:: unsigned long long PyLong_AsUnsignedLongLong(PyObject *pylong)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000258
Mark Dickinson21776072009-02-10 16:13:25 +0000259 .. index::
260 single: OverflowError (built-in exception)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000261
Benjamin Peterson47ff0732016-09-08 09:15:54 -0700262 Return a C :c:type:`unsigned long long` representation of *pylong*. *pylong*
263 must be an instance of :c:type:`PyLongObject`.
Mark Dickinsonb8dc3ab2012-06-23 12:12:52 +0100264
265 Raise :exc:`OverflowError` if the value of *pylong* is out of range for an
Benjamin Peterson47ff0732016-09-08 09:15:54 -0700266 :c:type:`unsigned long long`.
Mark Dickinson21776072009-02-10 16:13:25 +0000267
Gregory P. Smithf5b04a32018-01-28 17:48:31 -0800268 Returns ``(unsigned long long)-1`` on error.
269 Use :c:func:`PyErr_Occurred` to disambiguate.
270
Mark Dickinson21776072009-02-10 16:13:25 +0000271 .. versionchanged:: 3.1
Georg Brandl67b21b72010-08-17 15:07:14 +0000272 A negative *pylong* now raises :exc:`OverflowError`, not :exc:`TypeError`.
273
Georg Brandl54a3faa2008-01-20 09:30:57 +0000274
Mark Dickinsonb8dc3ab2012-06-23 12:12:52 +0100275.. c:function:: unsigned long PyLong_AsUnsignedLongMask(PyObject *obj)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000276
Mark Dickinson20941de2020-05-27 13:43:17 +0100277 Return a C :c:type:`unsigned long` representation of *obj*. If *obj* is not
278 an instance of :c:type:`PyLongObject`, first call its :meth:`__index__`
279 method (if present) to convert it to a :c:type:`PyLongObject`.
Mark Dickinsonb8dc3ab2012-06-23 12:12:52 +0100280
281 If the value of *obj* is out of range for an :c:type:`unsigned long`,
Serhiy Storchaka1ecf7d22016-10-27 21:41:19 +0300282 return the reduction of that value modulo ``ULONG_MAX + 1``.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000283
Zackery Spytzdc247652019-06-06 14:39:23 -0600284 Returns ``(unsigned long)-1`` on error. Use :c:func:`PyErr_Occurred` to
285 disambiguate.
Gregory P. Smithf5b04a32018-01-28 17:48:31 -0800286
Serhiy Storchaka6a44f6e2019-02-25 17:57:58 +0200287 .. versionchanged:: 3.8
288 Use :meth:`__index__` if available.
289
Mark Dickinson20941de2020-05-27 13:43:17 +0100290 .. versionchanged:: 3.10
291 This function will no longer use :meth:`__int__`.
Serhiy Storchaka6a44f6e2019-02-25 17:57:58 +0200292
Georg Brandl54a3faa2008-01-20 09:30:57 +0000293
Benjamin Peterson47ff0732016-09-08 09:15:54 -0700294.. c:function:: unsigned long long PyLong_AsUnsignedLongLongMask(PyObject *obj)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000295
Mark Dickinsonb8dc3ab2012-06-23 12:12:52 +0100296 Return a C :c:type:`unsigned long long` representation of *obj*. If *obj*
Serhiy Storchaka6a44f6e2019-02-25 17:57:58 +0200297 is not an instance of :c:type:`PyLongObject`, first call its
Mark Dickinson20941de2020-05-27 13:43:17 +0100298 :meth:`__index__` method (if present) to convert it to a
299 :c:type:`PyLongObject`.
Mark Dickinsonb8dc3ab2012-06-23 12:12:52 +0100300
301 If the value of *obj* is out of range for an :c:type:`unsigned long long`,
Sergey Fedoseeva2ff2832019-12-10 01:22:19 +0500302 return the reduction of that value modulo ``ULLONG_MAX + 1``.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000303
Zackery Spytzdc247652019-06-06 14:39:23 -0600304 Returns ``(unsigned long long)-1`` on error. Use :c:func:`PyErr_Occurred`
305 to disambiguate.
Gregory P. Smithf5b04a32018-01-28 17:48:31 -0800306
Serhiy Storchaka6a44f6e2019-02-25 17:57:58 +0200307 .. versionchanged:: 3.8
308 Use :meth:`__index__` if available.
309
Mark Dickinson20941de2020-05-27 13:43:17 +0100310 .. versionchanged:: 3.10
311 This function will no longer use :meth:`__int__`.
Serhiy Storchaka6a44f6e2019-02-25 17:57:58 +0200312
Georg Brandl54a3faa2008-01-20 09:30:57 +0000313
Georg Brandl60203b42010-10-06 10:11:56 +0000314.. c:function:: double PyLong_AsDouble(PyObject *pylong)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000315
Mark Dickinsonb8dc3ab2012-06-23 12:12:52 +0100316 Return a C :c:type:`double` representation of *pylong*. *pylong* must be
317 an instance of :c:type:`PyLongObject`.
318
319 Raise :exc:`OverflowError` if the value of *pylong* is out of range for a
320 :c:type:`double`.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000321
Serhiy Storchaka5bb00052018-02-09 13:31:19 +0200322 Returns ``-1.0`` on error. Use :c:func:`PyErr_Occurred` to disambiguate.
Gregory P. Smithf5b04a32018-01-28 17:48:31 -0800323
Georg Brandl54a3faa2008-01-20 09:30:57 +0000324
Georg Brandl60203b42010-10-06 10:11:56 +0000325.. c:function:: void* PyLong_AsVoidPtr(PyObject *pylong)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000326
Georg Brandl60203b42010-10-06 10:11:56 +0000327 Convert a Python integer *pylong* to a C :c:type:`void` pointer.
Christian Heimesc3f30c42008-02-22 16:37:40 +0000328 If *pylong* cannot be converted, an :exc:`OverflowError` will be raised. This
Georg Brandl60203b42010-10-06 10:11:56 +0000329 is only assured to produce a usable :c:type:`void` pointer for values created
330 with :c:func:`PyLong_FromVoidPtr`.
Gregory P. Smithf5b04a32018-01-28 17:48:31 -0800331
Serhiy Storchaka25fc0882019-10-30 12:03:20 +0200332 Returns ``NULL`` on error. Use :c:func:`PyErr_Occurred` to disambiguate.