Skip to content

Commit

Permalink
unix/moduos: Implement 2-arg version of os.getenv().
Browse files Browse the repository at this point in the history
This adds the `default` argument of `os.getenv(key, default=None)`.

Signed-off-by: David Lechner <david@pybricks.com>
  • Loading branch information
dlech authored and dpgeorge committed Dec 14, 2022
1 parent 0eba00a commit 958f748
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 17 deletions.
9 changes: 6 additions & 3 deletions ports/unix/moduos.c
Expand Up @@ -32,14 +32,17 @@
#include "py/runtime.h"
#include "py/mphal.h"

STATIC mp_obj_t mp_uos_getenv(mp_obj_t var_in) {
const char *s = getenv(mp_obj_str_get_str(var_in));
STATIC mp_obj_t mp_uos_getenv(size_t n_args, const mp_obj_t *args) {
const char *s = getenv(mp_obj_str_get_str(args[0]));
if (s == NULL) {
if (n_args == 2) {
return args[1];
}
return mp_const_none;
}
return mp_obj_new_str(s, strlen(s));
}
STATIC MP_DEFINE_CONST_FUN_OBJ_1(mp_uos_getenv_obj, mp_uos_getenv);
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_uos_getenv_obj, 1, 2, mp_uos_getenv);

STATIC mp_obj_t mp_uos_putenv(mp_obj_t key_in, mp_obj_t value_in) {
const char *key = mp_obj_str_get_str(key_in);
Expand Down
14 changes: 0 additions & 14 deletions tests/cpydiff/modules_os_getenv_argcount.py

This file was deleted.

0 comments on commit 958f748

Please sign in to comment.