blob: 72be0174657334a861be9736ecaa05dfc4181883 [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
Georg Brandl60203b42010-10-06 10:11:56 +000013.. c:type:: PyLongObject
Georg Brandl54a3faa2008-01-20 09:30:57 +000014
Georg Brandl60203b42010-10-06 10:11:56 +000015 This subtype of :c:type:`PyObject` represents a Python integer object.
Georg Brandl54a3faa2008-01-20 09:30:57 +000016
17
Georg Brandl60203b42010-10-06 10:11:56 +000018.. c:var:: PyTypeObject PyLong_Type
Georg Brandl54a3faa2008-01-20 09:30:57 +000019
Georg Brandl60203b42010-10-06 10:11:56 +000020 This instance of :c:type:`PyTypeObject` represents the Python integer type.
Georg Brandl2aff3352010-10-17 10:59:41 +000021 This is the same object as :class:`int` in the Python layer.
Georg Brandl54a3faa2008-01-20 09:30:57 +000022
23
Georg Brandl60203b42010-10-06 10:11:56 +000024.. c:function:: int PyLong_Check(PyObject *p)
Georg Brandl54a3faa2008-01-20 09:30:57 +000025
Georg Brandl60203b42010-10-06 10:11:56 +000026 Return true if its argument is a :c:type:`PyLongObject` or a subtype of
27 :c:type:`PyLongObject`.
Georg Brandl54a3faa2008-01-20 09:30:57 +000028
29
Georg Brandl60203b42010-10-06 10:11:56 +000030.. c:function:: int PyLong_CheckExact(PyObject *p)
Georg Brandl54a3faa2008-01-20 09:30:57 +000031
Georg Brandl60203b42010-10-06 10:11:56 +000032 Return true if its argument is a :c:type:`PyLongObject`, but not a subtype of
33 :c:type:`PyLongObject`.
Georg Brandl54a3faa2008-01-20 09:30:57 +000034
35
Georg Brandl60203b42010-10-06 10:11:56 +000036.. c:function:: PyObject* PyLong_FromLong(long v)
Georg Brandl54a3faa2008-01-20 09:30:57 +000037
Georg Brandl60203b42010-10-06 10:11:56 +000038 Return a new :c:type:`PyLongObject` object from *v*, or *NULL* on failure.
Georg Brandl54a3faa2008-01-20 09:30:57 +000039
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
Georg Brandl60203b42010-10-06 10:11:56 +000047.. c:function:: PyObject* PyLong_FromUnsignedLong(unsigned long v)
Georg Brandl54a3faa2008-01-20 09:30:57 +000048
Georg Brandl60203b42010-10-06 10:11:56 +000049 Return a new :c:type:`PyLongObject` object from a C :c:type:`unsigned long`, or
Georg Brandl54a3faa2008-01-20 09:30:57 +000050 *NULL* on failure.
51
52
Georg Brandl60203b42010-10-06 10:11:56 +000053.. c:function:: PyObject* PyLong_FromSsize_t(Py_ssize_t v)
Georg Brandl54a3faa2008-01-20 09:30:57 +000054
Georg Brandl60203b42010-10-06 10:11:56 +000055 Return a new :c:type:`PyLongObject` object from a C :c:type:`Py_ssize_t`, or
Christian Heimes81ee3ef2008-05-04 22:42:01 +000056 *NULL* on failure.
Georg Brandl54a3faa2008-01-20 09:30:57 +000057
58
Georg Brandl60203b42010-10-06 10:11:56 +000059.. c:function:: PyObject* PyLong_FromSize_t(size_t v)
Georg Brandl54a3faa2008-01-20 09:30:57 +000060
Georg Brandl60203b42010-10-06 10:11:56 +000061 Return a new :c:type:`PyLongObject` object from a C :c:type:`size_t`, or
Christian Heimes81ee3ef2008-05-04 22:42:01 +000062 *NULL* on failure.
Georg Brandl54a3faa2008-01-20 09:30:57 +000063
64
Georg Brandl60203b42010-10-06 10:11:56 +000065.. c:function:: PyObject* PyLong_FromLongLong(PY_LONG_LONG v)
Georg Brandl54a3faa2008-01-20 09:30:57 +000066
Georg Brandl60203b42010-10-06 10:11:56 +000067 Return a new :c:type:`PyLongObject` object from a C :c:type:`long long`, or *NULL*
Georg Brandl54a3faa2008-01-20 09:30:57 +000068 on failure.
69
70
Georg Brandl60203b42010-10-06 10:11:56 +000071.. c:function:: PyObject* PyLong_FromUnsignedLongLong(unsigned PY_LONG_LONG v)
Georg Brandl54a3faa2008-01-20 09:30:57 +000072
Georg Brandl60203b42010-10-06 10:11:56 +000073 Return a new :c:type:`PyLongObject` object from a C :c:type:`unsigned long long`,
Georg Brandl54a3faa2008-01-20 09:30:57 +000074 or *NULL* on failure.
75
76
Georg Brandl60203b42010-10-06 10:11:56 +000077.. c:function:: PyObject* PyLong_FromDouble(double v)
Georg Brandl54a3faa2008-01-20 09:30:57 +000078
Georg Brandl60203b42010-10-06 10:11:56 +000079 Return a new :c:type:`PyLongObject` object from the integer part of *v*, or
Georg Brandl54a3faa2008-01-20 09:30:57 +000080 *NULL* on failure.
81
82
Georg Brandl60203b42010-10-06 10:11:56 +000083.. c:function:: PyObject* PyLong_FromString(char *str, char **pend, int base)
Georg Brandl54a3faa2008-01-20 09:30:57 +000084
Georg Brandl60203b42010-10-06 10:11:56 +000085 Return a new :c:type:`PyLongObject` based on the string value in *str*, which
Georg Brandl54a3faa2008-01-20 09:30:57 +000086 is interpreted according to the radix in *base*. If *pend* is non-*NULL*,
Ezio Melottiea30c6f2010-01-30 13:32:14 +000087 *\*pend* will point to the first character in *str* which follows the
Georg Brandl54a3faa2008-01-20 09:30:57 +000088 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
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
103
Georg Brandl60203b42010-10-06 10:11:56 +0000104.. c:function:: PyObject* PyLong_FromVoidPtr(void *p)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000105
106 Create a Python integer from the pointer *p*. The pointer value can be
Georg Brandl60203b42010-10-06 10:11:56 +0000107 retrieved from the resulting value using :c:func:`PyLong_AsVoidPtr`.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000108
109
Georg Brandl48310cd2009-01-03 21:18:54 +0000110.. XXX alias PyLong_AS_LONG (for now)
Mark Dickinson0a229242012-06-23 10:49:12 +0100111.. c:function:: long PyLong_AsLong(PyObject *obj)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000112
113 .. index::
114 single: LONG_MAX
115 single: OverflowError (built-in exception)
116
Mark Dickinson0a229242012-06-23 10:49:12 +0100117 Return a C :c:type:`long` representation of *obj*. If *obj* is not an
118 instance of :c:type:`PyLongObject`, first call its :meth:`__int__` method
119 (if present) to convert it to a :c:type:`PyLongObject`.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000120
Mark Dickinson0a229242012-06-23 10:49:12 +0100121 Raise :exc:`OverflowError` if the value of *obj* is out of range for a
122 :c:type:`long`.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000123
Mark Dickinsonf0acfee2012-06-23 11:14:22 +0100124
Mark Dickinson0a229242012-06-23 10:49:12 +0100125.. c:function:: long PyLong_AsLongAndOverflow(PyObject *obj, int *overflow)
126
127 Return a C :c:type:`long` representation of *obj*. If *obj* is not an
128 instance of :c:type:`PyLongObject`, first call its :meth:`__int__` method
129 (if present) to convert it to a :c:type:`PyLongObject`.
130
131 If the value of *obj* is greater than :const:`LONG_MAX` or less than
132 :const:`LONG_MIN`, set *\*overflow* to ``1`` or ``-1``, respectively, and
133 return ``-1``; otherwise, set *\*overflow* to ``0``. If any other exception
134 occurs set *\*overflow* to ``0`` and return ``-1`` as usual.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000135
136
Mark Dickinsonf0acfee2012-06-23 11:14:22 +0100137.. c:function:: PY_LONG_LONG PyLong_AsLongLong(PyObject *obj)
Mark Dickinson93f562c2010-01-30 10:30:15 +0000138
Mark Dickinsonf0acfee2012-06-23 11:14:22 +0100139 .. index::
140 single: OverflowError (built-in exception)
141
142 Return a C :c:type:`long long` representation of *obj*. If *obj* is not an
143 instance of :c:type:`PyLongObject`, first call its :meth:`__int__` method
144 (if present) to convert it to a :c:type:`PyLongObject`.
145
146 Raise :exc:`OverflowError` if the value of *obj* is out of range for a
147 :c:type:`long`.
148
149
150.. c:function:: PY_LONG_LONG PyLong_AsLongLongAndOverflow(PyObject *obj, int *overflow)
151
152 Return a C :c:type:`long long` representation of *obj*. If *obj* is not an
153 instance of :c:type:`PyLongObject`, first call its :meth:`__int__` method
154 (if present) to convert it to a :c:type:`PyLongObject`.
155
156 If the value of *obj* is greater than :const:`PY_LLONG_MAX` or less than
157 :const:`PY_LLONG_MIN`, set *\*overflow* to ``1`` or ``-1``, respectively,
158 and return ``-1``; otherwise, set *\*overflow* to ``0``. If any other
159 exception occurs set *\*overflow* to ``0`` and return ``-1`` as usual.
Mark Dickinson93f562c2010-01-30 10:30:15 +0000160
161 .. versionadded:: 3.2
162
163
Georg Brandl60203b42010-10-06 10:11:56 +0000164.. c:function:: Py_ssize_t PyLong_AsSsize_t(PyObject *pylong)
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000165
166 .. index::
167 single: PY_SSIZE_T_MAX
168 single: OverflowError (built-in exception)
169
Mark Dickinsonb8dc3ab2012-06-23 12:12:52 +0100170 Return a C :c:type:`Py_ssize_t` representation of *pylong*. *pylong* must
171 be an instance of :c:type:`PyLongObject`.
172
173 Raise :exc:`OverflowError` if the value of *pylong* is out of range for a
174 :c:type:`Py_ssize_t`.
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000175
176
Georg Brandl60203b42010-10-06 10:11:56 +0000177.. c:function:: unsigned long PyLong_AsUnsignedLong(PyObject *pylong)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000178
179 .. index::
180 single: ULONG_MAX
181 single: OverflowError (built-in exception)
182
Mark Dickinsonb8dc3ab2012-06-23 12:12:52 +0100183 Return a C :c:type:`unsigned long` representation of *pylong*. *pylong*
184 must be an instance of :c:type:`PyLongObject`.
185
186 Raise :exc:`OverflowError` if the value of *pylong* is out of range for a
187 :c:type:`unsigned long`.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000188
189
Georg Brandl60203b42010-10-06 10:11:56 +0000190.. c:function:: size_t PyLong_AsSize_t(PyObject *pylong)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000191
Terry Jan Reedy65e69b32013-03-11 17:23:46 -0400192 Return a C :c:type:`size_t` representation of *pylong*. *pylong* must be
Mark Dickinsonb8dc3ab2012-06-23 12:12:52 +0100193 an instance of :c:type:`PyLongObject`.
194
195 Raise :exc:`OverflowError` if the value of *pylong* is out of range for a
196 :c:type:`size_t`.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000197
198
Georg Brandl60203b42010-10-06 10:11:56 +0000199.. c:function:: unsigned PY_LONG_LONG PyLong_AsUnsignedLongLong(PyObject *pylong)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000200
Mark Dickinson21776072009-02-10 16:13:25 +0000201 .. index::
202 single: OverflowError (built-in exception)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000203
Terry Jan Reedy65e69b32013-03-11 17:23:46 -0400204 Return a C :c:type:`unsigned PY_LONG_LONG` representation of *pylong*.
Mark Dickinsonb8dc3ab2012-06-23 12:12:52 +0100205 *pylong* must be an instance of :c:type:`PyLongObject`.
206
207 Raise :exc:`OverflowError` if the value of *pylong* is out of range for an
208 :c:type:`unsigned PY_LONG_LONG`.
Mark Dickinson21776072009-02-10 16:13:25 +0000209
210 .. versionchanged:: 3.1
Georg Brandl67b21b72010-08-17 15:07:14 +0000211 A negative *pylong* now raises :exc:`OverflowError`, not :exc:`TypeError`.
212
Georg Brandl54a3faa2008-01-20 09:30:57 +0000213
Mark Dickinsonb8dc3ab2012-06-23 12:12:52 +0100214.. c:function:: unsigned long PyLong_AsUnsignedLongMask(PyObject *obj)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000215
Mark Dickinsonb8dc3ab2012-06-23 12:12:52 +0100216 Return a C :c:type:`unsigned long` representation of *obj*. If *obj*
217 is not an instance of :c:type:`PyLongObject`, first call its :meth:`__int__`
218 method (if present) to convert it to a :c:type:`PyLongObject`.
219
220 If the value of *obj* is out of range for an :c:type:`unsigned long`,
221 return the reduction of that value modulo :const:`ULONG_MAX + 1`.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000222
223
Mark Dickinsonb8dc3ab2012-06-23 12:12:52 +0100224.. c:function:: unsigned PY_LONG_LONG PyLong_AsUnsignedLongLongMask(PyObject *obj)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000225
Mark Dickinsonb8dc3ab2012-06-23 12:12:52 +0100226 Return a C :c:type:`unsigned long long` representation of *obj*. If *obj*
227 is not an instance of :c:type:`PyLongObject`, first call its :meth:`__int__`
228 method (if present) to convert it to a :c:type:`PyLongObject`.
229
230 If the value of *obj* is out of range for an :c:type:`unsigned long long`,
231 return the reduction of that value modulo :const:`PY_ULLONG_MAX + 1`.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000232
233
Georg Brandl60203b42010-10-06 10:11:56 +0000234.. c:function:: double PyLong_AsDouble(PyObject *pylong)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000235
Mark Dickinsonb8dc3ab2012-06-23 12:12:52 +0100236 Return a C :c:type:`double` representation of *pylong*. *pylong* must be
237 an instance of :c:type:`PyLongObject`.
238
239 Raise :exc:`OverflowError` if the value of *pylong* is out of range for a
240 :c:type:`double`.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000241
242
Georg Brandl60203b42010-10-06 10:11:56 +0000243.. c:function:: void* PyLong_AsVoidPtr(PyObject *pylong)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000244
Georg Brandl60203b42010-10-06 10:11:56 +0000245 Convert a Python integer *pylong* to a C :c:type:`void` pointer.
Christian Heimesc3f30c42008-02-22 16:37:40 +0000246 If *pylong* cannot be converted, an :exc:`OverflowError` will be raised. This
Georg Brandl60203b42010-10-06 10:11:56 +0000247 is only assured to produce a usable :c:type:`void` pointer for values created
248 with :c:func:`PyLong_FromVoidPtr`.