From de8292202edef4adf591c1d2562c509638b6c820 Mon Sep 17 00:00:00 2001 From: Paul Sokolovsky Date: Thu, 17 Apr 2014 18:26:07 +0300 Subject: unix modtime: Adhere to MICROPY_ENABLE_FLOAT better. --- unix/modtime.c | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) (limited to 'unix/modtime.c') diff --git a/unix/modtime.c b/unix/modtime.c index 032528947..daf72ff88 100644 --- a/unix/modtime.c +++ b/unix/modtime.c @@ -11,18 +11,28 @@ #include "runtime.h" STATIC mp_obj_t mod_time_time() { +#if MICROPY_ENABLE_FLOAT + struct timeval tv; + gettimeofday(&tv, NULL); + mp_float_t val = tv.tv_sec + (mp_float_t)tv.tv_usec / 1000000; + return mp_obj_new_float(val); +#else return mp_obj_new_int((machine_int_t)time(NULL)); +#endif } STATIC MP_DEFINE_CONST_FUN_OBJ_0(mod_time_time_obj, mod_time_time); // Note: this is deprecated since CPy3.3, but pystone still uses it. STATIC mp_obj_t mod_time_clock() { -// return mp_obj_new_int((machine_int_t)clock()); - // POSIX requires CLOCKS_PER_SEC equals 1000000, so that's what we assume +#if MICROPY_ENABLE_FLOAT + // POSIX requires CLOCKS_PER_SEC equals 1000000, so that's what we assume. // float cannot represent full range of int32 precisely, so we pre-divide // int to reduce resolution, and then actually do float division hoping // to preserve integer part resolution. return mp_obj_new_float((float)(clock() / 1000) / 1000.0); +#else + return mp_obj_new_int((machine_int_t)clock()); +#endif } STATIC MP_DEFINE_CONST_FUN_OBJ_0(mod_time_clock_obj, mod_time_clock); -- cgit v1.2.3 From eb2fc9787a28554ac74995ab50cf326907f81ff0 Mon Sep 17 00:00:00 2001 From: Paul Sokolovsky Date: Thu, 17 Apr 2014 18:28:38 +0300 Subject: unix modtime: Convert to static module structures. --- unix/main.c | 3 --- unix/modtime.c | 29 +++++++++++++++++++++++------ unix/mpconfigport.h | 4 ++++ unix/qstrdefsport.h | 4 ++++ 4 files changed, 31 insertions(+), 9 deletions(-) (limited to 'unix/modtime.c') diff --git a/unix/main.c b/unix/main.c index b9b8fe0b9..d5b04d565 100644 --- a/unix/main.c +++ b/unix/main.c @@ -357,9 +357,6 @@ int main(int argc, char **argv) { #endif microsocket_init(); -#if MICROPY_MOD_TIME - time_init(); -#endif #if MICROPY_MOD_FFI ffi_init(); #endif diff --git a/unix/modtime.c b/unix/modtime.c index daf72ff88..a0d7cd046 100644 --- a/unix/modtime.c +++ b/unix/modtime.c @@ -51,9 +51,26 @@ STATIC mp_obj_t mod_time_sleep(mp_obj_t arg) { } STATIC MP_DEFINE_CONST_FUN_OBJ_1(mod_time_sleep_obj, mod_time_sleep); -void time_init() { - mp_obj_t m = mp_obj_new_module(QSTR_FROM_STR_STATIC("time")); - mp_store_attr(m, QSTR_FROM_STR_STATIC("time"), (mp_obj_t)&mod_time_time_obj); - mp_store_attr(m, QSTR_FROM_STR_STATIC("clock"), (mp_obj_t)&mod_time_clock_obj); - mp_store_attr(m, QSTR_FROM_STR_STATIC("sleep"), (mp_obj_t)&mod_time_sleep_obj); -} +STATIC const mp_map_elem_t mp_module_time_globals_table[] = { + { MP_OBJ_NEW_QSTR(MP_QSTR___name__), MP_OBJ_NEW_QSTR(MP_QSTR_time) }, + { MP_OBJ_NEW_QSTR(MP_QSTR_clock), (mp_obj_t)&mod_time_clock_obj }, + { MP_OBJ_NEW_QSTR(MP_QSTR_sleep), (mp_obj_t)&mod_time_sleep_obj }, + { MP_OBJ_NEW_QSTR(MP_QSTR_time), (mp_obj_t)&mod_time_time_obj }, +}; + +STATIC const mp_obj_dict_t mp_module_time_globals = { + .base = {&mp_type_dict}, + .map = { + .all_keys_are_qstrs = 1, + .table_is_fixed_array = 1, + .used = sizeof(mp_module_time_globals_table) / sizeof(mp_map_elem_t), + .alloc = sizeof(mp_module_time_globals_table) / sizeof(mp_map_elem_t), + .table = (mp_map_elem_t*)mp_module_time_globals_table, + }, +}; + +const mp_obj_module_t mp_module_time = { + .base = { &mp_type_module }, + .name = MP_QSTR_time, + .globals = (mp_obj_dict_t*)&mp_module_time_globals, +}; diff --git a/unix/mpconfigport.h b/unix/mpconfigport.h index cf6e6fae2..bfbb49aee 100644 --- a/unix/mpconfigport.h +++ b/unix/mpconfigport.h @@ -17,6 +17,10 @@ #define MICROPY_MOD_SYS_STDFILES (1) #define MICROPY_ENABLE_MOD_CMATH (1) +extern const struct _mp_obj_module_t mp_module_time; +#define MICROPY_EXTRA_BUILTIN_MODULES \ + { MP_OBJ_NEW_QSTR(MP_QSTR_time), (mp_obj_t)&mp_module_time }, \ + // type definitions for the specific machine #ifdef __LP64__ diff --git a/unix/qstrdefsport.h b/unix/qstrdefsport.h index 42f20d265..eb61e2a61 100644 --- a/unix/qstrdefsport.h +++ b/unix/qstrdefsport.h @@ -30,3 +30,7 @@ Q(fficallback) Q(ffivar) Q(func) Q(var) + +Q(time) +Q(clock) +Q(sleep) -- cgit v1.2.3