Skip to content

Commit

Permalink
get rid of pkg-config, migrate to Python 3.9 and add a few other opti…
Browse files Browse the repository at this point in the history
…ons in high_level_layer.go
  • Loading branch information
erstam committed Aug 29, 2022
1 parent 5ac8201 commit b60f686
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 7 deletions.
3 changes: 2 additions & 1 deletion dict.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,5 +138,6 @@ func PyDict_Next(p *PyObject, ppos *int, pkey, pvalue **PyObject) bool {

//PyDict_ClearFreeList : https://docs.python.org/3/c-api/dict.html#c.PyDict_ClearFreeList
func PyDict_ClearFreeList() int {
return int(C.PyDict_ClearFreeList())
// PyDict_ClearFreeList removed in Python 3.9, use PyGC_Collect instead.
return int(C.PyGC_Collect())
}
3 changes: 2 additions & 1 deletion float.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,5 +59,6 @@ func PyFloat_GetMin() float64 {

//PyFloat_ClearFreeList : https://docs.python.org/3/c-api/float.html#c.PyFloat_ClearFreeList
func PyFloat_ClearFreeList() int {
return int(C.PyFloat_ClearFreeList())
// PyFloat_ClearFreeList removed in Python 3.9, use PyGC_Collect instead.
return int(C.PyGC_Collect())
}
43 changes: 42 additions & 1 deletion high_level_layer.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,13 @@ Copyright 2018 Datadog, Inc.
package python3

/*
#cgo pkg-config: python3
#include "Python.h"
*/
import "C"
import (
"errors"
"fmt"
"strings"
"unsafe"
)

Expand Down Expand Up @@ -57,6 +58,46 @@ func PyRun_AnyFile(filename string) (int, error) {
return int(C.PyRun_AnyFileFlags(cfile, cfilename, nil)), nil
}

//PyRun_SimpleFile : https://docs.python.org/3.9/c-api/veryhigh.html?highlight=pycompilerflags#c.PyRun_SimpleFile
// "error" will be set if we fail to open "filename".
func PyRun_SimpleFile(filename string) (int, error) {
cfilename := C.CString(filename)
defer C.free(unsafe.Pointer(cfilename))

mode := C.CString("rb")
defer C.free(unsafe.Pointer(mode))

cfile, err := C.fopen(cfilename, mode)
if err != nil {
return -1, fmt.Errorf("fail to open '%s': %v", filename, err)
}
defer C.fclose(cfile)

// For now we have an issue (seg fault) with this function,
// so let's read the input file ourselves and execute as
// a string command instead.
//ret := C.PyRun_SimpleFileExFlags(cfile, cfilename, 1, nil)


C.fseek(cfile , 0 , C.SEEK_END);
lSize := C.ftell(cfile);
C.rewind(cfile);

cbuffer := C.CString(strings.Repeat("0", int(lSize)))
if cbuffer == nil {
return 1, errors.New("memory alloc fails")
};
defer C.free(unsafe.Pointer(cbuffer))

if 1!=C.fread( unsafe.Pointer(cbuffer) , C.ulonglong(lSize), 1 , cfile) {
return 1, errors.New("entire read fails")
}

ret := C.PyRun_SimpleStringFlags(cbuffer, nil)

return int(ret), nil
}

//PyRun_SimpleString : https://docs.python.org/3/c-api/veryhigh.html?highlight=pycompilerflags#c.PyRun_SimpleString
func PyRun_SimpleString(command string) int {
ccommand := C.CString(command)
Expand Down
3 changes: 2 additions & 1 deletion list.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,5 +84,6 @@ func PyList_AsTuple(list *PyObject) *PyObject {

//PyList_ClearFreeList : https://docs.python.org/3/c-api/list.html#c.PyList_ClearFreeList
func PyList_ClearFreeList() int {
return int(C.PyList_ClearFreeList())
// PyList_ClearFreeList removed in Python 3.9, use PyGC_Collect instead.
return int(C.PyGC_Collect())
}
4 changes: 4 additions & 0 deletions thread.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,19 @@ 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

/* Deprecated since Python 3.9, this function does nothing.
//PyEval_InitThreads : https://docs.python.org/3/c-api/init.html#c.PyEval_InitThreads
func PyEval_InitThreads() {
C.PyEval_InitThreads()
}
*/

/* Deprecated since Python 3.9, this function does nothing.
//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 {
Expand Down
6 changes: 3 additions & 3 deletions thread_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func TestThreadInitialization(t *testing.T) {

func TestGIL(t *testing.T) {
Py_Initialize()
PyEval_InitThreads()
//PyEval_InitThreads()

gil := PyGILState_Ensure()

Expand All @@ -30,7 +30,7 @@ func TestGIL(t *testing.T) {

func TestThreadState(t *testing.T) {
Py_Initialize()
PyEval_InitThreads()
//PyEval_InitThreads()

threadState := PyGILState_GetThisThreadState()

Expand All @@ -45,7 +45,7 @@ func TestThreadState(t *testing.T) {

func TestThreadSaveRestore(t *testing.T) {
Py_Initialize()
PyEval_InitThreads()
//PyEval_InitThreads()

threadState := PyEval_SaveThread()

Expand Down

0 comments on commit b60f686

Please sign in to comment.