System bindings

master
Rémi Calixte 2018-12-06 12:23:54 -05:00
parent a7c8efd9c1
commit f7d9a5db3d
No known key found for this signature in database
GPG Key ID: 9D8CE6B3C11285A0
4 changed files with 211 additions and 0 deletions

24
recursion.go Normal file
View File

@ -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()
}

31
reflection.go Normal file
View File

@ -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)))
}

89
sys.go Normal file
View File

@ -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())
}

67
thread.go Normal file
View File

@ -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
}