Skip to content
This repository was archived by the owner on Jul 24, 2023. It is now read-only.

Commit bd44a9e

Browse files
dennwcsbinet
authored andcommitted
implement functions to work with file descriptors
* add function to convert os.File to PyObject * helpers to set stdin, stdout, stderr
1 parent b3bf8f0 commit bd44a9e

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed

file.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package python
2+
3+
/*
4+
#include <stdio.h>
5+
#include "go-python.h"
6+
7+
PyObject*
8+
_gopy_PyFile_FromFile(int fd, char *name, char *mode) {
9+
FILE *f = fdopen(fd, mode);
10+
PyObject *py = PyFile_FromFile(f, name, mode, NULL);
11+
PyFile_SetBufSize(py, 0);
12+
return py;
13+
}
14+
15+
*/
16+
import "C"
17+
18+
import (
19+
"os"
20+
)
21+
22+
// FromFile converts a Go file to Python file object.
23+
// Calling close from Python will not close a file descriptor.
24+
func FromFile(f *os.File, mode string) *PyObject {
25+
p := C._gopy_PyFile_FromFile(C.int(f.Fd()), C.CString(f.Name()), C.CString(mode))
26+
return togo(p)
27+
}
28+
29+
// SetStdin sets a sys.stdin to a specified file descriptor.
30+
func SetStdin(f *os.File) error {
31+
return PySys_SetObject("stdin", FromFile(f, "r"))
32+
}
33+
34+
// SetStdout sets a sys.stdout to a specified file descriptor.
35+
func SetStdout(f *os.File) error {
36+
return PySys_SetObject("stdout", FromFile(f, "w"))
37+
}
38+
39+
// SetStderr sets a sys.stderr to a specified file descriptor.
40+
func SetStderr(f *os.File) error {
41+
return PySys_SetObject("stderr", FromFile(f, "w"))
42+
}

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