From 8775caf9f1c97ecc64f22e355b670ba74d5bd098 Mon Sep 17 00:00:00 2001 From: Paul Sokolovsky Date: Fri, 6 Feb 2015 00:18:11 +0200 Subject: [PATCH] modffi: Add .addr() method to just get symbol address. --- unix/modffi.c | 13 +++++++++++++ unix/qstrdefsport.h | 1 + 2 files changed, 14 insertions(+) diff --git a/unix/modffi.c b/unix/modffi.c index 6e0f06ec77..522e9cb884 100644 --- a/unix/modffi.c +++ b/unix/modffi.c @@ -274,6 +274,18 @@ STATIC mp_obj_t ffimod_var(mp_obj_t self_in, mp_obj_t vartype_in, mp_obj_t symna } MP_DEFINE_CONST_FUN_OBJ_3(ffimod_var_obj, ffimod_var); +STATIC mp_obj_t ffimod_addr(mp_obj_t self_in, mp_obj_t symname_in) { + mp_obj_ffimod_t *self = self_in; + const char *symname = mp_obj_str_get_str(symname_in); + + void *sym = dlsym(self->handle, symname); + if (sym == NULL) { + nlr_raise(mp_obj_new_exception_arg1(&mp_type_OSError, MP_OBJ_NEW_SMALL_INT(errno))); + } + return mp_obj_new_int((mp_int_t)sym); +} +MP_DEFINE_CONST_FUN_OBJ_2(ffimod_addr_obj, ffimod_addr); + STATIC mp_obj_t ffimod_make_new(mp_obj_t type_in, mp_uint_t n_args, mp_uint_t n_kw, const mp_obj_t *args) { (void)n_args; (void)n_kw; @@ -296,6 +308,7 @@ STATIC mp_obj_t ffimod_make_new(mp_obj_t type_in, mp_uint_t n_args, mp_uint_t n_ STATIC const mp_map_elem_t ffimod_locals_dict_table[] = { { MP_OBJ_NEW_QSTR(MP_QSTR_func), (mp_obj_t) &ffimod_func_obj }, { MP_OBJ_NEW_QSTR(MP_QSTR_var), (mp_obj_t) &ffimod_var_obj }, + { MP_OBJ_NEW_QSTR(MP_QSTR_addr), (mp_obj_t) &ffimod_addr_obj }, { MP_OBJ_NEW_QSTR(MP_QSTR_close), (mp_obj_t) &ffimod_close_obj }, }; diff --git a/unix/qstrdefsport.h b/unix/qstrdefsport.h index 873d7d17ae..12f4f26bf6 100644 --- a/unix/qstrdefsport.h +++ b/unix/qstrdefsport.h @@ -46,6 +46,7 @@ Q(fficallback) Q(ffivar) Q(as_bytearray) Q(callback) +Q(addr) Q(func) Q(var) Q(get)