On Thursday 20 September 2007 11:19, Paul Hankin wrote: >> I suppose I could get the relevant source from the 2.5 source and compile >> it as a custom package, but that wouldn't be very transparent for my >> users, and would probably be getting in way over my head. :) >> >> Ideas? Suggestions? > > Here's a wrapped pack: > > import struct > > pack_double_workaround = { > 'inf': struct.pack('Q', 0x7ff0000000000000L), > '-inf': struct.pack('Q', 0xfff0000000000000L), > 'nan': struct.pack('Q', 0x7ff8000000000000L) > } > > def pack_one_safe(f, a): > if f == 'd' and str(f) in pack_double_workaround: > return pack_double_workaround[str(f)] > return struct.pack(f, a) > > def pack(fmt, *args): > return ''.join(pack_one_safe(f, a) for f, a in zip(fmt, args)) > > Unpacking is similar: unpack doubles with 'Q' and test the > long for equality with +-inf, and find nan's by checking bits > 52 to 62. If the number's ok, unpack again using 'd'. > > You can get python values for nan, -inf and inf by using > float('nan'), float('-inf'), float('inf'). > > I've not been able to properly test this, as struct seems > to work fine in Python 2.3 and 2.4 on MacOS X.
Thanks for the ideas, Paul! I came up with something that works for me, but this has a few ideas that I'm going to implement in my wrapper to make for cleaner code. As to testing it on MacOS X: yeah, it can be a somewhat system-dependent problem, so may not show up on all architectures. Thanks for the tips! j -- Joshua Kugler Lead System Admin -- Senior Programmer http://www.eeinternet.com PGP Key: http://pgp.mit.edu/ ID 0xDB26D7CE -- http://mail.python.org/mailman/listinfo/python-list