bpo-37999: No longer use __int__ in implicit integer conversions. (GH-15636)
Only __index__ should be used to make integer conversions lossless.
diff --git a/Objects/longobject.c b/Objects/longobject.c
index 0ff0e80..a409948 100644
--- a/Objects/longobject.c
+++ b/Objects/longobject.c
@@ -119,123 +119,6 @@
return v;
}
-/* _PyLong_FromNbInt: Convert the given object to a PyLongObject
- using the nb_int slot, if available. Raise TypeError if either the
- nb_int slot is not available or the result of the call to nb_int
- returns something not of type int.
-*/
-PyObject *
-_PyLong_FromNbInt(PyObject *integral)
-{
- PyNumberMethods *nb;
- PyObject *result;
-
- /* Fast path for the case that we already have an int. */
- if (PyLong_CheckExact(integral)) {
- Py_INCREF(integral);
- return integral;
- }
-
- nb = Py_TYPE(integral)->tp_as_number;
- if (nb == NULL || nb->nb_int == NULL) {
- PyErr_Format(PyExc_TypeError,
- "an integer is required (got type %.200s)",
- Py_TYPE(integral)->tp_name);
- return NULL;
- }
-
- /* Convert using the nb_int slot, which should return something
- of exact type int. */
- result = nb->nb_int(integral);
- if (!result || PyLong_CheckExact(result))
- return result;
- if (!PyLong_Check(result)) {
- PyErr_Format(PyExc_TypeError,
- "__int__ returned non-int (type %.200s)",
- Py_TYPE(result)->tp_name);
- Py_DECREF(result);
- return NULL;
- }
- /* Issue #17576: warn if 'result' not of exact type int. */
- if (PyErr_WarnFormat(PyExc_DeprecationWarning, 1,
- "__int__ returned non-int (type %.200s). "
- "The ability to return an instance of a strict subclass of int "
- "is deprecated, and may be removed in a future version of Python.",
- Py_TYPE(result)->tp_name)) {
- Py_DECREF(result);
- return NULL;
- }
- return result;
-}
-
-/* Convert the given object to a PyLongObject using the nb_index or
- nb_int slots, if available (the latter is deprecated).
- Raise TypeError if either nb_index and nb_int slots are not
- available or the result of the call to nb_index or nb_int
- returns something not of type int.
- Should be replaced with PyNumber_Index after the end of the
- deprecation period.
-*/
-PyObject *
-_PyLong_FromNbIndexOrNbInt(PyObject *integral)
-{
- PyNumberMethods *nb;
- PyObject *result;
-
- /* Fast path for the case that we already have an int. */
- if (PyLong_CheckExact(integral)) {
- Py_INCREF(integral);
- return integral;
- }
-
- nb = Py_TYPE(integral)->tp_as_number;
- if (nb == NULL || (nb->nb_index == NULL && nb->nb_int == NULL)) {
- PyErr_Format(PyExc_TypeError,
- "an integer is required (got type %.200s)",
- Py_TYPE(integral)->tp_name);
- return NULL;
- }
-
- if (nb->nb_index) {
- /* Convert using the nb_index slot, which should return something
- of exact type int. */
- result = nb->nb_index(integral);
- if (!result || PyLong_CheckExact(result))
- return result;
- if (!PyLong_Check(result)) {
- PyErr_Format(PyExc_TypeError,
- "__index__ returned non-int (type %.200s)",
- Py_TYPE(result)->tp_name);
- Py_DECREF(result);
- return NULL;
- }
- /* Issue #17576: warn if 'result' not of exact type int. */
- if (PyErr_WarnFormat(PyExc_DeprecationWarning, 1,
- "__index__ returned non-int (type %.200s). "
- "The ability to return an instance of a strict subclass of int "
- "is deprecated, and may be removed in a future version of Python.",
- Py_TYPE(result)->tp_name))
- {
- Py_DECREF(result);
- return NULL;
- }
- return result;
- }
-
- result = _PyLong_FromNbInt(integral);
- if (result && PyErr_WarnFormat(PyExc_DeprecationWarning, 1,
- "an integer is required (got type %.200s). "
- "Implicit conversion to integers using __int__ is deprecated, "
- "and may be removed in a future version of Python.",
- Py_TYPE(integral)->tp_name))
- {
- Py_DECREF(result);
- return NULL;
- }
- return result;
-}
-
-
/* Allocate a new int object with size digits.
Return NULL and set exception if we run out of memory. */
@@ -511,7 +394,7 @@
v = (PyLongObject *)vv;
}
else {
- v = (PyLongObject *)_PyLong_FromNbIndexOrNbInt(vv);
+ v = (PyLongObject *)PyNumber_Index(vv);
if (v == NULL)
return -1;
do_decref = 1;
@@ -791,7 +674,7 @@
return _PyLong_AsUnsignedLongMask(op);
}
- lo = (PyLongObject *)_PyLong_FromNbIndexOrNbInt(op);
+ lo = (PyLongObject *)PyNumber_Index(op);
if (lo == NULL)
return (unsigned long)-1;
@@ -1249,7 +1132,7 @@
v = (PyLongObject *)vv;
}
else {
- v = (PyLongObject *)_PyLong_FromNbIndexOrNbInt(vv);
+ v = (PyLongObject *)PyNumber_Index(vv);
if (v == NULL)
return -1;
do_decref = 1;
@@ -1364,7 +1247,7 @@
return _PyLong_AsUnsignedLongLongMask(op);
}
- lo = (PyLongObject *)_PyLong_FromNbIndexOrNbInt(op);
+ lo = (PyLongObject *)PyNumber_Index(op);
if (lo == NULL)
return (unsigned long long)-1;
@@ -1404,7 +1287,7 @@
v = (PyLongObject *)vv;
}
else {
- v = (PyLongObject *)_PyLong_FromNbIndexOrNbInt(vv);
+ v = (PyLongObject *)PyNumber_Index(vv);
if (v == NULL)
return -1;
do_decref = 1;