Skip to content

Commit 5dc1d38

Browse files
committed
tests: restore getenv_int tests
1 parent c92ad33 commit 5dc1d38

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-1
lines changed

ports/unix/main.c

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,34 @@
5454
#include "genhdr/mpversion.h"
5555
#include "input.h"
5656

57+
#if defined(MICROPY_UNIX_COVERAGE) // CIRCUITPY-CHANGE
58+
#include "py/objstr.h"
59+
typedef int os_getenv_err_t;
60+
mp_obj_t common_hal_os_getenv(const char *key, mp_obj_t default_);
61+
os_getenv_err_t common_hal_os_getenv_str(const char *key, char *value, size_t value_len);
62+
os_getenv_err_t common_hal_os_getenv_int(const char *key, mp_int_t *value);
63+
64+
STATIC mp_obj_t mod_os_getenv_int(mp_obj_t var_in) {
65+
mp_int_t value;
66+
os_getenv_err_t result = common_hal_os_getenv_int(mp_obj_str_get_str(var_in), &value);
67+
if (result == 0) {
68+
return mp_obj_new_int(value);
69+
}
70+
return mp_const_none;
71+
}
72+
MP_DEFINE_CONST_FUN_OBJ_1(mod_os_getenv_int_obj, mod_os_getenv_int);
73+
74+
STATIC mp_obj_t mod_os_getenv_str(mp_obj_t var_in) {
75+
char buf[4096];
76+
os_getenv_err_t result = common_hal_os_getenv_str(mp_obj_str_get_str(var_in), buf, sizeof(buf));
77+
if (result == 0) {
78+
return mp_obj_new_str_copy(&mp_type_str, (byte *)buf, strlen(buf));
79+
}
80+
return mp_const_none;
81+
}
82+
MP_DEFINE_CONST_FUN_OBJ_1(mod_os_getenv_str_obj, mod_os_getenv_str);
83+
#endif
84+
5785
// Command line options, with their defaults
5886
STATIC bool compile_only = false;
5987
STATIC uint emit_opt = MP_EMIT_OPT_NONE;
@@ -596,6 +624,8 @@ MP_NOINLINE int main_(int argc, char **argv) {
596624
// CIRCUITPY-CHANGE: test native base classes work as needed by CircuitPython libraries.
597625
extern const mp_obj_type_t native_base_class_type;
598626
mp_store_global(MP_QSTR_NativeBaseClass, MP_OBJ_FROM_PTR(&native_base_class_type));
627+
mp_store_global(MP_QSTR_getenv_int, MP_OBJ_FROM_PTR(&mod_os_getenv_int_obj));
628+
mp_store_global(MP_QSTR_getenv_str, MP_OBJ_FROM_PTR(&mod_os_getenv_str_obj));
599629
}
600630
#endif
601631

tests/circuitpython/getenv.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ def run_test(key, content):
8282
run_test(f"key{i}", content_good)
8383

8484
run_test(f"K", b"K = 7\r\n")
85-
print(os.getenv_int("K"))
85+
print(getenv_int("K"))
8686

8787
# Test value without trailing newline
8888
run_test(f"noeol", b"noeol=3")

0 commit comments

Comments
 (0)