Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 1 | .. highlightlang:: c |
| 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 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [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 |
| 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 Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 50 | .. c:function:: PyObject* PyLong_FromUnsignedLong(unsigned long v) |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 51 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 52 | Return a new :c:type:`PyLongObject` object from a C :c:type:`unsigned long`, or |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 53 | *NULL* on failure. |
| 54 | |
| 55 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 56 | .. c:function:: PyObject* PyLong_FromSsize_t(Py_ssize_t v) |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 57 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 58 | Return a new :c:type:`PyLongObject` object from a C :c:type:`Py_ssize_t`, or |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 59 | *NULL* on failure. |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 60 | |
| 61 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 62 | .. c:function:: PyObject* PyLong_FromSize_t(size_t v) |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 63 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 64 | Return a new :c:type:`PyLongObject` object from a C :c:type:`size_t`, or |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 65 | *NULL* on failure. |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 66 | |
| 67 | |
Benjamin Peterson | 47ff073 | 2016-09-08 09:15:54 -0700 | [diff] [blame] | 68 | .. c:function:: PyObject* PyLong_FromLongLong(long long v) |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 69 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 70 | 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] | 71 | on failure. |
| 72 | |
| 73 | |
Benjamin Peterson | 47ff073 | 2016-09-08 09:15:54 -0700 | [diff] [blame] | 74 | .. c:function:: PyObject* PyLong_FromUnsignedLongLong(unsigned long long v) |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 75 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 76 | Return a new :c:type:`PyLongObject` object from a C :c:type:`unsigned long long`, |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 77 | or *NULL* on failure. |
| 78 | |
| 79 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 80 | .. c:function:: PyObject* PyLong_FromDouble(double v) |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 81 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 82 | Return a new :c:type:`PyLongObject` object from the integer part of *v*, or |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 83 | *NULL* on failure. |
| 84 | |
| 85 | |
Serhiy Storchaka | c679227 | 2013-10-19 21:03:34 +0300 | [diff] [blame] | 86 | .. c:function:: PyObject* PyLong_FromString(const char *str, char **pend, int base) |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 87 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 88 | Return a new :c:type:`PyLongObject` based on the string value in *str*, which |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 89 | 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] | 90 | *\*pend* will point to the first character in *str* which follows the |
csabella | 26896f2 | 2017-04-23 23:54:08 -0400 | [diff] [blame] | 91 | 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 Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 97 | |
| 98 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 99 | .. c:function:: PyObject* PyLong_FromUnicode(Py_UNICODE *u, Py_ssize_t length, int base) |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 100 | |
| 101 | Convert a sequence of Unicode digits to a Python integer value. The Unicode |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 102 | string is first encoded to a byte string using :c:func:`PyUnicode_EncodeDecimal` |
| 103 | and then converted using :c:func:`PyLong_FromString`. |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 104 | |
Georg Brandl | db6c7f5 | 2011-10-07 11:19:11 +0200 | [diff] [blame] | 105 | .. 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 Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 119 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 120 | .. c:function:: PyObject* PyLong_FromVoidPtr(void *p) |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 121 | |
| 122 | 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] | 123 | retrieved from the resulting value using :c:func:`PyLong_AsVoidPtr`. |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 124 | |
| 125 | |
Georg Brandl | 48310cd | 2009-01-03 21:18:54 +0000 | [diff] [blame] | 126 | .. XXX alias PyLong_AS_LONG (for now) |
Mark Dickinson | 0a22924 | 2012-06-23 10:49:12 +0100 | [diff] [blame] | 127 | .. c:function:: long PyLong_AsLong(PyObject *obj) |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 128 | |
| 129 | .. index:: |
| 130 | single: LONG_MAX |
| 131 | single: OverflowError (built-in exception) |
| 132 | |
Mark Dickinson | 0a22924 | 2012-06-23 10:49:12 +0100 | [diff] [blame] | 133 | Return a C :c:type:`long` representation of *obj*. If *obj* is not an |
Serhiy Storchaka | 6a44f6e | 2019-02-25 17:57:58 +0200 | [diff] [blame] | 134 | instance of :c:type:`PyLongObject`, first call its :meth:`__index__` or |
| 135 | :meth:`__int__` method (if present) to convert it to a |
| 136 | :c:type:`PyLongObject`. |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 137 | |
Mark Dickinson | 0a22924 | 2012-06-23 10:49:12 +0100 | [diff] [blame] | 138 | Raise :exc:`OverflowError` if the value of *obj* is out of range for a |
| 139 | :c:type:`long`. |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 140 | |
Serhiy Storchaka | 5bb0005 | 2018-02-09 13:31:19 +0200 | [diff] [blame] | 141 | Returns ``-1`` on error. Use :c:func:`PyErr_Occurred` to disambiguate. |
Gregory P. Smith | f5b04a3 | 2018-01-28 17:48:31 -0800 | [diff] [blame] | 142 | |
Serhiy Storchaka | 6a44f6e | 2019-02-25 17:57:58 +0200 | [diff] [blame] | 143 | .. versionchanged:: 3.8 |
| 144 | Use :meth:`__index__` if available. |
| 145 | |
| 146 | .. deprecated:: 3.8 |
| 147 | Using :meth:`__int__` is deprecated. |
| 148 | |
Mark Dickinson | f0acfee | 2012-06-23 11:14:22 +0100 | [diff] [blame] | 149 | |
Mark Dickinson | 0a22924 | 2012-06-23 10:49:12 +0100 | [diff] [blame] | 150 | .. c:function:: long PyLong_AsLongAndOverflow(PyObject *obj, int *overflow) |
| 151 | |
| 152 | Return a C :c:type:`long` representation of *obj*. If *obj* is not an |
Serhiy Storchaka | 6a44f6e | 2019-02-25 17:57:58 +0200 | [diff] [blame] | 153 | instance of :c:type:`PyLongObject`, first call its :meth:`__index__` or |
| 154 | :meth:`__int__` method (if present) to convert it to a |
| 155 | :c:type:`PyLongObject`. |
Mark Dickinson | 0a22924 | 2012-06-23 10:49:12 +0100 | [diff] [blame] | 156 | |
| 157 | If the value of *obj* is greater than :const:`LONG_MAX` or less than |
| 158 | :const:`LONG_MIN`, set *\*overflow* to ``1`` or ``-1``, respectively, and |
| 159 | return ``-1``; otherwise, set *\*overflow* to ``0``. If any other exception |
| 160 | occurs set *\*overflow* to ``0`` and return ``-1`` as usual. |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 161 | |
Serhiy Storchaka | 5bb0005 | 2018-02-09 13:31:19 +0200 | [diff] [blame] | 162 | Returns ``-1`` on error. Use :c:func:`PyErr_Occurred` to disambiguate. |
Gregory P. Smith | f5b04a3 | 2018-01-28 17:48:31 -0800 | [diff] [blame] | 163 | |
Serhiy Storchaka | 6a44f6e | 2019-02-25 17:57:58 +0200 | [diff] [blame] | 164 | .. versionchanged:: 3.8 |
| 165 | Use :meth:`__index__` if available. |
| 166 | |
| 167 | .. deprecated:: 3.8 |
| 168 | Using :meth:`__int__` is deprecated. |
| 169 | |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 170 | |
Benjamin Peterson | 47ff073 | 2016-09-08 09:15:54 -0700 | [diff] [blame] | 171 | .. c:function:: long long PyLong_AsLongLong(PyObject *obj) |
Mark Dickinson | 93f562c | 2010-01-30 10:30:15 +0000 | [diff] [blame] | 172 | |
Mark Dickinson | f0acfee | 2012-06-23 11:14:22 +0100 | [diff] [blame] | 173 | .. index:: |
| 174 | single: OverflowError (built-in exception) |
| 175 | |
| 176 | Return a C :c:type:`long long` representation of *obj*. If *obj* is not an |
Serhiy Storchaka | 6a44f6e | 2019-02-25 17:57:58 +0200 | [diff] [blame] | 177 | instance of :c:type:`PyLongObject`, first call its :meth:`__index__` or |
| 178 | :meth:`__int__` method (if present) to convert it to a |
| 179 | :c:type:`PyLongObject`. |
Mark Dickinson | f0acfee | 2012-06-23 11:14:22 +0100 | [diff] [blame] | 180 | |
| 181 | Raise :exc:`OverflowError` if the value of *obj* is out of range for a |
| 182 | :c:type:`long`. |
| 183 | |
Serhiy Storchaka | 5bb0005 | 2018-02-09 13:31:19 +0200 | [diff] [blame] | 184 | Returns ``-1`` on error. Use :c:func:`PyErr_Occurred` to disambiguate. |
Gregory P. Smith | f5b04a3 | 2018-01-28 17:48:31 -0800 | [diff] [blame] | 185 | |
Serhiy Storchaka | 6a44f6e | 2019-02-25 17:57:58 +0200 | [diff] [blame] | 186 | .. versionchanged:: 3.8 |
| 187 | Use :meth:`__index__` if available. |
| 188 | |
| 189 | .. deprecated:: 3.8 |
| 190 | Using :meth:`__int__` is deprecated. |
| 191 | |
Mark Dickinson | f0acfee | 2012-06-23 11:14:22 +0100 | [diff] [blame] | 192 | |
Benjamin Peterson | 47ff073 | 2016-09-08 09:15:54 -0700 | [diff] [blame] | 193 | .. c:function:: long long PyLong_AsLongLongAndOverflow(PyObject *obj, int *overflow) |
Mark Dickinson | f0acfee | 2012-06-23 11:14:22 +0100 | [diff] [blame] | 194 | |
| 195 | Return a C :c:type:`long long` representation of *obj*. If *obj* is not an |
Serhiy Storchaka | 6a44f6e | 2019-02-25 17:57:58 +0200 | [diff] [blame] | 196 | instance of :c:type:`PyLongObject`, first call its :meth:`__index__` or |
| 197 | :meth:`__int__` method (if present) to convert it to a |
| 198 | :c:type:`PyLongObject`. |
Mark Dickinson | f0acfee | 2012-06-23 11:14:22 +0100 | [diff] [blame] | 199 | |
| 200 | If the value of *obj* is greater than :const:`PY_LLONG_MAX` or less than |
| 201 | :const:`PY_LLONG_MIN`, set *\*overflow* to ``1`` or ``-1``, respectively, |
| 202 | and return ``-1``; otherwise, set *\*overflow* to ``0``. If any other |
| 203 | exception occurs set *\*overflow* to ``0`` and return ``-1`` as usual. |
Mark Dickinson | 93f562c | 2010-01-30 10:30:15 +0000 | [diff] [blame] | 204 | |
Serhiy Storchaka | 5bb0005 | 2018-02-09 13:31:19 +0200 | [diff] [blame] | 205 | Returns ``-1`` on error. Use :c:func:`PyErr_Occurred` to disambiguate. |
Gregory P. Smith | f5b04a3 | 2018-01-28 17:48:31 -0800 | [diff] [blame] | 206 | |
Mark Dickinson | 93f562c | 2010-01-30 10:30:15 +0000 | [diff] [blame] | 207 | .. versionadded:: 3.2 |
| 208 | |
Serhiy Storchaka | 6a44f6e | 2019-02-25 17:57:58 +0200 | [diff] [blame] | 209 | .. versionchanged:: 3.8 |
| 210 | Use :meth:`__index__` if available. |
| 211 | |
| 212 | .. deprecated:: 3.8 |
| 213 | Using :meth:`__int__` is deprecated. |
| 214 | |
Mark Dickinson | 93f562c | 2010-01-30 10:30:15 +0000 | [diff] [blame] | 215 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 216 | .. c:function:: Py_ssize_t PyLong_AsSsize_t(PyObject *pylong) |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 217 | |
| 218 | .. index:: |
| 219 | single: PY_SSIZE_T_MAX |
| 220 | single: OverflowError (built-in exception) |
| 221 | |
Mark Dickinson | b8dc3ab | 2012-06-23 12:12:52 +0100 | [diff] [blame] | 222 | Return a C :c:type:`Py_ssize_t` representation of *pylong*. *pylong* must |
| 223 | be an instance of :c:type:`PyLongObject`. |
| 224 | |
| 225 | Raise :exc:`OverflowError` if the value of *pylong* is out of range for a |
| 226 | :c:type:`Py_ssize_t`. |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 227 | |
Serhiy Storchaka | 5bb0005 | 2018-02-09 13:31:19 +0200 | [diff] [blame] | 228 | Returns ``-1`` on error. Use :c:func:`PyErr_Occurred` to disambiguate. |
Gregory P. Smith | f5b04a3 | 2018-01-28 17:48:31 -0800 | [diff] [blame] | 229 | |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 230 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 231 | .. c:function:: unsigned long PyLong_AsUnsignedLong(PyObject *pylong) |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 232 | |
| 233 | .. index:: |
| 234 | single: ULONG_MAX |
| 235 | single: OverflowError (built-in exception) |
| 236 | |
Mark Dickinson | b8dc3ab | 2012-06-23 12:12:52 +0100 | [diff] [blame] | 237 | Return a C :c:type:`unsigned long` representation of *pylong*. *pylong* |
| 238 | must be an instance of :c:type:`PyLongObject`. |
| 239 | |
| 240 | Raise :exc:`OverflowError` if the value of *pylong* is out of range for a |
| 241 | :c:type:`unsigned long`. |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 242 | |
Gregory P. Smith | f5b04a3 | 2018-01-28 17:48:31 -0800 | [diff] [blame] | 243 | Returns ``(unsigned long)-1`` on error. |
| 244 | Use :c:func:`PyErr_Occurred` to disambiguate. |
| 245 | |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 246 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 247 | .. c:function:: size_t PyLong_AsSize_t(PyObject *pylong) |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 248 | |
Gregory P. Smith | f5b04a3 | 2018-01-28 17:48:31 -0800 | [diff] [blame] | 249 | .. index:: |
| 250 | single: SIZE_MAX |
| 251 | single: OverflowError (built-in exception) |
| 252 | |
Terry Jan Reedy | 65e69b3 | 2013-03-11 17:23:46 -0400 | [diff] [blame] | 253 | 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] | 254 | an instance of :c:type:`PyLongObject`. |
| 255 | |
| 256 | Raise :exc:`OverflowError` if the value of *pylong* is out of range for a |
| 257 | :c:type:`size_t`. |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 258 | |
Gregory P. Smith | f5b04a3 | 2018-01-28 17:48:31 -0800 | [diff] [blame] | 259 | Returns ``(size_t)-1`` on error. |
| 260 | Use :c:func:`PyErr_Occurred` to disambiguate. |
| 261 | |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 262 | |
Benjamin Peterson | 47ff073 | 2016-09-08 09:15:54 -0700 | [diff] [blame] | 263 | .. c:function:: unsigned long long PyLong_AsUnsignedLongLong(PyObject *pylong) |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 264 | |
Mark Dickinson | 2177607 | 2009-02-10 16:13:25 +0000 | [diff] [blame] | 265 | .. index:: |
| 266 | single: OverflowError (built-in exception) |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 267 | |
Benjamin Peterson | 47ff073 | 2016-09-08 09:15:54 -0700 | [diff] [blame] | 268 | Return a C :c:type:`unsigned long long` representation of *pylong*. *pylong* |
| 269 | must be an instance of :c:type:`PyLongObject`. |
Mark Dickinson | b8dc3ab | 2012-06-23 12:12:52 +0100 | [diff] [blame] | 270 | |
| 271 | 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] | 272 | :c:type:`unsigned long long`. |
Mark Dickinson | 2177607 | 2009-02-10 16:13:25 +0000 | [diff] [blame] | 273 | |
Gregory P. Smith | f5b04a3 | 2018-01-28 17:48:31 -0800 | [diff] [blame] | 274 | Returns ``(unsigned long long)-1`` on error. |
| 275 | Use :c:func:`PyErr_Occurred` to disambiguate. |
| 276 | |
Mark Dickinson | 2177607 | 2009-02-10 16:13:25 +0000 | [diff] [blame] | 277 | .. versionchanged:: 3.1 |
Georg Brandl | 67b21b7 | 2010-08-17 15:07:14 +0000 | [diff] [blame] | 278 | A negative *pylong* now raises :exc:`OverflowError`, not :exc:`TypeError`. |
| 279 | |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 280 | |
Mark Dickinson | b8dc3ab | 2012-06-23 12:12:52 +0100 | [diff] [blame] | 281 | .. c:function:: unsigned long PyLong_AsUnsignedLongMask(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` 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 |
| 285 | :meth:`__index__` or :meth:`__int__` method (if present) to convert |
| 286 | it to a :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`, |
Serhiy Storchaka | 1ecf7d2 | 2016-10-27 21:41:19 +0300 | [diff] [blame] | 289 | return the reduction of that value modulo ``ULONG_MAX + 1``. |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 290 | |
Serhiy Storchaka | 5bb0005 | 2018-02-09 13:31:19 +0200 | [diff] [blame] | 291 | Returns ``-1`` on error. Use :c:func:`PyErr_Occurred` to disambiguate. |
Gregory P. Smith | f5b04a3 | 2018-01-28 17:48:31 -0800 | [diff] [blame] | 292 | |
Serhiy Storchaka | 6a44f6e | 2019-02-25 17:57:58 +0200 | [diff] [blame] | 293 | .. versionchanged:: 3.8 |
| 294 | Use :meth:`__index__` if available. |
| 295 | |
| 296 | .. deprecated:: 3.8 |
| 297 | Using :meth:`__int__` is deprecated. |
| 298 | |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 299 | |
Benjamin Peterson | 47ff073 | 2016-09-08 09:15:54 -0700 | [diff] [blame] | 300 | .. c:function:: unsigned long long PyLong_AsUnsignedLongLongMask(PyObject *obj) |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 301 | |
Mark Dickinson | b8dc3ab | 2012-06-23 12:12:52 +0100 | [diff] [blame] | 302 | 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] | 303 | is not an instance of :c:type:`PyLongObject`, first call its |
| 304 | :meth:`__index__` or :meth:`__int__` method (if present) to convert |
| 305 | it to a :c:type:`PyLongObject`. |
Mark Dickinson | b8dc3ab | 2012-06-23 12:12:52 +0100 | [diff] [blame] | 306 | |
| 307 | If the value of *obj* is out of range for an :c:type:`unsigned long long`, |
Serhiy Storchaka | 1ecf7d2 | 2016-10-27 21:41:19 +0300 | [diff] [blame] | 308 | return the reduction of that value modulo ``PY_ULLONG_MAX + 1``. |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 309 | |
Serhiy Storchaka | 5bb0005 | 2018-02-09 13:31:19 +0200 | [diff] [blame] | 310 | Returns ``-1`` on error. Use :c:func:`PyErr_Occurred` to disambiguate. |
Gregory P. Smith | f5b04a3 | 2018-01-28 17:48:31 -0800 | [diff] [blame] | 311 | |
Serhiy Storchaka | 6a44f6e | 2019-02-25 17:57:58 +0200 | [diff] [blame] | 312 | .. versionchanged:: 3.8 |
| 313 | Use :meth:`__index__` if available. |
| 314 | |
| 315 | .. deprecated:: 3.8 |
| 316 | Using :meth:`__int__` is deprecated. |
| 317 | |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 318 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 319 | .. c:function:: double PyLong_AsDouble(PyObject *pylong) |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 320 | |
Mark Dickinson | b8dc3ab | 2012-06-23 12:12:52 +0100 | [diff] [blame] | 321 | Return a C :c:type:`double` representation of *pylong*. *pylong* must be |
| 322 | an instance of :c:type:`PyLongObject`. |
| 323 | |
| 324 | Raise :exc:`OverflowError` if the value of *pylong* is out of range for a |
| 325 | :c:type:`double`. |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 326 | |
Serhiy Storchaka | 5bb0005 | 2018-02-09 13:31:19 +0200 | [diff] [blame] | 327 | 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] | 328 | |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 329 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 330 | .. c:function:: void* PyLong_AsVoidPtr(PyObject *pylong) |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 331 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 332 | Convert a Python integer *pylong* to a C :c:type:`void` pointer. |
Christian Heimes | c3f30c4 | 2008-02-22 16:37:40 +0000 | [diff] [blame] | 333 | If *pylong* cannot be converted, an :exc:`OverflowError` will be raised. This |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 334 | is only assured to produce a usable :c:type:`void` pointer for values created |
| 335 | with :c:func:`PyLong_FromVoidPtr`. |
Gregory P. Smith | f5b04a3 | 2018-01-28 17:48:31 -0800 | [diff] [blame] | 336 | |
Serhiy Storchaka | 5bb0005 | 2018-02-09 13:31:19 +0200 | [diff] [blame] | 337 | Returns *NULL* on error. Use :c:func:`PyErr_Occurred` to disambiguate. |