Skip to content
This repository was archived by the owner on Aug 10, 2022. It is now read-only.

Commit f7d9a5d

Browse files
committed
System bindings
1 parent a7c8efd commit f7d9a5d

File tree

4 files changed

+211
-0
lines changed

4 files changed

+211
-0
lines changed

recursion.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package python3
2+
3+
/*
4+
#include "Python.h"
5+
#include "macro.h"
6+
*/
7+
import "C"
8+
9+
import (
10+
"unsafe"
11+
)
12+
13+
//Py_EnterRecursiveCall : https://docs.python.org/3/c-api/exceptions.html#c.Py_EnterRecursiveCall
14+
func Py_EnterRecursiveCall(where string) int {
15+
cwhere := C.CString(where)
16+
C.free(unsafe.Pointer(cwhere))
17+
18+
return int(C._go_Py_EnterRecursiveCall(cwhere))
19+
}
20+
21+
//Py_LeaveRecursiveCall : https://docs.python.org/3/c-api/exceptions.html#c.Py_LeaveRecursiveCall
22+
func Py_LeaveRecursiveCall() {
23+
C._go_Py_LeaveRecursiveCall()
24+
}

reflection.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package python3
2+
3+
/*
4+
#include "Python.h"
5+
*/
6+
import "C"
7+
8+
//PyEval_GetBuiltins : https://docs.python.org/3/c-api/reflection.html?highlight=reflection#c.PyEval_GetBuiltins
9+
func PyEval_GetBuiltins() *PyObject {
10+
return togo(C.PyEval_GetBuiltins())
11+
}
12+
13+
//PyEval_GetLocals : https://docs.python.org/3/c-api/reflection.html?highlight=reflection#c.PyEval_GetLocals
14+
func PyEval_GetLocals() *PyObject {
15+
return togo(C.PyEval_GetLocals())
16+
}
17+
18+
//PyEval_GetGlobals : https://docs.python.org/3/c-api/reflection.html?highlight=reflection#c.PyEval_GetGlobals
19+
func PyEval_GetGlobals() *PyObject {
20+
return togo(C.PyEval_GetGlobals())
21+
}
22+
23+
//PyEval_GetFuncName : https://docs.python.org/3/c-api/reflection.html?highlight=reflection#c.PyEval_GetFuncName
24+
func PyEval_GetFuncName(pyFunc *PyObject) string {
25+
return C.GoString(C.PyEval_GetFuncName(toc(pyFunc)))
26+
}
27+
28+
//PyEval_GetFuncDesc : https://docs.python.org/3/c-api/reflection.html?highlight=reflection#c.PyEval_GetFuncDesc
29+
func PyEval_GetFuncDesc(pyFunc *PyObject) string {
30+
return C.GoString(C.PyEval_GetFuncDesc(toc(pyFunc)))
31+
}

sys.go

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
package python3
2+
3+
/*
4+
#include "Python.h"
5+
*/
6+
import "C"
7+
import (
8+
"fmt"
9+
"unsafe"
10+
)
11+
12+
//PySys_GetObject : https://docs.python.org/3/c-api/sys.html#c.PySys_GetObject
13+
func PySys_GetObject(name string) *PyObject {
14+
cname := C.CString(name)
15+
defer C.free(unsafe.Pointer(cname))
16+
17+
return togo(C.PySys_GetObject(cname))
18+
}
19+
20+
//PySys_SetObject : https://docs.python.org/3/c-api/sys.html#c.PySys_SetObject
21+
func PySys_SetObject(name string, v *PyObject) int {
22+
cname := C.CString(name)
23+
defer C.free(unsafe.Pointer(cname))
24+
25+
return int(C.PySys_SetObject(cname, toc(v)))
26+
}
27+
28+
//PySys_ResetWarnOptions : https://docs.python.org/3/c-api/sys.html#c.PySys_ResetWarnOptions
29+
func PySys_ResetWarnOptions() {
30+
C.PySys_ResetWarnOptions()
31+
}
32+
33+
//PySys_AddWarnOption : https://docs.python.org/3/c-api/sys.html#c.PySys_AddWarnOption
34+
func PySys_AddWarnOption(s string) error {
35+
cs := C.CString(s)
36+
defer C.free(unsafe.Pointer(cs))
37+
38+
ws := C.Py_DecodeLocale(cs, nil)
39+
if ws == nil {
40+
return fmt.Errorf("fail to call Py_DecodeLocale on '%s'", s)
41+
}
42+
defer C.PyMem_RawFree(unsafe.Pointer(ws))
43+
44+
C.PySys_AddWarnOption(ws)
45+
46+
return nil
47+
}
48+
49+
//PySys_AddWarnOptionUnicode : https://docs.python.org/3/c-api/sys.html#c.PySys_AddWarnOptionUnicode
50+
func PySys_AddWarnOptionUnicode(unicode *PyObject) {
51+
C.PySys_AddWarnOptionUnicode(toc(unicode))
52+
}
53+
54+
//PySys_SetPath : https://docs.python.org/3/c-api/sys.html#c.PySys_SetPath
55+
func PySys_SetPath(path string) error {
56+
cpath := C.CString(path)
57+
defer C.free(unsafe.Pointer(cpath))
58+
59+
wpath := C.Py_DecodeLocale(cpath, nil)
60+
if wpath == nil {
61+
return fmt.Errorf("fail to call Py_DecodeLocale on '%s'", path)
62+
}
63+
defer C.PyMem_RawFree(unsafe.Pointer(wpath))
64+
65+
C.PySys_SetPath(wpath)
66+
67+
return nil
68+
}
69+
70+
//PySys_AddXOption : https://docs.python.org/3/c-api/sys.html#c.PySys_AddXOption
71+
func PySys_AddXOption(s string) error {
72+
cs := C.CString(s)
73+
defer C.free(unsafe.Pointer(cs))
74+
75+
ws := C.Py_DecodeLocale(cs, nil)
76+
if ws == nil {
77+
return fmt.Errorf("fail to call Py_DecodeLocale on '%s'", s)
78+
}
79+
defer C.PyMem_RawFree(unsafe.Pointer(ws))
80+
81+
C.PySys_AddXOption(ws)
82+
83+
return nil
84+
}
85+
86+
//PySys_GetXOptions : https://docs.python.org/3/c-api/sys.html#c.PySys_GetXOptions
87+
func PySys_GetXOptions() *PyObject {
88+
return togo(C.PySys_GetXOptions())
89+
}

thread.go

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package python3
2+
3+
/*
4+
#include "Python.h"
5+
*/
6+
import "C"
7+
8+
//PyThreadState : https://docs.python.org/3/c-api/init.html#c.PyThreadState
9+
type PyThreadState C.PyThreadState
10+
11+
//PyGILState is an opaque “handle” to the thread state when PyGILState_Ensure() was called, and must be passed to PyGILState_Release() to ensure Python is left in the same state
12+
type PyGILState C.PyGILState_STATE
13+
14+
//PyEval_InitThreads : https://docs.python.org/3/c-api/init.html#c.PyEval_InitThreads
15+
func PyEval_InitThreads() {
16+
C.PyEval_InitThreads()
17+
}
18+
19+
//PyEval_ThreadsInitialized : https://docs.python.org/3/c-api/init.html#c.PyEval_ThreadsInitialized
20+
func PyEval_ThreadsInitialized() bool {
21+
return C.PyEval_ThreadsInitialized() != 0
22+
}
23+
24+
//PyEval_SaveThread : https://docs.python.org/3/c-api/init.html#c.PyEval_SaveThread
25+
func PyEval_SaveThread() *PyThreadState {
26+
return (*PyThreadState)(C.PyEval_SaveThread())
27+
}
28+
29+
//PyEval_RestoreThread : https://docs.python.org/3/c-api/init.html#c.PyEval_RestoreThread
30+
func PyEval_RestoreThread(tstate *PyThreadState) {
31+
C.PyEval_RestoreThread((*C.PyThreadState)(tstate))
32+
}
33+
34+
//PyThreadState_Get : https://docs.python.org/3/c-api/init.html#c.PyThreadState_Get
35+
func PyThreadState_Get() *PyThreadState {
36+
return (*PyThreadState)(C.PyThreadState_Get())
37+
}
38+
39+
//PyThreadState_Swap : https://docs.python.org/3/c-api/init.html#c.PyThreadState_Swap
40+
func PyThreadState_Swap(tstate *PyThreadState) *PyThreadState {
41+
return (*PyThreadState)(C.PyThreadState_Swap((*C.PyThreadState)(tstate)))
42+
}
43+
44+
//PyEval_ReInitThreads : https://docs.python.org/3/c-api/init.html#c.PyEval_ReInitThreads
45+
func PyEval_ReInitThreads() {
46+
C.PyEval_ReInitThreads()
47+
}
48+
49+
//PyGILState_Ensure : https://docs.python.org/3/c-api/init.html#c.PyGILState_Ensure
50+
func PyGILState_Ensure() PyGILState {
51+
return PyGILState(C.PyGILState_Ensure())
52+
}
53+
54+
//PyGILState_Release : https://docs.python.org/3/c-api/init.html#c.PyGILState_Release
55+
func PyGILState_Release(state PyGILState) {
56+
C.PyGILState_Release(C.PyGILState_STATE(state))
57+
}
58+
59+
//PyGILState_GetThisThreadState : https://docs.python.org/3/c-api/init.html#c.PyGILState_GetThisThreadState
60+
func PyGILState_GetThisThreadState() *PyThreadState {
61+
return (*PyThreadState)(C.PyGILState_GetThisThreadState())
62+
}
63+
64+
//PyGILState_Check : https://docs.python.org/3/c-api/init.html#c.PyGILState_Check
65+
func PyGILState_Check() bool {
66+
return C.PyGILState_Check() == 1
67+
}

0 commit comments

Comments
 (0)