Skip to content

Commit e563b2e

Browse files
committed
stdlib/os: add close
Signed-off-by: Sebastien Binet <binet@cern.ch>
1 parent 3e1daa8 commit e563b2e

File tree

3 files changed

+42
-0
lines changed

3 files changed

+42
-0
lines changed

stdlib/os/os.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ func init() {
4646

4747
methods := []*py.Method{
4848
py.MustNewMethod("_exit", _exit, 0, "Immediate program termination."),
49+
py.MustNewMethod("close", closefd, 0, closefd_doc),
4950
py.MustNewMethod("fdopen", fdopen, 0, fdopen_doc),
5051
py.MustNewMethod("getcwd", getCwd, 0, "Get the current working directory"),
5152
py.MustNewMethod("getcwdb", getCwdb, 0, "Get the current working directory in a byte slice"),
@@ -98,6 +99,35 @@ func getEnvVariables() py.StringDict {
9899
return dict
99100
}
100101

102+
const closefd_doc = `Close a file descriptor`
103+
104+
func closefd(self py.Object, args py.Tuple, kwargs py.StringDict) (py.Object, error) {
105+
var (
106+
pyfd py.Object
107+
)
108+
err := py.ParseTupleAndKeywords(args, kwargs, "i", []string{"fd"}, &pyfd)
109+
if err != nil {
110+
return nil, err
111+
}
112+
113+
var (
114+
fd = uintptr(pyfd.(py.Int))
115+
name = strconv.Itoa(int(fd))
116+
)
117+
118+
f := os.NewFile(fd, name)
119+
if f == nil {
120+
return nil, py.ExceptionNewf(py.OSError, "Bad file descriptor")
121+
}
122+
123+
err = f.Close()
124+
if err != nil {
125+
return nil, err
126+
}
127+
128+
return py.None, nil
129+
}
130+
101131
const fdopen_doc = `# Supply os.fdopen()`
102132

103133
func fdopen(self py.Object, args py.Tuple, kwargs py.StringDict) (py.Object, error) {

stdlib/os/testdata/test.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,17 @@
118118
else:
119119
print("os."+k+": [OK]")
120120

121+
## close
122+
import tempfile
123+
fd, tmp = tempfile.mkstemp()
124+
os.close(fd=fd)
125+
os.remove(tmp)
126+
try:
127+
os.close(-1)
128+
print("closing a bad file descriptor should have failed")
129+
except Exception as e:
130+
print("caught: %s [OK]" % e)
131+
121132
## fdopen
122133
import tempfile
123134
fd, tmp = tempfile.mkstemp()

stdlib/os/testdata/test_golden.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ os.pathsep: [OK]
2424
os.linesep: [OK]
2525
os.devnull: [OK]
2626
os.altsep: [OK]
27+
caught: OSError: 'Bad file descriptor' [OK]
2728
caught: SystemError - no such file or directory [OK]
2829
caught: FileExistsError [OK]
2930
caught: SystemError - directory not empty [OK]

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy