summaryrefslogtreecommitdiffstats
path: root/Doc
diff options
context:
space:
mode:
authorSerhiy Storchaka <storchaka@gmail.com>2019-02-25 15:57:58 (GMT)
committerGitHub <noreply@github.com>2019-02-25 15:57:58 (GMT)
commit6a44f6eef3d0958d88882347190b3e2d1222c2e9 (patch)
treebb6474b1e094a672837c3333a081fa4d5d7638f2 /Doc
parentd90a141bb947b68601f8d1f37bc98f7b524f0e01 (diff)
downloadcpython-6a44f6eef3d0958d88882347190b3e2d1222c2e9.zip
cpython-6a44f6eef3d0958d88882347190b3e2d1222c2e9.tar.gz
cpython-6a44f6eef3d0958d88882347190b3e2d1222c2e9.tar.bz2
bpo-36048: Use __index__() instead of __int__() for implicit conversion if available. (GH-11952)
Deprecate using the __int__() method in implicit conversions of Python numbers to C integers.
Diffstat (limited to 'Doc')
-rw-r--r--Doc/c-api/long.rst66
-rw-r--r--Doc/c-api/number.rst3
-rw-r--r--Doc/whatsnew/3.8.rst20
3 files changed, 77 insertions, 12 deletions
diff --git a/Doc/c-api/long.rst b/Doc/c-api/long.rst
index 5b1f386..8093f4b 100644
--- a/Doc/c-api/long.rst
+++ b/Doc/c-api/long.rst
@@ -131,20 +131,28 @@ distinguished from a number. Use :c:func:`PyErr_Occurred` to disambiguate.
single: OverflowError (built-in exception)
Return a C :c:type:`long` representation of *obj*. If *obj* is not an
- instance of :c:type:`PyLongObject`, first call its :meth:`__int__` method
- (if present) to convert it to a :c:type:`PyLongObject`.
+ instance of :c:type:`PyLongObject`, first call its :meth:`__index__` or
+ :meth:`__int__` method (if present) to convert it to a
+ :c:type:`PyLongObject`.
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.
+ .. versionchanged:: 3.8
+ Use :meth:`__index__` if available.
+
+ .. deprecated:: 3.8
+ Using :meth:`__int__` is deprecated.
+
.. c:function:: long PyLong_AsLongAndOverflow(PyObject *obj, int *overflow)
Return a C :c:type:`long` representation of *obj*. If *obj* is not an
- instance of :c:type:`PyLongObject`, first call its :meth:`__int__` method
- (if present) to convert it to a :c:type:`PyLongObject`.
+ instance of :c:type:`PyLongObject`, first call its :meth:`__index__` or
+ :meth:`__int__` method (if present) to convert it to a
+ :c:type:`PyLongObject`.
If the value of *obj* is greater than :const:`LONG_MAX` or less than
:const:`LONG_MIN`, set *\*overflow* to ``1`` or ``-1``, respectively, and
@@ -153,6 +161,12 @@ distinguished from a number. Use :c:func:`PyErr_Occurred` to disambiguate.
Returns ``-1`` on error. Use :c:func:`PyErr_Occurred` to disambiguate.
+ .. versionchanged:: 3.8
+ Use :meth:`__index__` if available.
+
+ .. deprecated:: 3.8
+ Using :meth:`__int__` is deprecated.
+
.. c:function:: long long PyLong_AsLongLong(PyObject *obj)
@@ -160,20 +174,28 @@ distinguished from a number. Use :c:func:`PyErr_Occurred` to disambiguate.
single: OverflowError (built-in exception)
Return a C :c:type:`long long` representation of *obj*. If *obj* is not an
- instance of :c:type:`PyLongObject`, first call its :meth:`__int__` method
- (if present) to convert it to a :c:type:`PyLongObject`.
+ instance of :c:type:`PyLongObject`, first call its :meth:`__index__` or
+ :meth:`__int__` method (if present) to convert it to a
+ :c:type:`PyLongObject`.
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.
+ .. versionchanged:: 3.8
+ Use :meth:`__index__` if available.
+
+ .. deprecated:: 3.8
+ Using :meth:`__int__` is deprecated.
+
.. c:function:: long long PyLong_AsLongLongAndOverflow(PyObject *obj, int *overflow)
Return a C :c:type:`long long` representation of *obj*. If *obj* is not an
- instance of :c:type:`PyLongObject`, first call its :meth:`__int__` method
- (if present) to convert it to a :c:type:`PyLongObject`.
+ instance of :c:type:`PyLongObject`, first call its :meth:`__index__` or
+ :meth:`__int__` method (if present) to convert it to a
+ :c:type:`PyLongObject`.
If the value of *obj* is greater than :const:`PY_LLONG_MAX` or less than
:const:`PY_LLONG_MIN`, set *\*overflow* to ``1`` or ``-1``, respectively,
@@ -184,6 +206,12 @@ distinguished from a number. Use :c:func:`PyErr_Occurred` to disambiguate.
.. versionadded:: 3.2
+ .. versionchanged:: 3.8
+ Use :meth:`__index__` if available.
+
+ .. deprecated:: 3.8
+ Using :meth:`__int__` is deprecated.
+
.. c:function:: Py_ssize_t PyLong_AsSsize_t(PyObject *pylong)
@@ -253,26 +281,40 @@ distinguished from a number. Use :c:func:`PyErr_Occurred` to disambiguate.
.. c:function:: unsigned long PyLong_AsUnsignedLongMask(PyObject *obj)
Return a C :c:type:`unsigned long` representation of *obj*. If *obj*
- is not an instance of :c:type:`PyLongObject`, first call its :meth:`__int__`
- method (if present) to convert it to a :c:type:`PyLongObject`.
+ is not an instance of :c:type:`PyLongObject`, first call its
+ :meth:`__index__` or :meth:`__int__` method (if present) to convert
+ it to a :c:type:`PyLongObject`.
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.
+ .. versionchanged:: 3.8
+ Use :meth:`__index__` if available.
+
+ .. deprecated:: 3.8
+ Using :meth:`__int__` is deprecated.
+
.. c:function:: unsigned long long PyLong_AsUnsignedLongLongMask(PyObject *obj)
Return a C :c:type:`unsigned long long` representation of *obj*. If *obj*
- is not an instance of :c:type:`PyLongObject`, first call its :meth:`__int__`
- method (if present) to convert it to a :c:type:`PyLongObject`.
+ is not an instance of :c:type:`PyLongObject`, first call its
+ :meth:`__index__` or :meth:`__int__` method (if present) to convert
+ it to a :c:type:`PyLongObject`.
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.
+ .. versionchanged:: 3.8
+ Use :meth:`__index__` if available.
+
+ .. deprecated:: 3.8
+ Using :meth:`__int__` is deprecated.
+
.. c:function:: double PyLong_AsDouble(PyObject *pylong)
diff --git a/Doc/c-api/number.rst b/Doc/c-api/number.rst
index 296b21c..9fb220b 100644
--- a/Doc/c-api/number.rst
+++ b/Doc/c-api/number.rst
@@ -11,6 +11,9 @@ Number Protocol
Returns ``1`` if the object *o* provides numeric protocols, and false otherwise.
This function always succeeds.
+ .. versionchanged:: 3.8
+ Returns ``1`` if *o* is an index integer.
+
.. c:function:: PyObject* PyNumber_Add(PyObject *o1, PyObject *o2)
diff --git a/Doc/whatsnew/3.8.rst b/Doc/whatsnew/3.8.rst
index 68a4457..154fd66 100644
--- a/Doc/whatsnew/3.8.rst
+++ b/Doc/whatsnew/3.8.rst
@@ -458,6 +458,17 @@ Build and C API Changes
(Contributed by Antoine Pitrou in :issue:`32430`.)
+* Functions that convert Python number to C integer like
+ :c:func:`PyLong_AsLong` and argument parsing functions like
+ :c:func:`PyArg_ParseTuple` with integer converting format units like ``'i'``
+ will now use the :meth:`~object.__index__` special method instead of
+ :meth:`~object.__int__`, if available. The deprecation warning will be
+ emitted for objects with the ``__int__()`` method but without the
+ ``__index__()`` method (like :class:`~decimal.Decimal` and
+ :class:`~fractions.Fraction`). :c:func:`PyNumber_Check` will now return
+ ``1`` for objects implementing ``__index__()``.
+ (Contributed by Serhiy Storchaka in :issue:`36048`.)
+
Deprecated
==========
@@ -508,6 +519,15 @@ Deprecated
* The :meth:`~threading.Thread.isAlive()` method of :class:`threading.Thread` has been deprecated.
(Contributed by Dong-hee Na in :issue:`35283`.)
+* Many builtin and extension functions that take integer arguments will
+ now emit a deprecation warning for :class:`~decimal.Decimal`\ s,
+ :class:`~fractions.Fraction`\ s and any other objects that can be converted
+ to integers only with a loss (e.g. that have the :meth:`~object.__int__`
+ method but do not have the :meth:`~object.__index__` method). In future
+ version they will be errors.
+ (Contributed by Serhiy Storchaka in :issue:`36048`.)
+
+
API and Feature Removals
========================