• 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`.
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`.
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. 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
50.. c:function:: PyObject* PyLong_FromUnsignedLong(unsigned long v)
51
52   Return a new :c:type:`PyLongObject` object from a C :c:type:`unsigned long`, or
53   ``NULL`` on failure.
54
55
56.. c:function:: PyObject* PyLong_FromSsize_t(Py_ssize_t v)
57
58   Return a new :c:type:`PyLongObject` object from a C :c:type:`Py_ssize_t`, or
59   ``NULL`` on failure.
60
61
62.. c:function:: PyObject* PyLong_FromSize_t(size_t v)
63
64   Return a new :c:type:`PyLongObject` object from a C :c:type:`size_t`, or
65   ``NULL`` on failure.
66
67
68.. c:function:: PyObject* PyLong_FromLongLong(long long v)
69
70   Return a new :c:type:`PyLongObject` object from a C :c:type:`long long`, or ``NULL``
71   on failure.
72
73
74.. c:function:: PyObject* PyLong_FromUnsignedLongLong(unsigned long long v)
75
76   Return a new :c:type:`PyLongObject` object from a C :c:type:`unsigned long long`,
77   or ``NULL`` on failure.
78
79
80.. c:function:: PyObject* PyLong_FromDouble(double v)
81
82   Return a new :c:type:`PyLongObject` object from the integer part of *v*, or
83   ``NULL`` on failure.
84
85
86.. c:function:: PyObject* PyLong_FromString(const char *str, char **pend, int base)
87
88   Return a new :c:type:`PyLongObject` based on the string value in *str*, which
89   is interpreted according to the radix in *base*.  If *pend* is non-``NULL``,
90   *\*pend* will point to the first character in *str* which follows the
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.
97
98
99.. c:function:: PyObject* PyLong_FromUnicode(Py_UNICODE *u, Py_ssize_t length, int base)
100
101   Convert a sequence of Unicode digits to a Python integer value.  The Unicode
102   string is first encoded to a byte string using :c:func:`PyUnicode_EncodeDecimal`
103   and then converted using :c:func:`PyLong_FromString`.
104
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
119
120.. c:function:: PyObject* PyLong_FromVoidPtr(void *p)
121
122   Create a Python integer from the pointer *p*. The pointer value can be
123   retrieved from the resulting value using :c:func:`PyLong_AsVoidPtr`.
124
125
126.. XXX alias PyLong_AS_LONG (for now)
127.. c:function:: long PyLong_AsLong(PyObject *obj)
128
129   .. index::
130      single: LONG_MAX
131      single: OverflowError (built-in exception)
132
133   Return a C :c:type:`long` representation of *obj*.  If *obj* is not an
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`.
137
138   Raise :exc:`OverflowError` if the value of *obj* is out of range for a
139   :c:type:`long`.
140
141   Returns ``-1`` on error.  Use :c:func:`PyErr_Occurred` to disambiguate.
142
143   .. versionchanged:: 3.8
144      Use :meth:`__index__` if available.
145
146   .. deprecated:: 3.8
147      Using :meth:`__int__` is deprecated.
148
149
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
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`.
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.
161
162   Returns ``-1`` on error.  Use :c:func:`PyErr_Occurred` to disambiguate.
163
164   .. versionchanged:: 3.8
165      Use :meth:`__index__` if available.
166
167   .. deprecated:: 3.8
168      Using :meth:`__int__` is deprecated.
169
170
171.. c:function:: long long PyLong_AsLongLong(PyObject *obj)
172
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
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`.
180
181   Raise :exc:`OverflowError` if the value of *obj* is out of range for a
182   :c:type:`long`.
183
184   Returns ``-1`` on error.  Use :c:func:`PyErr_Occurred` to disambiguate.
185
186   .. versionchanged:: 3.8
187      Use :meth:`__index__` if available.
188
189   .. deprecated:: 3.8
190      Using :meth:`__int__` is deprecated.
191
192
193.. c:function:: long long PyLong_AsLongLongAndOverflow(PyObject *obj, int *overflow)
194
195   Return a C :c:type:`long long` representation of *obj*.  If *obj* is not an
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`.
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.
204
205   Returns ``-1`` on error.  Use :c:func:`PyErr_Occurred` to disambiguate.
206
207   .. versionadded:: 3.2
208
209   .. versionchanged:: 3.8
210      Use :meth:`__index__` if available.
211
212   .. deprecated:: 3.8
213      Using :meth:`__int__` is deprecated.
214
215
216.. c:function:: Py_ssize_t PyLong_AsSsize_t(PyObject *pylong)
217
218   .. index::
219      single: PY_SSIZE_T_MAX
220      single: OverflowError (built-in exception)
221
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`.
227
228   Returns ``-1`` on error.  Use :c:func:`PyErr_Occurred` to disambiguate.
229
230
231.. c:function:: unsigned long PyLong_AsUnsignedLong(PyObject *pylong)
232
233   .. index::
234      single: ULONG_MAX
235      single: OverflowError (built-in exception)
236
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`.
242
243   Returns ``(unsigned long)-1`` on error.
244   Use :c:func:`PyErr_Occurred` to disambiguate.
245
246
247.. c:function:: size_t PyLong_AsSize_t(PyObject *pylong)
248
249   .. index::
250      single: SIZE_MAX
251      single: OverflowError (built-in exception)
252
253   Return a C :c:type:`size_t` representation of *pylong*.  *pylong* must be
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`.
258
259   Returns ``(size_t)-1`` on error.
260   Use :c:func:`PyErr_Occurred` to disambiguate.
261
262
263.. c:function:: unsigned long long PyLong_AsUnsignedLongLong(PyObject *pylong)
264
265   .. index::
266      single: OverflowError (built-in exception)
267
268   Return a C :c:type:`unsigned long long` representation of *pylong*.  *pylong*
269   must be an instance of :c:type:`PyLongObject`.
270
271   Raise :exc:`OverflowError` if the value of *pylong* is out of range for an
272   :c:type:`unsigned long long`.
273
274   Returns ``(unsigned long long)-1`` on error.
275   Use :c:func:`PyErr_Occurred` to disambiguate.
276
277   .. versionchanged:: 3.1
278      A negative *pylong* now raises :exc:`OverflowError`, not :exc:`TypeError`.
279
280
281.. c:function:: unsigned long PyLong_AsUnsignedLongMask(PyObject *obj)
282
283   Return a C :c:type:`unsigned long` representation of *obj*.  If *obj*
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`.
287
288   If the value of *obj* is out of range for an :c:type:`unsigned long`,
289   return the reduction of that value modulo ``ULONG_MAX + 1``.
290
291   Returns ``(unsigned long)-1`` on error.  Use :c:func:`PyErr_Occurred` to
292   disambiguate.
293
294   .. versionchanged:: 3.8
295      Use :meth:`__index__` if available.
296
297   .. deprecated:: 3.8
298      Using :meth:`__int__` is deprecated.
299
300
301.. c:function:: unsigned long long PyLong_AsUnsignedLongLongMask(PyObject *obj)
302
303   Return a C :c:type:`unsigned long long` representation of *obj*.  If *obj*
304   is not an instance of :c:type:`PyLongObject`, first call its
305   :meth:`__index__` or :meth:`__int__` method (if present) to convert
306   it to a :c:type:`PyLongObject`.
307
308   If the value of *obj* is out of range for an :c:type:`unsigned long long`,
309   return the reduction of that value modulo ``PY_ULLONG_MAX + 1``.
310
311   Returns ``(unsigned long long)-1`` on error.  Use :c:func:`PyErr_Occurred`
312   to disambiguate.
313
314   .. versionchanged:: 3.8
315      Use :meth:`__index__` if available.
316
317   .. deprecated:: 3.8
318      Using :meth:`__int__` is deprecated.
319
320
321.. c:function:: double PyLong_AsDouble(PyObject *pylong)
322
323   Return a C :c:type:`double` representation of *pylong*.  *pylong* must be
324   an instance of :c:type:`PyLongObject`.
325
326   Raise :exc:`OverflowError` if the value of *pylong* is out of range for a
327   :c:type:`double`.
328
329   Returns ``-1.0`` on error.  Use :c:func:`PyErr_Occurred` to disambiguate.
330
331
332.. c:function:: void* PyLong_AsVoidPtr(PyObject *pylong)
333
334   Convert a Python integer *pylong* to a C :c:type:`void` pointer.
335   If *pylong* cannot be converted, an :exc:`OverflowError` will be raised.  This
336   is only assured to produce a usable :c:type:`void` pointer for values created
337   with :c:func:`PyLong_FromVoidPtr`.
338
339   Returns ``NULL`` on error.  Use :c:func:`PyErr_Occurred` to disambiguate.
340