Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit de73c45

Browse files
committed
don't ignore exceptions from PyObject_IsTrue
1 parent e789016 commit de73c45

2 files changed

Lines changed: 14 additions & 2 deletions

File tree

Lib/test/test_struct.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -475,6 +475,9 @@ def test_unpack_with_buffer(self):
475475
self.assertEqual(value, 0x12345678)
476476

477477
def test_bool(self):
478+
class ExplodingBool(object):
479+
def __bool__(self):
480+
raise IOError
478481
for prefix in tuple("<>!=")+('',):
479482
false = (), [], [], '', 0
480483
true = [1], 'test', 5, -1, 0xffffffff+1, 0xffffffff/2
@@ -503,6 +506,9 @@ def test_bool(self):
503506
self.assertFalse(prefix, msg='encoded bool is not one byte: %r'
504507
%packed)
505508

509+
self.assertRaises(IOError, struct.pack, prefix + '?',
510+
ExplodingBool())
511+
506512
for c in [b'\x01', b'\x7f', b'\xff', b'\x0f', b'\xf0']:
507513
self.assertTrue(struct.unpack('>?', c)[0])
508514

Modules/_struct.c

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -591,9 +591,13 @@ np_ulonglong(char *p, PyObject *v, const formatdef *f)
591591
static int
592592
np_bool(char *p, PyObject *v, const formatdef *f)
593593
{
594-
BOOL_TYPE y;
594+
int y;
595+
BOOL_TYPE x;
595596
y = PyObject_IsTrue(v);
596-
memcpy(p, (char *)&y, sizeof y);
597+
if (y < 0)
598+
return -1;
599+
x = y;
600+
memcpy(p, (char *)&x, sizeof x);
597601
return 0;
598602
}
599603

@@ -865,6 +869,8 @@ bp_bool(char *p, PyObject *v, const formatdef *f)
865869
{
866870
char y;
867871
y = PyObject_IsTrue(v);
872+
if (y < 0)
873+
return -1;
868874
memcpy(p, (char *)&y, sizeof y);
869875
return 0;
870876
}

0 commit comments

Comments
 (0)