summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorBenjamin Peterson <benjamin@python.org>2013-01-02 18:21:32 (GMT)
committerBenjamin Peterson <benjamin@python.org>2013-01-02 18:21:32 (GMT)
commita708adfcf663e871dd3acdbea358f9bc047ec64b (patch)
tree82e9ee819d9c1a5d6a763489356085584322d210
parent140794d6e79c60642c3ff2634a233e55cd3faeea (diff)
downloadcpython-a708adfcf663e871dd3acdbea358f9bc047ec64b.zip
cpython-a708adfcf663e871dd3acdbea358f9bc047ec64b.tar.gz
cpython-a708adfcf663e871dd3acdbea358f9bc047ec64b.tar.bz2
call PyErr_Clear() when ignoring error from PyNumber_Int (closes #15516)
Patch from Tom Tromey.
-rw-r--r--Lib/test/test_format.py10
-rw-r--r--Misc/NEWS3
-rw-r--r--Objects/stringobject.c5
3 files changed, 17 insertions, 1 deletions
diff --git a/Lib/test/test_format.py b/Lib/test/test_format.py
index f39426b..dd30efa 100644
--- a/Lib/test/test_format.py
+++ b/Lib/test/test_format.py
@@ -234,6 +234,16 @@ class FormatTest(unittest.TestCase):
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 --git a/Misc/NEWS b/Misc/NEWS
index 5e719e5..45a4979 100644
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -9,6 +9,9 @@ What's New in Python 2.7.4
Core and Builtins
-----------------
+- Issue #15516: Fix a bug in PyString_FromFormat where it failed to properly
+ ignore errors from a __int__() method.
+
- Issue #16839: Fix a segfault when calling unicode() on a classic class early
in interpreter initialization.
diff --git a/Objects/stringobject.c b/Objects/stringobject.c
index 152ea21..dcab35e 100644
--- a/Objects/stringobject.c
+++ b/Objects/stringobject.c
@@ -4489,7 +4489,10 @@ PyString_Format(PyObject *format, PyObject *args)
}
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)) {