Debian Codegraph @

Modules/_ctypes/_ctypes_test.c

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