Skip to content

Commit 8bc3516

Browse files
committed
ffi: Implement ffivar.get()/set() methods.
Done by introducing another factored out helper API in binary.c. This API can be reused also by array and struct modules.
1 parent 70d7a83 commit 8bc3516

File tree

7 files changed

+130
-2
lines changed

7 files changed

+130
-2
lines changed

examples/unix/ffi_example.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
perror("ffi before error")
2020
open("somethingnonexistent__", 0)
2121
print(errno)
22+
print(errno.get())
2223
perror("ffi after error")
2324
print()
2425

py/binary.c

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
#include <stdlib.h>
2+
#include <stdint.h>
3+
#include <assert.h>
4+
5+
#include "misc.h"
6+
#include "mpconfig.h"
7+
#include "qstr.h"
8+
#include "obj.h"
9+
#include "objint.h"
10+
#include "binary.h"
11+
12+
// Helpers to work with binary-encoded data
13+
14+
mp_obj_t mp_binary_get_val(char typecode, void *p, int index) {
15+
int val = 0;
16+
switch (typecode) {
17+
case 'b':
18+
val = ((int8_t*)p)[index];
19+
break;
20+
case BYTEARRAY_TYPECODE:
21+
case 'B':
22+
val = ((uint8_t*)p)[index];
23+
break;
24+
case 'h':
25+
val = ((int16_t*)p)[index];
26+
break;
27+
case 'H':
28+
val = ((uint16_t*)p)[index];
29+
break;
30+
case 'i':
31+
case 'l':
32+
return mp_obj_new_int(((int32_t*)p)[index]);
33+
case 'I':
34+
case 'L':
35+
return mp_obj_new_int_from_uint(((uint32_t*)p)[index]);
36+
#if MICROPY_LONGINT_IMPL != MICROPY_LONGINT_IMPL_NONE
37+
case 'q':
38+
case 'Q':
39+
// TODO: Explode API more to cover signedness
40+
return mp_obj_new_int_from_ll(((long long*)p)[index]);
41+
#endif
42+
#if MICROPY_ENABLE_FLOAT
43+
case 'f':
44+
return mp_obj_new_float(((float*)p)[index]);
45+
case 'd':
46+
return mp_obj_new_float(((double*)p)[index]);
47+
#endif
48+
}
49+
return MP_OBJ_NEW_SMALL_INT(val);
50+
}
51+
52+
void mp_binary_set_val(char typecode, void *p, int index, mp_obj_t val_in) {
53+
machine_int_t val = 0;
54+
if (MP_OBJ_IS_INT(val_in)) {
55+
val = mp_obj_int_get(val_in);
56+
}
57+
58+
switch (typecode) {
59+
case 'b':
60+
((int8_t*)p)[index] = val;
61+
break;
62+
case BYTEARRAY_TYPECODE:
63+
case 'B':
64+
val = ((uint8_t*)p)[index] = val;
65+
break;
66+
case 'h':
67+
val = ((int16_t*)p)[index] = val;
68+
break;
69+
case 'H':
70+
val = ((uint16_t*)p)[index] = val;
71+
break;
72+
case 'i':
73+
case 'l':
74+
((int32_t*)p)[index] = val;
75+
break;
76+
case 'I':
77+
case 'L':
78+
((uint32_t*)p)[index] = val;
79+
break;
80+
#if MICROPY_LONGINT_IMPL != MICROPY_LONGINT_IMPL_NONE
81+
case 'q':
82+
case 'Q':
83+
assert(0);
84+
((long long*)p)[index] = val;
85+
break;
86+
#endif
87+
#if MICROPY_ENABLE_FLOAT
88+
case 'f':
89+
((float*)p)[index] = mp_obj_float_get(val_in);
90+
break;
91+
case 'd':
92+
((double*)p)[index] = mp_obj_float_get(val_in);
93+
break;
94+
#endif
95+
}
96+
}

py/binary.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// Use special typecode to differentiate repr() of bytearray vs array.array('B')
2+
// (underlyingly they're same).
3+
#define BYTEARRAY_TYPECODE 0
4+
5+
int mp_binary_get_size(char typecode);
6+
mp_obj_t mp_binary_get_val(char typecode, void *p, int index);
7+
void mp_binary_set_val(char typecode, void *p, int index, mp_obj_t val_in);

py/objint.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,7 @@ typedef struct _mp_obj_int_t {
88
void int_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t self_in, mp_print_kind_t kind);
99
mp_obj_t int_unary_op(int op, mp_obj_t o_in);
1010
mp_obj_t int_binary_op(int op, mp_obj_t lhs_in, mp_obj_t rhs_in);
11+
12+
#if MICROPY_LONGINT_IMPL != MICROPY_LONGINT_IMPL_NONE
13+
mp_obj_t mp_obj_new_int_from_ll(long long val);
14+
#endif

py/objint_longlong.c

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@
1313

1414
#if MICROPY_LONGINT_IMPL == MICROPY_LONGINT_IMPL_LONGLONG
1515

16-
STATIC mp_obj_t mp_obj_new_int_from_ll(long long val);
17-
1816
// Python3 no longer has "l" suffix for long ints. We allow to use it
1917
// for debugging purpose though.
2018
#ifdef DEBUG

py/py.mk

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ PY_O_BASENAME = \
6363
objzip.o \
6464
sequence.o \
6565
stream.o \
66+
binary.o \
6667
builtin.o \
6768
builtinimport.o \
6869
builtinevex.o \

unix/ffi.c

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include "qstr.h"
1111
#include "obj.h"
1212
#include "runtime.h"
13+
#include "binary.h"
1314

1415
typedef struct _mp_obj_opaque_t {
1516
mp_obj_base_t base;
@@ -295,10 +296,30 @@ static void ffivar_print(void (*print)(void *env, const char *fmt, ...), void *e
295296
print(env, "<ffivar @%p: 0x%x>", self->var, *(int*)self->var);
296297
}
297298

299+
static mp_obj_t ffivar_get(mp_obj_t self_in) {
300+
mp_obj_ffivar_t *self = self_in;
301+
return mp_binary_get_val(self->type, self->var, 0);
302+
}
303+
MP_DEFINE_CONST_FUN_OBJ_1(ffivar_get_obj, ffivar_get);
304+
305+
static mp_obj_t ffivar_set(mp_obj_t self_in, mp_obj_t val_in) {
306+
mp_obj_ffivar_t *self = self_in;
307+
mp_binary_set_val(self->type, self->var, 0, val_in);
308+
return mp_const_none;
309+
}
310+
MP_DEFINE_CONST_FUN_OBJ_2(ffivar_set_obj, ffivar_set);
311+
312+
static const mp_method_t ffivar_type_methods[] = {
313+
{ "get", &ffivar_get_obj },
314+
{ "set", &ffivar_set_obj },
315+
{ NULL, NULL },
316+
};
317+
298318
static const mp_obj_type_t ffivar_type = {
299319
{ &mp_const_type },
300320
"ffivar",
301321
.print = ffivar_print,
322+
.methods = ffivar_type_methods,
302323
};
303324

304325
// Generic opaque storage object

0 commit comments

Comments
 (0)