Skip to content

Commit 183b78d

Browse files
committed
added os.getcwdb()
1 parent c512761 commit 183b78d

File tree

3 files changed

+21
-2
lines changed

3 files changed

+21
-2
lines changed

main

5.87 MB
Binary file not shown.

os/os.module.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ func init() {
1717

1818
methods := []*py.Method{
1919
py.MustNewMethod("getcwd", getCwd, 0, "Get the current working directory"),
20+
py.MustNewMethod("getcwdb", getCwdb, 0, "Get the current working directory in a byte slice"),
2021
py.MustNewMethod("chdir", chdir, 0, "Change the current working directory"),
2122
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."),
2223
py.MustNewMethod("getpid", getpid, 0, "Return the current process id."),
@@ -62,6 +63,15 @@ func getCwd(self py.Object, args py.Tuple) (py.Object, error) {
6263
return py.String(dir), nil
6364
}
6465

66+
// gets the current working directory in a byte list
67+
func getCwdb(self py.Object, args py.Tuple) (py.Object, error) {
68+
dir, err := os.Getwd()
69+
if err != nil {
70+
return nil, py.ExceptionNewf(py.OSError, "Unable to get current working directory.")
71+
}
72+
return py.Bytes([]byte(dir)), nil
73+
}
74+
6575
// change current working directory
6676
func chdir(self py.Object, args py.Tuple) (py.Object, error) {
6777
if len(args) == 0 {

os/os.test.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import os
2+
import sys
23
import time
34

45
class bcolors:
@@ -17,6 +18,7 @@ class bcolors:
1718
print("os.error:", os.error) # prints <class 'OSError'>
1819
print("os.environ.get('HOME'):", os.environ.get("HOME")) # prints $HOME variable
1920
print("cwd():", os.getcwd()) # prints current working directory
21+
print("cwdb():", os.getcwdb())
2022
print("chdir(os.environ.get(\"HOME\"))):", os.chdir(os.environ.get("HOME"))) # changes the current working directory to $HOME
2123
print("cwd() after chdir():", os.getcwd()) # should print $HOME dir
2224
print("getenv(\"HOME\"):", os.getenv("HOME")) # should print $HOME dir
@@ -34,7 +36,6 @@ class bcolors:
3436
# tests
3537
passed_tests = 0
3638
failed = 0
37-
3839
def _pass(test_name):
3940
print(bcolors.OKGREEN+test_name+" passed!")
4041

@@ -160,18 +161,26 @@ def _fail(test_name):
160161
_fail('os.getenv("TEST_VAR") == None')
161162
failed+=1
162163

164+
if bytes(os.getcwd(), "utf-8") == os.getcwdb():
165+
_pass('bytes(os.getcwd(), "utf-8") == os.getcwdb()')
166+
passed_tests+=1
167+
else:
168+
_fail('bytes(os.getcwd(), "utf-8") == os.getcwdb()')
169+
failed+=1
170+
163171
print()
164172
print(bcolors.ENDC+bcolors.BOLD+bcolors.HEADER+"Please check if $HOME will be outputted...")
165173
print(bcolors.ENDC)
166174

167175
if os.system("pwd") == 0:
176+
print(bcolors.WARNING+'please manually check if your home directory was outputted above, if it isn\'t, this test is a fail! Result by status code: %s' % "pass")
168177
_pass('os.system("pwd")')
169178
passed_tests+=1
170179
else:
180+
print(bcolors.WARNING+'please manually check if your home directory was outputted above, if it isn\'t, this test is a fail! Result by status code: %s' % "fail")
171181
_fail('os.system("pwd")')
172182
failed+=1
173183

174-
time.sleep(2)
175184

176185
print()
177186
print(bcolors.ENDC+bcolors.BOLD+bcolors.HEADER+"----- Results -----")

0 commit comments

Comments
 (0)