ctype C library call always returns 0 with Python3
Nobody
nobody at nowhere.com
Sat May 19 07:20:24 EDT 2012
On Sat, 19 May 2012 11:30:46 +0200, Johannes Bauer wrote:
> import ctypes
> libc = ctypes.cdll.LoadLibrary("/lib64/libc-2.14.1.so")
> print(libc.strchr("abcdef", ord("d")))
In 3.x, a string will be passed as a wchar_t*, not a char*. IOW, the
memory pointed to by the first argument to strchr() will consist mostly of
NUL bytes.
Either use a "bytes" instead of a string:
> print(libc.strchr(b"abcdef", ord("d")))
1984444291
or specify the argument types to force a conversion:
> libc.strchr.argtypes = [c_char_p, c_int]
> print(libc.strchr("abcdef", ord("d")))
1984755787
More information about the Python-list
mailing list