[Python-checkins] cpython (2.7): call PyErr_Clear() when ignoring error from PyNumber_Int (closes #15516)

benjamin.peterson python-checkins at python.org
Wed Jan 2 19:23:46 CET 2013


http://hg.python.org/cpython/rev/80cfd4caa726
changeset:   81240:80cfd4caa726
branch:      2.7
parent:      81238:4264994fd6a3
user:        Benjamin Peterson <benjamin at python.org>
date:        Wed Jan 02 12:21:32 2013 -0600
summary:
  call PyErr_Clear() when ignoring error from PyNumber_Int (closes #15516)

Patch from Tom Tromey.

files:
  Lib/test/test_format.py |  10 ++++++++++
  Misc/NEWS               |   3 +++
  Objects/stringobject.c  |   5 ++++-
  3 files changed, 17 insertions(+), 1 deletions(-)


diff --git a/Lib/test/test_format.py b/Lib/test/test_format.py
--- a/Lib/test/test_format.py
+++ b/Lib/test/test_format.py
@@ -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 --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -9,6 +9,9 @@
 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
--- a/Objects/stringobject.c
+++ b/Objects/stringobject.c
@@ -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)) {

-- 
Repository URL: http://hg.python.org/cpython


More information about the Python-checkins mailing list