Author mark.dickinson
Recipients mark.dickinson
Date 2010-04-03.11:51:45
SpamBayes Score 2.0095e-14
Marked as misclassified No
Message-id <1270295507.67.0.95768179333.issue8300@psf.upfronthosting.co.za>
In-reply-to
Content
In Python 2.7, struct.pack with an integer format can handle non-integers that provide an __int__ method (although this *does* raise a DeprecationWarning).

Python 2.7a4+ (trunk:79659:79661, Apr  3 2010, 11:28:19) 
[GCC 4.2.1 (Apple Inc. build 5646) (dot 1)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from struct import pack
[35194 refs]
>>> pack('L', 3.1415)
'\x03\x00\x00\x00\x00\x00\x00\x00'
[35210 refs]

This behaviour isn't particularly desirable for floats or Decimal instances, but it's useful for integer-like objects.

In Python 3.x, there's no provision for handling integer-like objects than aren't actually integers.

I propose that in 3.x, struct.pack should try to convert any non-integer to an integer by using its __index__ method, before packing.
History
Date User Action Args
2010-04-03 11:51:47mark.dickinsonsetrecipients: + mark.dickinson
2010-04-03 11:51:47mark.dickinsonsetmessageid: <1270295507.67.0.95768179333.issue8300@psf.upfronthosting.co.za>
2010-04-03 11:51:46mark.dickinsonlinkissue8300 messages
2010-04-03 11:51:45mark.dickinsoncreate