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