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

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

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

问题

我有以下签名的Go库:

  1. //export getData
  2. func getData(symbol string, day int, month string, year int) string {
  3. return "getData2"
  4. }

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

  1. import ctypes
  2. library = ctypes.cdll.LoadLibrary('./lib.so')
  3. get_data = library.getData
  4. # 让Python将其值转换为C表示形式。
  5. get_data.argtypes = [ctypes.c_char_p, ctypes.c_int,ctypes.c_int,ctypes.c_int]
  6. get_data.restype = ctypes.c_wchar_p
  7. j = get_data(b"XYZ", 3, "JAN", 23)
  8. print(j.decode('utf-8'))

它会报错:

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

我正在使用Python 3.9

更新

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

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

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

  1. get_data = library.getData
  2. # 让Python将其值转换为C表示形式。
  3. # get_data.argtypes = [ctypes.c_char_p, ctypes.c_char_p,ctypes.c_char_p,ctypes.c_char_p]
  4. get_data.restype = ctypes.c_wchar_p
  5. j = get_data("BTC", "3", "JAN", "23")
  6. # j = get_data(b"BTC", 3, "JAN", 23)
  7. print(j.decode('utf-8'))

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

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

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

英文:

I have the go library with the following signature:

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

In Python I did like the below:

  1. import ctypes
  2. library = ctypes.cdll.LoadLibrary(&#39;./lib.so&#39;)
  3. get_data = library.getData
  4. # Make python convert its values to C representation.
  5. get_data.argtypes = [ctypes.c_char_p, ctypes.c_int,ctypes.c_int,ctypes.c_int]
  6. get_data.restype = ctypes.c_wchar
  7. # 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;))
  8. j= get_data(b&quot;XYZ&quot;, 3, &quot;JAN&quot;, 23)
  9. print(j)

and it gives the error

  1. 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:

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

And in Python like this:

  1. get_data = library.getData
  2. # Make python convert its values to C representation.
  3. # get_data.argtypes = [ctypes.c_char_p, ctypes.c_char_p,ctypes.c_char_p,ctypes.c_char_p]
  4. get_data.restype = ctypes.c_wchar_p
  5. j= get_data(&quot;BTC&quot;, &quot;3&quot;, &quot;JAN&quot;, &quot;23&quot;)
  6. # j= get_data(b&quot;BTC&quot;, 3, &quot;JAN&quot;, 23)
  7. 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:

  1. x := C.GoString(symbol) + &quot;-&quot; + C.GoString(day) + C.GoString(month) + C.GoString(year)
  2. 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:

确定