[Python-Dev] Unclear on the way forward with unsigned integers (original) (raw)

Thomas Wouters thomas@xs4all.net
Mon, 7 Oct 2002 03:01:02 +0200


On Mon, Oct 07, 2002 at 09:39:17AM +1000, Mark Hammond wrote:

Assume my extension module has an unsigned integer it wishes to return to Python. Further, assume that this unsigned integer is not really an integer as such, but more a set of bits, or some other value "encoded" in 32 bits, such as an enum. (To put it another way, the "signedness" of this value seems more random than chosen)

I see 2 choices for returning this value:

* Use PyIntFromLong() - this will give me a signed Python integer, but with an identical bit pattern.

If you intend to hand out the bitvector for informational purposes, and aren't too worried (because of your excellent documentational powers) about users messing up the bitvector in incorrect calculations before passing it back, or about them being dazzled by the concept of bitvectors as a whole, go for it. I'm personally still in that phase where I really like ANDing and ORing, even in Python :)

* Use PyLongFromUnsignedLong() - this function will correctly be signed, but may no longer fit in 32 bits.

You mean on the way back, I presume ?

Now, I think I am trying to stay too close to the hardware for a language like Python,

Yes, I think you are, actually. Converting those cute little 32 bits into a real Python object representing it more verbosely, so to speak, might seem a waste, but makes it a lot more user-friendly. It doesn't have to be a type on its own, it can be a tuple of strings or ints, depending on what kind of info you want to return.

If it's really a plain bitvector, you can also consider returning a string. A hobby project of mine, a type of MUD, converts internal bitvectors to strings where each letter from 'a' to 'F' represents a bit in the bitvector. So 0x8a would come out as "bdh". This is very suited to the project itself, since the internal language (MUSHcode, an ass-backwards retarded vaguely-lisplike death-trap of a recursively-parsed scripting language) has no bitwise operators, and the typical MUSHcoder has trouble grasping bitwise arithmatic anyway. Not that MUSHcode has an efficient way to say '"a" in "abc"', mind you, but they manage to do it in less than 5 functioncalls. (And yes, I am trying to replace MUSHcode with Python.)

I should point out that neither the bitvector-to-string code (and its reverse counterpart) nor the braindead internal language are mine :) I just inherited the project. But the bitvector routines are simple, I'm sure you'll have no trouble figuring them out ;) When using them in Python code you might want to add the flag-characters as constants in your module, so the test becomes:

if ce.hasdisk in ce.systemflags():

Insomaniac'ly y'rs,

-- Thomas Wouters <thomas@xs4all.net>

Hi! I'm a .signature virus! copy me into your .signature file to help me spread!