From: Benjamin Peterson Date: Wed, 7 Jul 2010 19:03:36 +0000 (+0000) Subject: Merged revisions 82628,82630 via svnmerge from X-Git-Tag: v2.7.1rc1~637 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=489113fd5f2e8e967d7c297697f951ec97c8b715;p=python Merged revisions 82628,82630 via svnmerge from 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 ........ --- diff --git a/Lib/test/test_struct.py b/Lib/test/test_struct.py index ce2c22da78..1877192be8 100644 --- a/Lib/test/test_struct.py +++ b/Lib/test/test_struct.py @@ -491,6 +491,9 @@ class StructTest(unittest.TestCase): self.test_unpack_from(cls=buffer) def test_bool(self): + class ExplodingBool(object): + def __nonzero__(self): + raise IOError for prefix in tuple("<>!=")+('',): false = (), [], [], '', 0 true = [1], 'test', 5, -1, 0xffffffffL+1, 0xffffffff//2 @@ -519,8 +522,11 @@ class StructTest(unittest.TestCase): self.assertFalse(prefix, msg='encoded bool is not one byte: %r' %packed) - for c in '\x01\x7f\xff\x0f\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]) @unittest.skipUnless(IS32BIT, "Specific to 32bit machines") def test_crasher(self): diff --git a/Modules/_struct.c b/Modules/_struct.c index 2eb461f4f9..52c5eeb170 100644 --- a/Modules/_struct.c +++ b/Modules/_struct.c @@ -636,9 +636,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; } @@ -910,6 +914,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; }