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
bpo-17576: Strict __int__ and __index__ return types; operator.index always uses __index__ #13740
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -66,10 +66,10 @@ def __index__(self): | |
direct_index = my_int.__index__() | ||
operator_index = operator.index(my_int) | ||
self.assertEqual(direct_index, 8) | ||
self.assertEqual(operator_index, 7) | ||
self.assertEqual(operator_index, 8) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is the key change: for a subclass of |
||
# Both results should be of exact type int. | ||
self.assertIs(type(direct_index), int) | ||
#self.assertIs(type(operator_index), int) | ||
self.assertIs(type(operator_index), int) | ||
|
||
def test_index_returns_int_subclass(self): | ||
class BadInt: | ||
|
@@ -81,13 +81,12 @@ def __index__(self): | |
return True | ||
|
||
bad_int = BadInt() | ||
with self.assertWarns(DeprecationWarning): | ||
n = operator.index(bad_int) | ||
self.assertEqual(n, 1) | ||
with self.assertRaises(TypeError): | ||
operator.index(bad_int) | ||
|
||
bad_int = BadInt2() | ||
n = operator.index(bad_int) | ||
self.assertEqual(n, 0) | ||
with self.assertRaises(TypeError): | ||
operator.index(bad_int) | ||
|
||
|
||
class SeqTestCase: | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
Remove long-deprecated allowance for ``__int__`` and ``__index__`` methods | ||
to return subclasses of ``int``. |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1269,36 +1269,30 @@ PyNumber_Index(PyObject *item) | |
return null_error(); | ||
} | ||
|
||
if (PyLong_Check(item)) { | ||
/* Fast path for the case that we already have an int. */ | ||
if (PyLong_CheckExact(item)) { | ||
Py_INCREF(item); | ||
return item; | ||
} | ||
|
||
/* In general, use __index__ */ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nitpick: redundant comment. I'll remove it. |
||
if (!PyIndex_Check(item)) { | ||
PyErr_Format(PyExc_TypeError, | ||
"'%.200s' object cannot be interpreted " | ||
"as an integer", item->ob_type->tp_name); | ||
return NULL; | ||
} | ||
result = item->ob_type->tp_as_number->nb_index(item); | ||
if (!result || PyLong_CheckExact(result)) | ||
if (result == NULL || PyLong_CheckExact(result)) { | ||
return result; | ||
if (!PyLong_Check(result)) { | ||
} | ||
else { | ||
PyErr_Format(PyExc_TypeError, | ||
"__index__ returned non-int (type %.200s)", | ||
result->ob_type->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.", | ||
result->ob_type->tp_name)) { | ||
Py_DECREF(result); | ||
return NULL; | ||
} | ||
return result; | ||
} | ||
|
||
/* Return an error on Overflow only if err is not NULL*/ | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nitpick: inconsistent arg ordering. Will fix.