Stéphane Wirtel | cbb6484 | 2019-05-17 11:55:34 +0200 | [diff] [blame] | 1 | .. highlight:: c |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 2 | |
| 3 | .. _longobjects: |
| 4 | |
| 5 | Integer Objects |
| 6 | --------------- |
| 7 | |
| 8 | .. index:: object: long integer |
| 9 | object: integer |
| 10 | |
| 11 | All integers are implemented as "long" integer objects of arbitrary size. |
| 12 | |
Gregory P. Smith | f5b04a3 | 2018-01-28 17:48:31 -0800 | [diff] [blame] | 13 | On error, most ``PyLong_As*`` APIs return ``(return type)-1`` which cannot be |
| 14 | distinguished from a number. Use :c:func:`PyErr_Occurred` to disambiguate. |
| 15 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 16 | .. c:type:: PyLongObject |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 17 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 18 | This subtype of :c:type:`PyObject` represents a Python integer object. |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 19 | |
| 20 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 21 | .. c:var:: PyTypeObject PyLong_Type |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 22 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 23 | This instance of :c:type:`PyTypeObject` represents the Python integer type. |
Georg Brandl | 2aff335 | 2010-10-17 10:59:41 +0000 | [diff] [blame] | 24 | This is the same object as :class:`int` in the Python layer. |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 25 | |
| 26 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 27 | .. c:function:: int PyLong_Check(PyObject *p) |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 28 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 29 | Return true if its argument is a :c:type:`PyLongObject` or a subtype of |
| 30 | :c:type:`PyLongObject`. |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 31 | |
| 32 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 33 | .. c:function:: int PyLong_CheckExact(PyObject *p) |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 34 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 35 | Return true if its argument is a :c:type:`PyLongObject`, but not a subtype of |
| 36 | :c:type:`PyLongObject`. |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 37 | |
| 38 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 39 | .. c:function:: PyObject* PyLong_FromLong(long v) |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 40 | |
Serhiy Storchaka | 25fc088 | 2019-10-30 12:03:20 +0200 | [diff] [blame] | 41 | Return a new :c:type:`PyLongObject` object from *v*, or ``NULL`` on failure. |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 42 | |
| 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 |
sgal | 1d8b04e | 2019-07-16 08:15:17 -0700 | [diff] [blame] | 45 | just get back a reference to the existing object. |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 46 | |
| 47 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 48 | .. c:function:: PyObject* PyLong_FromUnsignedLong(unsigned long v) |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 49 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 50 | Return a new :c:type:`PyLongObject` object from a C :c:type:`unsigned long`, or |
Serhiy Storchaka | 25fc088 | 2019-10-30 12:03:20 +0200 | [diff] [blame] | 51 | ``NULL`` on failure. |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 52 | |
| 53 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 54 | .. c:function:: PyObject* PyLong_FromSsize_t(Py_ssize_t v) |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 55 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 56 | Return a new :c:type:`PyLongObject` object from a C :c:type:`Py_ssize_t`, or |
Serhiy Storchaka | 25fc088 | 2019-10-30 12:03:20 +0200 | [diff] [blame] | 57 | ``NULL`` on failure. |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 58 | |
| 59 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 60 | .. c:function:: PyObject* PyLong_FromSize_t(size_t v) |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 61 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 62 | Return a new :c:type:`PyLongObject` object from a C :c:type:`size_t`, or |
Serhiy Storchaka | 25fc088 | 2019-10-30 12:03:20 +0200 | [diff] [blame] | 63 | ``NULL`` on failure. |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 64 | |
| 65 | |
Benjamin Peterson | 47ff073 | 2016-09-08 09:15:54 -0700 | [diff] [blame] | 66 | .. c:function:: PyObject* PyLong_FromLongLong(long long v) |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 67 | |
Serhiy Storchaka | 25fc088 | 2019-10-30 12:03:20 +0200 | [diff] [blame] | 68 | Return a new :c:type:`PyLongObject` object from a C :c:type:`long long`, or ``NULL`` |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 69 | on failure. |
| 70 | |
| 71 | |
Benjamin Peterson | 47ff073 | 2016-09-08 09:15:54 -0700 | [diff] [blame] | 72 | .. c:function:: PyObject* PyLong_FromUnsignedLongLong(unsigned long long v) |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 73 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 74 | Return a new :c:type:`PyLongObject` object from a C :c:type:`unsigned long long`, |
Serhiy Storchaka | 25fc088 | 2019-10-30 12:03:20 +0200 | [diff] [blame] | 75 | or ``NULL`` on failure. |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 76 | |
| 77 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 78 | .. c:function:: PyObject* PyLong_FromDouble(double v) |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 79 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 80 | Return a new :c:type:`PyLongObject` object from the integer part of *v*, or |
Serhiy Storchaka | 25fc088 | 2019-10-30 12:03:20 +0200 | [diff] [blame] | 81 | ``NULL`` on failure. |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 82 | |
| 83 | |
Serhiy Storchaka | c679227 | 2013-10-19 21:03:34 +0300 | [diff] [blame] | 84 | .. c:function:: PyObject* PyLong_FromString(const char *str, char **pend, int base) |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 85 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 86 | Return a new :c:type:`PyLongObject` based on the string value in *str*, which |
Serhiy Storchaka | 25fc088 | 2019-10-30 12:03:20 +0200 | [diff] [blame] | 87 | is interpreted according to the radix in *base*. If *pend* is non-``NULL``, |
Ezio Melotti | ea30c6f | 2010-01-30 13:32:14 +0000 | [diff] [blame] | 88 | *\*pend* will point to the first character in *str* which follows the |
csabella | 26896f2 | 2017-04-23 23:54:08 -0400 | [diff] [blame] | 89 | 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 Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 95 | |
| 96 | |
Georg Brandl | db6c7f5 | 2011-10-07 11:19:11 +0200 | [diff] [blame] | 97 | .. c:function:: PyObject* PyLong_FromUnicodeObject(PyObject *u, int base) |
| 98 | |
| 99 | Convert a sequence of Unicode digits in the string *u* to a Python integer |
Inada Naoki | 9c84417 | 2020-07-05 13:01:48 +0900 | [diff] [blame] | 100 | value. |
Georg Brandl | db6c7f5 | 2011-10-07 11:19:11 +0200 | [diff] [blame] | 101 | |
| 102 | .. versionadded:: 3.3 |
| 103 | |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 104 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 105 | .. c:function:: PyObject* PyLong_FromVoidPtr(void *p) |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 106 | |
| 107 | Create a Python integer from the pointer *p*. The pointer value can be |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 108 | retrieved from the resulting value using :c:func:`PyLong_AsVoidPtr`. |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 109 | |
| 110 | |
Georg Brandl | 48310cd | 2009-01-03 21:18:54 +0000 | [diff] [blame] | 111 | .. XXX alias PyLong_AS_LONG (for now) |
Mark Dickinson | 0a22924 | 2012-06-23 10:49:12 +0100 | [diff] [blame] | 112 | .. c:function:: long PyLong_AsLong(PyObject *obj) |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 113 | |
| 114 | .. index:: |
| 115 | single: LONG_MAX |
| 116 | single: OverflowError (built-in exception) |
| 117 | |
Mark Dickinson | 0a22924 | 2012-06-23 10:49:12 +0100 | [diff] [blame] | 118 | Return a C :c:type:`long` representation of *obj*. If *obj* is not an |
Mark Dickinson | 20941de | 2020-05-27 13:43:17 +0100 | [diff] [blame] | 119 | instance of :c:type:`PyLongObject`, first call its :meth:`__index__` method |
| 120 | (if present) to convert it to a :c:type:`PyLongObject`. |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 121 | |
Mark Dickinson | 0a22924 | 2012-06-23 10:49:12 +0100 | [diff] [blame] | 122 | Raise :exc:`OverflowError` if the value of *obj* is out of range for a |
| 123 | :c:type:`long`. |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 124 | |
Serhiy Storchaka | 5bb0005 | 2018-02-09 13:31:19 +0200 | [diff] [blame] | 125 | Returns ``-1`` on error. Use :c:func:`PyErr_Occurred` to disambiguate. |
Gregory P. Smith | f5b04a3 | 2018-01-28 17:48:31 -0800 | [diff] [blame] | 126 | |
Serhiy Storchaka | 6a44f6e | 2019-02-25 17:57:58 +0200 | [diff] [blame] | 127 | .. versionchanged:: 3.8 |
| 128 | Use :meth:`__index__` if available. |
| 129 | |
Mark Dickinson | 20941de | 2020-05-27 13:43:17 +0100 | [diff] [blame] | 130 | .. versionchanged:: 3.10 |
| 131 | This function will no longer use :meth:`__int__`. |
Serhiy Storchaka | 6a44f6e | 2019-02-25 17:57:58 +0200 | [diff] [blame] | 132 | |
Mark Dickinson | f0acfee | 2012-06-23 11:14:22 +0100 | [diff] [blame] | 133 | |
Mark Dickinson | 0a22924 | 2012-06-23 10:49:12 +0100 | [diff] [blame] | 134 | .. c:function:: long PyLong_AsLongAndOverflow(PyObject *obj, int *overflow) |
| 135 | |
| 136 | Return a C :c:type:`long` representation of *obj*. If *obj* is not an |
Mark Dickinson | 20941de | 2020-05-27 13:43:17 +0100 | [diff] [blame] | 137 | instance of :c:type:`PyLongObject`, first call its :meth:`__index__` |
| 138 | method (if present) to convert it to a :c:type:`PyLongObject`. |
Mark Dickinson | 0a22924 | 2012-06-23 10:49:12 +0100 | [diff] [blame] | 139 | |
| 140 | If the value of *obj* is greater than :const:`LONG_MAX` or less than |
| 141 | :const:`LONG_MIN`, set *\*overflow* to ``1`` or ``-1``, respectively, and |
| 142 | return ``-1``; otherwise, set *\*overflow* to ``0``. If any other exception |
| 143 | occurs set *\*overflow* to ``0`` and return ``-1`` as usual. |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 144 | |
Serhiy Storchaka | 5bb0005 | 2018-02-09 13:31:19 +0200 | [diff] [blame] | 145 | Returns ``-1`` on error. Use :c:func:`PyErr_Occurred` to disambiguate. |
Gregory P. Smith | f5b04a3 | 2018-01-28 17:48:31 -0800 | [diff] [blame] | 146 | |
Serhiy Storchaka | 6a44f6e | 2019-02-25 17:57:58 +0200 | [diff] [blame] | 147 | .. versionchanged:: 3.8 |
| 148 | Use :meth:`__index__` if available. |
| 149 | |
Mark Dickinson | 20941de | 2020-05-27 13:43:17 +0100 | [diff] [blame] | 150 | .. versionchanged:: 3.10 |
| 151 | This function will no longer use :meth:`__int__`. |
Serhiy Storchaka | 6a44f6e | 2019-02-25 17:57:58 +0200 | [diff] [blame] | 152 | |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 153 | |
Benjamin Peterson | 47ff073 | 2016-09-08 09:15:54 -0700 | [diff] [blame] | 154 | .. c:function:: long long PyLong_AsLongLong(PyObject *obj) |
Mark Dickinson | 93f562c | 2010-01-30 10:30:15 +0000 | [diff] [blame] | 155 | |
Mark Dickinson | f0acfee | 2012-06-23 11:14:22 +0100 | [diff] [blame] | 156 | .. index:: |
| 157 | single: OverflowError (built-in exception) |
| 158 | |
| 159 | Return a C :c:type:`long long` representation of *obj*. If *obj* is not an |
Mark Dickinson | 20941de | 2020-05-27 13:43:17 +0100 | [diff] [blame] | 160 | instance of :c:type:`PyLongObject`, first call its :meth:`__index__` method |
| 161 | (if present) to convert it to a :c:type:`PyLongObject`. |
Mark Dickinson | f0acfee | 2012-06-23 11:14:22 +0100 | [diff] [blame] | 162 | |
| 163 | Raise :exc:`OverflowError` if the value of *obj* is out of range for a |
Keith Erskine | 47be7d0 | 2020-01-21 13:14:13 -0600 | [diff] [blame] | 164 | :c:type:`long long`. |
Mark Dickinson | f0acfee | 2012-06-23 11:14:22 +0100 | [diff] [blame] | 165 | |
Serhiy Storchaka | 5bb0005 | 2018-02-09 13:31:19 +0200 | [diff] [blame] | 166 | Returns ``-1`` on error. Use :c:func:`PyErr_Occurred` to disambiguate. |
Gregory P. Smith | f5b04a3 | 2018-01-28 17:48:31 -0800 | [diff] [blame] | 167 | |
Serhiy Storchaka | 6a44f6e | 2019-02-25 17:57:58 +0200 | [diff] [blame] | 168 | .. versionchanged:: 3.8 |
| 169 | Use :meth:`__index__` if available. |
| 170 | |
Mark Dickinson | 20941de | 2020-05-27 13:43:17 +0100 | [diff] [blame] | 171 | .. versionchanged:: 3.10 |
| 172 | This function will no longer use :meth:`__int__`. |
Serhiy Storchaka | 6a44f6e | 2019-02-25 17:57:58 +0200 | [diff] [blame] | 173 | |
Mark Dickinson | f0acfee | 2012-06-23 11:14:22 +0100 | [diff] [blame] | 174 | |
Benjamin Peterson | 47ff073 | 2016-09-08 09:15:54 -0700 | [diff] [blame] | 175 | .. c:function:: long long PyLong_AsLongLongAndOverflow(PyObject *obj, int *overflow) |
Mark Dickinson | f0acfee | 2012-06-23 11:14:22 +0100 | [diff] [blame] | 176 | |
| 177 | Return a C :c:type:`long long` representation of *obj*. If *obj* is not an |
Mark Dickinson | 20941de | 2020-05-27 13:43:17 +0100 | [diff] [blame] | 178 | instance of :c:type:`PyLongObject`, first call its :meth:`__index__` method |
| 179 | (if present) to convert it to a :c:type:`PyLongObject`. |
Mark Dickinson | f0acfee | 2012-06-23 11:14:22 +0100 | [diff] [blame] | 180 | |
Sergey Fedoseev | 1f9f69d | 2019-12-05 19:55:28 +0500 | [diff] [blame] | 181 | If the value of *obj* is greater than :const:`LLONG_MAX` or less than |
| 182 | :const:`LLONG_MIN`, set *\*overflow* to ``1`` or ``-1``, respectively, |
Mark Dickinson | f0acfee | 2012-06-23 11:14:22 +0100 | [diff] [blame] | 183 | and return ``-1``; otherwise, set *\*overflow* to ``0``. If any other |
| 184 | exception occurs set *\*overflow* to ``0`` and return ``-1`` as usual. |
Mark Dickinson | 93f562c | 2010-01-30 10:30:15 +0000 | [diff] [blame] | 185 | |
Serhiy Storchaka | 5bb0005 | 2018-02-09 13:31:19 +0200 | [diff] [blame] | 186 | Returns ``-1`` on error. Use :c:func:`PyErr_Occurred` to disambiguate. |
Gregory P. Smith | f5b04a3 | 2018-01-28 17:48:31 -0800 | [diff] [blame] | 187 | |
Mark Dickinson | 93f562c | 2010-01-30 10:30:15 +0000 | [diff] [blame] | 188 | .. versionadded:: 3.2 |
| 189 | |
Serhiy Storchaka | 6a44f6e | 2019-02-25 17:57:58 +0200 | [diff] [blame] | 190 | .. versionchanged:: 3.8 |
| 191 | Use :meth:`__index__` if available. |
| 192 | |
Mark Dickinson | 20941de | 2020-05-27 13:43:17 +0100 | [diff] [blame] | 193 | .. versionchanged:: 3.10 |
| 194 | This function will no longer use :meth:`__int__`. |
Serhiy Storchaka | 6a44f6e | 2019-02-25 17:57:58 +0200 | [diff] [blame] | 195 | |
Mark Dickinson | 93f562c | 2010-01-30 10:30:15 +0000 | [diff] [blame] | 196 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 197 | .. c:function:: Py_ssize_t PyLong_AsSsize_t(PyObject *pylong) |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 198 | |
| 199 | .. index:: |
| 200 | single: PY_SSIZE_T_MAX |
| 201 | single: OverflowError (built-in exception) |
| 202 | |
Mark Dickinson | b8dc3ab | 2012-06-23 12:12:52 +0100 | [diff] [blame] | 203 | Return a C :c:type:`Py_ssize_t` representation of *pylong*. *pylong* must |
| 204 | be an instance of :c:type:`PyLongObject`. |
| 205 | |
| 206 | Raise :exc:`OverflowError` if the value of *pylong* is out of range for a |
| 207 | :c:type:`Py_ssize_t`. |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 208 | |
Serhiy Storchaka | 5bb0005 | 2018-02-09 13:31:19 +0200 | [diff] [blame] | 209 | Returns ``-1`` on error. Use :c:func:`PyErr_Occurred` to disambiguate. |
Gregory P. Smith | f5b04a3 | 2018-01-28 17:48:31 -0800 | [diff] [blame] | 210 | |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 211 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 212 | .. c:function:: unsigned long PyLong_AsUnsignedLong(PyObject *pylong) |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 213 | |
| 214 | .. index:: |
| 215 | single: ULONG_MAX |
| 216 | single: OverflowError (built-in exception) |
| 217 | |
Mark Dickinson | b8dc3ab | 2012-06-23 12:12:52 +0100 | [diff] [blame] | 218 | Return a C :c:type:`unsigned long` representation of *pylong*. *pylong* |
| 219 | must be an instance of :c:type:`PyLongObject`. |
| 220 | |
| 221 | Raise :exc:`OverflowError` if the value of *pylong* is out of range for a |
| 222 | :c:type:`unsigned long`. |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 223 | |
Gregory P. Smith | f5b04a3 | 2018-01-28 17:48:31 -0800 | [diff] [blame] | 224 | Returns ``(unsigned long)-1`` on error. |
| 225 | Use :c:func:`PyErr_Occurred` to disambiguate. |
| 226 | |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 227 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 228 | .. c:function:: size_t PyLong_AsSize_t(PyObject *pylong) |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 229 | |
Gregory P. Smith | f5b04a3 | 2018-01-28 17:48:31 -0800 | [diff] [blame] | 230 | .. index:: |
| 231 | single: SIZE_MAX |
| 232 | single: OverflowError (built-in exception) |
| 233 | |
Terry Jan Reedy | 65e69b3 | 2013-03-11 17:23:46 -0400 | [diff] [blame] | 234 | Return a C :c:type:`size_t` representation of *pylong*. *pylong* must be |
Mark Dickinson | b8dc3ab | 2012-06-23 12:12:52 +0100 | [diff] [blame] | 235 | an instance of :c:type:`PyLongObject`. |
| 236 | |
| 237 | Raise :exc:`OverflowError` if the value of *pylong* is out of range for a |
| 238 | :c:type:`size_t`. |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 239 | |
Gregory P. Smith | f5b04a3 | 2018-01-28 17:48:31 -0800 | [diff] [blame] | 240 | Returns ``(size_t)-1`` on error. |
| 241 | Use :c:func:`PyErr_Occurred` to disambiguate. |
| 242 | |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 243 | |
Benjamin Peterson | 47ff073 | 2016-09-08 09:15:54 -0700 | [diff] [blame] | 244 | .. c:function:: unsigned long long PyLong_AsUnsignedLongLong(PyObject *pylong) |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 245 | |
Mark Dickinson | 2177607 | 2009-02-10 16:13:25 +0000 | [diff] [blame] | 246 | .. index:: |
| 247 | single: OverflowError (built-in exception) |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 248 | |
Benjamin Peterson | 47ff073 | 2016-09-08 09:15:54 -0700 | [diff] [blame] | 249 | Return a C :c:type:`unsigned long long` representation of *pylong*. *pylong* |
| 250 | must be an instance of :c:type:`PyLongObject`. |
Mark Dickinson | b8dc3ab | 2012-06-23 12:12:52 +0100 | [diff] [blame] | 251 | |
| 252 | Raise :exc:`OverflowError` if the value of *pylong* is out of range for an |
Benjamin Peterson | 47ff073 | 2016-09-08 09:15:54 -0700 | [diff] [blame] | 253 | :c:type:`unsigned long long`. |
Mark Dickinson | 2177607 | 2009-02-10 16:13:25 +0000 | [diff] [blame] | 254 | |
Gregory P. Smith | f5b04a3 | 2018-01-28 17:48:31 -0800 | [diff] [blame] | 255 | Returns ``(unsigned long long)-1`` on error. |
| 256 | Use :c:func:`PyErr_Occurred` to disambiguate. |
| 257 | |
Mark Dickinson | 2177607 | 2009-02-10 16:13:25 +0000 | [diff] [blame] | 258 | .. versionchanged:: 3.1 |
Georg Brandl | 67b21b7 | 2010-08-17 15:07:14 +0000 | [diff] [blame] | 259 | A negative *pylong* now raises :exc:`OverflowError`, not :exc:`TypeError`. |
| 260 | |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 261 | |
Mark Dickinson | b8dc3ab | 2012-06-23 12:12:52 +0100 | [diff] [blame] | 262 | .. c:function:: unsigned long PyLong_AsUnsignedLongMask(PyObject *obj) |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 263 | |
Mark Dickinson | 20941de | 2020-05-27 13:43:17 +0100 | [diff] [blame] | 264 | Return a C :c:type:`unsigned long` representation of *obj*. If *obj* is not |
| 265 | an instance of :c:type:`PyLongObject`, first call its :meth:`__index__` |
| 266 | method (if present) to convert it to a :c:type:`PyLongObject`. |
Mark Dickinson | b8dc3ab | 2012-06-23 12:12:52 +0100 | [diff] [blame] | 267 | |
| 268 | If the value of *obj* is out of range for an :c:type:`unsigned long`, |
Serhiy Storchaka | 1ecf7d2 | 2016-10-27 21:41:19 +0300 | [diff] [blame] | 269 | return the reduction of that value modulo ``ULONG_MAX + 1``. |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 270 | |
Zackery Spytz | dc24765 | 2019-06-06 14:39:23 -0600 | [diff] [blame] | 271 | Returns ``(unsigned long)-1`` on error. Use :c:func:`PyErr_Occurred` to |
| 272 | disambiguate. |
Gregory P. Smith | f5b04a3 | 2018-01-28 17:48:31 -0800 | [diff] [blame] | 273 | |
Serhiy Storchaka | 6a44f6e | 2019-02-25 17:57:58 +0200 | [diff] [blame] | 274 | .. versionchanged:: 3.8 |
| 275 | Use :meth:`__index__` if available. |
| 276 | |
Mark Dickinson | 20941de | 2020-05-27 13:43:17 +0100 | [diff] [blame] | 277 | .. versionchanged:: 3.10 |
| 278 | This function will no longer use :meth:`__int__`. |
Serhiy Storchaka | 6a44f6e | 2019-02-25 17:57:58 +0200 | [diff] [blame] | 279 | |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 280 | |
Benjamin Peterson | 47ff073 | 2016-09-08 09:15:54 -0700 | [diff] [blame] | 281 | .. c:function:: unsigned long long PyLong_AsUnsignedLongLongMask(PyObject *obj) |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 282 | |
Mark Dickinson | b8dc3ab | 2012-06-23 12:12:52 +0100 | [diff] [blame] | 283 | Return a C :c:type:`unsigned long long` representation of *obj*. If *obj* |
Serhiy Storchaka | 6a44f6e | 2019-02-25 17:57:58 +0200 | [diff] [blame] | 284 | is not an instance of :c:type:`PyLongObject`, first call its |
Mark Dickinson | 20941de | 2020-05-27 13:43:17 +0100 | [diff] [blame] | 285 | :meth:`__index__` method (if present) to convert it to a |
| 286 | :c:type:`PyLongObject`. |
Mark Dickinson | b8dc3ab | 2012-06-23 12:12:52 +0100 | [diff] [blame] | 287 | |
| 288 | If the value of *obj* is out of range for an :c:type:`unsigned long long`, |
Sergey Fedoseev | a2ff283 | 2019-12-10 01:22:19 +0500 | [diff] [blame] | 289 | return the reduction of that value modulo ``ULLONG_MAX + 1``. |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 290 | |
Zackery Spytz | dc24765 | 2019-06-06 14:39:23 -0600 | [diff] [blame] | 291 | Returns ``(unsigned long long)-1`` on error. Use :c:func:`PyErr_Occurred` |
| 292 | to disambiguate. |
Gregory P. Smith | f5b04a3 | 2018-01-28 17:48:31 -0800 | [diff] [blame] | 293 | |
Serhiy Storchaka | 6a44f6e | 2019-02-25 17:57:58 +0200 | [diff] [blame] | 294 | .. versionchanged:: 3.8 |
| 295 | Use :meth:`__index__` if available. |
| 296 | |
Mark Dickinson | 20941de | 2020-05-27 13:43:17 +0100 | [diff] [blame] | 297 | .. versionchanged:: 3.10 |
| 298 | This function will no longer use :meth:`__int__`. |
Serhiy Storchaka | 6a44f6e | 2019-02-25 17:57:58 +0200 | [diff] [blame] | 299 | |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 300 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 301 | .. c:function:: double PyLong_AsDouble(PyObject *pylong) |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 302 | |
Mark Dickinson | b8dc3ab | 2012-06-23 12:12:52 +0100 | [diff] [blame] | 303 | Return a C :c:type:`double` representation of *pylong*. *pylong* must be |
| 304 | an instance of :c:type:`PyLongObject`. |
| 305 | |
| 306 | Raise :exc:`OverflowError` if the value of *pylong* is out of range for a |
| 307 | :c:type:`double`. |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 308 | |
Serhiy Storchaka | 5bb0005 | 2018-02-09 13:31:19 +0200 | [diff] [blame] | 309 | Returns ``-1.0`` on error. Use :c:func:`PyErr_Occurred` to disambiguate. |
Gregory P. Smith | f5b04a3 | 2018-01-28 17:48:31 -0800 | [diff] [blame] | 310 | |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 311 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 312 | .. c:function:: void* PyLong_AsVoidPtr(PyObject *pylong) |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 313 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 314 | Convert a Python integer *pylong* to a C :c:type:`void` pointer. |
Christian Heimes | c3f30c4 | 2008-02-22 16:37:40 +0000 | [diff] [blame] | 315 | If *pylong* cannot be converted, an :exc:`OverflowError` will be raised. This |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 316 | is only assured to produce a usable :c:type:`void` pointer for values created |
| 317 | with :c:func:`PyLong_FromVoidPtr`. |
Gregory P. Smith | f5b04a3 | 2018-01-28 17:48:31 -0800 | [diff] [blame] | 318 | |
Serhiy Storchaka | 25fc088 | 2019-10-30 12:03:20 +0200 | [diff] [blame] | 319 | Returns ``NULL`` on error. Use :c:func:`PyErr_Occurred` to disambiguate. |