diff -r 0fd2ea06e17d -r dac3a1042acd Lib/test/test_format.py --- a/Lib/test/test_format.py Thu Dec 06 16:32:37 2012 +0200 +++ b/Lib/test/test_format.py Thu Dec 06 10:37:54 2012 -0700 @@ -234,6 +234,16 @@ testformat('%g', 1.1, '1.1') testformat('%#g', 1.1, '1.10000') + # Regression test for http://bugs.python.org/issue15516. + class IntFails(object): + def __int__(self): + raise TestFailed + def __long__(self): + return 0 + + fst = IntFails() + testformat("%x", fst, '0') + # Test exception for unknown format characters if verbose: print 'Testing exceptions' diff -r 0fd2ea06e17d -r dac3a1042acd Objects/stringobject.c --- a/Objects/stringobject.c Thu Dec 06 16:32:37 2012 +0200 +++ b/Objects/stringobject.c Thu Dec 06 10:37:54 2012 -0700 @@ -4489,7 +4489,10 @@ } else { iobj = PyNumber_Int(v); - if (iobj==NULL) iobj = PyNumber_Long(v); + if (iobj==NULL) { + PyErr_Clear(); + iobj = PyNumber_Long(v); + } } if (iobj!=NULL) { if (PyInt_Check(iobj)) {