]> granicus.if.org Git - python/commitdiff
don't ignore exceptions from PyObject_IsTrue
authorBenjamin Peterson <benjamin@python.org>
Wed, 7 Jul 2010 18:54:59 +0000 (18:54 +0000)
committerBenjamin Peterson <benjamin@python.org>
Wed, 7 Jul 2010 18:54:59 +0000 (18:54 +0000)
Lib/test/test_struct.py
Modules/_struct.c

index 844828699374debeac491da3c9c1f1dae1793a1b..3168a7b5560921ad43db738758b521730a5084cf 100644 (file)
@@ -475,6 +475,9 @@ class StructTest(unittest.TestCase):
             self.assertEqual(value, 0x12345678)
 
     def test_bool(self):
+        class ExplodingBool(object):
+            def __bool__(self):
+                raise IOError
         for prefix in tuple("<>!=")+('',):
             false = (), [], [], '', 0
             true = [1], 'test', 5, -1, 0xffffffff+1, 0xffffffff/2
@@ -503,6 +506,9 @@ class StructTest(unittest.TestCase):
                 self.assertFalse(prefix, msg='encoded bool is not one byte: %r'
                                              %packed)
 
+            self.assertRaises(IOError, struct.pack, prefix + '?',
+                              ExplodingBool())
+
         for c in [b'\x01', b'\x7f', b'\xff', b'\x0f', b'\xf0']:
             self.assertTrue(struct.unpack('>?', c)[0])
 
index e1d016b0f5bf3c6fcde4ba90fc794c4c2f9f064b..b5f18a96adb22eaee448af7a0a360dd05e9c661b 100644 (file)
@@ -591,9 +591,13 @@ np_ulonglong(char *p, PyObject *v, const formatdef *f)
 static int
 np_bool(char *p, PyObject *v, const formatdef *f)
 {
-    BOOL_TYPE y;
+    int y;
+    BOOL_TYPE x;
     y = PyObject_IsTrue(v);
-    memcpy(p, (char *)&y, sizeof y);
+    if (y < 0)
+        return -1;
+    x = y;
+    memcpy(p, (char *)&x, sizeof x);
     return 0;
 }
 
@@ -865,6 +869,8 @@ bp_bool(char *p, PyObject *v, const formatdef *f)
 {
     char y;
     y = PyObject_IsTrue(v);
+    if (y < 0)
+        return -1;
     memcpy(p, (char *)&y, sizeof y);
     return 0;
 }