2 Star 0 Fork 0

mirrors_DataDog / go-python3

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
high_level_layer.go 2.00 KB
一键复制 编辑 原始数据 按行查看 历史
Ivo Anjo 提交于 2021-08-05 11:47 . Actually template the license
/*
Unless explicitly stated otherwise all files in this repository are licensed
under the MIT License.
This product includes software developed at Datadog (https://www.datadoghq.com/).
Copyright 2018 Datadog, Inc.
*/
package python3
/*
#cgo pkg-config: python3
#include "Python.h"
*/
import "C"
import (
"fmt"
"unsafe"
)
//Py_Main : https://docs.python.org/3/c-api/veryhigh.html?highlight=pycompilerflags#c.Py_Main
// "error" will be set if we fail to call "Py_DecodeLocale" on every "args".
func Py_Main(args []string) (int, error) {
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 {
return -1, fmt.Errorf("fail to call Py_DecodeLocale on '%s'", arg)
}
// Py_DecodeLocale requires a call to PyMem_RawFree to free the memory
defer C.PyMem_RawFree(unsafe.Pointer(warg))
argv[i] = warg
}
return int(C.Py_Main(argc, (**C.wchar_t)(unsafe.Pointer(&argv[0])))), nil
}
//PyRun_AnyFile : https://docs.python.org/3/c-api/veryhigh.html?highlight=pycompilerflags#c.PyRun_AnyFile
// "error" will be set if we fail to open "filename".
func PyRun_AnyFile(filename string) (int, error) {
cfilename := C.CString(filename)
defer C.free(unsafe.Pointer(cfilename))
mode := C.CString("r")
defer C.free(unsafe.Pointer(mode))
cfile, err := C.fopen(cfilename, mode)
if err != nil {
return -1, fmt.Errorf("fail to open '%s': %s", filename, err)
}
defer C.fclose(cfile)
// C.PyRun_AnyFile is a macro, using C.PyRun_AnyFileFlags instead
return int(C.PyRun_AnyFileFlags(cfile, cfilename, nil)), 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)
defer C.free(unsafe.Pointer(ccommand))
// C.PyRun_SimpleString is a macro, using C.PyRun_SimpleStringFlags instead
return int(C.PyRun_SimpleStringFlags(ccommand, nil))
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/mirrors_DataDog/go-python3.git
git@gitee.com:mirrors_DataDog/go-python3.git
mirrors_DataDog
go-python3
go-python3
master

搜索帮助