Commit 81794fcd authored by Damien George's avatar Damien George
Browse files

py/binary: Add support for array('q') and array('Q').

parent 22602cc3
...@@ -130,12 +130,12 @@ mp_obj_t mp_binary_get_val_array(char typecode, void *p, mp_uint_t index) { ...@@ -130,12 +130,12 @@ mp_obj_t mp_binary_get_val_array(char typecode, void *p, mp_uint_t index) {
return mp_obj_new_int(((long*)p)[index]); return mp_obj_new_int(((long*)p)[index]);
case 'L': case 'L':
return mp_obj_new_int_from_uint(((unsigned long*)p)[index]); return mp_obj_new_int_from_uint(((unsigned long*)p)[index]);
#if MICROPY_LONGINT_IMPL != MICROPY_LONGINT_IMPL_NONE #if MICROPY_LONGINT_IMPL != MICROPY_LONGINT_IMPL_NONE
case 'q': case 'q':
case 'Q':
// TODO: Explode API more to cover signedness
return mp_obj_new_int_from_ll(((long long*)p)[index]); return mp_obj_new_int_from_ll(((long long*)p)[index]);
#endif case 'Q':
return mp_obj_new_int_from_ull(((unsigned long long*)p)[index]);
#endif
#if MICROPY_PY_BUILTINS_FLOAT #if MICROPY_PY_BUILTINS_FLOAT
case 'f': case 'f':
return mp_obj_new_float(((float*)p)[index]); return mp_obj_new_float(((float*)p)[index]);
...@@ -316,6 +316,13 @@ void mp_binary_set_val_array(char typecode, void *p, mp_uint_t index, mp_obj_t v ...@@ -316,6 +316,13 @@ void mp_binary_set_val_array(char typecode, void *p, mp_uint_t index, mp_obj_t v
((mp_obj_t*)p)[index] = val_in; ((mp_obj_t*)p)[index] = val_in;
break; break;
default: default:
#if MICROPY_LONGINT_IMPL != MICROPY_LONGINT_IMPL_NONE
if ((typecode | 0x20) == 'q' && MP_OBJ_IS_TYPE(val_in, &mp_type_int)) {
mp_obj_int_to_bytes_impl(val_in, MP_ENDIANNESS_BIG,
sizeof(long long), (byte*)&((long long*)p)[index]);
return;
}
#endif
mp_binary_set_val_array_from_int(typecode, p, index, mp_obj_get_int(val_in)); mp_binary_set_val_array_from_int(typecode, p, index, mp_obj_get_int(val_in));
} }
} }
...@@ -347,13 +354,13 @@ void mp_binary_set_val_array_from_int(char typecode, void *p, mp_uint_t index, m ...@@ -347,13 +354,13 @@ void mp_binary_set_val_array_from_int(char typecode, void *p, mp_uint_t index, m
case 'L': case 'L':
((unsigned long*)p)[index] = val; ((unsigned long*)p)[index] = val;
break; break;
#if MICROPY_LONGINT_IMPL != MICROPY_LONGINT_IMPL_NONE #if MICROPY_LONGINT_IMPL != MICROPY_LONGINT_IMPL_NONE
case 'q': case 'q':
case 'Q':
assert(0);
((long long*)p)[index] = val; ((long long*)p)[index] = val;
case 'Q':
((unsigned long long*)p)[index] = val;
break; break;
#endif #endif
#if MICROPY_PY_BUILTINS_FLOAT #if MICROPY_PY_BUILTINS_FLOAT
case 'f': case 'f':
((float*)p)[index] = val; ((float*)p)[index] = val;
......
# test array('q') and array('Q')
from array import array
print(array('q'))
print(array('Q'))
print(array('q', [0]))
print(array('Q', [0]))
print(array('q', [-2**63, -1, 0, 1, 2, 2**63-1]))
print(array('Q', [0, 1, 2, 2**64-1]))
print(bytes(array('q', [-1])))
print(bytes(array('Q', [2**64-1])))
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment