Skip to content

Commit 4279363

Browse files
committed
fix error messages
1 parent cf0359c commit 4279363

File tree

3 files changed

+25
-17
lines changed

3 files changed

+25
-17
lines changed

main

5.87 MB
Binary file not shown.

os/os.module.go

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -64,22 +64,22 @@ func getCwd(self py.Object, args py.Tuple) (py.Object, error) {
6464

6565
// change current working directory
6666
func chdir(self py.Object, args py.Tuple) (py.Object, error) {
67-
if len(args) == 0 || len(args) > 1 {
68-
return nil, py.ExceptionNewf(py.TypeError, "One argument required")
67+
if len(args) == 0 {
68+
return nil, py.ExceptionNewf(py.TypeError, "Missing required argument 'path' (pos 1)")
6969
}
7070
if objectIsString(args[0]) {
7171
dir, err := py.ReprAsString(args[0])
7272
if err != nil {
73-
return nil, py.ExceptionNewf(py.TypeError, "Failed to parse string")
73+
return nil, py.ExceptionNewf(py.TypeError, "Failed to parse string") // never going to run
7474
}
7575
dir = strings.ReplaceAll(dir, "'", "")
7676
err = os.Chdir(dir)
7777
if err != nil {
78-
return nil, py.ExceptionNewf(py.OSError, "Couldn't change cwd; "+err.Error())
78+
return nil, py.ExceptionNewf(py.NotADirectoryError, "Couldn't change cwd; "+err.Error())
7979
}
8080
return py.None, nil
8181
}
82-
return nil, py.ExceptionNewf(py.TypeError, "Expected string argument at position 0")
82+
return nil, py.ExceptionNewf(py.TypeError, "str expected, not "+args[0].Type().Name)
8383
}
8484

8585
// get a enviroment variable by key
@@ -89,25 +89,27 @@ func getenv(self py.Object, args py.Tuple) (py.Object, error) {
8989
var default_ py.Object // return when not found
9090
var err error // error obj
9191

92-
if len(args) > 2 {
93-
return nil, py.ExceptionNewf(py.KeyError, "1 argument required, \"key\"")
94-
}
9592
if len(args) == 1 {
9693
if objectIsString(args[0]) {
9794
key = args[0]
9895
} else {
99-
return nil, py.ExceptionNewf(py.TypeError, "Expected argument of type string")
96+
return nil, py.ExceptionNewf(py.TypeError, "str expected (pos 1), not "+args[0].Type().Name)
10097
}
10198
default_ = py.None
10299
} else if len(args) == 2 {
100+
if objectIsString(args[0]) {
101+
key = args[0]
102+
} else {
103+
return nil, py.ExceptionNewf(py.TypeError, "str expected (pos 1), not "+args[0].Type().Name)
104+
}
103105
if objectIsString(args[1]) {
104106
key = args[0]
105107
default_ = args[1]
106108
} else {
107-
return nil, py.ExceptionNewf(py.TypeError, "Expected argument of type string")
109+
return nil, py.ExceptionNewf(py.TypeError, "str expected (pos 2), not"+args[1].Type().Name)
108110
}
109111
} else {
110-
return nil, py.ExceptionNewf(py.TypeError, "Expected argument of type string")
112+
return nil, py.ExceptionNewf(py.TypeError, "missing one required argument: 'name:str'")
111113
}
112114
var res py.Object // hold the result value
113115
res, err = getEnvVariables().M__getitem__(key)
@@ -142,7 +144,7 @@ func putenv(self py.Object, args py.Tuple) (py.Object, error) {
142144
}
143145
return py.None, nil
144146
}
145-
return nil, py.ExceptionNewf(py.TypeError, "Expected 2 arguments of type string")
147+
return nil, py.ExceptionNewf(py.TypeError, "missing required arguments: 'key:str' and 'value:str'")
146148
}
147149

148150
// Unset (delete) the environment variable named key.
@@ -161,7 +163,10 @@ func unsetenv(self py.Object, args py.Tuple) (py.Object, error) {
161163
return py.None, nil
162164
}
163165
}
164-
return nil, py.ExceptionNewf(py.TypeError, "Expected 1 argument of type string")
166+
if len(args) == 0 {
167+
return nil, py.ExceptionNewf(py.TypeError, "missing one required argument: 'key:str'")
168+
}
169+
return nil, py.ExceptionNewf(py.TypeError, "str expected, not "+args[0].Type().Name)
165170
}
166171

167172
// os._exit() immediate program termination; unlike sys.exit(), which raises a SystemExit, this function will termninate the program immediately.
@@ -187,7 +192,7 @@ func _exit(self py.Object, args py.Tuple) (py.Object, error) { // can never retu
187192
// os.system(command string) this function runs a shell command and directs the output to standard output.
188193
func system(self py.Object, args py.Tuple) (py.Object, error) {
189194
if len(args) == 0 && !(objectIsString(args[0])) {
190-
return nil, py.ExceptionNewf(py.TypeError, "Expected 1 or more arguments all of type string")
195+
return nil, py.ExceptionNewf(py.TypeError, "missing one required argument: 'command:str'")
191196
}
192197
var cargs []string
193198
_carg, err := py.ReprAsString(args[0])

os/os.test.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,10 @@ class bcolors:
1212
BOLD = '\033[1m'
1313
UNDERLINE = '\033[4m'
1414

15+
print("--------- Manual Check ---------")
16+
print("Check if the data below is correct:")
1517
print("os.error:", os.error) # prints <class 'OSError'>
16-
print("$HOME:", os.environ.get("HOME")) # prints $HOME variable
18+
print("os.environ.get('HOME'):", os.environ.get("HOME")) # prints $HOME variable
1719
print("cwd():", os.getcwd()) # prints current working directory
1820
print("chdir(os.environ.get(\"HOME\"))):", os.chdir(os.environ.get("HOME"))) # changes the current working directory to $HOME
1921
print("cwd() after chdir():", os.getcwd()) # should print $HOME dir
@@ -27,6 +29,7 @@ class bcolors:
2729
print()
2830
print("--------------")
2931
print()
32+
print("Tests:")
3033

3134
# tests
3235
passed_tests = 0
@@ -114,7 +117,7 @@ def _fail(test_name):
114117
except TypeError:
115118
_pass('os.unsetenv("1","2") (more arguments than needed)')
116119
passed_tests+=1
117-
120+
118121
if os.getcwd() == os.environ.get("HOME"):
119122
_pass('os.getcwd() == os.environ.get("HOME")')
120123
passed_tests+=1
@@ -140,7 +143,7 @@ def _fail(test_name):
140143
_pass('os.getenv("TEST_VAR") == "TEST_VALUE"')
141144
passed_tests+=1
142145
else:
143-
_fail('os.getenv("TEST_VAR") == "TEST_VALUE"')
146+
_fail('os.getenv("TEST_VAR") == "TEST_VALUE" actual %s' % os.getenv("TEST_VAR"))
144147
failed+=1
145148

146149
if os.unsetenv("TEST_VAR") == None:

0 commit comments

Comments
 (0)