1 #include <Python.h>
2
3 #ifdef MS_WIN32
4 #include <windows.h>
5 #endif
6
7 #include <stdlib.h> // qsort()
8
9 #define EXPORT(x) Py_EXPORTED_SYMBOL x
10
11 /* some functions handy for testing */
12
13 EXPORT(int)
_testfunc_cbk_reg_int(int a,int b,int c,int d,int e,int (* func)(int,int,int,int,int))14 _testfunc_cbk_reg_int(int a, int b, int c, int d, int e,
15 int (*func)(int, int, int, int, int))
16 {
17 return func(a*a, b*b, c*c, d*d, e*e);
18 }
19
20 EXPORT(double)
_testfunc_cbk_reg_double(double a,double b,double c,double d,double e,double (* func)(double,double,double,double,double))21 _testfunc_cbk_reg_double(double a, double b, double c, double d, double e,
22 double (*func)(double, double, double, double, double))
23 {
24 return func(a*a, b*b, c*c, d*d, e*e);
25 }
26
27 /*
28 * This structure should be the same as in test_callbacks.py and the
29 * method test_callback_large_struct. See issues 17310 and 20160: the
30 * structure must be larger than 8 bytes long.
31 */
32
33 typedef struct {
34 unsigned long first;
35 unsigned long second;
36 unsigned long third;
37 } Test;
38
39 EXPORT(void)
_testfunc_cbk_large_struct(Test in,void (* func)(Test))40 _testfunc_cbk_large_struct(Test in, void (*func)(Test))
41 {
42 func(in);
43 }
44
45 /*
46 * See issue 29565. Update a structure passed by value;
47 * the caller should not see any change.
48 */
49
50 EXPORT(void)
_testfunc_large_struct_update_value(Test in)51 _testfunc_large_struct_update_value(Test in)
52 {
53 ((volatile Test *)&in)->first = 0x0badf00d;
54 ((volatile Test *)&in)->second = 0x0badf00d;
55 ((volatile Test *)&in)->third = 0x0badf00d;
56 }
57
58 typedef struct {
59 unsigned int first;
60 unsigned int second;
61 } TestReg;
62
63
64 EXPORT(TestReg) last_tfrsuv_arg = {0};
65
66
67 EXPORT(void)
_testfunc_reg_struct_update_value(TestReg in)68 _testfunc_reg_struct_update_value(TestReg in)
69 {
70 last_tfrsuv_arg = in;
71 ((volatile TestReg *)&in)->first = 0x0badf00d;
72 ((volatile TestReg *)&in)->second = 0x0badf00d;
73 }
74
75 /*
76 * See bpo-22273. Structs containing arrays should work on Linux 64-bit.
77 */
78
79 typedef struct {
80 unsigned char data[16];
81 } Test2;
82
83 EXPORT(int)
_testfunc_array_in_struct1(Test2 in)84 _testfunc_array_in_struct1(Test2 in)
85 {
86 int result = 0;
87
88 for (unsigned i = 0; i < 16; i++)
89 result += in.data[i];
90 /* As the structure is passed by value, changes to it shouldn't be
91 * reflected in the caller.
92 */
93 memset(in.data, 0, sizeof(in.data));
94 return result;
95 }
96
97 typedef struct {
98 double data[2];
99 } Test3;
100
101 typedef struct {
102 float data[2];
103 float more_data[2];
104 } Test3B;
105
106 EXPORT(double)
_testfunc_array_in_struct2(Test3 in)107 _testfunc_array_in_struct2(Test3 in)
108 {
109 double result = 0;
110
111 for (unsigned i = 0; i < 2; i++)
112 result += in.data[i];
113 /* As the structure is passed by value, changes to it shouldn't be
114 * reflected in the caller.
115 */
116 memset(in.data, 0, sizeof(in.data));
117 return result;
118 }
119
120 EXPORT(double)
_testfunc_array_in_struct2a(Test3B in)121 _testfunc_array_in_struct2a(Test3B in)
122 {
123 double result = 0;
124
125 for (unsigned i = 0; i < 2; i++)
126 result += in.data[i];
127 for (unsigned i = 0; i < 2; i++)
128 result += in.more_data[i];
129 /* As the structure is passed by value, changes to it shouldn't be
130 * reflected in the caller.
131 */
132 memset(in.data, 0, sizeof(in.data));
133 return result;
134 }
135
136 typedef union {
137 long a_long;
138 struct {
139 int an_int;
140 int another_int;
141 } a_struct;
142 } Test4;
143
144 typedef struct {
145 int an_int;
146 struct {
147 int an_int;
148 Test4 a_union;
149 } nested;
150 int another_int;
151 } Test5;
152
153 EXPORT(long)
_testfunc_union_by_value1(Test4 in)154 _testfunc_union_by_value1(Test4 in) {
155 long result = in.a_long + in.a_struct.an_int + in.a_struct.another_int;
156
157 /* As the union/struct are passed by value, changes to them shouldn't be
158 * reflected in the caller.
159 */
160 memset(&in, 0, sizeof(in));
161 return result;
162 }
163
164 EXPORT(long)
_testfunc_union_by_value2(Test5 in)165 _testfunc_union_by_value2(Test5 in) {
166 long result = in.an_int + in.nested.an_int;
167
168 /* As the union/struct are passed by value, changes to them shouldn't be
169 * reflected in the caller.
170 */
171 memset(&in, 0, sizeof(in));
172 return result;
173 }
174
175 EXPORT(long)
_testfunc_union_by_reference1(Test4 * in)176 _testfunc_union_by_reference1(Test4 *in) {
177 long result = in->a_long;
178
179 memset(in, 0, sizeof(Test4));
180 return result;
181 }
182
183 EXPORT(long)
_testfunc_union_by_reference2(Test4 * in)184 _testfunc_union_by_reference2(Test4 *in) {
185 long result = in->a_struct.an_int + in->a_struct.another_int;
186
187 memset(in, 0, sizeof(Test4));
188 return result;
189 }
190
191 EXPORT(long)
_testfunc_union_by_reference3(Test5 * in)192 _testfunc_union_by_reference3(Test5 *in) {
193 long result = in->an_int + in->nested.an_int + in->another_int;
194
195 memset(in, 0, sizeof(Test5));
196 return result;
197 }
198
199 typedef struct {
200 signed int A: 1, B:2, C:3, D:2;
201 } Test6;
202
203 EXPORT(long)
_testfunc_bitfield_by_value1(Test6 in)204 _testfunc_bitfield_by_value1(Test6 in) {
205 long result = in.A + in.B + in.C + in.D;
206
207 /* As the struct is passed by value, changes to it shouldn't be
208 * reflected in the caller.
209 */
210 memset(&in, 0, sizeof(in));
211 return result;
212 }
213
214 EXPORT(long)
_testfunc_bitfield_by_reference1(Test6 * in)215 _testfunc_bitfield_by_reference1(Test6 *in) {
216 long result = in->A + in->B + in->C + in->D;
217
218 memset(in, 0, sizeof(Test6));
219 return result;
220 }
221
222 typedef struct {
223 unsigned int A: 1, B:2, C:3, D:2;
224 } Test7;
225
226 EXPORT(long)
_testfunc_bitfield_by_reference2(Test7 * in)227 _testfunc_bitfield_by_reference2(Test7 *in) {
228 long result = in->A + in->B + in->C + in->D;
229
230 memset(in, 0, sizeof(Test7));
231 return result;
232 }
233
234 typedef union {
235 signed int A: 1, B:2, C:3, D:2;
236 } Test8;
237
238 EXPORT(long)
_testfunc_bitfield_by_value2(Test8 in)239 _testfunc_bitfield_by_value2(Test8 in) {
240 long result = in.A + in.B + in.C + in.D;
241
242 /* As the struct is passed by value, changes to it shouldn't be
243 * reflected in the caller.
244 */
245 memset(&in, 0, sizeof(in));
246 return result;
247 }
248
testfunc_array(int values[4])249 EXPORT(void)testfunc_array(int values[4])
250 {
251 printf("testfunc_array %d %d %d %d\n",
252 values[0],
253 values[1],
254 values[2],
255 values[3]);
256 }
257
testfunc_Ddd(double a,double b)258 EXPORT(long double)testfunc_Ddd(double a, double b)
259 {
260 long double result = (long double)(a * b);
261 printf("testfunc_Ddd(%p, %p)\n", (void *)&a, (void *)&b);
262 printf("testfunc_Ddd(%g, %g)\n", a, b);
263 return result;
264 }
265
testfunc_DDD(long double a,long double b)266 EXPORT(long double)testfunc_DDD(long double a, long double b)
267 {
268 long double result = a * b;
269 printf("testfunc_DDD(%p, %p)\n", (void *)&a, (void *)&b);
270 printf("testfunc_DDD(%Lg, %Lg)\n", a, b);
271 return result;
272 }
273
testfunc_iii(int a,int b)274 EXPORT(int)testfunc_iii(int a, int b)
275 {
276 int result = a * b;
277 printf("testfunc_iii(%p, %p)\n", (void *)&a, (void *)&b);
278 return result;
279 }
280
myprintf(char * fmt,...)281 EXPORT(int)myprintf(char *fmt, ...)
282 {
283 int result;
284 va_list argptr;
285 va_start(argptr, fmt);
286 result = vprintf(fmt, argptr);
287 va_end(argptr);
288 return result;
289 }
290
my_strtok(char * token,const char * delim)291 EXPORT(char *)my_strtok(char *token, const char *delim)
292 {
293 return strtok(token, delim);
294 }
295
my_strchr(const char * s,int c)296 EXPORT(char *)my_strchr(const char *s, int c)
297 {
298 return strchr(s, c);
299 }
300
301
my_sqrt(double a)302 EXPORT(double) my_sqrt(double a)
303 {
304 return sqrt(a);
305 }
306
my_qsort(void * base,size_t num,size_t width,int (* compare)(const void *,const void *))307 EXPORT(void) my_qsort(void *base, size_t num, size_t width, int(*compare)(const void*, const void*))
308 {
309 qsort(base, num, width, compare);
310 }
311
_testfunc_ai8(int a[8])312 EXPORT(int *) _testfunc_ai8(int a[8])
313 {
314 return a;
315 }
316
_testfunc_v(int a,int b,int * presult)317 EXPORT(void) _testfunc_v(int a, int b, int *presult)
318 {
319 *presult = a + b;
320 }
321
_testfunc_i_bhilfd(signed char b,short h,int i,long l,float f,double d)322 EXPORT(int) _testfunc_i_bhilfd(signed char b, short h, int i, long l, float f, double d)
323 {
324 /* printf("_testfunc_i_bhilfd got %d %d %d %ld %f %f\n",
325 b, h, i, l, f, d);
326 */
327 return (int)(b + h + i + l + f + d);
328 }
329
_testfunc_f_bhilfd(signed char b,short h,int i,long l,float f,double d)330 EXPORT(float) _testfunc_f_bhilfd(signed char b, short h, int i, long l, float f, double d)
331 {
332 /* printf("_testfunc_f_bhilfd got %d %d %d %ld %f %f\n",
333 b, h, i, l, f, d);
334 */
335 return (float)(b + h + i + l + f + d);
336 }
337
_testfunc_d_bhilfd(signed char b,short h,int i,long l,float f,double d)338 EXPORT(double) _testfunc_d_bhilfd(signed char b, short h, int i, long l, float f, double d)
339 {
340 /* printf("_testfunc_d_bhilfd got %d %d %d %ld %f %f\n",
341 b, h, i, l, f, d);
342 */
343 return (double)(b + h + i + l + f + d);
344 }
345
_testfunc_D_bhilfD(signed char b,short h,int i,long l,float f,long double d)346 EXPORT(long double) _testfunc_D_bhilfD(signed char b, short h, int i, long l, float f, long double d)
347 {
348 /* printf("_testfunc_d_bhilfd got %d %d %d %ld %f %f\n",
349 b, h, i, l, f, d);
350 */
351 return (long double)(b + h + i + l + f + d);
352 }
353
_testfunc_p_p(void * s)354 EXPORT(char *) _testfunc_p_p(void *s)
355 {
356 return (char *)s;
357 }
358
_testfunc_c_p_p(int * argcp,char ** argv)359 EXPORT(void *) _testfunc_c_p_p(int *argcp, char **argv)
360 {
361 return argv[(*argcp)-1];
362 }
363
get_strchr(void)364 EXPORT(void *) get_strchr(void)
365 {
366 return (void *)strchr;
367 }
368
my_strdup(char * src)369 EXPORT(char *) my_strdup(char *src)
370 {
371 char *dst = (char *)malloc(strlen(src)+1);
372 if (!dst)
373 return NULL;
374 strcpy(dst, src);
375 return dst;
376 }
377
my_free(void * ptr)378 EXPORT(void)my_free(void *ptr)
379 {
380 free(ptr);
381 }
382
383 #ifdef HAVE_WCHAR_H
my_wcsdup(wchar_t * src)384 EXPORT(wchar_t *) my_wcsdup(wchar_t *src)
385 {
386 size_t len = wcslen(src);
387 wchar_t *ptr = (wchar_t *)malloc((len + 1) * sizeof(wchar_t));
388 if (ptr == NULL)
389 return NULL;
390 memcpy(ptr, src, (len+1) * sizeof(wchar_t));
391 return ptr;
392 }
393
my_wcslen(wchar_t * src)394 EXPORT(size_t) my_wcslen(wchar_t *src)
395 {
396 return wcslen(src);
397 }
398 #endif
399
400 #ifndef MS_WIN32
401 # ifndef __stdcall
402 # define __stdcall /* */
403 # endif
404 #endif
405
406 typedef struct {
407 int (*c)(int, int);
408 int (__stdcall *s)(int, int);
409 } FUNCS;
410
_testfunc_callfuncp(FUNCS * fp)411 EXPORT(int) _testfunc_callfuncp(FUNCS *fp)
412 {
413 fp->c(1, 2);
414 fp->s(3, 4);
415 return 0;
416 }
417
_testfunc_deref_pointer(int * pi)418 EXPORT(int) _testfunc_deref_pointer(int *pi)
419 {
420 return *pi;
421 }
422
423 #ifdef MS_WIN32
_testfunc_piunk(IUnknown FAR * piunk)424 EXPORT(int) _testfunc_piunk(IUnknown FAR *piunk)
425 {
426 piunk->lpVtbl->AddRef(piunk);
427 return piunk->lpVtbl->Release(piunk);
428 }
429 #endif
430
_testfunc_callback_with_pointer(int (* func)(int *))431 EXPORT(int) _testfunc_callback_with_pointer(int (*func)(int *))
432 {
433 int table[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
434
435 return (*func)(table);
436 }
437
_testfunc_q_bhilfdq(signed char b,short h,int i,long l,float f,double d,long long q)438 EXPORT(long long) _testfunc_q_bhilfdq(signed char b, short h, int i, long l, float f,
439 double d, long long q)
440 {
441 return (long long)(b + h + i + l + f + d + q);
442 }
443
_testfunc_q_bhilfd(signed char b,short h,int i,long l,float f,double d)444 EXPORT(long long) _testfunc_q_bhilfd(signed char b, short h, int i, long l, float f, double d)
445 {
446 return (long long)(b + h + i + l + f + d);
447 }
448
_testfunc_callback_i_if(int value,int (* func)(int))449 EXPORT(int) _testfunc_callback_i_if(int value, int (*func)(int))
450 {
451 int sum = 0;
452 while (value != 0) {
453 sum += func(value);
454 value /= 2;
455 }
456 return sum;
457 }
458
_testfunc_callback_q_qf(long long value,long long (* func)(long long))459 EXPORT(long long) _testfunc_callback_q_qf(long long value,
460 long long (*func)(long long))
461 {
462 long long sum = 0;
463
464 while (value != 0) {
465 sum += func(value);
466 value /= 2;
467 }
468 return sum;
469 }
470
471 typedef struct {
472 char *name;
473 char *value;
474 } SPAM;
475
476 typedef struct {
477 char *name;
478 int num_spams;
479 SPAM *spams;
480 } EGG;
481
482 SPAM my_spams[2] = {
483 { "name1", "value1" },
484 { "name2", "value2" },
485 };
486
487 EGG my_eggs[1] = {
488 { "first egg", 1, my_spams }
489 };
490
getSPAMANDEGGS(EGG ** eggs)491 EXPORT(int) getSPAMANDEGGS(EGG **eggs)
492 {
493 *eggs = my_eggs;
494 return 1;
495 }
496
497 typedef struct tagpoint {
498 int x;
499 int y;
500 } point;
501
_testfunc_byval(point in,point * pout)502 EXPORT(int) _testfunc_byval(point in, point *pout)
503 {
504 if (pout) {
505 pout->x = in.x;
506 pout->y = in.y;
507 }
508 return in.x + in.y;
509 }
510
511 EXPORT (int) an_integer = 42;
512
get_an_integer(void)513 EXPORT(int) get_an_integer(void)
514 {
515 return an_integer;
516 }
517
518 EXPORT(double)
integrate(double a,double b,double (* f)(double),long nstep)519 integrate(double a, double b, double (*f)(double), long nstep)
520 {
521 double x, sum=0.0, dx=(b-a)/(double)nstep;
522 for(x=a+0.5*dx; (b-x)*(x-a)>0.0; x+=dx)
523 sum += f(x);
524 return sum/(double)nstep;
525 }
526
527 typedef struct {
528 void (*initialize)(void *(*)(int), void(*)(void *));
529 } xxx_library;
530
_xxx_init(void * (* Xalloc)(int),void (* Xfree)(void *))531 static void _xxx_init(void *(*Xalloc)(int), void (*Xfree)(void *))
532 {
533 void *ptr;
534
535 printf("_xxx_init got %p %p\n", (void *)Xalloc, (void *)Xfree);
536 printf("calling\n");
537 ptr = Xalloc(32);
538 Xfree(ptr);
539 printf("calls done, ptr was %p\n", ptr);
540 }
541
542 xxx_library _xxx_lib = {
543 _xxx_init
544 };
545
EXPORT(xxx_library)546 EXPORT(xxx_library) *library_get(void)
547 {
548 return &_xxx_lib;
549 }
550
551 #ifdef MS_WIN32
552 /* See Don Box (german), pp 79ff. */
GetString(BSTR * pbstr)553 EXPORT(void) GetString(BSTR *pbstr)
554 {
555 *pbstr = SysAllocString(L"Goodbye!");
556 }
557 #endif
558
559 /*
560 * Some do-nothing functions, for speed tests
561 */
py_func_si(PyObject * self,PyObject * args)562 PyObject *py_func_si(PyObject *self, PyObject *args)
563 {
564 char *name;
565 int i;
566 if (!PyArg_ParseTuple(args, "si", &name, &i))
567 return NULL;
568 Py_RETURN_NONE;
569 }
570
_py_func_si(char * s,int i)571 EXPORT(void) _py_func_si(char *s, int i)
572 {
573 }
574
py_func(PyObject * self,PyObject * args)575 PyObject *py_func(PyObject *self, PyObject *args)
576 {
577 Py_RETURN_NONE;
578 }
579
_py_func(void)580 EXPORT(void) _py_func(void)
581 {
582 }
583
584 EXPORT(long long) last_tf_arg_s = 0;
585 EXPORT(unsigned long long) last_tf_arg_u = 0;
586
587 struct BITS {
588 signed int A: 1, B:2, C:3, D:4, E: 5, F: 6, G: 7, H: 8, I: 9;
589 /*
590 * The test case needs/uses "signed short" bitfields, but the
591 * IBM XLC compiler does not support this
592 */
593 #ifndef __xlc__
594 #define SIGNED_SHORT_BITFIELDS
595 short M: 1, N: 2, O: 3, P: 4, Q: 5, R: 6, S: 7;
596 #endif
597 };
598
unpack_bitfields(struct BITS * bits,char name)599 EXPORT(int) unpack_bitfields(struct BITS *bits, char name)
600 {
601 switch (name) {
602 case 'A': return bits->A;
603 case 'B': return bits->B;
604 case 'C': return bits->C;
605 case 'D': return bits->D;
606 case 'E': return bits->E;
607 case 'F': return bits->F;
608 case 'G': return bits->G;
609 case 'H': return bits->H;
610 case 'I': return bits->I;
611
612 #ifdef SIGNED_SHORT_BITFIELDS
613 case 'M': return bits->M;
614 case 'N': return bits->N;
615 case 'O': return bits->O;
616 case 'P': return bits->P;
617 case 'Q': return bits->Q;
618 case 'R': return bits->R;
619 case 'S': return bits->S;
620 #endif
621 }
622 return 999;
623 }
624
625 static PyMethodDef module_methods[] = {
626 /* {"get_last_tf_arg_s", get_last_tf_arg_s, METH_NOARGS},
627 {"get_last_tf_arg_u", get_last_tf_arg_u, METH_NOARGS},
628 */
629 {"func_si", py_func_si, METH_VARARGS},
630 {"func", py_func, METH_NOARGS},
631 { NULL, NULL, 0, NULL},
632 };
633
634 #define S last_tf_arg_s = (long long)c
635 #define U last_tf_arg_u = (unsigned long long)c
636
tf_b(signed char c)637 EXPORT(signed char) tf_b(signed char c) { S; return c/3; }
tf_B(unsigned char c)638 EXPORT(unsigned char) tf_B(unsigned char c) { U; return c/3; }
tf_h(short c)639 EXPORT(short) tf_h(short c) { S; return c/3; }
tf_H(unsigned short c)640 EXPORT(unsigned short) tf_H(unsigned short c) { U; return c/3; }
tf_i(int c)641 EXPORT(int) tf_i(int c) { S; return c/3; }
tf_I(unsigned int c)642 EXPORT(unsigned int) tf_I(unsigned int c) { U; return c/3; }
tf_l(long c)643 EXPORT(long) tf_l(long c) { S; return c/3; }
tf_L(unsigned long c)644 EXPORT(unsigned long) tf_L(unsigned long c) { U; return c/3; }
tf_q(long long c)645 EXPORT(long long) tf_q(long long c) { S; return c/3; }
tf_Q(unsigned long long c)646 EXPORT(unsigned long long) tf_Q(unsigned long long c) { U; return c/3; }
tf_f(float c)647 EXPORT(float) tf_f(float c) { S; return c/3; }
tf_d(double c)648 EXPORT(double) tf_d(double c) { S; return c/3; }
tf_D(long double c)649 EXPORT(long double) tf_D(long double c) { S; return c/3; }
650
651 #ifdef MS_WIN32
EXPORT(signed char)652 EXPORT(signed char) __stdcall s_tf_b(signed char c) { S; return c/3; }
EXPORT(unsigned char)653 EXPORT(unsigned char) __stdcall s_tf_B(unsigned char c) { U; return c/3; }
EXPORT(short)654 EXPORT(short) __stdcall s_tf_h(short c) { S; return c/3; }
EXPORT(unsigned short)655 EXPORT(unsigned short) __stdcall s_tf_H(unsigned short c) { U; return c/3; }
EXPORT(int)656 EXPORT(int) __stdcall s_tf_i(int c) { S; return c/3; }
EXPORT(unsigned int)657 EXPORT(unsigned int) __stdcall s_tf_I(unsigned int c) { U; return c/3; }
EXPORT(long)658 EXPORT(long) __stdcall s_tf_l(long c) { S; return c/3; }
EXPORT(unsigned long)659 EXPORT(unsigned long) __stdcall s_tf_L(unsigned long c) { U; return c/3; }
EXPORT(long long)660 EXPORT(long long) __stdcall s_tf_q(long long c) { S; return c/3; }
EXPORT(unsigned long long)661 EXPORT(unsigned long long) __stdcall s_tf_Q(unsigned long long c) { U; return c/3; }
EXPORT(float)662 EXPORT(float) __stdcall s_tf_f(float c) { S; return c/3; }
EXPORT(double)663 EXPORT(double) __stdcall s_tf_d(double c) { S; return c/3; }
EXPORT(long double)664 EXPORT(long double) __stdcall s_tf_D(long double c) { S; return c/3; }
665 #endif
666 /*******/
667
tf_bb(signed char x,signed char c)668 EXPORT(signed char) tf_bb(signed char x, signed char c) { S; return c/3; }
tf_bB(signed char x,unsigned char c)669 EXPORT(unsigned char) tf_bB(signed char x, unsigned char c) { U; return c/3; }
tf_bh(signed char x,short c)670 EXPORT(short) tf_bh(signed char x, short c) { S; return c/3; }
tf_bH(signed char x,unsigned short c)671 EXPORT(unsigned short) tf_bH(signed char x, unsigned short c) { U; return c/3; }
tf_bi(signed char x,int c)672 EXPORT(int) tf_bi(signed char x, int c) { S; return c/3; }
tf_bI(signed char x,unsigned int c)673 EXPORT(unsigned int) tf_bI(signed char x, unsigned int c) { U; return c/3; }
tf_bl(signed char x,long c)674 EXPORT(long) tf_bl(signed char x, long c) { S; return c/3; }
tf_bL(signed char x,unsigned long c)675 EXPORT(unsigned long) tf_bL(signed char x, unsigned long c) { U; return c/3; }
tf_bq(signed char x,long long c)676 EXPORT(long long) tf_bq(signed char x, long long c) { S; return c/3; }
tf_bQ(signed char x,unsigned long long c)677 EXPORT(unsigned long long) tf_bQ(signed char x, unsigned long long c) { U; return c/3; }
tf_bf(signed char x,float c)678 EXPORT(float) tf_bf(signed char x, float c) { S; return c/3; }
tf_bd(signed char x,double c)679 EXPORT(double) tf_bd(signed char x, double c) { S; return c/3; }
tf_bD(signed char x,long double c)680 EXPORT(long double) tf_bD(signed char x, long double c) { S; return c/3; }
tv_i(int c)681 EXPORT(void) tv_i(int c) { S; return; }
682
683 #ifdef MS_WIN32
EXPORT(signed char)684 EXPORT(signed char) __stdcall s_tf_bb(signed char x, signed char c) { S; return c/3; }
EXPORT(unsigned char)685 EXPORT(unsigned char) __stdcall s_tf_bB(signed char x, unsigned char c) { U; return c/3; }
EXPORT(short)686 EXPORT(short) __stdcall s_tf_bh(signed char x, short c) { S; return c/3; }
EXPORT(unsigned short)687 EXPORT(unsigned short) __stdcall s_tf_bH(signed char x, unsigned short c) { U; return c/3; }
EXPORT(int)688 EXPORT(int) __stdcall s_tf_bi(signed char x, int c) { S; return c/3; }
EXPORT(unsigned int)689 EXPORT(unsigned int) __stdcall s_tf_bI(signed char x, unsigned int c) { U; return c/3; }
EXPORT(long)690 EXPORT(long) __stdcall s_tf_bl(signed char x, long c) { S; return c/3; }
EXPORT(unsigned long)691 EXPORT(unsigned long) __stdcall s_tf_bL(signed char x, unsigned long c) { U; return c/3; }
EXPORT(long long)692 EXPORT(long long) __stdcall s_tf_bq(signed char x, long long c) { S; return c/3; }
EXPORT(unsigned long long)693 EXPORT(unsigned long long) __stdcall s_tf_bQ(signed char x, unsigned long long c) { U; return c/3; }
EXPORT(float)694 EXPORT(float) __stdcall s_tf_bf(signed char x, float c) { S; return c/3; }
EXPORT(double)695 EXPORT(double) __stdcall s_tf_bd(signed char x, double c) { S; return c/3; }
EXPORT(long double)696 EXPORT(long double) __stdcall s_tf_bD(signed char x, long double c) { S; return c/3; }
EXPORT(void)697 EXPORT(void) __stdcall s_tv_i(int c) { S; return; }
698 #endif
699
700 /********/
701
702 #ifndef MS_WIN32
703
704 typedef struct {
705 long x;
706 long y;
707 } POINT;
708
709 typedef struct {
710 long left;
711 long top;
712 long right;
713 long bottom;
714 } RECT;
715
716 #endif
717
PointInRect(RECT * prc,POINT pt)718 EXPORT(int) PointInRect(RECT *prc, POINT pt)
719 {
720 if (pt.x < prc->left)
721 return 0;
722 if (pt.x > prc->right)
723 return 0;
724 if (pt.y < prc->top)
725 return 0;
726 if (pt.y > prc->bottom)
727 return 0;
728 return 1;
729 }
730
731 EXPORT(long left = 10);
732 EXPORT(long top = 20);
733 EXPORT(long right = 30);
734 EXPORT(long bottom = 40);
735
ReturnRect(int i,RECT ar,RECT * br,POINT cp,RECT dr,RECT * er,POINT fp,RECT gr)736 EXPORT(RECT) ReturnRect(int i, RECT ar, RECT* br, POINT cp, RECT dr,
737 RECT *er, POINT fp, RECT gr)
738 {
739 /*Check input */
740 if (ar.left + br->left + dr.left + er->left + gr.left != left * 5)
741 {
742 ar.left = 100;
743 return ar;
744 }
745 if (ar.right + br->right + dr.right + er->right + gr.right != right * 5)
746 {
747 ar.right = 100;
748 return ar;
749 }
750 if (cp.x != fp.x)
751 {
752 ar.left = -100;
753 }
754 if (cp.y != fp.y)
755 {
756 ar.left = -200;
757 }
758 switch(i)
759 {
760 case 0:
761 return ar;
762 break;
763 case 1:
764 return dr;
765 break;
766 case 2:
767 return gr;
768 break;
769
770 }
771 return ar;
772 }
773
774 typedef struct {
775 short x;
776 short y;
777 } S2H;
778
ret_2h_func(S2H inp)779 EXPORT(S2H) ret_2h_func(S2H inp)
780 {
781 inp.x *= 2;
782 inp.y *= 3;
783 return inp;
784 }
785
786 typedef struct {
787 int a, b, c, d, e, f, g, h;
788 } S8I;
789
ret_8i_func(S8I inp)790 EXPORT(S8I) ret_8i_func(S8I inp)
791 {
792 inp.a *= 2;
793 inp.b *= 3;
794 inp.c *= 4;
795 inp.d *= 5;
796 inp.e *= 6;
797 inp.f *= 7;
798 inp.g *= 8;
799 inp.h *= 9;
800 return inp;
801 }
802
GetRectangle(int flag,RECT * prect)803 EXPORT(int) GetRectangle(int flag, RECT *prect)
804 {
805 if (flag == 0)
806 return 0;
807 prect->left = (int)flag;
808 prect->top = (int)flag + 1;
809 prect->right = (int)flag + 2;
810 prect->bottom = (int)flag + 3;
811 return 1;
812 }
813
TwoOutArgs(int a,int * pi,int b,int * pj)814 EXPORT(void) TwoOutArgs(int a, int *pi, int b, int *pj)
815 {
816 *pi += a;
817 *pj += b;
818 }
819
820 #ifdef MS_WIN32
821
822 typedef struct {
823 char f1;
824 } Size1;
825
826 typedef struct {
827 char f1;
828 char f2;
829 } Size2;
830
831 typedef struct {
832 char f1;
833 char f2;
834 char f3;
835 } Size3;
836
837 typedef struct {
838 char f1;
839 char f2;
840 char f3;
841 char f4;
842 } Size4;
843
844 typedef struct {
845 char f1;
846 char f2;
847 char f3;
848 char f4;
849 char f5;
850 } Size5;
851
852 typedef struct {
853 char f1;
854 char f2;
855 char f3;
856 char f4;
857 char f5;
858 char f6;
859 } Size6;
860
861 typedef struct {
862 char f1;
863 char f2;
864 char f3;
865 char f4;
866 char f5;
867 char f6;
868 char f7;
869 } Size7;
870
871 typedef struct {
872 char f1;
873 char f2;
874 char f3;
875 char f4;
876 char f5;
877 char f6;
878 char f7;
879 char f8;
880 } Size8;
881
882 typedef struct {
883 char f1;
884 char f2;
885 char f3;
886 char f4;
887 char f5;
888 char f6;
889 char f7;
890 char f8;
891 char f9;
892 } Size9;
893
894 typedef struct {
895 char f1;
896 char f2;
897 char f3;
898 char f4;
899 char f5;
900 char f6;
901 char f7;
902 char f8;
903 char f9;
904 char f10;
905 } Size10;
906
TestSize1()907 EXPORT(Size1) TestSize1() {
908 Size1 f;
909 f.f1 = 'a';
910 return f;
911 }
912
TestSize2()913 EXPORT(Size2) TestSize2() {
914 Size2 f;
915 f.f1 = 'a';
916 f.f2 = 'b';
917 return f;
918 }
919
TestSize3()920 EXPORT(Size3) TestSize3() {
921 Size3 f;
922 f.f1 = 'a';
923 f.f2 = 'b';
924 f.f3 = 'c';
925 return f;
926 }
927
TestSize4()928 EXPORT(Size4) TestSize4() {
929 Size4 f;
930 f.f1 = 'a';
931 f.f2 = 'b';
932 f.f3 = 'c';
933 f.f4 = 'd';
934 return f;
935 }
936
TestSize5()937 EXPORT(Size5) TestSize5() {
938 Size5 f;
939 f.f1 = 'a';
940 f.f2 = 'b';
941 f.f3 = 'c';
942 f.f4 = 'd';
943 f.f5 = 'e';
944 return f;
945 }
946
TestSize6()947 EXPORT(Size6) TestSize6() {
948 Size6 f;
949 f.f1 = 'a';
950 f.f2 = 'b';
951 f.f3 = 'c';
952 f.f4 = 'd';
953 f.f5 = 'e';
954 f.f6 = 'f';
955 return f;
956 }
957
TestSize7()958 EXPORT(Size7) TestSize7() {
959 Size7 f;
960 f.f1 = 'a';
961 f.f2 = 'b';
962 f.f3 = 'c';
963 f.f4 = 'd';
964 f.f5 = 'e';
965 f.f6 = 'f';
966 f.f7 = 'g';
967 return f;
968 }
969
TestSize8()970 EXPORT(Size8) TestSize8() {
971 Size8 f;
972 f.f1 = 'a';
973 f.f2 = 'b';
974 f.f3 = 'c';
975 f.f4 = 'd';
976 f.f5 = 'e';
977 f.f6 = 'f';
978 f.f7 = 'g';
979 f.f8 = 'h';
980 return f;
981 }
982
TestSize9()983 EXPORT(Size9) TestSize9() {
984 Size9 f;
985 f.f1 = 'a';
986 f.f2 = 'b';
987 f.f3 = 'c';
988 f.f4 = 'd';
989 f.f5 = 'e';
990 f.f6 = 'f';
991 f.f7 = 'g';
992 f.f8 = 'h';
993 f.f9 = 'i';
994 return f;
995 }
996
TestSize10()997 EXPORT(Size10) TestSize10() {
998 Size10 f;
999 f.f1 = 'a';
1000 f.f2 = 'b';
1001 f.f3 = 'c';
1002 f.f4 = 'd';
1003 f.f5 = 'e';
1004 f.f6 = 'f';
1005 f.f7 = 'g';
1006 f.f8 = 'h';
1007 f.f9 = 'i';
1008 f.f10 = 'j';
1009 return f;
1010 }
1011
1012 #endif
1013
1014 #ifdef MS_WIN32
EXPORT(S2H)1015 EXPORT(S2H) __stdcall s_ret_2h_func(S2H inp) { return ret_2h_func(inp); }
EXPORT(S8I)1016 EXPORT(S8I) __stdcall s_ret_8i_func(S8I inp) { return ret_8i_func(inp); }
1017 #endif
1018
1019 #ifdef MS_WIN32
1020 /* Should port this */
1021 #include <stdlib.h>
1022 #include <search.h>
1023
KeepObject(IUnknown * punk)1024 EXPORT (HRESULT) KeepObject(IUnknown *punk)
1025 {
1026 static IUnknown *pobj;
1027 if (punk)
1028 punk->lpVtbl->AddRef(punk);
1029 if (pobj)
1030 pobj->lpVtbl->Release(pobj);
1031 pobj = punk;
1032 return S_OK;
1033 }
1034
1035 #endif
1036
1037 EXPORT(int)
_testfunc_pylist_append(PyObject * list,PyObject * item)1038 _testfunc_pylist_append(PyObject *list, PyObject *item)
1039 {
1040 return PyList_Append(list, item);
1041 }
1042
1043 static struct PyModuleDef_Slot _ctypes_test_slots[] = {
1044 {0, NULL}
1045 };
1046
1047 static struct PyModuleDef _ctypes_testmodule = {
1048 PyModuleDef_HEAD_INIT,
1049 "_ctypes_test",
1050 NULL,
1051 0,
1052 module_methods,
1053 _ctypes_test_slots,
1054 NULL,
1055 NULL,
1056 NULL
1057 };
1058
1059 PyMODINIT_FUNC
PyInit__ctypes_test(void)1060 PyInit__ctypes_test(void)
1061 {
1062 return PyModuleDef_Init(&_ctypes_testmodule);
1063 }
1064