Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit b8dc3ab

Browse files
committed
Issue #12965: More PyLong_As* clarifications. Thanks Stefan Krah.
1 parent f0acfee commit b8dc3ab

1 file changed

Lines changed: 39 additions & 22 deletions

File tree

Doc/c-api/long.rst

Lines changed: 39 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -167,9 +167,11 @@ All integers are implemented as "long" integer objects of arbitrary size.
167167
single: PY_SSIZE_T_MAX
168168
single: OverflowError (built-in exception)
169169
170-
Return a C :c:type:`Py_ssize_t` representation of the contents of *pylong*.
171-
If *pylong* is greater than :const:`PY_SSIZE_T_MAX`, an :exc:`OverflowError`
172-
is raised and ``-1`` will be returned.
170+
Return a C :c:type:`Py_ssize_t` representation of *pylong*. *pylong* must
171+
be an instance of :c:type:`PyLongObject`.
172+
173+
Raise :exc:`OverflowError` if the value of *pylong* is out of range for a
174+
:c:type:`Py_ssize_t`.
173175
174176
175177
.. c:function:: unsigned long PyLong_AsUnsignedLong(PyObject *pylong)
@@ -178,49 +180,64 @@ All integers are implemented as "long" integer objects of arbitrary size.
178180
single: ULONG_MAX
179181
single: OverflowError (built-in exception)
180182
181-
Return a C :c:type:`unsigned long` representation of the contents of *pylong*.
182-
If *pylong* is greater than :const:`ULONG_MAX`, an :exc:`OverflowError` is
183-
raised.
183+
Return a C :c:type:`unsigned long` representation of *pylong*. *pylong*
184+
must be an instance of :c:type:`PyLongObject`.
185+
186+
Raise :exc:`OverflowError` if the value of *pylong* is out of range for a
187+
:c:type:`unsigned long`.
184188
185189
186190
.. c:function:: size_t PyLong_AsSize_t(PyObject *pylong)
187191
188-
Return a :c:type:`size_t` representation of the contents of *pylong*. If
189-
*pylong* is greater than the maximum value for a :c:type:`size_t`, an
190-
:exc:`OverflowError` is raised.
192+
Return a C :c:type:`size_t` representation of of *pylong*. *pylong* must be
193+
an instance of :c:type:`PyLongObject`.
194+
195+
Raise :exc:`OverflowError` if the value of *pylong* is out of range for a
196+
:c:type:`size_t`.
191197
192198
193199
.. c:function:: unsigned PY_LONG_LONG PyLong_AsUnsignedLongLong(PyObject *pylong)
194200
195201
.. index::
196202
single: OverflowError (built-in exception)
197203
198-
Return a C :c:type:`unsigned long long` from a Python integer. If
199-
*pylong* cannot be represented as an :c:type:`unsigned long long`,
200-
an :exc:`OverflowError` is raised and ``(unsigned long long)-1`` is
201-
returned.
204+
Return a C :c:type:`unsigned PY_LONG_LONG` representation of of *pylong*.
205+
*pylong* must be an instance of :c:type:`PyLongObject`.
206+
207+
Raise :exc:`OverflowError` if the value of *pylong* is out of range for an
208+
:c:type:`unsigned PY_LONG_LONG`.
202209
203210
.. versionchanged:: 3.1
204211
A negative *pylong* now raises :exc:`OverflowError`, not :exc:`TypeError`.
205212
206213
207-
.. c:function:: unsigned long PyLong_AsUnsignedLongMask(PyObject *io)
214+
.. c:function:: unsigned long PyLong_AsUnsignedLongMask(PyObject *obj)
208215
209-
Return a C :c:type:`unsigned long` from a Python integer, without checking for
210-
overflow.
216+
Return a C :c:type:`unsigned long` representation of *obj*. If *obj*
217+
is not an instance of :c:type:`PyLongObject`, first call its :meth:`__int__`
218+
method (if present) to convert it to a :c:type:`PyLongObject`.
211219
220+
If the value of *obj* is out of range for an :c:type:`unsigned long`,
221+
return the reduction of that value modulo :const:`ULONG_MAX + 1`.
212222
213-
.. c:function:: unsigned PY_LONG_LONG PyLong_AsUnsignedLongLongMask(PyObject *io)
214223
215-
Return a C :c:type:`unsigned long long` from a Python integer, without
216-
checking for overflow.
224+
.. c:function:: unsigned PY_LONG_LONG PyLong_AsUnsignedLongLongMask(PyObject *obj)
225+
226+
Return a C :c:type:`unsigned long long` representation of *obj*. If *obj*
227+
is not an instance of :c:type:`PyLongObject`, first call its :meth:`__int__`
228+
method (if present) to convert it to a :c:type:`PyLongObject`.
229+
230+
If the value of *obj* is out of range for an :c:type:`unsigned long long`,
231+
return the reduction of that value modulo :const:`PY_ULLONG_MAX + 1`.
217232
218233
219234
.. c:function:: double PyLong_AsDouble(PyObject *pylong)
220235
221-
Return a C :c:type:`double` representation of the contents of *pylong*. If
222-
*pylong* cannot be approximately represented as a :c:type:`double`, an
223-
:exc:`OverflowError` exception is raised and ``-1.0`` will be returned.
236+
Return a C :c:type:`double` representation of *pylong*. *pylong* must be
237+
an instance of :c:type:`PyLongObject`.
238+
239+
Raise :exc:`OverflowError` if the value of *pylong* is out of range for a
240+
:c:type:`double`.
224241
225242
226243
.. c:function:: void* PyLong_AsVoidPtr(PyObject *pylong)

0 commit comments

Comments
 (0)