drgn/generate_readwrite.py

35 lines
1.0 KiB
Python
Raw Normal View History

#!/usr/bin/env python3
SIZES = [8, 16, 32, 64]
def generate_read(name, read_type, ret_type=None):
if ret_type is None:
ret_type = read_type
print(f"""cdef inline int read_{name}(const char *buffer, Py_ssize_t buffer_size, Py_ssize_t *offset, {ret_type} *ret) except -1:
if not in_bounds(buffer_size, offset[0], sizeof({read_type})):
raise EOFError()
ret[0] = (<const {read_type} *>(buffer + offset[0]))[0]
offset[0] += sizeof({read_type})
return 0""")
if __name__ == '__main__':
first = True
for size in SIZES:
if first:
print('# Automatically generated by generate_readwrite.py')
else:
print()
print()
first = False
generate_read(f'u{size}', f'uint{size}_t')
if size < 64:
print()
print()
generate_read(f'u{size}_into_u64', f'uint{size}_t', f'uint64_t')
print()
print()
generate_read(f'u{size}_into_ssize_t', f'uint{size}_t', f'Py_ssize_t')