Issue #5768: Change to Unicode output logic and test case for same.
[python.git] / Modules / _ctypes / _ctypes_test.c
blob3f13180e0ac3df7323976cabaff036596bdcd016
1 /*****************************************************************
2 This file should be kept compatible with Python 2.3, see PEP 291.
3 *****************************************************************/
6 #include <Python.h>
8 /*
9 Backwards compatibility:
10 Python2.2 used LONG_LONG instead of PY_LONG_LONG
12 #if defined(HAVE_LONG_LONG) && !defined(PY_LONG_LONG)
13 #define PY_LONG_LONG LONG_LONG
14 #endif
16 #ifdef MS_WIN32
17 #include <windows.h>
18 #endif
20 #if defined(MS_WIN32) || defined(__CYGWIN__)
21 #define EXPORT(x) __declspec(dllexport) x
22 #else
23 #define EXPORT(x) x
24 #endif
26 /* some functions handy for testing */
28 EXPORT(void)testfunc_array(int values[4])
30 printf("testfunc_array %d %d %d %d\n",
31 values[0],
32 values[1],
33 values[2],
34 values[3]);
37 EXPORT(long double)testfunc_Ddd(double a, double b)
39 long double result = (long double)(a * b);
40 printf("testfunc_Ddd(%p, %p)\n", &a, &b);
41 printf("testfunc_Ddd(%g, %g)\n", a, b);
42 return result;
45 EXPORT(long double)testfunc_DDD(long double a, long double b)
47 long double result = a * b;
48 printf("testfunc_DDD(%p, %p)\n", &a, &b);
49 printf("testfunc_DDD(%Lg, %Lg)\n", a, b);
50 return result;
53 EXPORT(int)testfunc_iii(int a, int b)
55 int result = a * b;
56 printf("testfunc_iii(%p, %p)\n", &a, &b);
57 return result;
60 EXPORT(int)myprintf(char *fmt, ...)
62 int result;
63 va_list argptr;
64 va_start(argptr, fmt);
65 result = vprintf(fmt, argptr);
66 va_end(argptr);
67 return result;
70 EXPORT(char *)my_strtok(char *token, const char *delim)
72 return strtok(token, delim);
75 EXPORT(char *)my_strchr(const char *s, int c)
77 return strchr(s, c);
81 EXPORT(double) my_sqrt(double a)
83 return sqrt(a);
86 EXPORT(void) my_qsort(void *base, size_t num, size_t width, int(*compare)(const void*, const void*))
88 qsort(base, num, width, compare);
91 EXPORT(int *) _testfunc_ai8(int a[8])
93 return a;
96 EXPORT(void) _testfunc_v(int a, int b, int *presult)
98 *presult = a + b;
101 EXPORT(int) _testfunc_i_bhilfd(signed char b, short h, int i, long l, float f, double d)
103 /* printf("_testfunc_i_bhilfd got %d %d %d %ld %f %f\n",
104 b, h, i, l, f, d);
106 return (int)(b + h + i + l + f + d);
109 EXPORT(float) _testfunc_f_bhilfd(signed char b, short h, int i, long l, float f, double d)
111 /* printf("_testfunc_f_bhilfd got %d %d %d %ld %f %f\n",
112 b, h, i, l, f, d);
114 return (float)(b + h + i + l + f + d);
117 EXPORT(double) _testfunc_d_bhilfd(signed char b, short h, int i, long l, float f, double d)
119 /* printf("_testfunc_d_bhilfd got %d %d %d %ld %f %f\n",
120 b, h, i, l, f, d);
122 return (double)(b + h + i + l + f + d);
125 EXPORT(long double) _testfunc_D_bhilfD(signed char b, short h, int i, long l, float f, long double d)
127 /* printf("_testfunc_d_bhilfd got %d %d %d %ld %f %f\n",
128 b, h, i, l, f, d);
130 return (long double)(b + h + i + l + f + d);
133 EXPORT(char *) _testfunc_p_p(void *s)
135 return (char *)s;
138 EXPORT(void *) _testfunc_c_p_p(int *argcp, char **argv)
140 return argv[(*argcp)-1];
143 EXPORT(void *) get_strchr(void)
145 return (void *)strchr;
148 EXPORT(char *) my_strdup(char *src)
150 char *dst = (char *)malloc(strlen(src)+1);
151 if (!dst)
152 return NULL;
153 strcpy(dst, src);
154 return dst;
157 EXPORT(void)my_free(void *ptr)
159 free(ptr);
162 #ifdef HAVE_WCHAR_H
163 EXPORT(wchar_t *) my_wcsdup(wchar_t *src)
165 size_t len = wcslen(src);
166 wchar_t *ptr = (wchar_t *)malloc((len + 1) * sizeof(wchar_t));
167 if (ptr == NULL)
168 return NULL;
169 memcpy(ptr, src, (len+1) * sizeof(wchar_t));
170 return ptr;
173 EXPORT(size_t) my_wcslen(wchar_t *src)
175 return wcslen(src);
177 #endif
179 #ifndef MS_WIN32
180 # ifndef __stdcall
181 # define __stdcall /* */
182 # endif
183 #endif
185 typedef struct {
186 int (*c)(int, int);
187 int (__stdcall *s)(int, int);
188 } FUNCS;
190 EXPORT(int) _testfunc_callfuncp(FUNCS *fp)
192 fp->c(1, 2);
193 fp->s(3, 4);
194 return 0;
197 EXPORT(int) _testfunc_deref_pointer(int *pi)
199 return *pi;
202 #ifdef MS_WIN32
203 EXPORT(int) _testfunc_piunk(IUnknown FAR *piunk)
205 piunk->lpVtbl->AddRef(piunk);
206 return piunk->lpVtbl->Release(piunk);
208 #endif
210 EXPORT(int) _testfunc_callback_with_pointer(int (*func)(int *))
212 int table[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
214 return (*func)(table);
217 #ifdef HAVE_LONG_LONG
218 EXPORT(PY_LONG_LONG) _testfunc_q_bhilfdq(signed char b, short h, int i, long l, float f,
219 double d, PY_LONG_LONG q)
221 return (PY_LONG_LONG)(b + h + i + l + f + d + q);
224 EXPORT(PY_LONG_LONG) _testfunc_q_bhilfd(signed char b, short h, int i, long l, float f, double d)
226 return (PY_LONG_LONG)(b + h + i + l + f + d);
229 EXPORT(int) _testfunc_callback_i_if(int value, int (*func)(int))
231 int sum = 0;
232 while (value != 0) {
233 sum += func(value);
234 value /= 2;
236 return sum;
239 EXPORT(PY_LONG_LONG) _testfunc_callback_q_qf(PY_LONG_LONG value,
240 PY_LONG_LONG (*func)(PY_LONG_LONG))
242 PY_LONG_LONG sum = 0;
244 while (value != 0) {
245 sum += func(value);
246 value /= 2;
248 return sum;
251 #endif
253 typedef struct {
254 char *name;
255 char *value;
256 } SPAM;
258 typedef struct {
259 char *name;
260 int num_spams;
261 SPAM *spams;
262 } EGG;
264 SPAM my_spams[2] = {
265 { "name1", "value1" },
266 { "name2", "value2" },
269 EGG my_eggs[1] = {
270 { "first egg", 1, my_spams }
273 EXPORT(int) getSPAMANDEGGS(EGG **eggs)
275 *eggs = my_eggs;
276 return 1;
279 typedef struct tagpoint {
280 int x;
281 int y;
282 } point;
284 EXPORT(int) _testfunc_byval(point in, point *pout)
286 if (pout) {
287 pout->x = in.x;
288 pout->y = in.y;
290 return in.x + in.y;
293 EXPORT (int) an_integer = 42;
295 EXPORT(int) get_an_integer(void)
297 return an_integer;
300 EXPORT(double)
301 integrate(double a, double b, double (*f)(double), long nstep)
303 double x, sum=0.0, dx=(b-a)/(double)nstep;
304 for(x=a+0.5*dx; (b-x)*(x-a)>0.0; x+=dx)
305 sum += f(x);
306 return sum/(double)nstep;
309 typedef struct {
310 void (*initialize)(void *(*)(int), void(*)(void *));
311 } xxx_library;
313 static void _xxx_init(void *(*Xalloc)(int), void (*Xfree)(void *))
315 void *ptr;
317 printf("_xxx_init got %p %p\n", Xalloc, Xfree);
318 printf("calling\n");
319 ptr = Xalloc(32);
320 Xfree(ptr);
321 printf("calls done, ptr was %p\n", ptr);
324 xxx_library _xxx_lib = {
325 _xxx_init
328 EXPORT(xxx_library) *library_get(void)
330 return &_xxx_lib;
333 #ifdef MS_WIN32
334 /* See Don Box (german), pp 79ff. */
335 EXPORT(void) GetString(BSTR *pbstr)
337 *pbstr = SysAllocString(L"Goodbye!");
339 #endif
342 * Some do-nothing functions, for speed tests
344 PyObject *py_func_si(PyObject *self, PyObject *args)
346 char *name;
347 int i;
348 if (!PyArg_ParseTuple(args, "si", &name, &i))
349 return NULL;
350 Py_INCREF(Py_None);
351 return Py_None;
354 EXPORT(void) _py_func_si(char *s, int i)
358 PyObject *py_func(PyObject *self, PyObject *args)
360 Py_INCREF(Py_None);
361 return Py_None;
364 EXPORT(void) _py_func(void)
368 EXPORT(PY_LONG_LONG) last_tf_arg_s;
369 EXPORT(unsigned PY_LONG_LONG) last_tf_arg_u;
371 struct BITS {
372 int A: 1, B:2, C:3, D:4, E: 5, F: 6, G: 7, H: 8, I: 9;
373 short M: 1, N: 2, O: 3, P: 4, Q: 5, R: 6, S: 7;
376 DL_EXPORT(void) set_bitfields(struct BITS *bits, char name, int value)
378 switch (name) {
379 case 'A': bits->A = value; break;
380 case 'B': bits->B = value; break;
381 case 'C': bits->C = value; break;
382 case 'D': bits->D = value; break;
383 case 'E': bits->E = value; break;
384 case 'F': bits->F = value; break;
385 case 'G': bits->G = value; break;
386 case 'H': bits->H = value; break;
387 case 'I': bits->I = value; break;
389 case 'M': bits->M = value; break;
390 case 'N': bits->N = value; break;
391 case 'O': bits->O = value; break;
392 case 'P': bits->P = value; break;
393 case 'Q': bits->Q = value; break;
394 case 'R': bits->R = value; break;
395 case 'S': bits->S = value; break;
399 DL_EXPORT(int) unpack_bitfields(struct BITS *bits, char name)
401 switch (name) {
402 case 'A': return bits->A;
403 case 'B': return bits->B;
404 case 'C': return bits->C;
405 case 'D': return bits->D;
406 case 'E': return bits->E;
407 case 'F': return bits->F;
408 case 'G': return bits->G;
409 case 'H': return bits->H;
410 case 'I': return bits->I;
412 case 'M': return bits->M;
413 case 'N': return bits->N;
414 case 'O': return bits->O;
415 case 'P': return bits->P;
416 case 'Q': return bits->Q;
417 case 'R': return bits->R;
418 case 'S': return bits->S;
420 return 0;
423 static PyMethodDef module_methods[] = {
424 /* {"get_last_tf_arg_s", get_last_tf_arg_s, METH_NOARGS},
425 {"get_last_tf_arg_u", get_last_tf_arg_u, METH_NOARGS},
427 {"func_si", py_func_si, METH_VARARGS},
428 {"func", py_func, METH_NOARGS},
429 { NULL, NULL, 0, NULL},
432 #define S last_tf_arg_s = (PY_LONG_LONG)c
433 #define U last_tf_arg_u = (unsigned PY_LONG_LONG)c
435 EXPORT(signed char) tf_b(signed char c) { S; return c/3; }
436 EXPORT(unsigned char) tf_B(unsigned char c) { U; return c/3; }
437 EXPORT(short) tf_h(short c) { S; return c/3; }
438 EXPORT(unsigned short) tf_H(unsigned short c) { U; return c/3; }
439 EXPORT(int) tf_i(int c) { S; return c/3; }
440 EXPORT(unsigned int) tf_I(unsigned int c) { U; return c/3; }
441 EXPORT(long) tf_l(long c) { S; return c/3; }
442 EXPORT(unsigned long) tf_L(unsigned long c) { U; return c/3; }
443 EXPORT(PY_LONG_LONG) tf_q(PY_LONG_LONG c) { S; return c/3; }
444 EXPORT(unsigned PY_LONG_LONG) tf_Q(unsigned PY_LONG_LONG c) { U; return c/3; }
445 EXPORT(float) tf_f(float c) { S; return c/3; }
446 EXPORT(double) tf_d(double c) { S; return c/3; }
447 EXPORT(long double) tf_D(long double c) { S; return c/3; }
449 #ifdef MS_WIN32
450 EXPORT(signed char) __stdcall s_tf_b(signed char c) { S; return c/3; }
451 EXPORT(unsigned char) __stdcall s_tf_B(unsigned char c) { U; return c/3; }
452 EXPORT(short) __stdcall s_tf_h(short c) { S; return c/3; }
453 EXPORT(unsigned short) __stdcall s_tf_H(unsigned short c) { U; return c/3; }
454 EXPORT(int) __stdcall s_tf_i(int c) { S; return c/3; }
455 EXPORT(unsigned int) __stdcall s_tf_I(unsigned int c) { U; return c/3; }
456 EXPORT(long) __stdcall s_tf_l(long c) { S; return c/3; }
457 EXPORT(unsigned long) __stdcall s_tf_L(unsigned long c) { U; return c/3; }
458 EXPORT(PY_LONG_LONG) __stdcall s_tf_q(PY_LONG_LONG c) { S; return c/3; }
459 EXPORT(unsigned PY_LONG_LONG) __stdcall s_tf_Q(unsigned PY_LONG_LONG c) { U; return c/3; }
460 EXPORT(float) __stdcall s_tf_f(float c) { S; return c/3; }
461 EXPORT(double) __stdcall s_tf_d(double c) { S; return c/3; }
462 EXPORT(long double) __stdcall s_tf_D(long double c) { S; return c/3; }
463 #endif
464 /*******/
466 EXPORT(signed char) tf_bb(signed char x, signed char c) { S; return c/3; }
467 EXPORT(unsigned char) tf_bB(signed char x, unsigned char c) { U; return c/3; }
468 EXPORT(short) tf_bh(signed char x, short c) { S; return c/3; }
469 EXPORT(unsigned short) tf_bH(signed char x, unsigned short c) { U; return c/3; }
470 EXPORT(int) tf_bi(signed char x, int c) { S; return c/3; }
471 EXPORT(unsigned int) tf_bI(signed char x, unsigned int c) { U; return c/3; }
472 EXPORT(long) tf_bl(signed char x, long c) { S; return c/3; }
473 EXPORT(unsigned long) tf_bL(signed char x, unsigned long c) { U; return c/3; }
474 EXPORT(PY_LONG_LONG) tf_bq(signed char x, PY_LONG_LONG c) { S; return c/3; }
475 EXPORT(unsigned PY_LONG_LONG) tf_bQ(signed char x, unsigned PY_LONG_LONG c) { U; return c/3; }
476 EXPORT(float) tf_bf(signed char x, float c) { S; return c/3; }
477 EXPORT(double) tf_bd(signed char x, double c) { S; return c/3; }
478 EXPORT(long double) tf_bD(signed char x, long double c) { S; return c/3; }
479 EXPORT(void) tv_i(int c) { S; return; }
481 #ifdef MS_WIN32
482 EXPORT(signed char) __stdcall s_tf_bb(signed char x, signed char c) { S; return c/3; }
483 EXPORT(unsigned char) __stdcall s_tf_bB(signed char x, unsigned char c) { U; return c/3; }
484 EXPORT(short) __stdcall s_tf_bh(signed char x, short c) { S; return c/3; }
485 EXPORT(unsigned short) __stdcall s_tf_bH(signed char x, unsigned short c) { U; return c/3; }
486 EXPORT(int) __stdcall s_tf_bi(signed char x, int c) { S; return c/3; }
487 EXPORT(unsigned int) __stdcall s_tf_bI(signed char x, unsigned int c) { U; return c/3; }
488 EXPORT(long) __stdcall s_tf_bl(signed char x, long c) { S; return c/3; }
489 EXPORT(unsigned long) __stdcall s_tf_bL(signed char x, unsigned long c) { U; return c/3; }
490 EXPORT(PY_LONG_LONG) __stdcall s_tf_bq(signed char x, PY_LONG_LONG c) { S; return c/3; }
491 EXPORT(unsigned PY_LONG_LONG) __stdcall s_tf_bQ(signed char x, unsigned PY_LONG_LONG c) { U; return c/3; }
492 EXPORT(float) __stdcall s_tf_bf(signed char x, float c) { S; return c/3; }
493 EXPORT(double) __stdcall s_tf_bd(signed char x, double c) { S; return c/3; }
494 EXPORT(long double) __stdcall s_tf_bD(signed char x, long double c) { S; return c/3; }
495 EXPORT(void) __stdcall s_tv_i(int c) { S; return; }
496 #endif
498 /********/
500 #ifndef MS_WIN32
502 typedef struct {
503 long x;
504 long y;
505 } POINT;
507 typedef struct {
508 long left;
509 long top;
510 long right;
511 long bottom;
512 } RECT;
514 #endif
516 EXPORT(int) PointInRect(RECT *prc, POINT pt)
518 if (pt.x < prc->left)
519 return 0;
520 if (pt.x > prc->right)
521 return 0;
522 if (pt.y < prc->top)
523 return 0;
524 if (pt.y > prc->bottom)
525 return 0;
526 return 1;
529 typedef struct {
530 short x;
531 short y;
532 } S2H;
534 EXPORT(S2H) ret_2h_func(S2H inp)
536 inp.x *= 2;
537 inp.y *= 3;
538 return inp;
541 typedef struct {
542 int a, b, c, d, e, f, g, h;
543 } S8I;
545 EXPORT(S8I) ret_8i_func(S8I inp)
547 inp.a *= 2;
548 inp.b *= 3;
549 inp.c *= 4;
550 inp.d *= 5;
551 inp.e *= 6;
552 inp.f *= 7;
553 inp.g *= 8;
554 inp.h *= 9;
555 return inp;
558 EXPORT(int) GetRectangle(int flag, RECT *prect)
560 if (flag == 0)
561 return 0;
562 prect->left = (int)flag;
563 prect->top = (int)flag + 1;
564 prect->right = (int)flag + 2;
565 prect->bottom = (int)flag + 3;
566 return 1;
569 EXPORT(void) TwoOutArgs(int a, int *pi, int b, int *pj)
571 *pi += a;
572 *pj += b;
575 #ifdef MS_WIN32
576 EXPORT(S2H) __stdcall s_ret_2h_func(S2H inp) { return ret_2h_func(inp); }
577 EXPORT(S8I) __stdcall s_ret_8i_func(S8I inp) { return ret_8i_func(inp); }
578 #endif
580 #ifdef MS_WIN32
581 /* Should port this */
582 #include <stdlib.h>
583 #include <search.h>
585 EXPORT (HRESULT) KeepObject(IUnknown *punk)
587 static IUnknown *pobj;
588 if (punk)
589 punk->lpVtbl->AddRef(punk);
590 if (pobj)
591 pobj->lpVtbl->Release(pobj);
592 pobj = punk;
593 return S_OK;
596 #endif
598 DL_EXPORT(void)
599 init_ctypes_test(void)
601 Py_InitModule("_ctypes_test", module_methods);