]> granicus.if.org Git - python/commitdiff
Merged revisions 82628,82630 via svnmerge from
authorBenjamin Peterson <benjamin@python.org>
Wed, 7 Jul 2010 22:46:00 +0000 (22:46 +0000)
committerBenjamin Peterson <benjamin@python.org>
Wed, 7 Jul 2010 22:46:00 +0000 (22:46 +0000)
svn+ssh://pythondev@svn.python.org/python/branches/py3k

........
  r82628 | benjamin.peterson | 2010-07-07 13:44:05 -0500 (Wed, 07 Jul 2010) | 1 line

  this needn't be in the loop
........
  r82630 | benjamin.peterson | 2010-07-07 13:54:59 -0500 (Wed, 07 Jul 2010) | 1 line

  don't ignore exceptions from PyObject_IsTrue
........

Lib/test/test_struct.py
Modules/_struct.c

index 603289511cfe1dce7e59fb0cd2e054458d48e7b1..88404629e0c23566553a73616750484d31e44b7a 100644 (file)
@@ -480,6 +480,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
@@ -508,8 +511,11 @@ class StructTest(unittest.TestCase):
                 self.assertFalse(prefix, msg='encoded bool is not one byte: %r'
                                              %packed)
 
-            for c in [b'\x01', b'\x7f', b'\xff', b'\x0f', b'\xf0']:
-                self.assertTrue(struct.unpack('>?', c)[0])
+            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])
 
     def test_count_overflow(self):
         hugecount = '{}b'.format(sys.maxsize+1)
index f629817dcb26b65d748a835e35ac9e063fa4ccaa..ba8a8ed979e24b82d670c483902bd6ba3ec7c61e 100644 (file)
@@ -580,9 +580,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;
 }
 
@@ -854,6 +858,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;
 }