blob: 7453ccd131cd4cfbea3f09d9a0e837599b91c7ac [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 Brandlf6842722008-01-19 22:08:21 +000068.. cfunction:: PyObject* PyLong_FromLongLong(PY_LONG_LONG v)
69
70 Return a new :ctype:`PyLongObject` object from a C :ctype:`long long`, or *NULL*
71 on failure.
72
73
74.. cfunction:: PyObject* PyLong_FromUnsignedLongLong(unsigned PY_LONG_LONG v)
75
76 Return a new :ctype:`PyLongObject` object from a C :ctype:`unsigned long long`,
77 or *NULL* on failure.
78
79
80.. cfunction:: PyObject* PyLong_FromDouble(double v)
81
82 Return a new :ctype:`PyLongObject` object from the integer part of *v*, or
83 *NULL* on failure.
84
85
86.. cfunction:: PyObject* PyLong_FromString(char *str, char **pend, int base)
87
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.
97
98
99.. cfunction:: PyObject* PyLong_FromUnicode(Py_UNICODE *u, Py_ssize_t length, int base)
100
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
110 This function used an :ctype:`int` for *length*. This might require
111 changes in your code for properly supporting 64-bit systems.
112
Georg Brandlf6842722008-01-19 22:08:21 +0000113
114.. cfunction:: PyObject* PyLong_FromVoidPtr(void *p)
115
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`.
118
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
125.. cfunction:: long PyLong_AsLong(PyObject *pylong)
126
127 .. index::
128 single: LONG_MAX
129 single: OverflowError (built-in exception)
130
131 Return a C :ctype:`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
Mark Dickinsone31d3002009-12-21 11:21:25 +0000136.. cfunction:: long PyLong_AsLongAndOverflow(PyObject *pylong, int* overflow)
137
138 Return a C :ctype:`long` representation of the contents of
139 *pylong*. If *pylong* is greater than :const:`LONG_MAX` or less
140 than :const:`LONG_MIN`, set `*overflow` to ``1`` or ``-1``,
141 respectively, and return ``-1``; otherwise, set `*overflow` to
142 ``0``. If any other exception occurs (for example a TypeError or
143 MemoryError), then ``-1`` will be returned and ``*overflow`` will
144 be ``0``.
145
146 .. versionadded:: 2.7
147
148
Georg Brandl78b3ee82008-04-26 18:31:07 +0000149.. cfunction:: Py_ssize_t PyLong_AsSsize_t(PyObject *pylong)
150
151 .. index::
152 single: PY_SSIZE_T_MAX
153 single: OverflowError (built-in exception)
154
155 Return a C :ctype:`Py_ssize_t` representation of the contents of *pylong*. If
156 *pylong* is greater than :const:`PY_SSIZE_T_MAX`, an :exc:`OverflowError` is raised
157 and ``-1`` will be returned.
158
Georg Brandl74c018a2009-03-31 15:46:30 +0000159 .. versionadded:: 2.6
Georg Brandlf6842722008-01-19 22:08:21 +0000160
161
162.. cfunction:: unsigned long PyLong_AsUnsignedLong(PyObject *pylong)
163
164 .. index::
165 single: ULONG_MAX
166 single: OverflowError (built-in exception)
167
168 Return a C :ctype:`unsigned long` representation of the contents of *pylong*.
169 If *pylong* is greater than :const:`ULONG_MAX`, an :exc:`OverflowError` is
170 raised.
171
172
173.. cfunction:: PY_LONG_LONG PyLong_AsLongLong(PyObject *pylong)
174
Mark Dickinson4015f622009-02-10 15:46:50 +0000175 .. index::
176 single: OverflowError (built-in exception)
177
178 Return a C :ctype:`long long` from a Python long integer. If
179 *pylong* cannot be represented as a :ctype:`long long`, an
180 :exc:`OverflowError` is raised and ``-1`` is returned.
Georg Brandlf6842722008-01-19 22:08:21 +0000181
182 .. versionadded:: 2.2
183
184
185.. cfunction:: unsigned PY_LONG_LONG PyLong_AsUnsignedLongLong(PyObject *pylong)
186
Mark Dickinson4015f622009-02-10 15:46:50 +0000187 .. index::
188 single: OverflowError (built-in exception)
189
190 Return a C :ctype:`unsigned long long` from a Python long integer. If
191 *pylong* cannot be represented as an :ctype:`unsigned long long`, an
192 :exc:`OverflowError` is raised and ``(unsigned long long)-1`` is
193 returned.
Georg Brandlf6842722008-01-19 22:08:21 +0000194
195 .. versionadded:: 2.2
196
Mark Dickinson4015f622009-02-10 15:46:50 +0000197 .. versionchanged:: 2.7
198 A negative *pylong* now raises :exc:`OverflowError`, not
199 :exc:`TypeError`.
200
Georg Brandlf6842722008-01-19 22:08:21 +0000201
202.. cfunction:: unsigned long PyLong_AsUnsignedLongMask(PyObject *io)
203
204 Return a C :ctype:`unsigned long` from a Python long integer, without checking
205 for overflow.
206
207 .. versionadded:: 2.3
208
209
210.. cfunction:: unsigned PY_LONG_LONG PyLong_AsUnsignedLongLongMask(PyObject *io)
211
212 Return a C :ctype:`unsigned long long` from a Python long integer, without
213 checking for overflow.
214
215 .. versionadded:: 2.3
216
217
218.. cfunction:: double PyLong_AsDouble(PyObject *pylong)
219
220 Return a C :ctype:`double` representation of the contents of *pylong*. If
221 *pylong* cannot be approximately represented as a :ctype:`double`, an
222 :exc:`OverflowError` exception is raised and ``-1.0`` will be returned.
223
224
225.. cfunction:: void* PyLong_AsVoidPtr(PyObject *pylong)
226
227 Convert a Python integer or long integer *pylong* to a C :ctype:`void` pointer.
228 If *pylong* cannot be converted, an :exc:`OverflowError` will be raised. This
229 is only assured to produce a usable :ctype:`void` pointer for values created
230 with :cfunc:`PyLong_FromVoidPtr`.
231
232 .. versionadded:: 1.5.2
233
234 .. versionchanged:: 2.5
Georg Brandl907a7202008-02-22 12:31:45 +0000235 For values outside 0..LONG_MAX, both signed and unsigned integers are accepted.
Georg Brandlf6842722008-01-19 22:08:21 +0000236
237