[Python-Dev] Possible bugs in test_struct

Collin Winter collinw at gmail.com
Thu Mar 29 21:50:14 CEST 2007

While converting test_struct to use unittest, I came across these two issues:

1) r51119 by Bob Ippolito added a test case for "SF bug 1530559:
struct.pack raises TypeError where it used to convert." (Handy diff at
 As far as I can tell, this test doesn't work, thanks to the lines

check_float_coerce = with_warning_restore(deprecated_err)
def test_1530559():
    for endian in ('', '>', '<'):
        for fmt in ('B', 'H', 'I', 'L', 'b', 'h', 'i', 'l'):
            check_float_coerce(endian + fmt, 1.0)
            check_float_coerce(endian + fmt, 1.5)

The problem is that the calls to check_float_coerce() attempt to call
the string produced by "endian + fmt", so a TypeError will always be

Fixing the test to do what was actually intended causes a TestFailed
error to be raised with the message "did not raise error for float
coerce". I'm confused as to why this is a test failure, when it seems
like "rais[ing an] error for float coerce" is exactly what we're
trying to avoid. Anyone know what's going on here?

2) test_705836() features the code

    big = (1 << 25) - 1
    big = math.ldexp(big, 127 - 24)
        packed = struct.pack(">f", big)
    except OverflowError:
        TestFailed("expected OverflowError")

Actually raising the TestFailed exception (as opposed to just
instantiating it) causes a test failure. Is this a bug in struct or a
bad test?

Collin Winter

More information about the Python-Dev mailing list