Skip to content

Commit 1d174c6

Browse files
committed
add environ global
1 parent 38cad1d commit 1d174c6

File tree

3 files changed

+21
-1
lines changed

3 files changed

+21
-1
lines changed

main

240 Bytes
Binary file not shown.

os/os.module.go

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ package os
55
// license that can be found in the LICENSE file.
66

77
import (
8+
"os"
9+
"strings"
10+
811
"github.com/go-python/gpython/py"
912
)
1013

@@ -13,7 +16,8 @@ func init() {
1316
methods := []*py.Method{}
1417

1518
globals := py.StringDict{
16-
"error": py.OSError,
19+
"error": py.OSError,
20+
"environ": getEnvVariables(),
1721
}
1822

1923
py.RegisterModule(&py.ModuleImpl{
@@ -25,3 +29,15 @@ func init() {
2529
Globals: globals,
2630
})
2731
}
32+
33+
// gets and parses all process enviroment variables
34+
func getEnvVariables() py.StringDict {
35+
env_variables := os.Environ() // this gets all the enviroment variables
36+
dict := py.NewStringDict()
37+
for _, evar := range env_variables {
38+
key_value := strings.Split(evar, "=") // returns a []string containing [key,value]
39+
dict.M__setitem__(py.String(key_value[0]), py.String(key_value[1]))
40+
}
41+
42+
return dict
43+
}

os/os.test.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import os
2+
3+
print(os.error)
4+
print(os.environ.get("HOME")) # prints $HOME variable

0 commit comments

Comments
 (0)