[issue8300] Allow struct.pack to handle objects with an __index__ method.

Mark Dickinson report at bugs.python.org
Sat Apr 3 15:57:09 CEST 2010


Mark Dickinson <dickinsm at gmail.com> added the comment:

That patch was a bit hasty in many respects;  here's a better one.

For 2.7, the scheme is as follows:  when packing a non-integer with an integer format:

(1) First __index__ is tried
(2) If the __index__ method doesn't exist, or the call to __index__ raises TypeError, then the __int__ method is tried.
(3) If the __index__ method raises something other than TypeError, or returns a non-integer, then struct.pack fails.

----------
Added file: http://bugs.python.org/file16748/struct_index_trunk2.patch

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue8300>
_______________________________________


More information about the Python-bugs-list mailing list