How does ctypes know when to throw an ArgumentError, but only occasionally?

I am experimenting with ctypes and I found something that baffled me. I have a .so file that exports this function:

void hello(int a) {
    printf("hello, a=%d", a);
}

I call it from Python, but I put a parameter of the wrong type to see what happens:

mydll = ctypes.CDLL("libhello.so")
mydll.hello('this is string')

This does not cause an error and just prints hello, a=-1219868708. But if I pass a double instead of - mydll.hello(1.0), then it throws an exception:

ctypes.ArgumentError: argument 1:: Don't know how to convert parameter 1

It seems strange to me that exported C functions usually do not have information about their signature (unlike C ++ functions), just the name of the function and possibly the total number of parameter bytes.

, , , , ctypes , , 2 - mydll.hello('string1', 'string2'), ; hello, a=-1223698284.

, . ctypes, , a double , , 2 ?

+4
1

ctypes :

, , , Unicode Python, .

, Python float, ctypes (c_double c_float). ArgumentError.

, : , double float , C. , ctypes , C, , .

+3

Source: https://habr.com/ru/post/1615891/


All Articles