1.. highlight:: c
2
3.. _longobjects:
4
5Integer Objects
6---------------
7
8.. index:: pair: object; long integer
9           pair: object; integer
10
11All integers are implemented as "long" integer objects of arbitrary size.
12
13On error, most ``PyLong_As*`` APIs return ``(return type)-1`` which cannot be
14distinguished from a number.  Use :c:func:`PyErr_Occurred` to disambiguate.
15
16.. c:type:: PyLongObject
17
18   This subtype of :c:type:`PyObject` represents a Python integer object.
19
20
21.. c:var:: PyTypeObject PyLong_Type
22
23   This instance of :c:type:`PyTypeObject` represents the Python integer type.
24   This is the same object as :class:`int` in the Python layer.
25
26
27.. c:function:: int PyLong_Check(PyObject *p)
28
29   Return true if its argument is a :c:type:`PyLongObject` or a subtype of
30   :c:type:`PyLongObject`.  This function always succeeds.
31
32
33.. c:function:: int PyLong_CheckExact(PyObject *p)
34
35   Return true if its argument is a :c:type:`PyLongObject`, but not a subtype of
36   :c:type:`PyLongObject`.  This function always succeeds.
37
38
39.. c:function:: PyObject* PyLong_FromLong(long v)
40
41   Return a new :c:type:`PyLongObject` object from *v*, or ``NULL`` on failure.
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.
46
47
48.. c:function:: PyObject* PyLong_FromUnsignedLong(unsigned long v)
49
50   Return a new :c:type:`PyLongObject` object from a C :c:expr:`unsigned long`, or
51   ``NULL`` on failure.
52
53
54.. c:function:: PyObject* PyLong_FromSsize_t(Py_ssize_t v)
55
56   Return a new :c:type:`PyLongObject` object from a C :c:type:`Py_ssize_t`, or
57   ``NULL`` on failure.
58
59
60.. c:function:: PyObject* PyLong_FromSize_t(size_t v)
61
62   Return a new :c:type:`PyLongObject` object from a C :c:type:`size_t`, or
63   ``NULL`` on failure.
64
65
66.. c:function:: PyObject* PyLong_FromLongLong(long long v)
67
68   Return a new :c:type:`PyLongObject` object from a C :c:expr:`long long`, or ``NULL``
69   on failure.
70
71
72.. c:function:: PyObject* PyLong_FromUnsignedLongLong(unsigned long long v)
73
74   Return a new :c:type:`PyLongObject` object from a C :c:expr:`unsigned long long`,
75   or ``NULL`` on failure.
76
77
78.. c:function:: PyObject* PyLong_FromDouble(double v)
79
80   Return a new :c:type:`PyLongObject` object from the integer part of *v*, or
81   ``NULL`` on failure.
82
83
84.. c:function:: PyObject* PyLong_FromString(const char *str, char **pend, int base)
85
86   Return a new :c:type:`PyLongObject` based on the string value in *str*, which
87   is interpreted according to the radix in *base*.  If *pend* is non-``NULL``,
88   *\*pend* will point to the first character in *str* which follows the
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.
95
96   .. seealso:: Python methods :meth:`int.to_bytes` and :meth:`int.from_bytes`
97      to convert a :c:type:`PyLongObject` to/from an array of bytes in base
98      ``256``. You can call those from C using :c:func:`PyObject_CallMethod`.
99
100
101.. c:function:: PyObject* PyLong_FromUnicodeObject(PyObject *u, int base)
102
103   Convert a sequence of Unicode digits in the string *u* to a Python integer
104   value.
105
106   .. versionadded:: 3.3
107
108
109.. c:function:: PyObject* PyLong_FromVoidPtr(void *p)
110
111   Create a Python integer from the pointer *p*. The pointer value can be
112   retrieved from the resulting value using :c:func:`PyLong_AsVoidPtr`.
113
114
115.. XXX alias PyLong_AS_LONG (for now)
116.. c:function:: long PyLong_AsLong(PyObject *obj)
117
118   .. index::
119      single: LONG_MAX
120      single: OverflowError (built-in exception)
121
122   Return a C :c:expr:`long` representation of *obj*.  If *obj* is not an
123   instance of :c:type:`PyLongObject`, first call its :meth:`__index__` method
124   (if present) to convert it to a :c:type:`PyLongObject`.
125
126   Raise :exc:`OverflowError` if the value of *obj* is out of range for a
127   :c:expr:`long`.
128
129   Returns ``-1`` on error.  Use :c:func:`PyErr_Occurred` to disambiguate.
130
131   .. versionchanged:: 3.8
132      Use :meth:`__index__` if available.
133
134   .. versionchanged:: 3.10
135      This function will no longer use :meth:`__int__`.
136
137
138.. c:function:: long PyLong_AsLongAndOverflow(PyObject *obj, int *overflow)
139
140   Return a C :c:expr:`long` representation of *obj*.  If *obj* is not an
141   instance of :c:type:`PyLongObject`, first call its :meth:`__index__`
142   method (if present) to convert it to a :c:type:`PyLongObject`.
143
144   If the value of *obj* is greater than :const:`LONG_MAX` or less than
145   :const:`LONG_MIN`, set *\*overflow* to ``1`` or ``-1``, respectively, and
146   return ``-1``; otherwise, set *\*overflow* to ``0``.  If any other exception
147   occurs set *\*overflow* to ``0`` and return ``-1`` as usual.
148
149   Returns ``-1`` on error.  Use :c:func:`PyErr_Occurred` to disambiguate.
150
151   .. versionchanged:: 3.8
152      Use :meth:`__index__` if available.
153
154   .. versionchanged:: 3.10
155      This function will no longer use :meth:`__int__`.
156
157
158.. c:function:: long long PyLong_AsLongLong(PyObject *obj)
159
160   .. index::
161      single: OverflowError (built-in exception)
162
163   Return a C :c:expr:`long long` representation of *obj*.  If *obj* is not an
164   instance of :c:type:`PyLongObject`, first call its :meth:`__index__` method
165   (if present) to convert it to a :c:type:`PyLongObject`.
166
167   Raise :exc:`OverflowError` if the value of *obj* is out of range for a
168   :c:expr:`long long`.
169
170   Returns ``-1`` on error.  Use :c:func:`PyErr_Occurred` to disambiguate.
171
172   .. versionchanged:: 3.8
173      Use :meth:`__index__` if available.
174
175   .. versionchanged:: 3.10
176      This function will no longer use :meth:`__int__`.
177
178
179.. c:function:: long long PyLong_AsLongLongAndOverflow(PyObject *obj, int *overflow)
180
181   Return a C :c:expr:`long long` representation of *obj*.  If *obj* is not an
182   instance of :c:type:`PyLongObject`, first call its :meth:`__index__` method
183   (if present) to convert it to a :c:type:`PyLongObject`.
184
185   If the value of *obj* is greater than :const:`LLONG_MAX` or less than
186   :const:`LLONG_MIN`, set *\*overflow* to ``1`` or ``-1``, respectively,
187   and return ``-1``; otherwise, set *\*overflow* to ``0``.  If any other
188   exception occurs set *\*overflow* to ``0`` and return ``-1`` as usual.
189
190   Returns ``-1`` on error.  Use :c:func:`PyErr_Occurred` to disambiguate.
191
192   .. versionadded:: 3.2
193
194   .. versionchanged:: 3.8
195      Use :meth:`__index__` if available.
196
197   .. versionchanged:: 3.10
198      This function will no longer use :meth:`__int__`.
199
200
201.. c:function:: Py_ssize_t PyLong_AsSsize_t(PyObject *pylong)
202
203   .. index::
204      single: PY_SSIZE_T_MAX
205      single: OverflowError (built-in exception)
206
207   Return a C :c:type:`Py_ssize_t` representation of *pylong*.  *pylong* must
208   be an instance of :c:type:`PyLongObject`.
209
210   Raise :exc:`OverflowError` if the value of *pylong* is out of range for a
211   :c:type:`Py_ssize_t`.
212
213   Returns ``-1`` on error.  Use :c:func:`PyErr_Occurred` to disambiguate.
214
215
216.. c:function:: unsigned long PyLong_AsUnsignedLong(PyObject *pylong)
217
218   .. index::
219      single: ULONG_MAX
220      single: OverflowError (built-in exception)
221
222   Return a C :c:expr:`unsigned long` representation of *pylong*.  *pylong*
223   must 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:expr:`unsigned long`.
227
228   Returns ``(unsigned long)-1`` on error.
229   Use :c:func:`PyErr_Occurred` to disambiguate.
230
231
232.. c:function:: size_t PyLong_AsSize_t(PyObject *pylong)
233
234   .. index::
235      single: SIZE_MAX
236      single: OverflowError (built-in exception)
237
238   Return a C :c:type:`size_t` representation of *pylong*.  *pylong* must be
239   an instance of :c:type:`PyLongObject`.
240
241   Raise :exc:`OverflowError` if the value of *pylong* is out of range for a
242   :c:type:`size_t`.
243
244   Returns ``(size_t)-1`` on error.
245   Use :c:func:`PyErr_Occurred` to disambiguate.
246
247
248.. c:function:: unsigned long long PyLong_AsUnsignedLongLong(PyObject *pylong)
249
250   .. index::
251      single: OverflowError (built-in exception)
252
253   Return a C :c:expr:`unsigned long long` representation of *pylong*.  *pylong*
254   must be an instance of :c:type:`PyLongObject`.
255
256   Raise :exc:`OverflowError` if the value of *pylong* is out of range for an
257   :c:expr:`unsigned long long`.
258
259   Returns ``(unsigned long long)-1`` on error.
260   Use :c:func:`PyErr_Occurred` to disambiguate.
261
262   .. versionchanged:: 3.1
263      A negative *pylong* now raises :exc:`OverflowError`, not :exc:`TypeError`.
264
265
266.. c:function:: unsigned long PyLong_AsUnsignedLongMask(PyObject *obj)
267
268   Return a C :c:expr:`unsigned long` representation of *obj*.  If *obj* is not
269   an instance of :c:type:`PyLongObject`, first call its :meth:`__index__`
270   method (if present) to convert it to a :c:type:`PyLongObject`.
271
272   If the value of *obj* is out of range for an :c:expr:`unsigned long`,
273   return the reduction of that value modulo ``ULONG_MAX + 1``.
274
275   Returns ``(unsigned long)-1`` on error.  Use :c:func:`PyErr_Occurred` to
276   disambiguate.
277
278   .. versionchanged:: 3.8
279      Use :meth:`__index__` if available.
280
281   .. versionchanged:: 3.10
282      This function will no longer use :meth:`__int__`.
283
284
285.. c:function:: unsigned long long PyLong_AsUnsignedLongLongMask(PyObject *obj)
286
287   Return a C :c:expr:`unsigned long long` representation of *obj*.  If *obj*
288   is not an instance of :c:type:`PyLongObject`, first call its
289   :meth:`__index__` method (if present) to convert it to a
290   :c:type:`PyLongObject`.
291
292   If the value of *obj* is out of range for an :c:expr:`unsigned long long`,
293   return the reduction of that value modulo ``ULLONG_MAX + 1``.
294
295   Returns ``(unsigned long long)-1`` on error.  Use :c:func:`PyErr_Occurred`
296   to disambiguate.
297
298   .. versionchanged:: 3.8
299      Use :meth:`__index__` if available.
300
301   .. versionchanged:: 3.10
302      This function will no longer use :meth:`__int__`.
303
304
305.. c:function:: double PyLong_AsDouble(PyObject *pylong)
306
307   Return a C :c:expr:`double` representation of *pylong*.  *pylong* must be
308   an instance of :c:type:`PyLongObject`.
309
310   Raise :exc:`OverflowError` if the value of *pylong* is out of range for a
311   :c:expr:`double`.
312
313   Returns ``-1.0`` on error.  Use :c:func:`PyErr_Occurred` to disambiguate.
314
315
316.. c:function:: void* PyLong_AsVoidPtr(PyObject *pylong)
317
318   Convert a Python integer *pylong* to a C :c:expr:`void` pointer.
319   If *pylong* cannot be converted, an :exc:`OverflowError` will be raised.  This
320   is only assured to produce a usable :c:expr:`void` pointer for values created
321   with :c:func:`PyLong_FromVoidPtr`.
322
323   Returns ``NULL`` on error.  Use :c:func:`PyErr_Occurred` to disambiguate.
324