| 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 |  | 
 | 13 | .. ctype:: PyLongObject | 
 | 14 |  | 
 | 15 |    This subtype of :ctype:`PyObject` represents a Python integer object. | 
 | 16 |  | 
 | 17 |  | 
 | 18 | .. cvar:: PyTypeObject PyLong_Type | 
 | 19 |  | 
 | 20 |    This instance of :ctype:`PyTypeObject` represents the Python integer type. | 
 | 21 |    This is the same object as ``int``. | 
 | 22 |  | 
 | 23 |  | 
 | 24 | .. cfunction:: int PyLong_Check(PyObject *p) | 
 | 25 |  | 
 | 26 |    Return true if its argument is a :ctype:`PyLongObject` or a subtype of | 
 | 27 |    :ctype:`PyLongObject`. | 
 | 28 |  | 
 | 29 |  | 
 | 30 | .. cfunction:: int PyLong_CheckExact(PyObject *p) | 
 | 31 |  | 
 | 32 |    Return true if its argument is a :ctype:`PyLongObject`, but not a subtype of | 
 | 33 |    :ctype:`PyLongObject`. | 
 | 34 |  | 
 | 35 |  | 
 | 36 | .. cfunction:: PyObject* PyLong_FromLong(long v) | 
 | 37 |  | 
 | 38 |    Return a new :ctype:`PyLongObject` object from *v*, or *NULL* on failure. | 
 | 39 |  | 
 | 40 |    The current implementation keeps an array of integer objects for all integers | 
 | 41 |    between ``-5`` and ``256``, when you create an int in that range you actually | 
 | 42 |    just get back a reference to the existing object. So it should be possible to | 
 | 43 |    change the value of ``1``.  I suspect the behaviour of Python in this case is | 
 | 44 |    undefined. :-) | 
 | 45 |  | 
 | 46 |  | 
 | 47 | .. cfunction:: PyObject* PyLong_FromUnsignedLong(unsigned long v) | 
 | 48 |  | 
 | 49 |    Return a new :ctype:`PyLongObject` object from a C :ctype:`unsigned long`, or | 
 | 50 |    *NULL* on failure. | 
 | 51 |  | 
 | 52 |  | 
 | 53 | .. cfunction:: PyObject* PyLong_FromSsize_t(Py_ssize_t v) | 
 | 54 |  | 
| Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 55 |    Return a new :ctype:`PyLongObject` object from a C :ctype:`Py_ssize_t`, or | 
 | 56 |    *NULL* on failure. | 
| Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 57 |  | 
 | 58 |  | 
 | 59 | .. cfunction:: PyObject* PyLong_FromSize_t(size_t v) | 
 | 60 |  | 
| Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 61 |    Return a new :ctype:`PyLongObject` object from a C :ctype:`size_t`, or | 
 | 62 |    *NULL* on failure. | 
| Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 63 |  | 
 | 64 |  | 
 | 65 | .. cfunction:: PyObject* PyLong_FromLongLong(PY_LONG_LONG v) | 
 | 66 |  | 
 | 67 |    Return a new :ctype:`PyLongObject` object from a C :ctype:`long long`, or *NULL* | 
 | 68 |    on failure. | 
 | 69 |  | 
 | 70 |  | 
 | 71 | .. cfunction:: PyObject* PyLong_FromUnsignedLongLong(unsigned PY_LONG_LONG v) | 
 | 72 |  | 
 | 73 |    Return a new :ctype:`PyLongObject` object from a C :ctype:`unsigned long long`, | 
 | 74 |    or *NULL* on failure. | 
 | 75 |  | 
 | 76 |  | 
 | 77 | .. cfunction:: PyObject* PyLong_FromDouble(double v) | 
 | 78 |  | 
 | 79 |    Return a new :ctype:`PyLongObject` object from the integer part of *v*, or | 
 | 80 |    *NULL* on failure. | 
 | 81 |  | 
 | 82 |  | 
 | 83 | .. cfunction:: PyObject* PyLong_FromString(char *str, char **pend, int base) | 
 | 84 |  | 
 | 85 |    Return a new :ctype:`PyLongObject` based on the string value in *str*, which | 
 | 86 |    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] | 87 |    *\*pend* will point to the first character in *str* which follows the | 
| Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 88 |    representation of the number.  If *base* is ``0``, the radix will be | 
 | 89 |    determined based on the leading characters of *str*: if *str* starts with | 
 | 90 |    ``'0x'`` or ``'0X'``, radix 16 will be used; if *str* starts with ``'0o'`` or | 
 | 91 |    ``'0O'``, radix 8 will be used; if *str* starts with ``'0b'`` or ``'0B'``, | 
 | 92 |    radix 2 will be used; otherwise radix 10 will be used.  If *base* is not | 
 | 93 |    ``0``, it must be between ``2`` and ``36``, inclusive.  Leading spaces are | 
 | 94 |    ignored.  If there are no digits, :exc:`ValueError` will be raised. | 
 | 95 |  | 
 | 96 |  | 
 | 97 | .. cfunction:: PyObject* PyLong_FromUnicode(Py_UNICODE *u, Py_ssize_t length, int base) | 
 | 98 |  | 
 | 99 |    Convert a sequence of Unicode digits to a Python integer value.  The Unicode | 
 | 100 |    string is first encoded to a byte string using :cfunc:`PyUnicode_EncodeDecimal` | 
 | 101 |    and then converted using :cfunc:`PyLong_FromString`. | 
 | 102 |  | 
 | 103 |  | 
 | 104 | .. cfunction:: PyObject* PyLong_FromVoidPtr(void *p) | 
 | 105 |  | 
 | 106 |    Create a Python integer from the pointer *p*. The pointer value can be | 
 | 107 |    retrieved from the resulting value using :cfunc:`PyLong_AsVoidPtr`. | 
 | 108 |  | 
 | 109 |  | 
| Georg Brandl | 48310cd | 2009-01-03 21:18:54 +0000 | [diff] [blame] | 110 | .. XXX alias PyLong_AS_LONG (for now) | 
| Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 111 | .. cfunction:: long PyLong_AsLong(PyObject *pylong) | 
 | 112 |  | 
 | 113 |    .. index:: | 
 | 114 |       single: LONG_MAX | 
 | 115 |       single: OverflowError (built-in exception) | 
 | 116 |  | 
 | 117 |    Return a C :ctype:`long` representation of the contents of *pylong*.  If | 
 | 118 |    *pylong* is greater than :const:`LONG_MAX`, raise an :exc:`OverflowError`, | 
 | 119 |    and return -1. Convert non-long objects automatically to long first, | 
 | 120 |    and return -1 if that raises exceptions. | 
 | 121 |  | 
| Ezio Melotti | ea30c6f | 2010-01-30 13:32:14 +0000 | [diff] [blame] | 122 | .. cfunction:: long PyLong_AsLongAndOverflow(PyObject *pylong, int *overflow) | 
| Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 123 |  | 
| Mark Dickinson | 309aa2d | 2009-12-21 12:37:06 +0000 | [diff] [blame] | 124 |    Return a C :ctype:`long` representation of the contents of | 
 | 125 |    *pylong*.  If *pylong* is greater than :const:`LONG_MAX` or less | 
| Ezio Melotti | ea30c6f | 2010-01-30 13:32:14 +0000 | [diff] [blame] | 126 |    than :const:`LONG_MIN`, set *\*overflow* to ``1`` or ``-1``, | 
 | 127 |    respectively, and return ``-1``; otherwise, set *\*overflow* to | 
| Mark Dickinson | 309aa2d | 2009-12-21 12:37:06 +0000 | [diff] [blame] | 128 |    ``0``.  If any other exception occurs (for example a TypeError or | 
| Ezio Melotti | ea30c6f | 2010-01-30 13:32:14 +0000 | [diff] [blame] | 129 |    MemoryError), then ``-1`` will be returned and *\*overflow* will | 
| Mark Dickinson | 309aa2d | 2009-12-21 12:37:06 +0000 | [diff] [blame] | 130 |    be ``0``. | 
| Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 131 |  | 
 | 132 |  | 
| Ezio Melotti | ea30c6f | 2010-01-30 13:32:14 +0000 | [diff] [blame] | 133 | .. cfunction:: PY_LONG_LONG PyLong_AsLongLongAndOverflow(PyObject *pylong, int *overflow) | 
| Mark Dickinson | 93f562c | 2010-01-30 10:30:15 +0000 | [diff] [blame] | 134 |  | 
 | 135 |    Return a C :ctype:`long long` representation of the contents of | 
 | 136 |    *pylong*.  If *pylong* is greater than :const:`PY_LLONG_MAX` or less | 
| Ezio Melotti | ea30c6f | 2010-01-30 13:32:14 +0000 | [diff] [blame] | 137 |    than :const:`PY_LLONG_MIN`, set *\*overflow* to ``1`` or ``-1``, | 
 | 138 |    respectively, and return ``-1``; otherwise, set *\*overflow* to | 
| Mark Dickinson | 93f562c | 2010-01-30 10:30:15 +0000 | [diff] [blame] | 139 |    ``0``.  If any other exception occurs (for example a TypeError or | 
| Ezio Melotti | ea30c6f | 2010-01-30 13:32:14 +0000 | [diff] [blame] | 140 |    MemoryError), then ``-1`` will be returned and *\*overflow* will | 
| Mark Dickinson | 93f562c | 2010-01-30 10:30:15 +0000 | [diff] [blame] | 141 |    be ``0``. | 
 | 142 |  | 
 | 143 |    .. versionadded:: 3.2 | 
 | 144 |  | 
 | 145 |  | 
| Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 146 | .. cfunction:: Py_ssize_t PyLong_AsSsize_t(PyObject *pylong) | 
 | 147 |  | 
 | 148 |    .. index:: | 
 | 149 |       single: PY_SSIZE_T_MAX | 
 | 150 |       single: OverflowError (built-in exception) | 
 | 151 |  | 
| Georg Brandl | 5081f7e | 2009-03-31 15:49:02 +0000 | [diff] [blame] | 152 |    Return a C :ctype:`Py_ssize_t` representation of the contents of *pylong*. | 
 | 153 |    If *pylong* is greater than :const:`PY_SSIZE_T_MAX`, an :exc:`OverflowError` | 
 | 154 |    is raised and ``-1`` will be returned. | 
| Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 155 |  | 
 | 156 |  | 
| Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 157 | .. cfunction:: unsigned long PyLong_AsUnsignedLong(PyObject *pylong) | 
 | 158 |  | 
 | 159 |    .. index:: | 
 | 160 |       single: ULONG_MAX | 
 | 161 |       single: OverflowError (built-in exception) | 
 | 162 |  | 
 | 163 |    Return a C :ctype:`unsigned long` representation of the contents of *pylong*. | 
 | 164 |    If *pylong* is greater than :const:`ULONG_MAX`, an :exc:`OverflowError` is | 
 | 165 |    raised. | 
 | 166 |  | 
 | 167 |  | 
| Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 168 | .. cfunction:: size_t PyLong_AsSize_t(PyObject *pylong) | 
 | 169 |  | 
 | 170 |    Return a :ctype:`size_t` representation of the contents of *pylong*.  If | 
 | 171 |    *pylong* is greater than the maximum value for a :ctype:`size_t`, an | 
 | 172 |    :exc:`OverflowError` is raised. | 
 | 173 |  | 
 | 174 |  | 
 | 175 | .. cfunction:: PY_LONG_LONG PyLong_AsLongLong(PyObject *pylong) | 
 | 176 |  | 
| Mark Dickinson | 2177607 | 2009-02-10 16:13:25 +0000 | [diff] [blame] | 177 |    .. index:: | 
 | 178 |       single: OverflowError (built-in exception) | 
| Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 179 |  | 
| Mark Dickinson | 2177607 | 2009-02-10 16:13:25 +0000 | [diff] [blame] | 180 |    Return a C :ctype:`long long` from a Python integer.  If *pylong* | 
 | 181 |    cannot be represented as a :ctype:`long long`, an | 
 | 182 |    :exc:`OverflowError` is raised and ``-1`` is returned. | 
| Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 183 |  | 
 | 184 | .. cfunction:: unsigned PY_LONG_LONG PyLong_AsUnsignedLongLong(PyObject *pylong) | 
 | 185 |  | 
| Mark Dickinson | 2177607 | 2009-02-10 16:13:25 +0000 | [diff] [blame] | 186 |    .. index:: | 
 | 187 |       single: OverflowError (built-in exception) | 
| Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 188 |  | 
| Mark Dickinson | 2177607 | 2009-02-10 16:13:25 +0000 | [diff] [blame] | 189 |    Return a C :ctype:`unsigned long long` from a Python integer. If | 
 | 190 |    *pylong* cannot be represented as an :ctype:`unsigned long long`, | 
 | 191 |    an :exc:`OverflowError` is raised and ``(unsigned long long)-1`` is | 
 | 192 |    returned. | 
 | 193 |  | 
 | 194 |    .. versionchanged:: 3.1 | 
 | 195 |       A negative *pylong* now raises :exc:`OverflowError`, not | 
 | 196 |       :exc:`TypeError`. | 
| Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 197 |  | 
 | 198 | .. cfunction:: unsigned long PyLong_AsUnsignedLongMask(PyObject *io) | 
 | 199 |  | 
 | 200 |    Return a C :ctype:`unsigned long` from a Python integer, without checking for | 
 | 201 |    overflow. | 
 | 202 |  | 
 | 203 |  | 
 | 204 | .. cfunction:: unsigned PY_LONG_LONG PyLong_AsUnsignedLongLongMask(PyObject *io) | 
 | 205 |  | 
 | 206 |    Return a C :ctype:`unsigned long long` from a Python integer, without | 
 | 207 |    checking for overflow. | 
 | 208 |  | 
 | 209 |  | 
 | 210 | .. cfunction:: double PyLong_AsDouble(PyObject *pylong) | 
 | 211 |  | 
 | 212 |    Return a C :ctype:`double` representation of the contents of *pylong*.  If | 
 | 213 |    *pylong* cannot be approximately represented as a :ctype:`double`, an | 
 | 214 |    :exc:`OverflowError` exception is raised and ``-1.0`` will be returned. | 
 | 215 |  | 
 | 216 |  | 
 | 217 | .. cfunction:: void* PyLong_AsVoidPtr(PyObject *pylong) | 
 | 218 |  | 
| Christian Heimes | c3f30c4 | 2008-02-22 16:37:40 +0000 | [diff] [blame] | 219 |    Convert a Python integer *pylong* to a C :ctype:`void` pointer. | 
 | 220 |    If *pylong* cannot be converted, an :exc:`OverflowError` will be raised.  This | 
 | 221 |    is only assured to produce a usable :ctype:`void` pointer for values created | 
 | 222 |    with :cfunc:`PyLong_FromVoidPtr`. |