Issue #5262: Improved fix.
[python.git] / Doc / c-api / long.rst
blobc9cb034dc9677a04f37d86ad3103c1652d5f464c
1 .. highlightlang:: c
3 .. _longobjects:
5 Long Integer Objects
6 --------------------
8 .. index:: object: long integer
11 .. ctype:: PyLongObject
13    This subtype of :ctype:`PyObject` represents a Python long integer object.
16 .. cvar:: PyTypeObject PyLong_Type
18    .. index:: single: LongType (in modules types)
20    This instance of :ctype:`PyTypeObject` represents the Python long integer type.
21    This is the same object as ``long`` and ``types.LongType``.
24 .. cfunction:: int PyLong_Check(PyObject *p)
26    Return true if its argument is a :ctype:`PyLongObject` or a subtype of
27    :ctype:`PyLongObject`.
29    .. versionchanged:: 2.2
30       Allowed subtypes to be accepted.
33 .. cfunction:: int PyLong_CheckExact(PyObject *p)
35    Return true if its argument is a :ctype:`PyLongObject`, but not a subtype of
36    :ctype:`PyLongObject`.
38    .. versionadded:: 2.2
41 .. cfunction:: PyObject* PyLong_FromLong(long v)
43    Return a new :ctype:`PyLongObject` object from *v*, or *NULL* on failure.
46 .. cfunction:: PyObject* PyLong_FromUnsignedLong(unsigned long v)
48    Return a new :ctype:`PyLongObject` object from a C :ctype:`unsigned long`, or
49    *NULL* on failure.
52 .. cfunction:: PyObject* PyLong_FromSsize_t(Py_ssize_t v)
54    Return a new :ctype:`PyLongObject` object from a C :ctype:`Py_ssize_t`, or
55    *NULL* on failure.
57    .. versionadded:: 2.6
60 .. cfunction:: PyObject* PyLong_FromSize_t(size_t v)
62    Return a new :ctype:`PyLongObject` object from a C :ctype:`size_t`, or
63    *NULL* on failure.
65    .. versionadded:: 2.6
68 .. cfunction:: PyObject* PyLong_FromLongLong(PY_LONG_LONG v)
70    Return a new :ctype:`PyLongObject` object from a C :ctype:`long long`, or *NULL*
71    on failure.
74 .. cfunction:: PyObject* PyLong_FromUnsignedLongLong(unsigned PY_LONG_LONG v)
76    Return a new :ctype:`PyLongObject` object from a C :ctype:`unsigned long long`,
77    or *NULL* on failure.
80 .. cfunction:: PyObject* PyLong_FromDouble(double v)
82    Return a new :ctype:`PyLongObject` object from the integer part of *v*, or
83    *NULL* on failure.
86 .. cfunction:: PyObject* PyLong_FromString(char *str, char **pend, int base)
88    Return a new :ctype:`PyLongObject` based on the string value in *str*, which is
89    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``, the radix will be determined
92    based on the leading characters of *str*: if *str* starts with ``'0x'`` or
93    ``'0X'``, radix 16 will be used; if *str* starts with ``'0'``, radix 8 will be
94    used; otherwise radix 10 will be used.  If *base* is not ``0``, it must be
95    between ``2`` and ``36``, inclusive.  Leading spaces are ignored.  If there are
96    no digits, :exc:`ValueError` will be raised.
99 .. cfunction:: PyObject* PyLong_FromUnicode(Py_UNICODE *u, Py_ssize_t length, int base)
101    Convert a sequence of Unicode digits to a Python long integer value.  The first
102    parameter, *u*, points to the first character of the Unicode string, *length*
103    gives the number of characters, and *base* is the radix for the conversion.  The
104    radix must be in the range [2, 36]; if it is out of range, :exc:`ValueError`
105    will be raised.
107    .. versionadded:: 1.6
109    .. versionchanged:: 2.5
110       This function used an :ctype:`int` for *length*. This might require
111       changes in your code for properly supporting 64-bit systems.
114 .. cfunction:: PyObject* PyLong_FromVoidPtr(void *p)
116    Create a Python integer or long integer from the pointer *p*. The pointer value
117    can be retrieved from the resulting value using :cfunc:`PyLong_AsVoidPtr`.
119    .. versionadded:: 1.5.2
121    .. versionchanged:: 2.5
122       If the integer is larger than LONG_MAX, a positive long integer is returned.
125 .. cfunction:: long PyLong_AsLong(PyObject *pylong)
127    .. index::
128       single: LONG_MAX
129       single: OverflowError (built-in exception)
131    Return a C :ctype:`long` representation of the contents of *pylong*.  If
132    *pylong* is greater than :const:`LONG_MAX`, an :exc:`OverflowError` is raised
133    and ``-1`` will be returned.
136 .. cfunction:: Py_ssize_t PyLong_AsSsize_t(PyObject *pylong)
138    .. index::
139       single: PY_SSIZE_T_MAX
140       single: OverflowError (built-in exception)
142    Return a C :ctype:`Py_ssize_t` representation of the contents of *pylong*.  If
143    *pylong* is greater than :const:`PY_SSIZE_T_MAX`, an :exc:`OverflowError` is raised
144    and ``-1`` will be returned.
146    .. versionadded:: 2.6
149 .. cfunction:: unsigned long PyLong_AsUnsignedLong(PyObject *pylong)
151    .. index::
152       single: ULONG_MAX
153       single: OverflowError (built-in exception)
155    Return a C :ctype:`unsigned long` representation of the contents of *pylong*.
156    If *pylong* is greater than :const:`ULONG_MAX`, an :exc:`OverflowError` is
157    raised.
160 .. cfunction:: PY_LONG_LONG PyLong_AsLongLong(PyObject *pylong)
162    .. index::
163       single: OverflowError (built-in exception)
165    Return a C :ctype:`long long` from a Python long integer.  If
166    *pylong* cannot be represented as a :ctype:`long long`, an
167    :exc:`OverflowError` is raised and ``-1`` is returned.
169    .. versionadded:: 2.2
172 .. cfunction:: unsigned PY_LONG_LONG PyLong_AsUnsignedLongLong(PyObject *pylong)
174    .. index::
175       single: OverflowError (built-in exception)
177    Return a C :ctype:`unsigned long long` from a Python long integer. If
178    *pylong* cannot be represented as an :ctype:`unsigned long long`, an
179    :exc:`OverflowError` is raised and ``(unsigned long long)-1`` is
180    returned.
182    .. versionadded:: 2.2
184    .. versionchanged:: 2.7
185       A negative *pylong* now raises :exc:`OverflowError`, not
186       :exc:`TypeError`.
189 .. cfunction:: unsigned long PyLong_AsUnsignedLongMask(PyObject *io)
191    Return a C :ctype:`unsigned long` from a Python long integer, without checking
192    for overflow.
194    .. versionadded:: 2.3
197 .. cfunction:: unsigned PY_LONG_LONG PyLong_AsUnsignedLongLongMask(PyObject *io)
199    Return a C :ctype:`unsigned long long` from a Python long integer, without
200    checking for overflow.
202    .. versionadded:: 2.3
205 .. cfunction:: double PyLong_AsDouble(PyObject *pylong)
207    Return a C :ctype:`double` representation of the contents of *pylong*.  If
208    *pylong* cannot be approximately represented as a :ctype:`double`, an
209    :exc:`OverflowError` exception is raised and ``-1.0`` will be returned.
212 .. cfunction:: void* PyLong_AsVoidPtr(PyObject *pylong)
214    Convert a Python integer or long integer *pylong* to a C :ctype:`void` pointer.
215    If *pylong* cannot be converted, an :exc:`OverflowError` will be raised.  This
216    is only assured to produce a usable :ctype:`void` pointer for values created
217    with :cfunc:`PyLong_FromVoidPtr`.
219    .. versionadded:: 1.5.2
221    .. versionchanged:: 2.5
222       For values outside 0..LONG_MAX, both signed and unsigned integers are accepted.