0

I have the go library with the following signature:

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

In Python I did like the below:

import ctypes
library = ctypes.cdll.LoadLibrary('./lib.so')
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("BTC".encode('utf-8'), "3", "JAN".encode('utf-8'), "23".encode('utf-8'))
j= get_data(b"XYZ", 3, "JAN", 23)
print(j)

and it gives the error

ctypes.ArgumentError: argument 3: <class 'TypeError'>: 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) + "-" + 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("BTC", "3", "JAN", "23")
# j= get_data(b"BTC", 3, "JAN", 23)
print(j.decode('utf-8'))

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) + "-" + C.GoString(day) + C.GoString(month) + C.GoString(year)
    log.Println(x)

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

4
  • I don't know Go so other things could be wrong as well, but your .argtypes don't match the arguments (specifically the 3rd parameter as the error states), and according to the answer to your other question returning Go strings isn't supported for C interfacing. Commented Jan 2, 2023 at 19:28
  • Go strings and C strings are not the same. You must convert between them when crossing the language barrier. The cgo documentation has some information about this. Commented Jan 2, 2023 at 22:34
  • @HymnsForDisco question updated, can you please help? Commented Jan 3, 2023 at 6:33
  • Why is get_data.argtypes = ... commented out? Also, you shouldn't be using wchar_t, which is for "wide characters". Commented Jan 3, 2023 at 7:06

1 Answer 1

1

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]

Sign up to request clarification or add additional context in comments.

2 Comments

Now it gives a new error: ValueError: character U+100392f7 is not in range [U+0000; U+10ffff]
As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.