@@ -92,40 +92,21 @@ func chdir(self py.Object, args py.Tuple) (py.Object, error) {
9292// getenv returns the value of the environment variable key.
9393// If no such environment variable exists and a default value was provided, that value is returned.
9494func getenv (self py.Object , args py.Tuple ) (py.Object , error ) {
95- var (
96- key py.Object
97- dflt py.Object = py .None
98- err error
99- )
100- if len (args ) == 0 {
95+ if len (args ) < 1 {
10196 return nil , py .ExceptionNewf (py .TypeError , "missing one required argument: 'name:str'" )
10297 }
103- if ! objectIsString (args [0 ]) {
98+ k , ok := args [0 ].(py.String )
99+ if ! ok {
104100 return nil , py .ExceptionNewf (py .TypeError , "str expected (pos 1), not " + args [0 ].Type ().Name )
105101 }
106- switch len (args ) {
107- case 1 :
108- key = args [0 ]
109- dflt = py .None
110- case 2 :
111- key = args [0 ]
112- if ! objectIsString (args [1 ]) {
113- return nil , py .ExceptionNewf (py .TypeError , "str expected (pos 2), not " + args [1 ].Type ().Name )
114- }
115- dflt = args [1 ]
116- }
117- gokey , err := py .ReprAsString (key )
118- if err != nil {
119- return dflt , nil
102+ v , ok := os .LookupEnv (string (k ))
103+ if ok {
104+ return py .String (v ), nil
120105 }
121- gokey = strings .ReplaceAll (gokey , "'" , "" )
122- for _ , evar := range os .Environ () {
123- key_value := strings .SplitN (evar , "=" , 2 ) // returns a []string containing [key,value]
124- if key_value [0 ] == gokey {
125- return py .String (key_value [1 ]), nil
126- }
106+ if len (args ) == 2 {
107+ return args [1 ], nil
127108 }
128- return dflt , nil
109+ return py . None , nil
129110}
130111
131112// get the current process' pid
0 commit comments