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

Commit c9a2cf7

Browse files
committed
object: use fopen on windows
on Windows, fdopen is broken. use fopen instead. Fixes #21.
1 parent 022f9f5 commit c9a2cf7

File tree

3 files changed

+43
-10
lines changed

3 files changed

+43
-10
lines changed

object.go

-10
Original file line numberDiff line numberDiff line change
@@ -91,16 +91,6 @@ func int2err(i C.int) error {
9191
return &gopy_err{fmt.Sprintf("error in C-Python (rc=%i)", int(i))}
9292
}
9393

94-
// file2py opens a stdC file from a Go os.File. Note the returned file has
95-
// been newly opened: the caller must close it with C.fclose(retval).
96-
func file2py(f *os.File, mode string) *C.FILE {
97-
cmode := C.CString(mode)
98-
defer C.free(unsafe.Pointer(cmode))
99-
fd := f.Fd()
100-
file := C.fdopen(C.int(fd), cmode)
101-
return file
102-
}
103-
10494
func file2go(f *C.FILE) *os.File {
10595
return nil
10696
}

object_posix.go

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package python
2+
3+
//#include <stdlib.h>
4+
//#include <string.h>
5+
//#include <stdio.h>
6+
import "C"
7+
8+
import (
9+
"os"
10+
"unsafe"
11+
)
12+
13+
// file2py opens a stdC file from a Go os.File. Note the returned file has
14+
// been newly opened: the caller must close it with C.fclose(retval).
15+
func file2py(f *os.File, mode string) *C.FILE {
16+
cmode := C.CString(mode)
17+
defer C.free(unsafe.Pointer(cmode))
18+
fd := f.Fd()
19+
file := C.fdopen(C.int(fd), cmode)
20+
return file
21+
}

object_windows.go

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package python
2+
3+
//#include <stdlib.h>
4+
//#include <string.h>
5+
//#include <stdio.h>
6+
import "C"
7+
8+
import (
9+
"os"
10+
"unsafe"
11+
)
12+
13+
// file2py opens a stdC file from a Go os.File. Note the returned file has
14+
// been newly opened: the caller must close it with C.fclose(retval).
15+
func file2py(f *os.File, mode string) *C.FILE {
16+
cmode := C.CString(mode)
17+
defer C.free(unsafe.Pointer(cmode))
18+
name := C.CString(file.Name())
19+
defer C.free(unsafe.Pointer(name))
20+
file := C.fopen(name, cmode)
21+
return file
22+
}

0 commit comments

Comments
 (0)