blob: 28fb589a6f0b07e3c3831b65472e69f3e73d6032 [file] [log] [blame]
Georg Brandlf6842722008-01-19 22:08:21 +00001.. highlightlang:: c
2
3.. _longobjects:
4
5Long Integer Objects
6--------------------
7
8.. index:: object: long integer
9
10
Sandro Tosi98ed08f2012-01-14 16:42:02 +010011.. c:type:: PyLongObject
Georg Brandlf6842722008-01-19 22:08:21 +000012
Sandro Tosi98ed08f2012-01-14 16:42:02 +010013 This subtype of :c:type:`PyObject` represents a Python long integer object.
Georg Brandlf6842722008-01-19 22:08:21 +000014
15
Sandro Tosi98ed08f2012-01-14 16:42:02 +010016.. c:var:: PyTypeObject PyLong_Type
Georg Brandlf6842722008-01-19 22:08:21 +000017
18 .. index:: single: LongType (in modules types)
19
Sandro Tosi98ed08f2012-01-14 16:42:02 +010020 This instance of :c:type:`PyTypeObject` represents the Python long integer type.
Georg Brandlf6842722008-01-19 22:08:21 +000021 This is the same object as ``long`` and ``types.LongType``.
22
23
Sandro Tosi98ed08f2012-01-14 16:42:02 +010024.. c:function:: int PyLong_Check(PyObject *p)
Georg Brandlf6842722008-01-19 22:08:21 +000025
Sandro Tosi98ed08f2012-01-14 16:42:02 +010026 Return true if its argument is a :c:type:`PyLongObject` or a subtype of
27 :c:type:`PyLongObject`.
Georg Brandlf6842722008-01-19 22:08:21 +000028
29 .. versionchanged:: 2.2
30 Allowed subtypes to be accepted.
31
32
Sandro Tosi98ed08f2012-01-14 16:42:02 +010033.. c:function:: int PyLong_CheckExact(PyObject *p)
Georg Brandlf6842722008-01-19 22:08:21 +000034
Sandro Tosi98ed08f2012-01-14 16:42:02 +010035 Return true if its argument is a :c:type:`PyLongObject`, but not a subtype of
36 :c:type:`PyLongObject`.
Georg Brandlf6842722008-01-19 22:08:21 +000037
38 .. versionadded:: 2.2
39
40
Sandro Tosi98ed08f2012-01-14 16:42:02 +010041.. c:function:: PyObject* PyLong_FromLong(long v)
Georg Brandlf6842722008-01-19 22:08:21 +000042
Sandro Tosi98ed08f2012-01-14 16:42:02 +010043 Return a new :c:type:`PyLongObject` object from *v*, or *NULL* on failure.
Georg Brandlf6842722008-01-19 22:08:21 +000044
45
Sandro Tosi98ed08f2012-01-14 16:42:02 +010046.. c:function:: PyObject* PyLong_FromUnsignedLong(unsigned long v)
Georg Brandlf6842722008-01-19 22:08:21 +000047
Sandro Tosi98ed08f2012-01-14 16:42:02 +010048 Return a new :c:type:`PyLongObject` object from a C :c:type:`unsigned long`, or
Georg Brandlf6842722008-01-19 22:08:21 +000049 *NULL* on failure.
50
51
Sandro Tosi98ed08f2012-01-14 16:42:02 +010052.. c:function:: PyObject* PyLong_FromSsize_t(Py_ssize_t v)
Georg Brandl78b3ee82008-04-26 18:31:07 +000053
Sandro Tosi98ed08f2012-01-14 16:42:02 +010054 Return a new :c:type:`PyLongObject` object from a C :c:type:`Py_ssize_t`, or
Georg Brandl78b3ee82008-04-26 18:31:07 +000055 *NULL* on failure.
56
Georg Brandl74c018a2009-03-31 15:46:30 +000057 .. versionadded:: 2.6
Georg Brandl78b3ee82008-04-26 18:31:07 +000058
59
Sandro Tosi98ed08f2012-01-14 16:42:02 +010060.. c:function:: PyObject* PyLong_FromSize_t(size_t v)
Georg Brandl78b3ee82008-04-26 18:31:07 +000061
Sandro Tosi98ed08f2012-01-14 16:42:02 +010062 Return a new :c:type:`PyLongObject` object from a C :c:type:`size_t`, or
Georg Brandl78b3ee82008-04-26 18:31:07 +000063 *NULL* on failure.
64
Georg Brandl74c018a2009-03-31 15:46:30 +000065 .. versionadded:: 2.6
Georg Brandl78b3ee82008-04-26 18:31:07 +000066
67
Sandro Tosi98ed08f2012-01-14 16:42:02 +010068.. c:function:: PyObject* PyLong_FromLongLong(PY_LONG_LONG v)
Georg Brandlf6842722008-01-19 22:08:21 +000069
Sandro Tosi98ed08f2012-01-14 16:42:02 +010070 Return a new :c:type:`PyLongObject` object from a C :c:type:`long long`, or *NULL*
Georg Brandlf6842722008-01-19 22:08:21 +000071 on failure.
72
73
Sandro Tosi98ed08f2012-01-14 16:42:02 +010074.. c:function:: PyObject* PyLong_FromUnsignedLongLong(unsigned PY_LONG_LONG v)
Georg Brandlf6842722008-01-19 22:08:21 +000075
Sandro Tosi98ed08f2012-01-14 16:42:02 +010076 Return a new :c:type:`PyLongObject` object from a C :c:type:`unsigned long long`,
Georg Brandlf6842722008-01-19 22:08:21 +000077 or *NULL* on failure.
78
79
Sandro Tosi98ed08f2012-01-14 16:42:02 +010080.. c:function:: PyObject* PyLong_FromDouble(double v)
Georg Brandlf6842722008-01-19 22:08:21 +000081
Sandro Tosi98ed08f2012-01-14 16:42:02 +010082 Return a new :c:type:`PyLongObject` object from the integer part of *v*, or
Georg Brandlf6842722008-01-19 22:08:21 +000083 *NULL* on failure.
84
85
Sandro Tosi98ed08f2012-01-14 16:42:02 +010086.. c:function:: PyObject* PyLong_FromString(char *str, char **pend, int base)
Georg Brandlf6842722008-01-19 22:08:21 +000087
Sandro Tosi98ed08f2012-01-14 16:42:02 +010088 Return a new :c:type:`PyLongObject` based on the string value in *str*, which is
Georg Brandlf6842722008-01-19 22:08:21 +000089 interpreted according to the radix in *base*. If *pend* is non-*NULL*,
Ezio Melotti91d26b42010-01-30 13:27:05 +000090 *\*pend* will point to the first character in *str* which follows the
Georg Brandlf6842722008-01-19 22:08:21 +000091 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.
97
98
Sandro Tosi98ed08f2012-01-14 16:42:02 +010099.. c:function:: PyObject* PyLong_FromUnicode(Py_UNICODE *u, Py_ssize_t length, int base)
Georg Brandlf6842722008-01-19 22:08:21 +0000100
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.
106
107 .. versionadded:: 1.6
108
Jeroen Ruigrok van der Werven089c5cd2009-04-25 17:59:03 +0000109 .. versionchanged:: 2.5
Sandro Tosi98ed08f2012-01-14 16:42:02 +0100110 This function used an :c:type:`int` for *length*. This might require
Jeroen Ruigrok van der Werven089c5cd2009-04-25 17:59:03 +0000111 changes in your code for properly supporting 64-bit systems.
112
Georg Brandlf6842722008-01-19 22:08:21 +0000113
Sandro Tosi98ed08f2012-01-14 16:42:02 +0100114.. c:function:: PyObject* PyLong_FromVoidPtr(void *p)
Georg Brandlf6842722008-01-19 22:08:21 +0000115
116 Create a Python integer or long integer from the pointer *p*. The pointer value
Sandro Tosi98ed08f2012-01-14 16:42:02 +0100117 can be retrieved from the resulting value using :c:func:`PyLong_AsVoidPtr`.
Georg Brandlf6842722008-01-19 22:08:21 +0000118
119 .. versionadded:: 1.5.2
120
121 .. versionchanged:: 2.5
122 If the integer is larger than LONG_MAX, a positive long integer is returned.
123
124
Sandro Tosi98ed08f2012-01-14 16:42:02 +0100125.. c:function:: long PyLong_AsLong(PyObject *pylong)
Georg Brandlf6842722008-01-19 22:08:21 +0000126
127 .. index::
128 single: LONG_MAX
129 single: OverflowError (built-in exception)
130
Sandro Tosi98ed08f2012-01-14 16:42:02 +0100131 Return a C :c:type:`long` representation of the contents of *pylong*. If
Georg Brandl78b3ee82008-04-26 18:31:07 +0000132 *pylong* is greater than :const:`LONG_MAX`, an :exc:`OverflowError` is raised
Georg Brandlc62ef8b2009-01-03 20:55:06 +0000133 and ``-1`` will be returned.
Georg Brandl78b3ee82008-04-26 18:31:07 +0000134
135
Sandro Tosi98ed08f2012-01-14 16:42:02 +0100136.. c:function:: long PyLong_AsLongAndOverflow(PyObject *pylong, int *overflow)
Mark Dickinsone31d3002009-12-21 11:21:25 +0000137
Sandro Tosi98ed08f2012-01-14 16:42:02 +0100138 Return a C :c:type:`long` representation of the contents of
Mark Dickinsone31d3002009-12-21 11:21:25 +0000139 *pylong*. If *pylong* is greater than :const:`LONG_MAX` or less
Ezio Melotti91d26b42010-01-30 13:27:05 +0000140 than :const:`LONG_MIN`, set *\*overflow* to ``1`` or ``-1``,
141 respectively, and return ``-1``; otherwise, set *\*overflow* to
Mark Dickinsone31d3002009-12-21 11:21:25 +0000142 ``0``. If any other exception occurs (for example a TypeError or
Ezio Melotti91d26b42010-01-30 13:27:05 +0000143 MemoryError), then ``-1`` will be returned and *\*overflow* will
Mark Dickinsone31d3002009-12-21 11:21:25 +0000144 be ``0``.
145
146 .. versionadded:: 2.7
147
148
Sandro Tosi98ed08f2012-01-14 16:42:02 +0100149.. c:function:: PY_LONG_LONG PyLong_AsLongLongAndOverflow(PyObject *pylong, int *overflow)
Mark Dickinsona36507c2010-01-30 10:08:33 +0000150
Sandro Tosi98ed08f2012-01-14 16:42:02 +0100151 Return a C :c:type:`long long` representation of the contents of
Mark Dickinsona36507c2010-01-30 10:08:33 +0000152 *pylong*. If *pylong* is greater than :const:`PY_LLONG_MAX` or less
Ezio Melotti91d26b42010-01-30 13:27:05 +0000153 than :const:`PY_LLONG_MIN`, set *\*overflow* to ``1`` or ``-1``,
154 respectively, and return ``-1``; otherwise, set *\*overflow* to
Mark Dickinsona36507c2010-01-30 10:08:33 +0000155 ``0``. If any other exception occurs (for example a TypeError or
Ezio Melotti91d26b42010-01-30 13:27:05 +0000156 MemoryError), then ``-1`` will be returned and *\*overflow* will
Mark Dickinsona36507c2010-01-30 10:08:33 +0000157 be ``0``.
158
159 .. versionadded:: 2.7
160
161
Sandro Tosi98ed08f2012-01-14 16:42:02 +0100162.. c:function:: Py_ssize_t PyLong_AsSsize_t(PyObject *pylong)
Georg Brandl78b3ee82008-04-26 18:31:07 +0000163
164 .. index::
165 single: PY_SSIZE_T_MAX
166 single: OverflowError (built-in exception)
167
Sandro Tosi98ed08f2012-01-14 16:42:02 +0100168 Return a C :c:type:`Py_ssize_t` representation of the contents of *pylong*. If
Georg Brandl78b3ee82008-04-26 18:31:07 +0000169 *pylong* is greater than :const:`PY_SSIZE_T_MAX`, an :exc:`OverflowError` is raised
170 and ``-1`` will be returned.
171
Georg Brandl74c018a2009-03-31 15:46:30 +0000172 .. versionadded:: 2.6
Georg Brandlf6842722008-01-19 22:08:21 +0000173
174
Sandro Tosi98ed08f2012-01-14 16:42:02 +0100175.. c:function:: unsigned long PyLong_AsUnsignedLong(PyObject *pylong)
Georg Brandlf6842722008-01-19 22:08:21 +0000176
177 .. index::
178 single: ULONG_MAX
179 single: OverflowError (built-in exception)
180
Sandro Tosi98ed08f2012-01-14 16:42:02 +0100181 Return a C :c:type:`unsigned long` representation of the contents of *pylong*.
Georg Brandlf6842722008-01-19 22:08:21 +0000182 If *pylong* is greater than :const:`ULONG_MAX`, an :exc:`OverflowError` is
183 raised.
184
185
Sandro Tosi98ed08f2012-01-14 16:42:02 +0100186.. c:function:: PY_LONG_LONG PyLong_AsLongLong(PyObject *pylong)
Georg Brandlf6842722008-01-19 22:08:21 +0000187
Mark Dickinson4015f622009-02-10 15:46:50 +0000188 .. index::
189 single: OverflowError (built-in exception)
190
Sandro Tosi98ed08f2012-01-14 16:42:02 +0100191 Return a C :c:type:`long long` from a Python long integer. If
192 *pylong* cannot be represented as a :c:type:`long long`, an
Mark Dickinson4015f622009-02-10 15:46:50 +0000193 :exc:`OverflowError` is raised and ``-1`` is returned.
Georg Brandlf6842722008-01-19 22:08:21 +0000194
195 .. versionadded:: 2.2
196
197
Sandro Tosi98ed08f2012-01-14 16:42:02 +0100198.. c:function:: unsigned PY_LONG_LONG PyLong_AsUnsignedLongLong(PyObject *pylong)
Georg Brandlf6842722008-01-19 22:08:21 +0000199
Mark Dickinson4015f622009-02-10 15:46:50 +0000200 .. index::
201 single: OverflowError (built-in exception)
202
Sandro Tosi98ed08f2012-01-14 16:42:02 +0100203 Return a C :c:type:`unsigned long long` from a Python long integer. If
204 *pylong* cannot be represented as an :c:type:`unsigned long long`, an
Mark Dickinson4015f622009-02-10 15:46:50 +0000205 :exc:`OverflowError` is raised and ``(unsigned long long)-1`` is
206 returned.
Georg Brandlf6842722008-01-19 22:08:21 +0000207
208 .. versionadded:: 2.2
209
Mark Dickinson4015f622009-02-10 15:46:50 +0000210 .. versionchanged:: 2.7
211 A negative *pylong* now raises :exc:`OverflowError`, not
212 :exc:`TypeError`.
213
Georg Brandlf6842722008-01-19 22:08:21 +0000214
Sandro Tosi98ed08f2012-01-14 16:42:02 +0100215.. c:function:: unsigned long PyLong_AsUnsignedLongMask(PyObject *io)
Georg Brandlf6842722008-01-19 22:08:21 +0000216
Sandro Tosi98ed08f2012-01-14 16:42:02 +0100217 Return a C :c:type:`unsigned long` from a Python long integer, without checking
Georg Brandlf6842722008-01-19 22:08:21 +0000218 for overflow.
219
220 .. versionadded:: 2.3
221
222
Sandro Tosi98ed08f2012-01-14 16:42:02 +0100223.. c:function:: unsigned PY_LONG_LONG PyLong_AsUnsignedLongLongMask(PyObject *io)
Georg Brandlf6842722008-01-19 22:08:21 +0000224
Sandro Tosi98ed08f2012-01-14 16:42:02 +0100225 Return a C :c:type:`unsigned long long` from a Python long integer, without
Georg Brandlf6842722008-01-19 22:08:21 +0000226 checking for overflow.
227
228 .. versionadded:: 2.3
229
230
Sandro Tosi98ed08f2012-01-14 16:42:02 +0100231.. c:function:: double PyLong_AsDouble(PyObject *pylong)
Georg Brandlf6842722008-01-19 22:08:21 +0000232
Sandro Tosi98ed08f2012-01-14 16:42:02 +0100233 Return a C :c:type:`double` representation of the contents of *pylong*. If
234 *pylong* cannot be approximately represented as a :c:type:`double`, an
Georg Brandlf6842722008-01-19 22:08:21 +0000235 :exc:`OverflowError` exception is raised and ``-1.0`` will be returned.
236
237
Sandro Tosi98ed08f2012-01-14 16:42:02 +0100238.. c:function:: void* PyLong_AsVoidPtr(PyObject *pylong)
Georg Brandlf6842722008-01-19 22:08:21 +0000239
Sandro Tosi98ed08f2012-01-14 16:42:02 +0100240 Convert a Python integer or long integer *pylong* to a C :c:type:`void` pointer.
Georg Brandlf6842722008-01-19 22:08:21 +0000241 If *pylong* cannot be converted, an :exc:`OverflowError` will be raised. This
Sandro Tosi98ed08f2012-01-14 16:42:02 +0100242 is only assured to produce a usable :c:type:`void` pointer for values created
243 with :c:func:`PyLong_FromVoidPtr`.
Georg Brandlf6842722008-01-19 22:08:21 +0000244
245 .. versionadded:: 1.5.2
246
247 .. versionchanged:: 2.5
Georg Brandl907a7202008-02-22 12:31:45 +0000248 For values outside 0..LONG_MAX, both signed and unsigned integers are accepted.
Georg Brandlf6842722008-01-19 22:08:21 +0000249
250