Line data Source code
1 : #include <Python.h>
2 :
3 : #ifdef MS_WIN32
4 : #include <windows.h>
5 : #endif
6 :
7 : #if defined(MS_WIN32) || defined(__CYGWIN__)
8 : #define EXPORT(x) __declspec(dllexport) x
9 : #else
10 : #define EXPORT(x) x
11 : #endif
12 :
13 : /* some functions handy for testing */
14 :
15 : EXPORT(int)
16 0 : _testfunc_cbk_reg_int(int a, int b, int c, int d, int e,
17 : int (*func)(int, int, int, int, int))
18 : {
19 0 : return func(a*a, b*b, c*c, d*d, e*e);
20 : }
21 :
22 : EXPORT(double)
23 0 : _testfunc_cbk_reg_double(double a, double b, double c, double d, double e,
24 : double (*func)(double, double, double, double, double))
25 : {
26 0 : return func(a*a, b*b, c*c, d*d, e*e);
27 : }
28 :
29 0 : EXPORT(void)testfunc_array(int values[4])
30 : {
31 0 : printf("testfunc_array %d %d %d %d\n",
32 : values[0],
33 0 : values[1],
34 0 : values[2],
35 0 : values[3]);
36 0 : }
37 :
38 0 : EXPORT(long double)testfunc_Ddd(double a, double b)
39 : {
40 0 : long double result = (long double)(a * b);
41 0 : printf("testfunc_Ddd(%p, %p)\n", &a, &b);
42 0 : printf("testfunc_Ddd(%g, %g)\n", a, b);
43 0 : return result;
44 : }
45 :
46 0 : EXPORT(long double)testfunc_DDD(long double a, long double b)
47 : {
48 0 : long double result = a * b;
49 0 : printf("testfunc_DDD(%p, %p)\n", &a, &b);
50 0 : printf("testfunc_DDD(%Lg, %Lg)\n", a, b);
51 0 : return result;
52 : }
53 :
54 0 : EXPORT(int)testfunc_iii(int a, int b)
55 : {
56 0 : int result = a * b;
57 0 : printf("testfunc_iii(%p, %p)\n", &a, &b);
58 0 : return result;
59 : }
60 :
61 0 : EXPORT(int)myprintf(char *fmt, ...)
62 : {
63 : int result;
64 : va_list argptr;
65 0 : va_start(argptr, fmt);
66 0 : result = vprintf(fmt, argptr);
67 0 : va_end(argptr);
68 0 : return result;
69 : }
70 :
71 0 : EXPORT(char *)my_strtok(char *token, const char *delim)
72 : {
73 0 : return strtok(token, delim);
74 : }
75 :
76 0 : EXPORT(char *)my_strchr(const char *s, int c)
77 : {
78 0 : return strchr(s, c);
79 : }
80 :
81 :
82 0 : EXPORT(double) my_sqrt(double a)
83 : {
84 0 : return sqrt(a);
85 : }
86 :
87 0 : EXPORT(void) my_qsort(void *base, size_t num, size_t width, int(*compare)(const void*, const void*))
88 : {
89 0 : qsort(base, num, width, compare);
90 0 : }
91 :
92 0 : EXPORT(int *) _testfunc_ai8(int a[8])
93 : {
94 0 : return a;
95 : }
96 :
97 0 : EXPORT(void) _testfunc_v(int a, int b, int *presult)
98 : {
99 0 : *presult = a + b;
100 0 : }
101 :
102 0 : EXPORT(int) _testfunc_i_bhilfd(signed char b, short h, int i, long l, float f, double d)
103 : {
104 : /* printf("_testfunc_i_bhilfd got %d %d %d %ld %f %f\n",
105 : b, h, i, l, f, d);
106 : */
107 0 : return (int)(b + h + i + l + f + d);
108 : }
109 :
110 0 : EXPORT(float) _testfunc_f_bhilfd(signed char b, short h, int i, long l, float f, double d)
111 : {
112 : /* printf("_testfunc_f_bhilfd got %d %d %d %ld %f %f\n",
113 : b, h, i, l, f, d);
114 : */
115 0 : return (float)(b + h + i + l + f + d);
116 : }
117 :
118 0 : EXPORT(double) _testfunc_d_bhilfd(signed char b, short h, int i, long l, float f, double d)
119 : {
120 : /* printf("_testfunc_d_bhilfd got %d %d %d %ld %f %f\n",
121 : b, h, i, l, f, d);
122 : */
123 0 : return (double)(b + h + i + l + f + d);
124 : }
125 :
126 0 : EXPORT(long double) _testfunc_D_bhilfD(signed char b, short h, int i, long l, float f, long double d)
127 : {
128 : /* printf("_testfunc_d_bhilfd got %d %d %d %ld %f %f\n",
129 : b, h, i, l, f, d);
130 : */
131 0 : return (long double)(b + h + i + l + f + d);
132 : }
133 :
134 0 : EXPORT(char *) _testfunc_p_p(void *s)
135 : {
136 0 : return (char *)s;
137 : }
138 :
139 0 : EXPORT(void *) _testfunc_c_p_p(int *argcp, char **argv)
140 : {
141 0 : return argv[(*argcp)-1];
142 : }
143 :
144 0 : EXPORT(void *) get_strchr(void)
145 : {
146 0 : return (void *)strchr;
147 : }
148 :
149 0 : EXPORT(char *) my_strdup(char *src)
150 : {
151 0 : char *dst = (char *)malloc(strlen(src)+1);
152 0 : if (!dst)
153 0 : return NULL;
154 0 : strcpy(dst, src);
155 0 : return dst;
156 : }
157 :
158 0 : EXPORT(void)my_free(void *ptr)
159 : {
160 0 : free(ptr);
161 0 : }
162 :
163 : #ifdef HAVE_WCHAR_H
164 0 : EXPORT(wchar_t *) my_wcsdup(wchar_t *src)
165 : {
166 0 : size_t len = wcslen(src);
167 0 : wchar_t *ptr = (wchar_t *)malloc((len + 1) * sizeof(wchar_t));
168 0 : if (ptr == NULL)
169 0 : return NULL;
170 0 : memcpy(ptr, src, (len+1) * sizeof(wchar_t));
171 0 : return ptr;
172 : }
173 :
174 0 : EXPORT(size_t) my_wcslen(wchar_t *src)
175 : {
176 0 : return wcslen(src);
177 : }
178 : #endif
179 :
180 : #ifndef MS_WIN32
181 : # ifndef __stdcall
182 : # define __stdcall /* */
183 : # endif
184 : #endif
185 :
186 : typedef struct {
187 : int (*c)(int, int);
188 : int (__stdcall *s)(int, int);
189 : } FUNCS;
190 :
191 0 : EXPORT(int) _testfunc_callfuncp(FUNCS *fp)
192 : {
193 0 : fp->c(1, 2);
194 0 : fp->s(3, 4);
195 0 : return 0;
196 : }
197 :
198 0 : EXPORT(int) _testfunc_deref_pointer(int *pi)
199 : {
200 0 : return *pi;
201 : }
202 :
203 : #ifdef MS_WIN32
204 : EXPORT(int) _testfunc_piunk(IUnknown FAR *piunk)
205 : {
206 : piunk->lpVtbl->AddRef(piunk);
207 : return piunk->lpVtbl->Release(piunk);
208 : }
209 : #endif
210 :
211 0 : EXPORT(int) _testfunc_callback_with_pointer(int (*func)(int *))
212 : {
213 0 : int table[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
214 :
215 0 : return (*func)(table);
216 : }
217 :
218 : #ifdef HAVE_LONG_LONG
219 0 : EXPORT(PY_LONG_LONG) _testfunc_q_bhilfdq(signed char b, short h, int i, long l, float f,
220 : double d, PY_LONG_LONG q)
221 : {
222 0 : return (PY_LONG_LONG)(b + h + i + l + f + d + q);
223 : }
224 :
225 0 : EXPORT(PY_LONG_LONG) _testfunc_q_bhilfd(signed char b, short h, int i, long l, float f, double d)
226 : {
227 0 : return (PY_LONG_LONG)(b + h + i + l + f + d);
228 : }
229 :
230 0 : EXPORT(int) _testfunc_callback_i_if(int value, int (*func)(int))
231 : {
232 0 : int sum = 0;
233 0 : while (value != 0) {
234 0 : sum += func(value);
235 0 : value /= 2;
236 : }
237 0 : return sum;
238 : }
239 :
240 0 : EXPORT(PY_LONG_LONG) _testfunc_callback_q_qf(PY_LONG_LONG value,
241 : PY_LONG_LONG (*func)(PY_LONG_LONG))
242 : {
243 0 : PY_LONG_LONG sum = 0;
244 :
245 0 : while (value != 0) {
246 0 : sum += func(value);
247 0 : value /= 2;
248 : }
249 0 : return sum;
250 : }
251 :
252 : #endif
253 :
254 : typedef struct {
255 : char *name;
256 : char *value;
257 : } SPAM;
258 :
259 : typedef struct {
260 : char *name;
261 : int num_spams;
262 : SPAM *spams;
263 : } EGG;
264 :
265 : SPAM my_spams[2] = {
266 : { "name1", "value1" },
267 : { "name2", "value2" },
268 : };
269 :
270 : EGG my_eggs[1] = {
271 : { "first egg", 1, my_spams }
272 : };
273 :
274 0 : EXPORT(int) getSPAMANDEGGS(EGG **eggs)
275 : {
276 0 : *eggs = my_eggs;
277 0 : return 1;
278 : }
279 :
280 : typedef struct tagpoint {
281 : int x;
282 : int y;
283 : } point;
284 :
285 0 : EXPORT(int) _testfunc_byval(point in, point *pout)
286 : {
287 0 : if (pout) {
288 0 : pout->x = in.x;
289 0 : pout->y = in.y;
290 : }
291 0 : return in.x + in.y;
292 : }
293 :
294 : EXPORT (int) an_integer = 42;
295 :
296 0 : EXPORT(int) get_an_integer(void)
297 : {
298 0 : return an_integer;
299 : }
300 :
301 : EXPORT(double)
302 0 : integrate(double a, double b, double (*f)(double), long nstep)
303 : {
304 0 : double x, sum=0.0, dx=(b-a)/(double)nstep;
305 0 : for(x=a+0.5*dx; (b-x)*(x-a)>0.0; x+=dx)
306 0 : sum += f(x);
307 0 : return sum/(double)nstep;
308 : }
309 :
310 : typedef struct {
311 : void (*initialize)(void *(*)(int), void(*)(void *));
312 : } xxx_library;
313 :
314 0 : static void _xxx_init(void *(*Xalloc)(int), void (*Xfree)(void *))
315 : {
316 : void *ptr;
317 :
318 0 : printf("_xxx_init got %p %p\n", Xalloc, Xfree);
319 0 : printf("calling\n");
320 0 : ptr = Xalloc(32);
321 0 : Xfree(ptr);
322 0 : printf("calls done, ptr was %p\n", ptr);
323 0 : }
324 :
325 : xxx_library _xxx_lib = {
326 : _xxx_init
327 : };
328 :
329 0 : EXPORT(xxx_library) *library_get(void)
330 : {
331 0 : return &_xxx_lib;
332 : }
333 :
334 : #ifdef MS_WIN32
335 : /* See Don Box (german), pp 79ff. */
336 : EXPORT(void) GetString(BSTR *pbstr)
337 : {
338 : *pbstr = SysAllocString(L"Goodbye!");
339 : }
340 : #endif
341 :
342 : /*
343 : * Some do-nothing functions, for speed tests
344 : */
345 0 : PyObject *py_func_si(PyObject *self, PyObject *args)
346 : {
347 : char *name;
348 : int i;
349 0 : if (!PyArg_ParseTuple(args, "si", &name, &i))
350 0 : return NULL;
351 0 : Py_INCREF(Py_None);
352 0 : return Py_None;
353 : }
354 :
355 0 : EXPORT(void) _py_func_si(char *s, int i)
356 : {
357 0 : }
358 :
359 0 : PyObject *py_func(PyObject *self, PyObject *args)
360 : {
361 0 : Py_INCREF(Py_None);
362 0 : return Py_None;
363 : }
364 :
365 0 : EXPORT(void) _py_func(void)
366 : {
367 0 : }
368 :
369 : EXPORT(PY_LONG_LONG) last_tf_arg_s;
370 : EXPORT(unsigned PY_LONG_LONG) last_tf_arg_u;
371 :
372 : struct BITS {
373 : int A: 1, B:2, C:3, D:4, E: 5, F: 6, G: 7, H: 8, I: 9;
374 : short M: 1, N: 2, O: 3, P: 4, Q: 5, R: 6, S: 7;
375 : };
376 :
377 0 : EXPORT(void) set_bitfields(struct BITS *bits, char name, int value)
378 : {
379 0 : switch (name) {
380 0 : case 'A': bits->A = value; break;
381 0 : case 'B': bits->B = value; break;
382 0 : case 'C': bits->C = value; break;
383 0 : case 'D': bits->D = value; break;
384 0 : case 'E': bits->E = value; break;
385 0 : case 'F': bits->F = value; break;
386 0 : case 'G': bits->G = value; break;
387 0 : case 'H': bits->H = value; break;
388 0 : case 'I': bits->I = value; break;
389 :
390 0 : case 'M': bits->M = value; break;
391 0 : case 'N': bits->N = value; break;
392 0 : case 'O': bits->O = value; break;
393 0 : case 'P': bits->P = value; break;
394 0 : case 'Q': bits->Q = value; break;
395 0 : case 'R': bits->R = value; break;
396 0 : case 'S': bits->S = value; break;
397 : }
398 0 : }
399 :
400 0 : EXPORT(int) unpack_bitfields(struct BITS *bits, char name)
401 : {
402 0 : switch (name) {
403 0 : case 'A': return bits->A;
404 0 : case 'B': return bits->B;
405 0 : case 'C': return bits->C;
406 0 : case 'D': return bits->D;
407 0 : case 'E': return bits->E;
408 0 : case 'F': return bits->F;
409 0 : case 'G': return bits->G;
410 0 : case 'H': return bits->H;
411 0 : case 'I': return bits->I;
412 :
413 0 : case 'M': return bits->M;
414 0 : case 'N': return bits->N;
415 0 : case 'O': return bits->O;
416 0 : case 'P': return bits->P;
417 0 : case 'Q': return bits->Q;
418 0 : case 'R': return bits->R;
419 0 : case 'S': return bits->S;
420 : }
421 0 : return 0;
422 : }
423 :
424 : static PyMethodDef module_methods[] = {
425 : /* {"get_last_tf_arg_s", get_last_tf_arg_s, METH_NOARGS},
426 : {"get_last_tf_arg_u", get_last_tf_arg_u, METH_NOARGS},
427 : */
428 : {"func_si", py_func_si, METH_VARARGS},
429 : {"func", py_func, METH_NOARGS},
430 : { NULL, NULL, 0, NULL},
431 : };
432 :
433 : #define S last_tf_arg_s = (PY_LONG_LONG)c
434 : #define U last_tf_arg_u = (unsigned PY_LONG_LONG)c
435 :
436 0 : EXPORT(signed char) tf_b(signed char c) { S; return c/3; }
437 0 : EXPORT(unsigned char) tf_B(unsigned char c) { U; return c/3; }
438 0 : EXPORT(short) tf_h(short c) { S; return c/3; }
439 0 : EXPORT(unsigned short) tf_H(unsigned short c) { U; return c/3; }
440 0 : EXPORT(int) tf_i(int c) { S; return c/3; }
441 0 : EXPORT(unsigned int) tf_I(unsigned int c) { U; return c/3; }
442 0 : EXPORT(long) tf_l(long c) { S; return c/3; }
443 0 : EXPORT(unsigned long) tf_L(unsigned long c) { U; return c/3; }
444 0 : EXPORT(PY_LONG_LONG) tf_q(PY_LONG_LONG c) { S; return c/3; }
445 0 : EXPORT(unsigned PY_LONG_LONG) tf_Q(unsigned PY_LONG_LONG c) { U; return c/3; }
446 0 : EXPORT(float) tf_f(float c) { S; return c/3; }
447 0 : EXPORT(double) tf_d(double c) { S; return c/3; }
448 0 : EXPORT(long double) tf_D(long double c) { S; return c/3; }
449 :
450 : #ifdef MS_WIN32
451 : EXPORT(signed char) __stdcall s_tf_b(signed char c) { S; return c/3; }
452 : EXPORT(unsigned char) __stdcall s_tf_B(unsigned char c) { U; return c/3; }
453 : EXPORT(short) __stdcall s_tf_h(short c) { S; return c/3; }
454 : EXPORT(unsigned short) __stdcall s_tf_H(unsigned short c) { U; return c/3; }
455 : EXPORT(int) __stdcall s_tf_i(int c) { S; return c/3; }
456 : EXPORT(unsigned int) __stdcall s_tf_I(unsigned int c) { U; return c/3; }
457 : EXPORT(long) __stdcall s_tf_l(long c) { S; return c/3; }
458 : EXPORT(unsigned long) __stdcall s_tf_L(unsigned long c) { U; return c/3; }
459 : EXPORT(PY_LONG_LONG) __stdcall s_tf_q(PY_LONG_LONG c) { S; return c/3; }
460 : EXPORT(unsigned PY_LONG_LONG) __stdcall s_tf_Q(unsigned PY_LONG_LONG c) { U; return c/3; }
461 : EXPORT(float) __stdcall s_tf_f(float c) { S; return c/3; }
462 : EXPORT(double) __stdcall s_tf_d(double c) { S; return c/3; }
463 : EXPORT(long double) __stdcall s_tf_D(long double c) { S; return c/3; }
464 : #endif
465 : /*******/
466 :
467 0 : EXPORT(signed char) tf_bb(signed char x, signed char c) { S; return c/3; }
468 0 : EXPORT(unsigned char) tf_bB(signed char x, unsigned char c) { U; return c/3; }
469 0 : EXPORT(short) tf_bh(signed char x, short c) { S; return c/3; }
470 0 : EXPORT(unsigned short) tf_bH(signed char x, unsigned short c) { U; return c/3; }
471 0 : EXPORT(int) tf_bi(signed char x, int c) { S; return c/3; }
472 0 : EXPORT(unsigned int) tf_bI(signed char x, unsigned int c) { U; return c/3; }
473 0 : EXPORT(long) tf_bl(signed char x, long c) { S; return c/3; }
474 0 : EXPORT(unsigned long) tf_bL(signed char x, unsigned long c) { U; return c/3; }
475 0 : EXPORT(PY_LONG_LONG) tf_bq(signed char x, PY_LONG_LONG c) { S; return c/3; }
476 0 : EXPORT(unsigned PY_LONG_LONG) tf_bQ(signed char x, unsigned PY_LONG_LONG c) { U; return c/3; }
477 0 : EXPORT(float) tf_bf(signed char x, float c) { S; return c/3; }
478 0 : EXPORT(double) tf_bd(signed char x, double c) { S; return c/3; }
479 0 : EXPORT(long double) tf_bD(signed char x, long double c) { S; return c/3; }
480 0 : EXPORT(void) tv_i(int c) { S; return; }
481 :
482 : #ifdef MS_WIN32
483 : EXPORT(signed char) __stdcall s_tf_bb(signed char x, signed char c) { S; return c/3; }
484 : EXPORT(unsigned char) __stdcall s_tf_bB(signed char x, unsigned char c) { U; return c/3; }
485 : EXPORT(short) __stdcall s_tf_bh(signed char x, short c) { S; return c/3; }
486 : EXPORT(unsigned short) __stdcall s_tf_bH(signed char x, unsigned short c) { U; return c/3; }
487 : EXPORT(int) __stdcall s_tf_bi(signed char x, int c) { S; return c/3; }
488 : EXPORT(unsigned int) __stdcall s_tf_bI(signed char x, unsigned int c) { U; return c/3; }
489 : EXPORT(long) __stdcall s_tf_bl(signed char x, long c) { S; return c/3; }
490 : EXPORT(unsigned long) __stdcall s_tf_bL(signed char x, unsigned long c) { U; return c/3; }
491 : EXPORT(PY_LONG_LONG) __stdcall s_tf_bq(signed char x, PY_LONG_LONG c) { S; return c/3; }
492 : EXPORT(unsigned PY_LONG_LONG) __stdcall s_tf_bQ(signed char x, unsigned PY_LONG_LONG c) { U; return c/3; }
493 : EXPORT(float) __stdcall s_tf_bf(signed char x, float c) { S; return c/3; }
494 : EXPORT(double) __stdcall s_tf_bd(signed char x, double c) { S; return c/3; }
495 : EXPORT(long double) __stdcall s_tf_bD(signed char x, long double c) { S; return c/3; }
496 : EXPORT(void) __stdcall s_tv_i(int c) { S; return; }
497 : #endif
498 :
499 : /********/
500 :
501 : #ifndef MS_WIN32
502 :
503 : typedef struct {
504 : long x;
505 : long y;
506 : } POINT;
507 :
508 : typedef struct {
509 : long left;
510 : long top;
511 : long right;
512 : long bottom;
513 : } RECT;
514 :
515 : #endif
516 :
517 0 : EXPORT(int) PointInRect(RECT *prc, POINT pt)
518 : {
519 0 : if (pt.x < prc->left)
520 0 : return 0;
521 0 : if (pt.x > prc->right)
522 0 : return 0;
523 0 : if (pt.y < prc->top)
524 0 : return 0;
525 0 : if (pt.y > prc->bottom)
526 0 : return 0;
527 0 : return 1;
528 : }
529 :
530 : typedef struct {
531 : short x;
532 : short y;
533 : } S2H;
534 :
535 0 : EXPORT(S2H) ret_2h_func(S2H inp)
536 : {
537 0 : inp.x *= 2;
538 0 : inp.y *= 3;
539 0 : return inp;
540 : }
541 :
542 : typedef struct {
543 : int a, b, c, d, e, f, g, h;
544 : } S8I;
545 :
546 0 : EXPORT(S8I) ret_8i_func(S8I inp)
547 : {
548 0 : inp.a *= 2;
549 0 : inp.b *= 3;
550 0 : inp.c *= 4;
551 0 : inp.d *= 5;
552 0 : inp.e *= 6;
553 0 : inp.f *= 7;
554 0 : inp.g *= 8;
555 0 : inp.h *= 9;
556 0 : return inp;
557 : }
558 :
559 0 : EXPORT(int) GetRectangle(int flag, RECT *prect)
560 : {
561 0 : if (flag == 0)
562 0 : return 0;
563 0 : prect->left = (int)flag;
564 0 : prect->top = (int)flag + 1;
565 0 : prect->right = (int)flag + 2;
566 0 : prect->bottom = (int)flag + 3;
567 0 : return 1;
568 : }
569 :
570 0 : EXPORT(void) TwoOutArgs(int a, int *pi, int b, int *pj)
571 : {
572 0 : *pi += a;
573 0 : *pj += b;
574 0 : }
575 :
576 : #ifdef MS_WIN32
577 : EXPORT(S2H) __stdcall s_ret_2h_func(S2H inp) { return ret_2h_func(inp); }
578 : EXPORT(S8I) __stdcall s_ret_8i_func(S8I inp) { return ret_8i_func(inp); }
579 : #endif
580 :
581 : #ifdef MS_WIN32
582 : /* Should port this */
583 : #include <stdlib.h>
584 : #include <search.h>
585 :
586 : EXPORT (HRESULT) KeepObject(IUnknown *punk)
587 : {
588 : static IUnknown *pobj;
589 : if (punk)
590 : punk->lpVtbl->AddRef(punk);
591 : if (pobj)
592 : pobj->lpVtbl->Release(pobj);
593 : pobj = punk;
594 : return S_OK;
595 : }
596 :
597 : #endif
598 :
599 :
600 : static struct PyModuleDef _ctypes_testmodule = {
601 : PyModuleDef_HEAD_INIT,
602 : "_ctypes_test",
603 : NULL,
604 : -1,
605 : module_methods,
606 : NULL,
607 : NULL,
608 : NULL,
609 : NULL
610 : };
611 :
612 : PyMODINIT_FUNC
613 0 : PyInit__ctypes_test(void)
614 : {
615 0 : return PyModule_Create(&_ctypes_testmodule);
616 : }
|