diff -r 82ba1f46def5 Lib/test/test_int.py --- a/Lib/test/test_int.py Thu Sep 27 20:07:45 2012 +1000 +++ b/Lib/test/test_int.py Thu Sep 27 18:08:24 2012 +0100 @@ -305,6 +305,18 @@ self.fail("Failed to raise TypeError with %s" % ((base, trunc_result_base),)) + # Regression test for bugs.python.org/issue16060. + class BadInt(trunc_result_base): + def __int__(self): + return 42.0 + + class TruncReturnsBadInt(base): + def __trunc__(self): + return BadInt() + + with self.assertRaises(TypeError): + int(TruncReturnsBadInt()) + def test_error_message(self): testlist = ('\xbd', '123\xbd', ' 123 456 ') for s in testlist: diff -r 82ba1f46def5 Objects/abstract.c --- a/Objects/abstract.c Thu Sep 27 20:07:45 2012 +1000 +++ b/Objects/abstract.c Thu Sep 27 18:08:24 2012 +0100 @@ -1228,11 +1228,10 @@ nb = Py_TYPE(integral)->tp_as_number; if (nb->nb_int) { PyObject *as_int = nb->nb_int(integral); - Py_DECREF(integral); - if (!as_int) - return NULL; - if (PyLong_Check(as_int)) + if (!as_int || PyLong_Check(as_int)) { + Py_DECREF(integral); return as_int; + } Py_DECREF(as_int); } PyErr_Format(PyExc_TypeError, error_format, Py_TYPE(integral)->tp_name);