Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit a708adf

Browse files
committed
call PyErr_Clear() when ignoring error from PyNumber_Int (closes python#15516)
Patch from Tom Tromey.
1 parent 140794d commit a708adf

3 files changed

Lines changed: 17 additions & 1 deletion

File tree

Lib/test/test_format.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,16 @@ def test_format(self):
234234
testformat('%g', 1.1, '1.1')
235235
testformat('%#g', 1.1, '1.10000')
236236

237+
# Regression test for http://bugs.python.org/issue15516.
238+
class IntFails(object):
239+
def __int__(self):
240+
raise TestFailed
241+
def __long__(self):
242+
return 0
243+
244+
fst = IntFails()
245+
testformat("%x", fst, '0')
246+
237247
# Test exception for unknown format characters
238248
if verbose:
239249
print 'Testing exceptions'

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ What's New in Python 2.7.4
99
Core and Builtins
1010
-----------------
1111

12+
- Issue #15516: Fix a bug in PyString_FromFormat where it failed to properly
13+
ignore errors from a __int__() method.
14+
1215
- Issue #16839: Fix a segfault when calling unicode() on a classic class early
1316
in interpreter initialization.
1417

Objects/stringobject.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4489,7 +4489,10 @@ PyString_Format(PyObject *format, PyObject *args)
44894489
}
44904490
else {
44914491
iobj = PyNumber_Int(v);
4492-
if (iobj==NULL) iobj = PyNumber_Long(v);
4492+
if (iobj==NULL) {
4493+
PyErr_Clear();
4494+
iobj = PyNumber_Long(v);
4495+
}
44934496
}
44944497
if (iobj!=NULL) {
44954498
if (PyInt_Check(iobj)) {

0 commit comments

Comments
 (0)