Debian Codegraph @

lib_pypy/_ctypes_test.c

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