FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Stdlib os closefd by sbinet · Pull Request #190 · go-python/gpython · GitHub

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .go  (1) .py  (2) .txt  (1) All 3 file types selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
30 changes: 30 additions & 0 deletions stdlib/os/os.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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

Back | FazBrowse Home | New Git URL