Skip to content

[3.6] Document the error return of PyLong_As* APIs. (GH-5396) #5404

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 29, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions Doc/c-api/long.rst
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ Integer Objects

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

On error, most ``PyLong_As*`` APIs return ``(return type)-1`` which cannot be
distinguished from a number. Use :c:func:`PyErr_Occurred` to disambiguate.

.. c:type:: PyLongObject

This subtype of :c:type:`PyObject` represents a Python integer object.
Expand Down Expand Up @@ -134,6 +137,8 @@ All integers are implemented as "long" integer objects of arbitrary size.
Raise :exc:`OverflowError` if the value of *obj* is out of range for a
:c:type:`long`.

Returns -1 on error. Use :c:func:`PyErr_Occurred` to disambiguate.


.. c:function:: long PyLong_AsLongAndOverflow(PyObject *obj, int *overflow)

Expand All @@ -146,6 +151,8 @@ All integers are implemented as "long" integer objects of arbitrary size.
return ``-1``; otherwise, set *\*overflow* to ``0``. If any other exception
occurs set *\*overflow* to ``0`` and return ``-1`` as usual.

Returns -1 on error. Use :c:func:`PyErr_Occurred` to disambiguate.


.. c:function:: long long PyLong_AsLongLong(PyObject *obj)

Expand All @@ -159,6 +166,8 @@ All integers are implemented as "long" integer objects of arbitrary size.
Raise :exc:`OverflowError` if the value of *obj* is out of range for a
:c:type:`long`.

Returns -1 on error. Use :c:func:`PyErr_Occurred` to disambiguate.


.. c:function:: long long PyLong_AsLongLongAndOverflow(PyObject *obj, int *overflow)

Expand All @@ -171,6 +180,8 @@ All integers are implemented as "long" integer objects of arbitrary size.
and return ``-1``; otherwise, set *\*overflow* to ``0``. If any other
exception occurs set *\*overflow* to ``0`` and return ``-1`` as usual.

Returns -1 on error. Use :c:func:`PyErr_Occurred` to disambiguate.

.. versionadded:: 3.2


Expand All @@ -186,6 +197,8 @@ All integers are implemented as "long" integer objects of arbitrary size.
Raise :exc:`OverflowError` if the value of *pylong* is out of range for a
:c:type:`Py_ssize_t`.

Returns -1 on error. Use :c:func:`PyErr_Occurred` to disambiguate.


.. c:function:: unsigned long PyLong_AsUnsignedLong(PyObject *pylong)

Expand All @@ -199,15 +212,25 @@ All integers are implemented as "long" integer objects of arbitrary size.
Raise :exc:`OverflowError` if the value of *pylong* is out of range for a
:c:type:`unsigned long`.

Returns ``(unsigned long)-1`` on error.
Use :c:func:`PyErr_Occurred` to disambiguate.


.. c:function:: size_t PyLong_AsSize_t(PyObject *pylong)

.. index::
single: SIZE_MAX
single: OverflowError (built-in exception)

Return a C :c:type:`size_t` representation of *pylong*. *pylong* must be
an instance of :c:type:`PyLongObject`.

Raise :exc:`OverflowError` if the value of *pylong* is out of range for a
:c:type:`size_t`.

Returns ``(size_t)-1`` on error.
Use :c:func:`PyErr_Occurred` to disambiguate.


.. c:function:: unsigned long long PyLong_AsUnsignedLongLong(PyObject *pylong)

Expand All @@ -220,6 +243,9 @@ All integers are implemented as "long" integer objects of arbitrary size.
Raise :exc:`OverflowError` if the value of *pylong* is out of range for an
:c:type:`unsigned long long`.

Returns ``(unsigned long long)-1`` on error.
Use :c:func:`PyErr_Occurred` to disambiguate.

.. versionchanged:: 3.1
A negative *pylong* now raises :exc:`OverflowError`, not :exc:`TypeError`.

Expand All @@ -233,6 +259,8 @@ All integers are implemented as "long" integer objects of arbitrary size.
If the value of *obj* is out of range for an :c:type:`unsigned long`,
return the reduction of that value modulo ``ULONG_MAX + 1``.

Returns -1 on error. Use :c:func:`PyErr_Occurred` to disambiguate.


.. c:function:: unsigned long long PyLong_AsUnsignedLongLongMask(PyObject *obj)

Expand All @@ -243,6 +271,8 @@ All integers are implemented as "long" integer objects of arbitrary size.
If the value of *obj* is out of range for an :c:type:`unsigned long long`,
return the reduction of that value modulo ``PY_ULLONG_MAX + 1``.

Returns -1 on error. Use :c:func:`PyErr_Occurred` to disambiguate.


.. c:function:: double PyLong_AsDouble(PyObject *pylong)

Expand All @@ -252,10 +282,14 @@ All integers are implemented as "long" integer objects of arbitrary size.
Raise :exc:`OverflowError` if the value of *pylong* is out of range for a
:c:type:`double`.

Returns -1.0 on error. Use :c:func:`PyErr_Occurred` to disambiguate.


.. c:function:: void* PyLong_AsVoidPtr(PyObject *pylong)

Convert a Python integer *pylong* to a C :c:type:`void` pointer.
If *pylong* cannot be converted, an :exc:`OverflowError` will be raised. This
is only assured to produce a usable :c:type:`void` pointer for values created
with :c:func:`PyLong_FromVoidPtr`.

Returns NULL on error. Use :c:func:`PyErr_Occurred` to disambiguate.