引数の型を伝えるのは良い考えです。ctypes
間違っている場合は教えてくれます:
Python 3.2.3 (default, Apr 11 2012, 07:15:24) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import ctypes
>>> libc = ctypes.CDLL("msvcrt")
>>> libc.strlen('hello')
1
>>> libc.strlen.argtypes=[ctypes.c_char_p]
>>> libc.strlen('hello')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ctypes.ArgumentError: argument 1: <class 'TypeError'>: wrong type
Python 3 では、文字列はデフォルトの Unicode です。 b''
構文はバイト文字列であり、これは strlen が機能するものです:
>>> libc.strlen(b'hello')
5
少なくとも Windows では、strlen のワイド バージョンがあります。
>>> libc.wcslen.argtypes=[ctypes.c_wchar_p]
>>> libc.wcslen('hello')
5