From 5dc1d389d1c6dfc495e6267043fd6bb344347621 Mon Sep 17 00:00:00 2001 From: Jeff Epler Date: Tue, 21 Nov 2023 08:45:01 -0600 Subject: [PATCH] tests: restore getenv_int tests --- ports/unix/main.c | 30 ++++++++++++++++++++++++++++++ tests/circuitpython/getenv.py | 2 +- 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/ports/unix/main.c b/ports/unix/main.c index cc876970af..c24be9624b 100644 --- a/ports/unix/main.c +++ b/ports/unix/main.c @@ -54,6 +54,34 @@ #include "genhdr/mpversion.h" #include "input.h" +#if defined(MICROPY_UNIX_COVERAGE) // CIRCUITPY-CHANGE +#include "py/objstr.h" +typedef int os_getenv_err_t; +mp_obj_t common_hal_os_getenv(const char *key, mp_obj_t default_); +os_getenv_err_t common_hal_os_getenv_str(const char *key, char *value, size_t value_len); +os_getenv_err_t common_hal_os_getenv_int(const char *key, mp_int_t *value); + +STATIC mp_obj_t mod_os_getenv_int(mp_obj_t var_in) { + mp_int_t value; + os_getenv_err_t result = common_hal_os_getenv_int(mp_obj_str_get_str(var_in), &value); + if (result == 0) { + return mp_obj_new_int(value); + } + return mp_const_none; +} +MP_DEFINE_CONST_FUN_OBJ_1(mod_os_getenv_int_obj, mod_os_getenv_int); + +STATIC mp_obj_t mod_os_getenv_str(mp_obj_t var_in) { + char buf[4096]; + os_getenv_err_t result = common_hal_os_getenv_str(mp_obj_str_get_str(var_in), buf, sizeof(buf)); + if (result == 0) { + return mp_obj_new_str_copy(&mp_type_str, (byte *)buf, strlen(buf)); + } + return mp_const_none; +} +MP_DEFINE_CONST_FUN_OBJ_1(mod_os_getenv_str_obj, mod_os_getenv_str); +#endif + // Command line options, with their defaults STATIC bool compile_only = false; STATIC uint emit_opt = MP_EMIT_OPT_NONE; @@ -596,6 +624,8 @@ MP_NOINLINE int main_(int argc, char **argv) { // CIRCUITPY-CHANGE: test native base classes work as needed by CircuitPython libraries. extern const mp_obj_type_t native_base_class_type; mp_store_global(MP_QSTR_NativeBaseClass, MP_OBJ_FROM_PTR(&native_base_class_type)); + mp_store_global(MP_QSTR_getenv_int, MP_OBJ_FROM_PTR(&mod_os_getenv_int_obj)); + mp_store_global(MP_QSTR_getenv_str, MP_OBJ_FROM_PTR(&mod_os_getenv_str_obj)); } #endif diff --git a/tests/circuitpython/getenv.py b/tests/circuitpython/getenv.py index 857968b45b..1bd2ea5b65 100644 --- a/tests/circuitpython/getenv.py +++ b/tests/circuitpython/getenv.py @@ -82,7 +82,7 @@ for i in range(13): run_test(f"key{i}", content_good) run_test(f"K", b"K = 7\r\n") -print(os.getenv_int("K")) +print(getenv_int("K")) # Test value without trailing newline run_test(f"noeol", b"noeol=3")