RPython: Is it possible to specify/enforce a type on a variable?

181 Views Asked by At

I'm using os.read() to read a files' binary contents into a list. PyPy seems to have determined that all elements of the list will be of type int. Would it be possible to explicitly annotate the variable such that all elements will be considered to be of type uint64?

Edit:

# Converts 8 characters into a 64 bit unsigned integer
def b64(b):
    v = 0
    for i in range(8):
        v |= ord(b[8-1-i])<<(i*8)

    return v

# The file consists of 64-bit values
# The first value is the length of the file
# Then follow exactly that number of 64-bit values
# The latter are stored in a list, and returned from the function
def unpack(data):
    l = b64(data)
    result = []
    offset = 8
    for i in range(l):
        value = b64(data[offset:offset+8])
        assert value >= 0
        result.append(value)
        offset += 8
    return result

f = os.open(argv[1], os.O_RDONLY)
data = os.read(f, 2**32)
os.close(f)

unpacked = unpack(data)

The result returned from unpack() should be a list of unsigned 64-bit values, but right now it seems to assume that they are signed 32-bit values. I've tried to use the assert, but to no effect.

I need something like result = SomeList<UInt64> or similar. Later, I transform this list into a list of list of UInt64 and need to ensure the same (the semantics of my VM depend on it).

1

There are 1 best solutions below

0
Armin Rigo On

Plain Python integers, in RPython, turn into signed values of N bits, where N = 32 or 64 depending on the pointer size:

http://rpython.readthedocs.io/en/latest/rpython.html#integer-types

If you need something outside that range, use r_uint or variants like r_uint64.