forked from sbinet/go-python
-
Notifications
You must be signed in to change notification settings - Fork 1
/
file.go
47 lines (39 loc) · 1.1 KB
/
file.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package python
/*
#include <stdio.h>
#include "go-python.h"
PyObject*
_gopy_PyFile_FromFile(int fd, char *name, char *mode) {
FILE *f = fdopen(fd, mode);
PyObject *py = PyFile_FromFile(f, name, mode, NULL);
PyFile_SetBufSize(py, 0);
return py;
}
*/
import "C"
import (
"os"
"unsafe"
)
// FromFile converts a Go file to Python file object.
// Calling close from Python will not close a file descriptor.
func FromFile(f *os.File, mode string) *PyObject {
cname := C.CString(f.Name())
cmode := C.CString(mode)
p := C._gopy_PyFile_FromFile(C.int(f.Fd()), cname, cmode)
C.free(unsafe.Pointer(cname))
C.free(unsafe.Pointer(cmode))
return togo(p)
}
// SetStdin sets a sys.stdin to a specified file descriptor.
func SetStdin(f *os.File) error {
return PySys_SetObject("stdin", FromFile(f, "r"))
}
// SetStdout sets a sys.stdout to a specified file descriptor.
func SetStdout(f *os.File) error {
return PySys_SetObject("stdout", FromFile(f, "w"))
}
// SetStderr sets a sys.stderr to a specified file descriptor.
func SetStderr(f *os.File) error {
return PySys_SetObject("stderr", FromFile(f, "w"))
}