Skip to content

Commit 5097f6f

Browse files
committed
added putenv()
1 parent fc05b1b commit 5097f6f

File tree

3 files changed

+35
-2
lines changed

3 files changed

+35
-2
lines changed

main

4.17 KB
Binary file not shown.

os/os.module.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ func init() {
1919
py.MustNewMethod("chdir", chdir, 0, "Change the current working directory"),
2020
py.MustNewMethod("getenv", getenv, 0, "Return the value of the environment variable key if it exists, or default if it doesn’t. key, default and the result are str."),
2121
py.MustNewMethod("getpid", getpid, 0, "Return the current process id."),
22+
py.MustNewMethod("putenv", putenv, 0, "Set the environment variable named key to the string value."),
2223
}
2324

2425
globals := py.StringDict{
@@ -117,3 +118,31 @@ func getenv(self py.Object, args py.Tuple) (py.Object, error) {
117118
func getpid(self py.Object, args py.Tuple) (py.Object, error) {
118119
return py.Int(os.Getpid()), nil
119120
}
121+
122+
// Set the environment variable named key to the string value.
123+
func putenv(self py.Object, args py.Tuple) (py.Object, error) {
124+
if len(args) == 2 {
125+
if reflect.TypeOf(args[0]).String() == "py.String" && reflect.TypeOf(args[1]).String() == "py.String" {
126+
_k, err := py.ReprAsString(args[0])
127+
if err != nil {
128+
return nil, py.ExceptionNewf(py.TypeError, "Unable to parse string") // this will never execute
129+
}
130+
key := strings.ReplaceAll(_k, "'", "") // required
131+
_v, err := py.ReprAsString(args[1])
132+
if err != nil {
133+
return nil, py.ExceptionNewf(py.TypeError, "Unable to parse string") // this will never execute
134+
}
135+
value := strings.ReplaceAll(_v, "'", "")
136+
137+
err = os.Setenv(key, value)
138+
if err != nil {
139+
return nil, py.ExceptionNewf(py.OSError, "Unable to set enviroment variable") // this will never execute
140+
}
141+
return py.None, nil
142+
} else {
143+
return nil, py.ExceptionNewf(py.TypeError, "Expected 2 arguments of type string")
144+
}
145+
} else {
146+
return nil, py.ExceptionNewf(py.TypeError, "Expected 2 arguments of type string")
147+
}
148+
}

os/os.test.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,14 @@
66
print()
77
print("cwd():", os.getcwd()) # prints current working directory
88
print()
9-
print("chdir():", os.chdir(os.environ.get("HOME"))) # changes the current working directory to $HOME
9+
print("chdir(os.environ.get(\"HOME\"))):", os.chdir(os.environ.get("HOME"))) # changes the current working directory to $HOME
1010
print()
1111
print("cwd() after chdir():", os.getcwd()) # should print $HOME dir
1212
print()
1313
print("getenv(\"HOME\"):", os.getenv("HOME")) # should print $HOME dir
1414
print()
15-
print("getpid():", os.getpid()) # prints the process id
15+
print("getpid():", os.getpid()) # prints the process id
16+
print()
17+
print('putenv("TEST_VAR", "TEST_VALUE"):', os.putenv("TEST_VAR", "TEST_VALUE")) # creates a new enviroment variable
18+
print()
19+
print('getenv("TEST_VAR"):', os.getenv("TEST_VAR"))

0 commit comments

Comments
 (0)