summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--Lib/test/test_struct.py6
-rw-r--r--Modules/_struct.c10
2 files changed, 14 insertions, 2 deletions
diff --git a/Lib/test/test_struct.py b/Lib/test/test_struct.py
index 8448286..3168a7b 100644
--- a/Lib/test/test_struct.py
+++ b/Lib/test/test_struct.py
@@ -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])
diff --git a/Modules/_struct.c b/Modules/_struct.c
index e1d016b..b5f18a9 100644
--- a/Modules/_struct.c
+++ b/Modules/_struct.c
@@ -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;
}