]> granicus.if.org Git - python/commitdiff
call PyErr_Clear() when ignoring error from PyNumber_Int (closes #15516)
authorBenjamin Peterson <benjamin@python.org>
Wed, 2 Jan 2013 18:21:32 +0000 (12:21 -0600)
committerBenjamin Peterson <benjamin@python.org>
Wed, 2 Jan 2013 18:21:32 +0000 (12:21 -0600)
Patch from Tom Tromey.

Lib/test/test_format.py
Misc/NEWS
Objects/stringobject.c

index f39426b82bb12e843113418ebf4841dfee2055fc..dd30efabb6c0746656d0047a595c1a69f4a882a2 100644 (file)
@@ -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'
index 5e719e52ad012307adbf6ed5afdf51c1024e0652..45a49793b3b5135d3a316a25e87e3a5af1211711 100644 (file)
--- 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.
 
index 152ea215f37ef4766d51fbf2bf4091caadda5a1e..dcab35e8434d9c8fa8e7c256d0adbc7fe6382e1b 100644 (file)
@@ -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)) {