From f7d9a5db3da1efc615a5e933d02f352554341acf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9mi=20Calixte?= Date: Thu, 6 Dec 2018 12:23:54 -0500 Subject: [PATCH] System bindings --- recursion.go | 24 ++++++++++++++ reflection.go | 31 ++++++++++++++++++ sys.go | 89 +++++++++++++++++++++++++++++++++++++++++++++++++++ thread.go | 67 ++++++++++++++++++++++++++++++++++++++ 4 files changed, 211 insertions(+) create mode 100644 recursion.go create mode 100644 reflection.go create mode 100644 sys.go create mode 100644 thread.go diff --git a/recursion.go b/recursion.go new file mode 100644 index 0000000..8d133ff --- /dev/null +++ b/recursion.go @@ -0,0 +1,24 @@ +package python3 + +/* +#include "Python.h" +#include "macro.h" +*/ +import "C" + +import ( + "unsafe" +) + +//Py_EnterRecursiveCall : https://docs.python.org/3/c-api/exceptions.html#c.Py_EnterRecursiveCall +func Py_EnterRecursiveCall(where string) int { + cwhere := C.CString(where) + C.free(unsafe.Pointer(cwhere)) + + return int(C._go_Py_EnterRecursiveCall(cwhere)) +} + +//Py_LeaveRecursiveCall : https://docs.python.org/3/c-api/exceptions.html#c.Py_LeaveRecursiveCall +func Py_LeaveRecursiveCall() { + C._go_Py_LeaveRecursiveCall() +} diff --git a/reflection.go b/reflection.go new file mode 100644 index 0000000..cbdc175 --- /dev/null +++ b/reflection.go @@ -0,0 +1,31 @@ +package python3 + +/* +#include "Python.h" +*/ +import "C" + +//PyEval_GetBuiltins : https://docs.python.org/3/c-api/reflection.html?highlight=reflection#c.PyEval_GetBuiltins +func PyEval_GetBuiltins() *PyObject { + return togo(C.PyEval_GetBuiltins()) +} + +//PyEval_GetLocals : https://docs.python.org/3/c-api/reflection.html?highlight=reflection#c.PyEval_GetLocals +func PyEval_GetLocals() *PyObject { + return togo(C.PyEval_GetLocals()) +} + +//PyEval_GetGlobals : https://docs.python.org/3/c-api/reflection.html?highlight=reflection#c.PyEval_GetGlobals +func PyEval_GetGlobals() *PyObject { + return togo(C.PyEval_GetGlobals()) +} + +//PyEval_GetFuncName : https://docs.python.org/3/c-api/reflection.html?highlight=reflection#c.PyEval_GetFuncName +func PyEval_GetFuncName(pyFunc *PyObject) string { + return C.GoString(C.PyEval_GetFuncName(toc(pyFunc))) +} + +//PyEval_GetFuncDesc : https://docs.python.org/3/c-api/reflection.html?highlight=reflection#c.PyEval_GetFuncDesc +func PyEval_GetFuncDesc(pyFunc *PyObject) string { + return C.GoString(C.PyEval_GetFuncDesc(toc(pyFunc))) +} diff --git a/sys.go b/sys.go new file mode 100644 index 0000000..d85d8ab --- /dev/null +++ b/sys.go @@ -0,0 +1,89 @@ +package python3 + +/* +#include "Python.h" +*/ +import "C" +import ( + "fmt" + "unsafe" +) + +//PySys_GetObject : https://docs.python.org/3/c-api/sys.html#c.PySys_GetObject +func PySys_GetObject(name string) *PyObject { + cname := C.CString(name) + defer C.free(unsafe.Pointer(cname)) + + return togo(C.PySys_GetObject(cname)) +} + +//PySys_SetObject : https://docs.python.org/3/c-api/sys.html#c.PySys_SetObject +func PySys_SetObject(name string, v *PyObject) int { + cname := C.CString(name) + defer C.free(unsafe.Pointer(cname)) + + return int(C.PySys_SetObject(cname, toc(v))) +} + +//PySys_ResetWarnOptions : https://docs.python.org/3/c-api/sys.html#c.PySys_ResetWarnOptions +func PySys_ResetWarnOptions() { + C.PySys_ResetWarnOptions() +} + +//PySys_AddWarnOption : https://docs.python.org/3/c-api/sys.html#c.PySys_AddWarnOption +func PySys_AddWarnOption(s string) error { + cs := C.CString(s) + defer C.free(unsafe.Pointer(cs)) + + ws := C.Py_DecodeLocale(cs, nil) + if ws == nil { + return fmt.Errorf("fail to call Py_DecodeLocale on '%s'", s) + } + defer C.PyMem_RawFree(unsafe.Pointer(ws)) + + C.PySys_AddWarnOption(ws) + + return nil +} + +//PySys_AddWarnOptionUnicode : https://docs.python.org/3/c-api/sys.html#c.PySys_AddWarnOptionUnicode +func PySys_AddWarnOptionUnicode(unicode *PyObject) { + C.PySys_AddWarnOptionUnicode(toc(unicode)) +} + +//PySys_SetPath : https://docs.python.org/3/c-api/sys.html#c.PySys_SetPath +func PySys_SetPath(path string) error { + cpath := C.CString(path) + defer C.free(unsafe.Pointer(cpath)) + + wpath := C.Py_DecodeLocale(cpath, nil) + if wpath == nil { + return fmt.Errorf("fail to call Py_DecodeLocale on '%s'", path) + } + defer C.PyMem_RawFree(unsafe.Pointer(wpath)) + + C.PySys_SetPath(wpath) + + return nil +} + +//PySys_AddXOption : https://docs.python.org/3/c-api/sys.html#c.PySys_AddXOption +func PySys_AddXOption(s string) error { + cs := C.CString(s) + defer C.free(unsafe.Pointer(cs)) + + ws := C.Py_DecodeLocale(cs, nil) + if ws == nil { + return fmt.Errorf("fail to call Py_DecodeLocale on '%s'", s) + } + defer C.PyMem_RawFree(unsafe.Pointer(ws)) + + C.PySys_AddXOption(ws) + + return nil +} + +//PySys_GetXOptions : https://docs.python.org/3/c-api/sys.html#c.PySys_GetXOptions +func PySys_GetXOptions() *PyObject { + return togo(C.PySys_GetXOptions()) +} diff --git a/thread.go b/thread.go new file mode 100644 index 0000000..9da3782 --- /dev/null +++ b/thread.go @@ -0,0 +1,67 @@ +package python3 + +/* +#include "Python.h" +*/ +import "C" + +//PyThreadState : https://docs.python.org/3/c-api/init.html#c.PyThreadState +type PyThreadState C.PyThreadState + +//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 +type PyGILState C.PyGILState_STATE + +//PyEval_InitThreads : https://docs.python.org/3/c-api/init.html#c.PyEval_InitThreads +func PyEval_InitThreads() { + C.PyEval_InitThreads() +} + +//PyEval_ThreadsInitialized : https://docs.python.org/3/c-api/init.html#c.PyEval_ThreadsInitialized +func PyEval_ThreadsInitialized() bool { + return C.PyEval_ThreadsInitialized() != 0 +} + +//PyEval_SaveThread : https://docs.python.org/3/c-api/init.html#c.PyEval_SaveThread +func PyEval_SaveThread() *PyThreadState { + return (*PyThreadState)(C.PyEval_SaveThread()) +} + +//PyEval_RestoreThread : https://docs.python.org/3/c-api/init.html#c.PyEval_RestoreThread +func PyEval_RestoreThread(tstate *PyThreadState) { + C.PyEval_RestoreThread((*C.PyThreadState)(tstate)) +} + +//PyThreadState_Get : https://docs.python.org/3/c-api/init.html#c.PyThreadState_Get +func PyThreadState_Get() *PyThreadState { + return (*PyThreadState)(C.PyThreadState_Get()) +} + +//PyThreadState_Swap : https://docs.python.org/3/c-api/init.html#c.PyThreadState_Swap +func PyThreadState_Swap(tstate *PyThreadState) *PyThreadState { + return (*PyThreadState)(C.PyThreadState_Swap((*C.PyThreadState)(tstate))) +} + +//PyEval_ReInitThreads : https://docs.python.org/3/c-api/init.html#c.PyEval_ReInitThreads +func PyEval_ReInitThreads() { + C.PyEval_ReInitThreads() +} + +//PyGILState_Ensure : https://docs.python.org/3/c-api/init.html#c.PyGILState_Ensure +func PyGILState_Ensure() PyGILState { + return PyGILState(C.PyGILState_Ensure()) +} + +//PyGILState_Release : https://docs.python.org/3/c-api/init.html#c.PyGILState_Release +func PyGILState_Release(state PyGILState) { + C.PyGILState_Release(C.PyGILState_STATE(state)) +} + +//PyGILState_GetThisThreadState : https://docs.python.org/3/c-api/init.html#c.PyGILState_GetThisThreadState +func PyGILState_GetThisThreadState() *PyThreadState { + return (*PyThreadState)(C.PyGILState_GetThisThreadState()) +} + +//PyGILState_Check : https://docs.python.org/3/c-api/init.html#c.PyGILState_Check +func PyGILState_Check() bool { + return C.PyGILState_Check() == 1 +}