2018-12-13 22:09:42 +00:00
|
|
|
/*
|
|
|
|
Unless explicitly stated otherwise all files in this repository are licensed
|
2021-08-05 10:47:52 +00:00
|
|
|
under the MIT License.
|
2018-12-13 22:09:42 +00:00
|
|
|
This product includes software developed at Datadog (https://www.datadoghq.com/).
|
|
|
|
Copyright 2018 Datadog, Inc.
|
|
|
|
*/
|
|
|
|
|
2018-12-06 17:13:32 +00:00
|
|
|
package python3
|
|
|
|
|
|
|
|
/*
|
|
|
|
#cgo pkg-config: python3
|
|
|
|
#include "Python.h"
|
|
|
|
*/
|
|
|
|
import "C"
|
|
|
|
import (
|
2018-12-07 16:51:35 +00:00
|
|
|
"fmt"
|
2018-12-06 17:13:32 +00:00
|
|
|
"unsafe"
|
|
|
|
)
|
|
|
|
|
|
|
|
//Py_Main : https://docs.python.org/3/c-api/veryhigh.html?highlight=pycompilerflags#c.Py_Main
|
2018-12-07 00:05:44 +00:00
|
|
|
// "error" will be set if we fail to call "Py_DecodeLocale" on every "args".
|
|
|
|
func Py_Main(args []string) (int, error) {
|
2018-12-06 17:13:32 +00:00
|
|
|
argc := C.int(len(args))
|
|
|
|
argv := make([]*C.wchar_t, argc, argc)
|
|
|
|
for i, arg := range args {
|
|
|
|
carg := C.CString(arg)
|
|
|
|
defer C.free(unsafe.Pointer(carg))
|
|
|
|
|
|
|
|
warg := C.Py_DecodeLocale(carg, nil)
|
|
|
|
if warg == nil {
|
2018-12-07 00:05:44 +00:00
|
|
|
return -1, fmt.Errorf("fail to call Py_DecodeLocale on '%s'", arg)
|
2018-12-06 17:13:32 +00:00
|
|
|
}
|
|
|
|
// Py_DecodeLocale requires a call to PyMem_RawFree to free the memory
|
|
|
|
defer C.PyMem_RawFree(unsafe.Pointer(warg))
|
|
|
|
argv[i] = warg
|
|
|
|
}
|
|
|
|
|
2018-12-07 00:05:44 +00:00
|
|
|
return int(C.Py_Main(argc, (**C.wchar_t)(unsafe.Pointer(&argv[0])))), nil
|
2018-12-06 17:13:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//PyRun_AnyFile : https://docs.python.org/3/c-api/veryhigh.html?highlight=pycompilerflags#c.PyRun_AnyFile
|
2018-12-07 00:05:44 +00:00
|
|
|
// "error" will be set if we fail to open "filename".
|
|
|
|
func PyRun_AnyFile(filename string) (int, error) {
|
2018-12-06 17:13:32 +00:00
|
|
|
cfilename := C.CString(filename)
|
|
|
|
defer C.free(unsafe.Pointer(cfilename))
|
|
|
|
|
|
|
|
mode := C.CString("r")
|
|
|
|
defer C.free(unsafe.Pointer(mode))
|
|
|
|
|
2018-12-07 00:05:44 +00:00
|
|
|
cfile, err := C.fopen(cfilename, mode)
|
|
|
|
if err != nil {
|
|
|
|
return -1, fmt.Errorf("fail to open '%s': %s", filename, err)
|
2018-12-06 17:13:32 +00:00
|
|
|
}
|
|
|
|
defer C.fclose(cfile)
|
|
|
|
|
|
|
|
// C.PyRun_AnyFile is a macro, using C.PyRun_AnyFileFlags instead
|
2018-12-07 00:05:44 +00:00
|
|
|
return int(C.PyRun_AnyFileFlags(cfile, cfilename, nil)), nil
|
2018-12-06 17:13:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//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)
|
|
|
|
defer C.free(unsafe.Pointer(ccommand))
|
|
|
|
|
|
|
|
// C.PyRun_SimpleString is a macro, using C.PyRun_SimpleStringFlags instead
|
|
|
|
return int(C.PyRun_SimpleStringFlags(ccommand, nil))
|
|
|
|
}
|