从 Python 调用 Go
Calling Go from Python
我试图 运行 来自 python 的一个简单的 go 脚本,但出现了分段错误。这是我的代码:
main.go
package main
import (
/*
typedef struct foo{
int a;
int b;
int c;
int d;
int e;
int f;
} foo;
*/
"C"
)
func main() {}
//export Foo
func Foo(t []int) C.foo {
return C.foo{}
}
main.py
# loading shared object
lib = cdll.LoadLibrary("main.so")
# go type
class GoSlice(Structure):
_fields_ = [("data", POINTER(c_void_p)), ("len", c_longlong), ("cap", c_longlong)]
lib.Foo.argtypes = [GoSlice]
lib.Foo.restype = c_void_p
t = GoSlice((c_void_p * 5)(1, 2, 3, 4, 5), 5, 5)
f = lib.Foo(t)
print(f)
有了那个代码,我得到了
140362617287784
[1] 23067 segmentation fault python3 main.py
现在,如果我从 main.go
中删除 e
和 f
,我会得到
None
并且没有更多的分段错误。
为什么结构中的成员数量在这里很重要?
[EDIT] 两者都 运行ning 在同一个地方,我 运行 一个命令 clear && go build -o main.so -buildmode=c-shared main.go && python3 main.py
您的 GO/C 代码是正确的。问题出在 python 脚本中。 lib.Foo.restype = c_void_p
调用需要一个空指针,但库 return 是一个 C 结构。您需要在 python 中定义 return 类型的 ctypes 结构,然后它将按预期工作。
main.go:
package main
import (
/*
typedef struct foo{
int a;
int b;
int c;
int d;
int e;
int f;
} foo;
*/
"C"
)
func main() {
}
//export Foo
func Foo(t []int) C.foo {
foo := C.foo{}
foo.a = 1 // setting some values to avoid seeing zeros
foo.b = 2
return foo
}
main.py:
from ctypes import *
# loading shared object
lib = cdll.LoadLibrary("main.so")
# go type
class GoSlice(Structure):
_fields_ = [("data", POINTER(c_void_p)), ("len", c_longlong), ("cap", c_longlong)]
class Foo(Structure):
_fields_ = [('a', c_int),
('b', c_int),
('c', c_int),
('d', c_int),
('e', c_int),
('f', c_int)]
lib.Foo.argtypes = [GoSlice]
lib.Foo.restype = Foo
t = GoSlice((c_void_p * 5)(1, 2, 3, 4, 5), 5, 5)
f = lib.Foo(t)
print(f)
print(f.a)
print(f.b)
然后 运行 go build -o main.so -buildmode=c-shared main.go && python main.py
它会打印:
go build -o main.so -buildmode=c-shared main.go && python3 main.py
<__main__.Foo object at 0x102608830>
1
2
我试图 运行 来自 python 的一个简单的 go 脚本,但出现了分段错误。这是我的代码:
main.go
package main
import (
/*
typedef struct foo{
int a;
int b;
int c;
int d;
int e;
int f;
} foo;
*/
"C"
)
func main() {}
//export Foo
func Foo(t []int) C.foo {
return C.foo{}
}
main.py
# loading shared object
lib = cdll.LoadLibrary("main.so")
# go type
class GoSlice(Structure):
_fields_ = [("data", POINTER(c_void_p)), ("len", c_longlong), ("cap", c_longlong)]
lib.Foo.argtypes = [GoSlice]
lib.Foo.restype = c_void_p
t = GoSlice((c_void_p * 5)(1, 2, 3, 4, 5), 5, 5)
f = lib.Foo(t)
print(f)
有了那个代码,我得到了
140362617287784
[1] 23067 segmentation fault python3 main.py
现在,如果我从 main.go
中删除 e
和 f
,我会得到
None
并且没有更多的分段错误。
为什么结构中的成员数量在这里很重要?
[EDIT] 两者都 运行ning 在同一个地方,我 运行 一个命令 clear && go build -o main.so -buildmode=c-shared main.go && python3 main.py
您的 GO/C 代码是正确的。问题出在 python 脚本中。 lib.Foo.restype = c_void_p
调用需要一个空指针,但库 return 是一个 C 结构。您需要在 python 中定义 return 类型的 ctypes 结构,然后它将按预期工作。
main.go:
package main
import (
/*
typedef struct foo{
int a;
int b;
int c;
int d;
int e;
int f;
} foo;
*/
"C"
)
func main() {
}
//export Foo
func Foo(t []int) C.foo {
foo := C.foo{}
foo.a = 1 // setting some values to avoid seeing zeros
foo.b = 2
return foo
}
main.py:
from ctypes import *
# loading shared object
lib = cdll.LoadLibrary("main.so")
# go type
class GoSlice(Structure):
_fields_ = [("data", POINTER(c_void_p)), ("len", c_longlong), ("cap", c_longlong)]
class Foo(Structure):
_fields_ = [('a', c_int),
('b', c_int),
('c', c_int),
('d', c_int),
('e', c_int),
('f', c_int)]
lib.Foo.argtypes = [GoSlice]
lib.Foo.restype = Foo
t = GoSlice((c_void_p * 5)(1, 2, 3, 4, 5), 5, 5)
f = lib.Foo(t)
print(f)
print(f.a)
print(f.b)
然后 运行 go build -o main.so -buildmode=c-shared main.go && python main.py
它会打印:
go build -o main.so -buildmode=c-shared main.go && python3 main.py
<__main__.Foo object at 0x102608830>
1
2