[ python-Feature Requests-887237 ] Machine integers

SourceForge.net noreply at sourceforge.net
Fri Jan 30 09:16:11 EST 2004


Feature Requests item #887237, was opened at 2004-01-29 21:34
Message generated for change (Comment added) made by theller
You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=355470&aid=887237&group_id=5470

Category: Python Library
Group: None
Status: Open
Resolution: None
Priority: 5
Submitted By: Fredrik Johansson (fredrik_j)
Assigned to: Nobody/Anonymous (nobody)
Summary: Machine integers

Initial Comment:
I think it'd be useful with a built-in module (I
propose the name 'mint' :), providing classes for
emulating machine ints. There should be support for
ints of different sizes (at least 8, 16, 32, 64),
signedness and endianness. The machine ints would react
to overflow (negative values) and bit-level operations
in the same way their C equivalents would.

One idea for a feature would be a intobj.pack() (or
str() or whatever) method that could be used instead of
the struct module's pack(). int16(12345).pack() ->
"\x30\x39". int32(-1).pack() -> "\0xff\0xff\0xff\0xff",
etc.

A related idea is that these ints could provide slicing
to extract individual sets of bits or bytes. For
example, uint32()[8:16] could return an int (uint8?)
made from the bits in the int's second byte (whether
it's the second from the left or the right might be
fuzzy, I do realize).

Applications for this would be algorithmic code
involving bit fiddling (useful for cryptographic
applications, perhaps) and, obviously, programs that
have to operate on any binary data.  It might also
resolve some issues related to the unification of longs
and ints.

----------------------------------------------------------------------

>Comment By: Thomas Heller (theller)
Date: 2004-01-30 15:16

Message:
Logged In: YES 
user_id=11105

You might look at the ctypes module (google for it) - it
implements mutable C compatible data types.

----------------------------------------------------------------------

Comment By: Fredrik Johansson (fredrik_j)
Date: 2004-01-30 15:08

Message:
Logged In: YES 
user_id=756835

As for mainstream uses, I think replacing struct.pack would
be one.

----------------------------------------------------------------------

Comment By: Raymond Hettinger (rhettinger)
Date: 2004-01-30 01:35

Message:
Logged In: YES 
user_id=80475

FTI, there was a related feature request,
www.python.org/sf/846568 for treating numbers as bitarrays.
 As proposed, it was invalid because numbers in Python are
immutable (no bit assignments allowed).

That could be combined with this idea generic numeric
register class that optionally specifies size (if bound),
base, endianess, etc.  

bcdreg = register(places=5, base=10)
bcdreg.assign(697)
bcdreg[-1] --> 7
bcdreg.multiply(1000) --> 97000

None of this is for the mainstream user and it would be
unlikely to ever be included in the standard library.  The
best approach would be to experiment with an API in pure
python, perhaps publish it as a recipe, and if it is well
received, put a C version on the vaults of parnassus.



----------------------------------------------------------------------

You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=355470&aid=887237&group_id=5470



More information about the Python-bugs-list mailing list