• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1.. highlight:: 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
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:type:`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:type:`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:type:`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
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
100   value.
101
102   .. versionadded:: 3.3
103
104
105.. c:function:: PyObject* PyLong_FromVoidPtr(void *p)
106
107   Create a Python integer from the pointer *p*. The pointer value can be
108   retrieved from the resulting value using :c:func:`PyLong_AsVoidPtr`.
109
110
111.. XXX alias PyLong_AS_LONG (for now)
112.. c:function:: long PyLong_AsLong(PyObject *obj)
113
114   .. index::
115      single: LONG_MAX
116      single: OverflowError (built-in exception)
117
118   Return a C :c:type:`long` representation of *obj*.  If *obj* is not an
119   instance of :c:type:`PyLongObject`, first call its :meth:`__index__` method
120   (if present) to convert it to a :c:type:`PyLongObject`.
121
122   Raise :exc:`OverflowError` if the value of *obj* is out of range for a
123   :c:type:`long`.
124
125   Returns ``-1`` on error.  Use :c:func:`PyErr_Occurred` to disambiguate.
126
127   .. versionchanged:: 3.8
128      Use :meth:`__index__` if available.
129
130   .. versionchanged:: 3.10
131      This function will no longer use :meth:`__int__`.
132
133
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
137   instance of :c:type:`PyLongObject`, first call its :meth:`__index__`
138   method (if present) to convert it to a :c:type:`PyLongObject`.
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.
144
145   Returns ``-1`` on error.  Use :c:func:`PyErr_Occurred` to disambiguate.
146
147   .. versionchanged:: 3.8
148      Use :meth:`__index__` if available.
149
150   .. versionchanged:: 3.10
151      This function will no longer use :meth:`__int__`.
152
153
154.. c:function:: long long PyLong_AsLongLong(PyObject *obj)
155
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
160   instance of :c:type:`PyLongObject`, first call its :meth:`__index__` method
161   (if present) to convert it to a :c:type:`PyLongObject`.
162
163   Raise :exc:`OverflowError` if the value of *obj* is out of range for a
164   :c:type:`long long`.
165
166   Returns ``-1`` on error.  Use :c:func:`PyErr_Occurred` to disambiguate.
167
168   .. versionchanged:: 3.8
169      Use :meth:`__index__` if available.
170
171   .. versionchanged:: 3.10
172      This function will no longer use :meth:`__int__`.
173
174
175.. c:function:: long long PyLong_AsLongLongAndOverflow(PyObject *obj, int *overflow)
176
177   Return a C :c:type:`long long` representation of *obj*.  If *obj* is not an
178   instance of :c:type:`PyLongObject`, first call its :meth:`__index__` method
179   (if present) to convert it to a :c:type:`PyLongObject`.
180
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,
183   and return ``-1``; otherwise, set *\*overflow* to ``0``.  If any other
184   exception occurs set *\*overflow* to ``0`` and return ``-1`` as usual.
185
186   Returns ``-1`` on error.  Use :c:func:`PyErr_Occurred` to disambiguate.
187
188   .. versionadded:: 3.2
189
190   .. versionchanged:: 3.8
191      Use :meth:`__index__` if available.
192
193   .. versionchanged:: 3.10
194      This function will no longer use :meth:`__int__`.
195
196
197.. c:function:: Py_ssize_t PyLong_AsSsize_t(PyObject *pylong)
198
199   .. index::
200      single: PY_SSIZE_T_MAX
201      single: OverflowError (built-in exception)
202
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`.
208
209   Returns ``-1`` on error.  Use :c:func:`PyErr_Occurred` to disambiguate.
210
211
212.. c:function:: unsigned long PyLong_AsUnsignedLong(PyObject *pylong)
213
214   .. index::
215      single: ULONG_MAX
216      single: OverflowError (built-in exception)
217
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`.
223
224   Returns ``(unsigned long)-1`` on error.
225   Use :c:func:`PyErr_Occurred` to disambiguate.
226
227
228.. c:function:: size_t PyLong_AsSize_t(PyObject *pylong)
229
230   .. index::
231      single: SIZE_MAX
232      single: OverflowError (built-in exception)
233
234   Return a C :c:type:`size_t` representation of *pylong*.  *pylong* must be
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`.
239
240   Returns ``(size_t)-1`` on error.
241   Use :c:func:`PyErr_Occurred` to disambiguate.
242
243
244.. c:function:: unsigned long long PyLong_AsUnsignedLongLong(PyObject *pylong)
245
246   .. index::
247      single: OverflowError (built-in exception)
248
249   Return a C :c:type:`unsigned long long` representation of *pylong*.  *pylong*
250   must be an instance of :c:type:`PyLongObject`.
251
252   Raise :exc:`OverflowError` if the value of *pylong* is out of range for an
253   :c:type:`unsigned long long`.
254
255   Returns ``(unsigned long long)-1`` on error.
256   Use :c:func:`PyErr_Occurred` to disambiguate.
257
258   .. versionchanged:: 3.1
259      A negative *pylong* now raises :exc:`OverflowError`, not :exc:`TypeError`.
260
261
262.. c:function:: unsigned long PyLong_AsUnsignedLongMask(PyObject *obj)
263
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`.
267
268   If the value of *obj* is out of range for an :c:type:`unsigned long`,
269   return the reduction of that value modulo ``ULONG_MAX + 1``.
270
271   Returns ``(unsigned long)-1`` on error.  Use :c:func:`PyErr_Occurred` to
272   disambiguate.
273
274   .. versionchanged:: 3.8
275      Use :meth:`__index__` if available.
276
277   .. versionchanged:: 3.10
278      This function will no longer use :meth:`__int__`.
279
280
281.. c:function:: unsigned long long PyLong_AsUnsignedLongLongMask(PyObject *obj)
282
283   Return a C :c:type:`unsigned long long` representation of *obj*.  If *obj*
284   is not an instance of :c:type:`PyLongObject`, first call its
285   :meth:`__index__` method (if present) to convert it to a
286   :c:type:`PyLongObject`.
287
288   If the value of *obj* is out of range for an :c:type:`unsigned long long`,
289   return the reduction of that value modulo ``ULLONG_MAX + 1``.
290
291   Returns ``(unsigned long long)-1`` on error.  Use :c:func:`PyErr_Occurred`
292   to disambiguate.
293
294   .. versionchanged:: 3.8
295      Use :meth:`__index__` if available.
296
297   .. versionchanged:: 3.10
298      This function will no longer use :meth:`__int__`.
299
300
301.. c:function:: double PyLong_AsDouble(PyObject *pylong)
302
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`.
308
309   Returns ``-1.0`` on error.  Use :c:func:`PyErr_Occurred` to disambiguate.
310
311
312.. c:function:: void* PyLong_AsVoidPtr(PyObject *pylong)
313
314   Convert a Python integer *pylong* to a C :c:type:`void` pointer.
315   If *pylong* cannot be converted, an :exc:`OverflowError` will be raised.  This
316   is only assured to produce a usable :c:type:`void` pointer for values created
317   with :c:func:`PyLong_FromVoidPtr`.
318
319   Returns ``NULL`` on error.  Use :c:func:`PyErr_Occurred` to disambiguate.
320