Skip to content

Stdlib os closefd #190

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions stdlib/os/os.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand Down Expand Up @@ -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) {
Expand Down
11 changes: 11 additions & 0 deletions stdlib/os/testdata/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
1 change: 1 addition & 0 deletions stdlib/os/testdata/test_golden.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
2 changes: 1 addition & 1 deletion stdlib/tempfile/testdata/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down