Skip to content

Commit 81794fc

Browse files
committed
py/binary: Add support for array('q') and array('Q').
1 parent 22602cc commit 81794fc

File tree

2 files changed

+30
-8
lines changed

2 files changed

+30
-8
lines changed

py/binary.c

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -130,12 +130,12 @@ mp_obj_t mp_binary_get_val_array(char typecode, void *p, mp_uint_t index) {
130130
return mp_obj_new_int(((long*)p)[index]);
131131
case 'L':
132132
return mp_obj_new_int_from_uint(((unsigned long*)p)[index]);
133-
#if MICROPY_LONGINT_IMPL != MICROPY_LONGINT_IMPL_NONE
133+
#if MICROPY_LONGINT_IMPL != MICROPY_LONGINT_IMPL_NONE
134134
case 'q':
135-
case 'Q':
136-
// TODO: Explode API more to cover signedness
137135
return mp_obj_new_int_from_ll(((long long*)p)[index]);
138-
#endif
136+
case 'Q':
137+
return mp_obj_new_int_from_ull(((unsigned long long*)p)[index]);
138+
#endif
139139
#if MICROPY_PY_BUILTINS_FLOAT
140140
case 'f':
141141
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
316316
((mp_obj_t*)p)[index] = val_in;
317317
break;
318318
default:
319+
#if MICROPY_LONGINT_IMPL != MICROPY_LONGINT_IMPL_NONE
320+
if ((typecode | 0x20) == 'q' && MP_OBJ_IS_TYPE(val_in, &mp_type_int)) {
321+
mp_obj_int_to_bytes_impl(val_in, MP_ENDIANNESS_BIG,
322+
sizeof(long long), (byte*)&((long long*)p)[index]);
323+
return;
324+
}
325+
#endif
319326
mp_binary_set_val_array_from_int(typecode, p, index, mp_obj_get_int(val_in));
320327
}
321328
}
@@ -347,13 +354,13 @@ void mp_binary_set_val_array_from_int(char typecode, void *p, mp_uint_t index, m
347354
case 'L':
348355
((unsigned long*)p)[index] = val;
349356
break;
350-
#if MICROPY_LONGINT_IMPL != MICROPY_LONGINT_IMPL_NONE
357+
#if MICROPY_LONGINT_IMPL != MICROPY_LONGINT_IMPL_NONE
351358
case 'q':
352-
case 'Q':
353-
assert(0);
354359
((long long*)p)[index] = val;
360+
case 'Q':
361+
((unsigned long long*)p)[index] = val;
355362
break;
356-
#endif
363+
#endif
357364
#if MICROPY_PY_BUILTINS_FLOAT
358365
case 'f':
359366
((float*)p)[index] = val;

tests/basics/array_q.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# test array('q') and array('Q')
2+
3+
from array import array
4+
5+
print(array('q'))
6+
print(array('Q'))
7+
8+
print(array('q', [0]))
9+
print(array('Q', [0]))
10+
11+
print(array('q', [-2**63, -1, 0, 1, 2, 2**63-1]))
12+
print(array('Q', [0, 1, 2, 2**64-1]))
13+
14+
print(bytes(array('q', [-1])))
15+
print(bytes(array('Q', [2**64-1])))

0 commit comments

Comments
 (0)