在Python和Go库之间传递字符串和整数参数时遇到了困难。

huangapple go评论76阅读模式
英文:

Strugglign to pass string and int parameters from Python to Go library

问题

我有以下签名的Go库:

//export getData
func getData(symbol string, day int, month string, year int) string {
	return "getData2"
}

在Python中,我像下面这样做:

import ctypes
library = ctypes.cdll.LoadLibrary('./lib.so')
get_data = library.getData

# 让Python将其值转换为C表示形式。
get_data.argtypes = [ctypes.c_char_p, ctypes.c_int,ctypes.c_int,ctypes.c_int]
get_data.restype = ctypes.c_wchar_p

j = get_data(b"XYZ", 3, "JAN", 23)
print(j.decode('utf-8'))

它会报错:

ctypes.ArgumentError: argument 3: <class 'TypeError'>: wrong type

我正在使用Python 3.9

更新

我对Go函数签名进行了更改,如下所示:

func getData(symbol, day, month, year *C.char) *C.char {
	var instrumentName, combine string
	x := C.GoString(symbol) + "-" + C.GoString(day) + C.GoString(month) + C.GoString(year)
	log.Println(x)
 ....

在Python中,我进行了如下更改:

get_data = library.getData

# 让Python将其值转换为C表示形式。
# get_data.argtypes = [ctypes.c_char_p, ctypes.c_char_p,ctypes.c_char_p,ctypes.c_char_p]
get_data.restype = ctypes.c_wchar_p

j = get_data("BTC", "3", "JAN", "23")
# j = get_data(b"BTC", 3, "JAN", 23)
print(j.decode('utf-8'))

没有参数问题,但我遇到的问题是它在Go代码中获取每个参数的第一个字符,即:

x := C.GoString(symbol) + "-" + C.GoString(day) + C.GoString(month) + C.GoString(year)
log.Println(x)

所以它打印的不是BTC-3JAN23,而是B-3J2

英文:

I have the go library with the following signature:

//export getData
func getData(symbol string, day int, month string, year int) string {
	return &quot;getData2&quot;
}

In Python I did like the below:

import ctypes
library = ctypes.cdll.LoadLibrary(&#39;./lib.so&#39;)
get_data = library.getData

# Make python convert its values to C representation.
get_data.argtypes = [ctypes.c_char_p, ctypes.c_int,ctypes.c_int,ctypes.c_int]
get_data.restype = ctypes.c_wchar

# j= get_data(&quot;BTC&quot;.encode(&#39;utf-8&#39;), &quot;3&quot;, &quot;JAN&quot;.encode(&#39;utf-8&#39;), &quot;23&quot;.encode(&#39;utf-8&#39;))
j= get_data(b&quot;XYZ&quot;, 3, &quot;JAN&quot;, 23)
print(j)

and it gives the error

ctypes.ArgumentError: argument 3: &lt;class &#39;TypeError&#39;&gt;: wrong type

I am using Python 3.9

Updates

I made changes in Go Function Signature like this:

func getData(symbol, day, month, year *C.char) *C.char {
	var instrumentName, combine string
	x := C.GoString(symbol) + &quot;-&quot; + C.GoString(day) + C.GoString(month) + C.GoString(year)
	log.Println(x)
 ....

And in Python like this:

get_data = library.getData

# Make python convert its values to C representation.
# get_data.argtypes = [ctypes.c_char_p, ctypes.c_char_p,ctypes.c_char_p,ctypes.c_char_p]
get_data.restype = ctypes.c_wchar_p

j= get_data(&quot;BTC&quot;, &quot;3&quot;, &quot;JAN&quot;, &quot;23&quot;)
# j= get_data(b&quot;BTC&quot;, 3, &quot;JAN&quot;, 23)
print(j.decode(&#39;utf-8&#39;))

No parameter issue but the issue I am getting that it is fetching first param of each param in Go code, that is:

x := C.GoString(symbol) + &quot;-&quot; + C.GoString(day) + C.GoString(month) + C.GoString(year)
	log.Println(x)

So instead of printing BTC-3JAN23, it prints B-3J2

答案1

得分: 1

在第4行,你写的是get_data.argtypes = [ctypes.c_char_p, ctypes.c_int,ctypes.c_int,ctypes.c_int],但是由于第3个参数是一个字符串,所以应该改为get_data.argtypes = [ctypes.c_char_p, ctypes.c_int,ctypes.c_char_p,ctypes.c_int]

英文:

On line 4 you did get_data.argtypes = [ctypes.c_char_p, ctypes.c_int,ctypes.c_int,ctypes.c_int] but since the 3rd argument is a string then it should be get_data.argtypes = [ctypes.c_char_p, ctypes.c_int,ctypes.c_char_p,ctypes.c_int]

huangapple
  • 本文由 发表于 2023年1月3日 03:24:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/74986298.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定