Skip to content

Commit 6bbbb1a

Browse files
committed
unix/modffi: Support passing float/double args.
1 parent f1ed8c8 commit 6bbbb1a

File tree

3 files changed

+28
-0
lines changed

3 files changed

+28
-0
lines changed

tests/unix/ffi_float.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,11 @@ def ffi_open(names):
2424

2525
strtod = libc.func("d", "strtod", "sp")
2626
print('%.6f' % strtod('1.23', None))
27+
28+
# test passing double and float args
29+
libm = ffi_open(('libm.so', 'libc.so.0', 'libc.so.6', 'libc.dylib'))
30+
tgamma = libm.func('d', 'tgamma', 'd')
31+
tgammaf = libm.func('f', 'tgammaf', 'f')
32+
for fun in (tgamma, tgammaf):
33+
for val in (0.5, 1, 1.0, 1.5, 4, 4.0):
34+
print('%.6f' % fun(val))

tests/unix/ffi_float.py.exp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,14 @@
11
1.230000
22
1.230000
3+
1.772454
4+
1.000000
5+
1.000000
6+
0.886227
7+
6.000000
8+
6.000000
9+
1.772454
10+
1.000000
11+
1.000000
12+
0.886227
13+
6.000000
14+
6.000000

unix/modffi.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -356,6 +356,14 @@ STATIC mp_obj_t ffifunc_call(mp_obj_t self_in, mp_uint_t n_args, mp_uint_t n_kw,
356356
mp_obj_t a = args[i];
357357
if (*argtype == 'O') {
358358
values[i] = (ffi_arg)a;
359+
#if MICROPY_PY_BUILTINS_FLOAT
360+
} else if (*argtype == 'f') {
361+
float *p = (float*)&values[i];
362+
*p = mp_obj_get_float(a);
363+
} else if (*argtype == 'd') {
364+
double *p = (double*)&values[i];
365+
*p = mp_obj_get_float(a);
366+
#endif
359367
} else if (a == mp_const_none) {
360368
values[i] = 0;
361369
} else if (MP_OBJ_IS_INT(a)) {

0 commit comments

Comments
 (0)