1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
|
from ctypes import c_char, byref, cdll, c_int, c_char_p
import os, sys
if os.name == "nt":
strchr = cdll.msvcrt.strchr
elif os.name == "posix":
if sys.platform == "darwin":
strchr = cdll.LoadLibrary("/usr/lib/libc.dylib").strchr
elif sys.platform == "cygwin":
strchr = cdll.LoadLibrary("/bin/cygwin1.dll").strchr
else:
strchr = cdll.LoadLibrary("/lib/libc.so.6").strchr
strchr.restype = c_char_p
strchr.argtypes = [c_char_p, c_int]
# It is now possible to call cdecl functions with more arguments
# than specified ibn argtypes (as in C)
##try:
## strchr("abc", 2, 3)
##except TypeError:
## pass
##else:
## raise Exception("didn't catch wrong number of args")
try:
strchr()
except TypeError:
pass
else:
raise ("didn't catch wrong number of args")
strchr.restype = c_char_p
strchr.argtypes = [c_char_p, c_int]
assert "def" == strchr("abcdef", ord("d"))
assert None == strchr("abcdef", ord("x"))
strchr.argtypes = [c_char_p, c_char]
assert None == strchr("abcdef", "x")
assert "def" == strchr("abcdef", "d")
|