Skip to content

Commit 5869861

Browse files
miss-islingtongpshead
authored andcommitted
Document the error return of PyLong_As* APIs. (pythonGH-5396) (python#5404)
Document the error return of PyLong_As* APIs. A frequent Python C API usage error is neglecting to check the return value and/or PyErr_Occurred(). (cherry picked from commit f5b04a3)
1 parent 0cecc22 commit 5869861

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

Doc/c-api/long.rst

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ Integer Objects
1010

1111
All integers are implemented as "long" integer objects of arbitrary size.
1212

13+
On error, most ``PyLong_As*`` APIs return ``(return type)-1`` which cannot be
14+
distinguished from a number. Use :c:func:`PyErr_Occurred` to disambiguate.
15+
1316
.. c:type:: PyLongObject
1417
1518
This subtype of :c:type:`PyObject` represents a Python integer object.
@@ -134,6 +137,8 @@ All integers are implemented as "long" integer objects of arbitrary size.
134137
Raise :exc:`OverflowError` if the value of *obj* is out of range for a
135138
:c:type:`long`.
136139
140+
Returns -1 on error. Use :c:func:`PyErr_Occurred` to disambiguate.
141+
137142
138143
.. c:function:: long PyLong_AsLongAndOverflow(PyObject *obj, int *overflow)
139144
@@ -146,6 +151,8 @@ All integers are implemented as "long" integer objects of arbitrary size.
146151
return ``-1``; otherwise, set *\*overflow* to ``0``. If any other exception
147152
occurs set *\*overflow* to ``0`` and return ``-1`` as usual.
148153
154+
Returns -1 on error. Use :c:func:`PyErr_Occurred` to disambiguate.
155+
149156
150157
.. c:function:: long long PyLong_AsLongLong(PyObject *obj)
151158
@@ -159,6 +166,8 @@ All integers are implemented as "long" integer objects of arbitrary size.
159166
Raise :exc:`OverflowError` if the value of *obj* is out of range for a
160167
:c:type:`long`.
161168
169+
Returns -1 on error. Use :c:func:`PyErr_Occurred` to disambiguate.
170+
162171
163172
.. c:function:: long long PyLong_AsLongLongAndOverflow(PyObject *obj, int *overflow)
164173
@@ -171,6 +180,8 @@ All integers are implemented as "long" integer objects of arbitrary size.
171180
and return ``-1``; otherwise, set *\*overflow* to ``0``. If any other
172181
exception occurs set *\*overflow* to ``0`` and return ``-1`` as usual.
173182
183+
Returns -1 on error. Use :c:func:`PyErr_Occurred` to disambiguate.
184+
174185
.. versionadded:: 3.2
175186
176187
@@ -186,6 +197,8 @@ All integers are implemented as "long" integer objects of arbitrary size.
186197
Raise :exc:`OverflowError` if the value of *pylong* is out of range for a
187198
:c:type:`Py_ssize_t`.
188199
200+
Returns -1 on error. Use :c:func:`PyErr_Occurred` to disambiguate.
201+
189202
190203
.. c:function:: unsigned long PyLong_AsUnsignedLong(PyObject *pylong)
191204
@@ -199,15 +212,25 @@ All integers are implemented as "long" integer objects of arbitrary size.
199212
Raise :exc:`OverflowError` if the value of *pylong* is out of range for a
200213
:c:type:`unsigned long`.
201214
215+
Returns ``(unsigned long)-1`` on error.
216+
Use :c:func:`PyErr_Occurred` to disambiguate.
217+
202218
203219
.. c:function:: size_t PyLong_AsSize_t(PyObject *pylong)
204220
221+
.. index::
222+
single: SIZE_MAX
223+
single: OverflowError (built-in exception)
224+
205225
Return a C :c:type:`size_t` representation of *pylong*. *pylong* must be
206226
an instance of :c:type:`PyLongObject`.
207227
208228
Raise :exc:`OverflowError` if the value of *pylong* is out of range for a
209229
:c:type:`size_t`.
210230
231+
Returns ``(size_t)-1`` on error.
232+
Use :c:func:`PyErr_Occurred` to disambiguate.
233+
211234
212235
.. c:function:: unsigned long long PyLong_AsUnsignedLongLong(PyObject *pylong)
213236
@@ -220,6 +243,9 @@ All integers are implemented as "long" integer objects of arbitrary size.
220243
Raise :exc:`OverflowError` if the value of *pylong* is out of range for an
221244
:c:type:`unsigned long long`.
222245
246+
Returns ``(unsigned long long)-1`` on error.
247+
Use :c:func:`PyErr_Occurred` to disambiguate.
248+
223249
.. versionchanged:: 3.1
224250
A negative *pylong* now raises :exc:`OverflowError`, not :exc:`TypeError`.
225251
@@ -233,6 +259,8 @@ All integers are implemented as "long" integer objects of arbitrary size.
233259
If the value of *obj* is out of range for an :c:type:`unsigned long`,
234260
return the reduction of that value modulo ``ULONG_MAX + 1``.
235261
262+
Returns -1 on error. Use :c:func:`PyErr_Occurred` to disambiguate.
263+
236264
237265
.. c:function:: unsigned long long PyLong_AsUnsignedLongLongMask(PyObject *obj)
238266
@@ -243,6 +271,8 @@ All integers are implemented as "long" integer objects of arbitrary size.
243271
If the value of *obj* is out of range for an :c:type:`unsigned long long`,
244272
return the reduction of that value modulo ``PY_ULLONG_MAX + 1``.
245273
274+
Returns -1 on error. Use :c:func:`PyErr_Occurred` to disambiguate.
275+
246276
247277
.. c:function:: double PyLong_AsDouble(PyObject *pylong)
248278
@@ -252,10 +282,14 @@ All integers are implemented as "long" integer objects of arbitrary size.
252282
Raise :exc:`OverflowError` if the value of *pylong* is out of range for a
253283
:c:type:`double`.
254284
285+
Returns -1.0 on error. Use :c:func:`PyErr_Occurred` to disambiguate.
286+
255287
256288
.. c:function:: void* PyLong_AsVoidPtr(PyObject *pylong)
257289
258290
Convert a Python integer *pylong* to a C :c:type:`void` pointer.
259291
If *pylong* cannot be converted, an :exc:`OverflowError` will be raised. This
260292
is only assured to produce a usable :c:type:`void` pointer for values created
261293
with :c:func:`PyLong_FromVoidPtr`.
294+
295+
Returns NULL on error. Use :c:func:`PyErr_Occurred` to disambiguate.

0 commit comments

Comments
 (0)