tests: restore getenv_int tests

This commit is contained in:
Jeff Epler 2023-11-21 08:45:01 -06:00
parent c92ad33a9c
commit 5dc1d389d1
No known key found for this signature in database
GPG Key ID: D5BF15AB975AB4DE
2 changed files with 31 additions and 1 deletions

View File

@ -54,6 +54,34 @@
#include "genhdr/mpversion.h" #include "genhdr/mpversion.h"
#include "input.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 // Command line options, with their defaults
STATIC bool compile_only = false; STATIC bool compile_only = false;
STATIC uint emit_opt = MP_EMIT_OPT_NONE; 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. // CIRCUITPY-CHANGE: test native base classes work as needed by CircuitPython libraries.
extern const mp_obj_type_t native_base_class_type; 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_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 #endif

View File

@ -82,7 +82,7 @@ for i in range(13):
run_test(f"key{i}", content_good) run_test(f"key{i}", content_good)
run_test(f"K", b"K = 7\r\n") run_test(f"K", b"K = 7\r\n")
print(os.getenv_int("K")) print(getenv_int("K"))
# Test value without trailing newline # Test value without trailing newline
run_test(f"noeol", b"noeol=3") run_test(f"noeol", b"noeol=3")