blob: 11d290836c0c329c4bff1d82387498ddbfabfa98 [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
11.. ctype:: PyLongObject
12
13 This subtype of :ctype:`PyObject` represents a Python long integer object.
14
15
16.. cvar:: PyTypeObject PyLong_Type
17
18 .. index:: single: LongType (in modules types)
19
20 This instance of :ctype:`PyTypeObject` represents the Python long integer type.
21 This is the same object as ``long`` and ``types.LongType``.
22
23
24.. cfunction:: int PyLong_Check(PyObject *p)
25
26 Return true if its argument is a :ctype:`PyLongObject` or a subtype of
27 :ctype:`PyLongObject`.
28
29 .. versionchanged:: 2.2
30 Allowed subtypes to be accepted.
31
32
33.. cfunction:: int PyLong_CheckExact(PyObject *p)
34
35 Return true if its argument is a :ctype:`PyLongObject`, but not a subtype of
36 :ctype:`PyLongObject`.
37
38 .. versionadded:: 2.2
39
40
41.. cfunction:: PyObject* PyLong_FromLong(long v)
42
43 Return a new :ctype:`PyLongObject` object from *v*, or *NULL* on failure.
44
45
46.. cfunction:: PyObject* PyLong_FromUnsignedLong(unsigned long v)
47
48 Return a new :ctype:`PyLongObject` object from a C :ctype:`unsigned long`, or
49 *NULL* on failure.
50
51
Georg Brandl78b3ee82008-04-26 18:31:07 +000052.. cfunction:: PyObject* PyLong_FromSsize_t(Py_ssize_t v)
53
54 Return a new :ctype:`PyLongObject` object from a C :ctype:`Py_ssize_t`, or
55 *NULL* on failure.
56
Georg Brandl74c018a2009-03-31 15:46:30 +000057 .. versionadded:: 2.6
Georg Brandl78b3ee82008-04-26 18:31:07 +000058
59
60.. cfunction:: PyObject* PyLong_FromSize_t(size_t v)
61
62 Return a new :ctype:`PyLongObject` object from a C :ctype:`size_t`, or
63 *NULL* on failure.
64
Georg Brandl74c018a2009-03-31 15:46:30 +000065 .. versionadded:: 2.6
Georg Brandl78b3ee82008-04-26 18:31:07 +000066
67
Georg Brandl79cdff02010-10-17 10:54:57 +000068.. cfunction:: PyObject* PyLong_FromSsize_t(Py_ssize_t v)
69
70 Return a new :ctype:`PyLongObject` object with a value of *v*, or *NULL*
71 on failure.
72
73 .. versionadded:: 2.6
74
75
76.. cfunction:: PyObject* PyLong_FromSize_t(size_t v)
77
78 Return a new :ctype:`PyLongObject` object with a value of *v*, or *NULL*
79 on failure.
80
81 .. versionadded:: 2.6
82
83
Georg Brandlf6842722008-01-19 22:08:21 +000084.. cfunction:: PyObject* PyLong_FromLongLong(PY_LONG_LONG v)
85
86 Return a new :ctype:`PyLongObject` object from a C :ctype:`long long`, or *NULL*
87 on failure.
88
89
90.. cfunction:: PyObject* PyLong_FromUnsignedLongLong(unsigned PY_LONG_LONG v)
91
92 Return a new :ctype:`PyLongObject` object from a C :ctype:`unsigned long long`,
93 or *NULL* on failure.
94
95
96.. cfunction:: PyObject* PyLong_FromDouble(double v)
97
98 Return a new :ctype:`PyLongObject` object from the integer part of *v*, or
99 *NULL* on failure.
100
101
102.. cfunction:: PyObject* PyLong_FromString(char *str, char **pend, int base)
103
104 Return a new :ctype:`PyLongObject` based on the string value in *str*, which is
105 interpreted according to the radix in *base*. If *pend* is non-*NULL*,
Ezio Melotti91d26b42010-01-30 13:27:05 +0000106 *\*pend* will point to the first character in *str* which follows the
Georg Brandlf6842722008-01-19 22:08:21 +0000107 representation of the number. If *base* is ``0``, the radix will be determined
108 based on the leading characters of *str*: if *str* starts with ``'0x'`` or
109 ``'0X'``, radix 16 will be used; if *str* starts with ``'0'``, radix 8 will be
110 used; otherwise radix 10 will be used. If *base* is not ``0``, it must be
111 between ``2`` and ``36``, inclusive. Leading spaces are ignored. If there are
112 no digits, :exc:`ValueError` will be raised.
113
114
115.. cfunction:: PyObject* PyLong_FromUnicode(Py_UNICODE *u, Py_ssize_t length, int base)
116
117 Convert a sequence of Unicode digits to a Python long integer value. The first
118 parameter, *u*, points to the first character of the Unicode string, *length*
119 gives the number of characters, and *base* is the radix for the conversion. The
120 radix must be in the range [2, 36]; if it is out of range, :exc:`ValueError`
121 will be raised.
122
123 .. versionadded:: 1.6
124
Jeroen Ruigrok van der Werven089c5cd2009-04-25 17:59:03 +0000125 .. versionchanged:: 2.5
126 This function used an :ctype:`int` for *length*. This might require
127 changes in your code for properly supporting 64-bit systems.
128
Georg Brandlf6842722008-01-19 22:08:21 +0000129
130.. cfunction:: PyObject* PyLong_FromVoidPtr(void *p)
131
132 Create a Python integer or long integer from the pointer *p*. The pointer value
133 can be retrieved from the resulting value using :cfunc:`PyLong_AsVoidPtr`.
134
135 .. versionadded:: 1.5.2
136
137 .. versionchanged:: 2.5
138 If the integer is larger than LONG_MAX, a positive long integer is returned.
139
140
141.. cfunction:: long PyLong_AsLong(PyObject *pylong)
142
143 .. index::
144 single: LONG_MAX
145 single: OverflowError (built-in exception)
146
147 Return a C :ctype:`long` representation of the contents of *pylong*. If
Georg Brandl78b3ee82008-04-26 18:31:07 +0000148 *pylong* is greater than :const:`LONG_MAX`, an :exc:`OverflowError` is raised
Georg Brandlc62ef8b2009-01-03 20:55:06 +0000149 and ``-1`` will be returned.
Georg Brandl78b3ee82008-04-26 18:31:07 +0000150
151
Ezio Melotti99af6482010-01-30 13:08:54 +0000152.. cfunction:: long PyLong_AsLongAndOverflow(PyObject *pylong, int *overflow)
Mark Dickinsone31d3002009-12-21 11:21:25 +0000153
154 Return a C :ctype:`long` representation of the contents of
155 *pylong*. If *pylong* is greater than :const:`LONG_MAX` or less
Ezio Melotti91d26b42010-01-30 13:27:05 +0000156 than :const:`LONG_MIN`, set *\*overflow* to ``1`` or ``-1``,
157 respectively, and return ``-1``; otherwise, set *\*overflow* to
Mark Dickinsone31d3002009-12-21 11:21:25 +0000158 ``0``. If any other exception occurs (for example a TypeError or
Ezio Melotti91d26b42010-01-30 13:27:05 +0000159 MemoryError), then ``-1`` will be returned and *\*overflow* will
Mark Dickinsone31d3002009-12-21 11:21:25 +0000160 be ``0``.
161
162 .. versionadded:: 2.7
163
164
Ezio Melotti99af6482010-01-30 13:08:54 +0000165.. cfunction:: PY_LONG_LONG PyLong_AsLongLongAndOverflow(PyObject *pylong, int *overflow)
Mark Dickinsona36507c2010-01-30 10:08:33 +0000166
167 Return a C :ctype:`long long` representation of the contents of
168 *pylong*. If *pylong* is greater than :const:`PY_LLONG_MAX` or less
Ezio Melotti91d26b42010-01-30 13:27:05 +0000169 than :const:`PY_LLONG_MIN`, set *\*overflow* to ``1`` or ``-1``,
170 respectively, and return ``-1``; otherwise, set *\*overflow* to
Mark Dickinsona36507c2010-01-30 10:08:33 +0000171 ``0``. If any other exception occurs (for example a TypeError or
Ezio Melotti91d26b42010-01-30 13:27:05 +0000172 MemoryError), then ``-1`` will be returned and *\*overflow* will
Mark Dickinsona36507c2010-01-30 10:08:33 +0000173 be ``0``.
174
175 .. versionadded:: 2.7
176
177
Georg Brandl78b3ee82008-04-26 18:31:07 +0000178.. cfunction:: Py_ssize_t PyLong_AsSsize_t(PyObject *pylong)
179
180 .. index::
181 single: PY_SSIZE_T_MAX
182 single: OverflowError (built-in exception)
183
184 Return a C :ctype:`Py_ssize_t` representation of the contents of *pylong*. If
185 *pylong* is greater than :const:`PY_SSIZE_T_MAX`, an :exc:`OverflowError` is raised
186 and ``-1`` will be returned.
187
Georg Brandl74c018a2009-03-31 15:46:30 +0000188 .. versionadded:: 2.6
Georg Brandlf6842722008-01-19 22:08:21 +0000189
190
191.. cfunction:: unsigned long PyLong_AsUnsignedLong(PyObject *pylong)
192
193 .. index::
194 single: ULONG_MAX
195 single: OverflowError (built-in exception)
196
197 Return a C :ctype:`unsigned long` representation of the contents of *pylong*.
198 If *pylong* is greater than :const:`ULONG_MAX`, an :exc:`OverflowError` is
199 raised.
200
201
Georg Brandl79cdff02010-10-17 10:54:57 +0000202.. cfunction:: Py_ssize_t PyLong_AsSsize_t(PyObject *pylong)
203
204 .. index::
205 single: PY_SSIZE_T_MAX
206
207 Return a :ctype:`Py_ssize_t` representation of the contents of *pylong*. If
208 *pylong* is greater than :const:`PY_SSIZE_T_MAX`, an :exc:`OverflowError` is
209 raised.
210
211 .. versionadded:: 2.6
212
213
Georg Brandlf6842722008-01-19 22:08:21 +0000214.. cfunction:: PY_LONG_LONG PyLong_AsLongLong(PyObject *pylong)
215
Mark Dickinson4015f622009-02-10 15:46:50 +0000216 .. index::
217 single: OverflowError (built-in exception)
218
219 Return a C :ctype:`long long` from a Python long integer. If
220 *pylong* cannot be represented as a :ctype:`long long`, an
221 :exc:`OverflowError` is raised and ``-1`` is returned.
Georg Brandlf6842722008-01-19 22:08:21 +0000222
223 .. versionadded:: 2.2
224
225
226.. cfunction:: unsigned PY_LONG_LONG PyLong_AsUnsignedLongLong(PyObject *pylong)
227
Mark Dickinson4015f622009-02-10 15:46:50 +0000228 .. index::
229 single: OverflowError (built-in exception)
230
231 Return a C :ctype:`unsigned long long` from a Python long integer. If
232 *pylong* cannot be represented as an :ctype:`unsigned long long`, an
233 :exc:`OverflowError` is raised and ``(unsigned long long)-1`` is
234 returned.
Georg Brandlf6842722008-01-19 22:08:21 +0000235
236 .. versionadded:: 2.2
237
Mark Dickinson4015f622009-02-10 15:46:50 +0000238 .. versionchanged:: 2.7
239 A negative *pylong* now raises :exc:`OverflowError`, not
240 :exc:`TypeError`.
241
Georg Brandlf6842722008-01-19 22:08:21 +0000242
243.. cfunction:: unsigned long PyLong_AsUnsignedLongMask(PyObject *io)
244
245 Return a C :ctype:`unsigned long` from a Python long integer, without checking
246 for overflow.
247
248 .. versionadded:: 2.3
249
250
251.. cfunction:: unsigned PY_LONG_LONG PyLong_AsUnsignedLongLongMask(PyObject *io)
252
253 Return a C :ctype:`unsigned long long` from a Python long integer, without
254 checking for overflow.
255
256 .. versionadded:: 2.3
257
258
259.. cfunction:: double PyLong_AsDouble(PyObject *pylong)
260
261 Return a C :ctype:`double` representation of the contents of *pylong*. If
262 *pylong* cannot be approximately represented as a :ctype:`double`, an
263 :exc:`OverflowError` exception is raised and ``-1.0`` will be returned.
264
265
266.. cfunction:: void* PyLong_AsVoidPtr(PyObject *pylong)
267
268 Convert a Python integer or long integer *pylong* to a C :ctype:`void` pointer.
269 If *pylong* cannot be converted, an :exc:`OverflowError` will be raised. This
270 is only assured to produce a usable :ctype:`void` pointer for values created
271 with :cfunc:`PyLong_FromVoidPtr`.
272
273 .. versionadded:: 1.5.2
274
275 .. versionchanged:: 2.5
Georg Brandl907a7202008-02-22 12:31:45 +0000276 For values outside 0..LONG_MAX, both signed and unsigned integers are accepted.
Georg Brandlf6842722008-01-19 22:08:21 +0000277
278