ethanfurman / aenum

Advanced Enumerations for Python
183 stars 14 forks source link

Can't set Flag enum from integer if MSB is set with any other bits (Python 2) #11

Closed BrendanSimon closed 3 years ago

BrendanSimon commented 3 years ago

The following code works on Python 3 but not Python 2.

I get the error:

AttributeError: type object 'Permission' has no attribute '__qualname__'

From what I can deduce, the problem is with the most significant bit of the natural integer size of the system (bit-31 for 32-bit system, or bit-63 for 64-bit system). I can set that msb if that's the only bit present in the integer mask, but if others are set then the exception occurs.

On further investigation, anything that is a Python long type (Python 2) is not settable unless it is the only bit in the mask.

from aenum import Flag

class Permission( Flag ):
    EXEC    = 1 << 0
    WRITE   = 1 << 1
    READ    = 1 << 2
    MSB32   = 1 << 31
    MSB64   = 1 << 63

# set individual bits
print( Permission( 0x00000001 ) )
print( Permission( 0x00000002 ) )
print( Permission( 0x00000004 ) )

# set multiple bits
print( Permission( 0x00000007 ) )

# 32-bit system test
print( Permission( 0x80000000 ) )
print( Permission( 0x80000002 ) )

# 64-bit system test
print( Permission( 0x8000000000000000 ) )
print( Permission( 0x8000000000000002 ) )

On Python 2.7 (32-bit arm linux), I get the following output and error message.

# python2 test_aenum.py
Permission.EXEC
Permission.WRITE
Permission.READ
Permission.EXEC|READ|WRITE
Permission.MSB32
Traceback (most recent call last):
  File "test_aenum.py", line 21, in <module>
    print( Permission( 0x80000002 ) )
  File "/tmp/altfs/usr/local/lib/python2.7/dist-packages/aenum/__init__.py", line 2546, in __call__
    return cls.__new__(cls, value)
  File "/tmp/altfs/usr/local/lib/python2.7/dist-packages/aenum/__init__.py", line 2905, in __new__
    raise exc
AttributeError: type object 'Permission' has no attribute '__qualname__'

On Python 3.7 (64-bit linux), I get the following output.

$ python3 test_aenum.py 
Permission.EXEC
Permission.WRITE
Permission.READ
Permission.EXEC|WRITE|READ
Permission.MSB32
Permission.WRITE|MSB32
Permission.MSB64
Permission.WRITE|MSB64
ethanfurman commented 3 years ago

There are several issues here:

The error message was easy enough to fix. Now to figure out how to fix the rest.

ethanfurman commented 3 years ago

Okay, the problem is "fixed":

I didn't see any issues in the Python 3 series.