File: test_clib.py

package info (click to toggle)
ctypes 0.9.5-1
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 2,492 kB
  • ctags: 2,598
  • sloc: ansic: 14,485; sh: 9,719; asm: 5,509; python: 5,186; makefile: 198
file content (274 lines) | stat: -rw-r--r-- 6,467 bytes parent folder | download
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
# Demonstrate some functions from the standard C library.

from ctypes import cdll, POINTER, CFUNCTYPE

import os, sys
from ctypes import c_char_p, c_double, c_char, sizeof

def c_string(init, size=None):
    """c_string(aString) -> character array
    c_string(anInteger) -> character array
    c_string(aString, anInteger) -> character array
    """
    if isinstance(init, str):
        if size is None:
            size = len(init)+1
        buftype = c_char * size
        buf = buftype()
        buf.value = init
        return buf
    elif isinstance(init, int):
        buftype = c_char * init
        buf = buftype()
        return buf
    raise TypeError, init

if os.name == "nt":
    libc = cdll.msvcrt
    printf = libc.printf
    sscanf = libc.sscanf
    snprintf = libc._snprintf
    libmath = cdll.msvcrt

    msvcrt = cdll.msvcrt
elif os.name == "posix":
    if sys.platform == "darwin":
        libc = cdll.LoadLibrary("/usr/lib/libc.dylib")
        libmath = cdll.LoadLibrary("/usr/lib/libm.dylib")
    elif sys.platform == "cygwin":
        libmath = libc = cdll.LoadLibrary("/bin/cygwin1.dll")
    else:
        libc = cdll.LoadLibrary("/lib/libc.so.6")
        libmath = cdll.LoadLibrary("/lib/libm.so.6")
    printf = libc.printf
    sscanf = libc.sscanf
    snprintf = libc.snprintf

from ctypes import Structure, c_int, c_float, byref

def test_printf():
    result = printf("%s%s %d 0x%x %f %d\n",
                    "Hello, ",
##                    u"World!",
                    "World!",
                    c_int(100),
                    c_int(100),
                    c_double(3.14159265),
                    100)

    print "printf returned %d\n" % result

def test_time():
    t = c_int()
    result = libc.time(byref(t))

    print "time since epoch in seconds:", result, t.value

def test_sscanf():
    t1 = c_int(3)
    t2 = c_float(32)
    f = c_double()
    result = sscanf("1234 Hello, 5678 World 3.14159265",
                    "%d Hello, %f World %lf %lf",
                    byref(t1),
                    byref(t2),
                    byref(f))
    print "sscanf found %d fields:" % result,
    print t1.value, t2.value, f.value

def test_snprintf():
    buf = c_string("\000"*32)
    print snprintf(buf, sizeof(buf), "Hello, world")
    print "value: %r" % buf.value

def test_strtok():
    strtok = libc.strtok
    strtok.restype = c_char_p

    delim = " "

    t = c_string("Hi, how are you?")
    s = t # we must hold a ref to the original string
    while 1:
        r = strtok(s, delim)
        if not r:
            break
        s = 0
        print "\t", r
    print
    
def test_sqrt():
    sqrt = libmath.sqrt
    sqrt.restype = c_double
    print sqrt(c_double(2))

# Other examples:
#
# sqrt: returns double
# div: returns a structure containig two ints
# ldiv: returns a structure containing two longs
# _findfirst, 
# _findfirst64, returns __int64
# fopen - returns FILE *
# localtime, gmtime, ... - return struct tm *

def test_qsort():
    import array, random

##    class CMPFUNC(CFuncPtr):
##        _argtypes_ = c_int, c_int
##        _flags_ = FUNCFLAG_CDECL

    CMPFUNC = CFUNCTYPE(c_int, c_int, c_int)

    def compare(a, b):
        ad = c_int.from_address(a)
        bd = c_int.from_address(b)
        return cmp(ad.value, bd.value)


    data = array.array("i", range(5))
    random.shuffle(data)

    print "BEFORE qsort", data
    libc.qsort(data.buffer_info()[0],
               len(data),
               4,
               CMPFUNC(compare))
    print "AFTER qsort", data
    print

def test_qsort_1():
    from _ctypes import Array
    import random

    class IntArray10(Array):
        _type_ = c_int
        _length_ = 10

    l = range(10)
    random.shuffle(l)

    ia10 = IntArray10(*l)

    print "BEFORE sorting:",
    for item in ia10:
        print item,
    print

    # It would be even nicer to define the callback so that
    # it auto-converts the pointer into the value,
    # then we could use callback(cmp, ...)

    def compare(a, b):
        ad = c_int.from_address(a)
        bd = c_int.from_address(b)
        return cmp(ad.value, bd.value)
    
##    class CMPFUNC(CFuncPtr):
##        _argtypes_ = c_int, c_int
##        _flags_ = FUNCFLAG_CDECL

    CMPFUNC = CFUNCTYPE(c_int, c_int, c_int)
    
    libc.qsort(ia10,
               len(ia10),
               4,
               CMPFUNC(compare))
    
    print "AFTER  sorting:", 
    for item in ia10:
        print item,
    print
    print

def test_qsort_2():
    import random

    IntArray10 = c_int * 10

    ia10 = IntArray10()

    for i in range(10):
        num = random.choice(range(100))
        ia10[i] = c_int(num)
    print "BEFORE sorting:",
    for item in ia10:
        print item,
    print

    # It would be even nicer to define the callback so that
    # it auto-converts the pointer into the value,
    # then we could use callback(cmp, ...)

    def compare(a, b):
        ad = c_int.from_address(a)
        bd = c_int.from_address(b)
        return cmp(ad.value, bd.value)
    
##    class CMPFUNC(CFuncPtr):
##        _argtypes_ = c_int, c_int
##        _flags_ = FUNCFLAG_CDECL

    CMPFUNC = CFUNCTYPE(c_int, c_int, c_int)

    libc.qsort(ia10,
               len(ia10),
               4,
               CMPFUNC(compare))

    print "AFTER  sorting:", 
    for item in ia10:
        print item,
    print
    print

def test_qsort_5():
    # demonstrates callback functions specified by using types in the _types_ list
    import random

    IntArray10 = c_int * 10

    ia10 = IntArray10()

    for i in range(10):
        num = random.choice(range(100))
        ia10[i] = num
    print "BEFORE sorting:",
    for item in ia10:
        print item,
    print

    def compare(a, b):
        "the callback now receives pointers to c_int:"
        return cmp(a.contents.value, b.contents.value)
    
##    class CMPFUNC(CFuncPtr):
##        _argtypes_ = POINTER(c_int), POINTER(c_int)
##        _flags_ = FUNCFLAG_CDECL

    CMPFUNC = CFUNCTYPE(c_int, POINTER(c_int), POINTER(c_int))

    libc.qsort(ia10,
               len(ia10),
               4,
               CMPFUNC(compare))

    print "AFTER  sorting:", 
    for item in ia10:
        print item,
    print
    print


if __name__ == '__main__':
    test_printf()
    test_time()
    test_sscanf()
    test_snprintf()
    test_strtok()
    test_sqrt()
    test_qsort()
    test_qsort_1()
    test_qsort_2()
    test_qsort_5()