Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add os.environ.setdefault(str, str) functionality #585

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions stdlib/os/__init__.codon
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ class EnvMap:
self._init_if_needed()
return self._map.items()

def setdefault(self, key: str, default: str = "") -> str:
self._init_if_needed()
return self._map.setdefault(key, default)

environ = EnvMap()

def getenv(key: str, default: str = "") -> str:
Expand Down
1 change: 1 addition & 0 deletions test/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -475,6 +475,7 @@ INSTANTIATE_TEST_SUITE_P(
"stdlib/sort_test.codon",
"stdlib/heapq_test.codon",
"stdlib/operator_test.codon",
"stdlib/os_test.codon",
"python/pybridge.codon"
),
testing::Values(true, false),
Expand Down
11 changes: 11 additions & 0 deletions test/stdlib/os_test.codon
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import os

@test
def test_environ_setdefault():
rnd_env_key = "RND_KZBAF"
assert os.environ.setdefault(rnd_env_key, "VALUE_1") == "VALUE_1"
assert os.environ[rnd_env_key] == "VALUE_1"
assert os.environ.setdefault(rnd_env_key, "VALUE_2") == "VALUE_1"
assert os.environ[rnd_env_key] == "VALUE_1"

test_environ_setdefault()