static mp_obj_t module_thrd_get_env(mp_obj_t name_in) { const char *value_p; value_p = thrd_get_env(mp_obj_str_get_str(name_in)); if (value_p == NULL) { return (mp_const_none); } return (mp_obj_new_str(value_p, strlen(value_p), 0)); }
/** * Get current working directory * * def getcwd() */ static mp_obj_t os_getcwd(void) { const char *path_p; path_p = thrd_get_env("CWD"); if (path_p == NULL) { nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_OSError, "No such file or directory: ''")); } return (mp_obj_new_str(path_p, strlen(path_p), false)); }
int test_env(struct harness_t *harness_p) { struct thrd_environment_variable_t global_variables[2]; struct thrd_environment_variable_t variables[4]; /* Default thread environment is setup correctly. */ BTASSERT(thrd_get_env("FOO") == NULL); /* Set and get are not possible for a thread without an environment. */ BTASSERT(thrd_init_env(NULL, 0) == 0); BTASSERT(thrd_set_env("CWD", "/") == -1); BTASSERT(thrd_get_env("CWD") == NULL); /* Initialize the global environment. */ BTASSERT(thrd_init_global_env(global_variables, membersof(global_variables)) == 0); /* Set and get global variables. */ BTASSERT(thrd_set_global_env("N1", "G1") == 0); BTASSERT(thrd_get_global_env("N1") != NULL); BTASSERT(strcmp(thrd_get_global_env("N1"), "G1") == 0); BTASSERT(thrd_set_global_env("N2", "G2") == 0); BTASSERT(thrd_get_global_env("N2") != NULL); BTASSERT(strcmp(thrd_get_env("N2"), "G2") == 0); /* Get from global environment using the thread local get function. */ BTASSERT(thrd_get_env("N1") != NULL); BTASSERT(strcmp(thrd_get_env("N1"), "G1") == 0); /* Initialize the environment for the current thread. */ BTASSERT(thrd_init_env(variables, membersof(variables)) == 0); /* Set and get variables. The global N1 is overridden. */ BTASSERT(thrd_set_env("N1", "L1") == 0); BTASSERT(thrd_get_env("N1") != NULL); BTASSERT(strcmp(thrd_get_env("N1"), "L1") == 0); BTASSERT(thrd_set_env("N2", "L2") == 0); BTASSERT(thrd_get_env("N2") != NULL); BTASSERT(strcmp(thrd_get_env("N2"), "L2") == 0); BTASSERT(thrd_set_env("N3", "L3") == 0); BTASSERT(thrd_get_env("N3") != NULL); BTASSERT(strcmp(thrd_get_env("N3"), "L3") == 0); BTASSERT(thrd_set_env("N4", "L4") == 0); BTASSERT(thrd_get_env("N4") != NULL); BTASSERT(strcmp(thrd_get_env("N4"), "L4") == 0); /* Overwrite a value. */ BTASSERT(thrd_set_env("N4", "L44") == 0); BTASSERT(thrd_get_env("N4") != NULL); BTASSERT(strcmp(thrd_get_env("N4"), "L44") == 0); /* No free space. */ BTASSERT(thrd_set_env("N5", "L5") == -1); /* Remove a variable. */ BTASSERT(thrd_set_env("N2", NULL) == 0); /* Set and get another variable. */ BTASSERT(thrd_set_env("N6", "L6") == 0); BTASSERT(thrd_get_env("N6") != NULL); BTASSERT(strcmp(thrd_get_env("N6"), "L6") == 0); /* Get a non-existing variable. */ BTASSERT(thrd_get_env("N7") == NULL); /* Remove the local environment. */ BTASSERT(thrd_init_env(NULL, 0) == 0); /* Remove the global environment. */ BTASSERT(thrd_init_global_env(NULL, 0) == 0); return (0); }