Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Use nb_index for the result of __trunc__.
  • Loading branch information
serhiy-storchaka committed Feb 24, 2019
commit 44b5acd5558d8ddd4c8c862688c48e7ce2016b48
3 changes: 2 additions & 1 deletion Lib/test/test_int.py
Original file line number Diff line number Diff line change
Expand Up @@ -385,7 +385,8 @@ def __int__(self):
class TruncReturnsNonInt(base):
def __trunc__(self):
return Integral()
self.assertEqual(int(TruncReturnsNonInt()), 42)
with self.assertWarns(DeprecationWarning):
self.assertEqual(int(TruncReturnsNonInt()), 42)

class NonIntegral(trunc_result_base):
def __trunc__(self):
Expand Down
4 changes: 2 additions & 2 deletions Objects/abstract.c
Original file line number Diff line number Diff line change
Expand Up @@ -1387,15 +1387,15 @@ PyNumber_Long(PyObject *o)
/* __trunc__ is specified to return an Integral type,
but int() needs to return an int. */
m = result->ob_type->tp_as_number;
if (m == NULL || m->nb_int == NULL) {
if (m == NULL || (m->nb_index == NULL && m->nb_int == NULL)) {
PyErr_Format(
PyExc_TypeError,
"__trunc__ returned non-Integral (type %.200s)",
result->ob_type->tp_name);
Py_DECREF(result);
return NULL;
}
Py_SETREF(result, _PyLong_FromNbInt(result));
Py_SETREF(result, _PyLong_FromNbIndexOrNbInt(result));
if (result != NULL && !PyLong_CheckExact(result)) {
Py_SETREF(result, _PyLong_Copy((PyLongObject *)result));
}
Expand Down