From e563b2e760ba9aed7af9e24f81fb55eba1a04e21 Mon Sep 17 00:00:00 2001 From: Sebastien Binet Date: Wed, 25 May 2022 19:20:25 +0200 Subject: [PATCH 1/2] stdlib/os: add close Signed-off-by: Sebastien Binet --- stdlib/os/os.go | 30 ++++++++++++++++++++++++++++++ stdlib/os/testdata/test.py | 11 +++++++++++ stdlib/os/testdata/test_golden.txt | 1 + 3 files changed, 42 insertions(+) diff --git a/stdlib/os/os.go b/stdlib/os/os.go index e8b3ec7a..f63586ce 100644 --- a/stdlib/os/os.go +++ b/stdlib/os/os.go @@ -46,6 +46,7 @@ func init() { methods := []*py.Method{ py.MustNewMethod("_exit", _exit, 0, "Immediate program termination."), + py.MustNewMethod("close", closefd, 0, closefd_doc), py.MustNewMethod("fdopen", fdopen, 0, fdopen_doc), py.MustNewMethod("getcwd", getCwd, 0, "Get the current working directory"), py.MustNewMethod("getcwdb", getCwdb, 0, "Get the current working directory in a byte slice"), @@ -98,6 +99,35 @@ func getEnvVariables() py.StringDict { return dict } +const closefd_doc = `Close a file descriptor` + +func closefd(self py.Object, args py.Tuple, kwargs py.StringDict) (py.Object, error) { + var ( + pyfd py.Object + ) + err := py.ParseTupleAndKeywords(args, kwargs, "i", []string{"fd"}, &pyfd) + if err != nil { + return nil, err + } + + var ( + fd = uintptr(pyfd.(py.Int)) + name = strconv.Itoa(int(fd)) + ) + + f := os.NewFile(fd, name) + if f == nil { + return nil, py.ExceptionNewf(py.OSError, "Bad file descriptor") + } + + err = f.Close() + if err != nil { + return nil, err + } + + return py.None, nil +} + const fdopen_doc = `# Supply os.fdopen()` func fdopen(self py.Object, args py.Tuple, kwargs py.StringDict) (py.Object, error) { diff --git a/stdlib/os/testdata/test.py b/stdlib/os/testdata/test.py index bdbf606e..bbdf8b69 100644 --- a/stdlib/os/testdata/test.py +++ b/stdlib/os/testdata/test.py @@ -118,6 +118,17 @@ else: print("os."+k+": [OK]") +## close +import tempfile +fd, tmp = tempfile.mkstemp() +os.close(fd=fd) +os.remove(tmp) +try: + os.close(-1) + print("closing a bad file descriptor should have failed") +except Exception as e: + print("caught: %s [OK]" % e) + ## fdopen import tempfile fd, tmp = tempfile.mkstemp() diff --git a/stdlib/os/testdata/test_golden.txt b/stdlib/os/testdata/test_golden.txt index 43c83011..4a0f640a 100644 --- a/stdlib/os/testdata/test_golden.txt +++ b/stdlib/os/testdata/test_golden.txt @@ -24,6 +24,7 @@ os.pathsep: [OK] os.linesep: [OK] os.devnull: [OK] os.altsep: [OK] +caught: OSError: 'Bad file descriptor' [OK] caught: SystemError - no such file or directory [OK] caught: FileExistsError [OK] caught: SystemError - directory not empty [OK] From 80944be95fc263ed7845f6a7eafb6a4b1831c1a6 Mon Sep 17 00:00:00 2001 From: Sebastien Binet Date: Wed, 25 May 2022 19:20:43 +0200 Subject: [PATCH 2/2] stdlib/tempfile: use os.close Signed-off-by: Sebastien Binet --- stdlib/tempfile/testdata/test.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/stdlib/tempfile/testdata/test.py b/stdlib/tempfile/testdata/test.py index 19cef5bc..0fdecc9d 100644 --- a/stdlib/tempfile/testdata/test.py +++ b/stdlib/tempfile/testdata/test.py @@ -62,7 +62,7 @@ print("INVALID error caught: %s" % e) def remove(fd, name): - os.fdopen(fd).close() + os.close(fd) os.remove(name) ## mkstemp