Python-2.7.3/Modules/_ctypes/_ctypes_test.c

No issues found

  1 /*****************************************************************
  2   This file should be kept compatible with Python 2.3, see PEP 291.
  3  *****************************************************************/
  4 
  5 
  6 #include <Python.h>
  7 
  8 /*
  9   Backwards compatibility:
 10   Python2.2 used LONG_LONG instead of PY_LONG_LONG
 11 */
 12 #if defined(HAVE_LONG_LONG) && !defined(PY_LONG_LONG)
 13 #define PY_LONG_LONG LONG_LONG
 14 #endif
 15 
 16 #ifdef MS_WIN32
 17 #include <windows.h>
 18 #endif
 19 
 20 #if defined(MS_WIN32) || defined(__CYGWIN__)
 21 #define EXPORT(x) __declspec(dllexport) x
 22 #else
 23 #define EXPORT(x) x
 24 #endif
 25 
 26 /* some functions handy for testing */
 27 
 28 EXPORT(int)
 29 _testfunc_cbk_reg_int(int a, int b, int c, int d, int e,
 30                       int (*func)(int, int, int, int, int))
 31 {
 32     return func(a*a, b*b, c*c, d*d, e*e);
 33 }
 34 
 35 EXPORT(double)
 36 _testfunc_cbk_reg_double(double a, double b, double c, double d, double e,
 37                          double (*func)(double, double, double, double, double))
 38 {
 39     return func(a*a, b*b, c*c, d*d, e*e);
 40 }
 41 
 42 EXPORT(void)testfunc_array(int values[4])
 43 {
 44     printf("testfunc_array %d %d %d %d\n",
 45            values[0],
 46            values[1],
 47            values[2],
 48            values[3]);
 49 }
 50 
 51 EXPORT(long double)testfunc_Ddd(double a, double b)
 52 {
 53     long double result = (long double)(a * b);
 54     printf("testfunc_Ddd(%p, %p)\n", &a, &b);
 55     printf("testfunc_Ddd(%g, %g)\n", a, b);
 56     return result;
 57 }
 58 
 59 EXPORT(long double)testfunc_DDD(long double a, long double b)
 60 {
 61     long double result = a * b;
 62     printf("testfunc_DDD(%p, %p)\n", &a, &b);
 63     printf("testfunc_DDD(%Lg, %Lg)\n", a, b);
 64     return result;
 65 }
 66 
 67 EXPORT(int)testfunc_iii(int a, int b)
 68 {
 69     int result = a * b;
 70     printf("testfunc_iii(%p, %p)\n", &a, &b);
 71     return result;
 72 }
 73 
 74 EXPORT(int)myprintf(char *fmt, ...)
 75 {
 76     int result;
 77     va_list argptr;
 78     va_start(argptr, fmt);
 79     result = vprintf(fmt, argptr);
 80     va_end(argptr);
 81     return result;
 82 }
 83 
 84 EXPORT(char *)my_strtok(char *token, const char *delim)
 85 {
 86     return strtok(token, delim);
 87 }
 88 
 89 EXPORT(char *)my_strchr(const char *s, int c)
 90 {
 91     return strchr(s, c);
 92 }
 93 
 94 
 95 EXPORT(double) my_sqrt(double a)
 96 {
 97     return sqrt(a);
 98 }
 99 
100 EXPORT(void) my_qsort(void *base, size_t num, size_t width, int(*compare)(const void*, const void*))
101 {
102     qsort(base, num, width, compare);
103 }
104 
105 EXPORT(int *) _testfunc_ai8(int a[8])
106 {
107     return a;
108 }
109 
110 EXPORT(void) _testfunc_v(int a, int b, int *presult)
111 {
112     *presult = a + b;
113 }
114 
115 EXPORT(int) _testfunc_i_bhilfd(signed char b, short h, int i, long l, float f, double d)
116 {
117 /*      printf("_testfunc_i_bhilfd got %d %d %d %ld %f %f\n",
118                b, h, i, l, f, d);
119 */
120     return (int)(b + h + i + l + f + d);
121 }
122 
123 EXPORT(float) _testfunc_f_bhilfd(signed char b, short h, int i, long l, float f, double d)
124 {
125 /*      printf("_testfunc_f_bhilfd got %d %d %d %ld %f %f\n",
126                b, h, i, l, f, d);
127 */
128     return (float)(b + h + i + l + f + d);
129 }
130 
131 EXPORT(double) _testfunc_d_bhilfd(signed char b, short h, int i, long l, float f, double d)
132 {
133 /*      printf("_testfunc_d_bhilfd got %d %d %d %ld %f %f\n",
134                b, h, i, l, f, d);
135 */
136     return (double)(b + h + i + l + f + d);
137 }
138 
139 EXPORT(long double) _testfunc_D_bhilfD(signed char b, short h, int i, long l, float f, long double d)
140 {
141 /*      printf("_testfunc_d_bhilfd got %d %d %d %ld %f %f\n",
142                b, h, i, l, f, d);
143 */
144     return (long double)(b + h + i + l + f + d);
145 }
146 
147 EXPORT(char *) _testfunc_p_p(void *s)
148 {
149     return (char *)s;
150 }
151 
152 EXPORT(void *) _testfunc_c_p_p(int *argcp, char **argv)
153 {
154     return argv[(*argcp)-1];
155 }
156 
157 EXPORT(void *) get_strchr(void)
158 {
159     return (void *)strchr;
160 }
161 
162 EXPORT(char *) my_strdup(char *src)
163 {
164     char *dst = (char *)malloc(strlen(src)+1);
165     if (!dst)
166         return NULL;
167     strcpy(dst, src);
168     return dst;
169 }
170 
171 EXPORT(void)my_free(void *ptr)
172 {
173     free(ptr);
174 }
175 
176 #ifdef HAVE_WCHAR_H
177 EXPORT(wchar_t *) my_wcsdup(wchar_t *src)
178 {
179     size_t len = wcslen(src);
180     wchar_t *ptr = (wchar_t *)malloc((len + 1) * sizeof(wchar_t));
181     if (ptr == NULL)
182         return NULL;
183     memcpy(ptr, src, (len+1) * sizeof(wchar_t));
184     return ptr;
185 }
186 
187 EXPORT(size_t) my_wcslen(wchar_t *src)
188 {
189     return wcslen(src);
190 }
191 #endif
192 
193 #ifndef MS_WIN32
194 # ifndef __stdcall
195 #  define __stdcall /* */
196 # endif
197 #endif
198 
199 typedef struct {
200     int (*c)(int, int);
201     int (__stdcall *s)(int, int);
202 } FUNCS;
203 
204 EXPORT(int) _testfunc_callfuncp(FUNCS *fp)
205 {
206     fp->c(1, 2);
207     fp->s(3, 4);
208     return 0;
209 }
210 
211 EXPORT(int) _testfunc_deref_pointer(int *pi)
212 {
213     return *pi;
214 }
215 
216 #ifdef MS_WIN32
217 EXPORT(int) _testfunc_piunk(IUnknown FAR *piunk)
218 {
219     piunk->lpVtbl->AddRef(piunk);
220     return piunk->lpVtbl->Release(piunk);
221 }
222 #endif
223 
224 EXPORT(int) _testfunc_callback_with_pointer(int (*func)(int *))
225 {
226     int table[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
227 
228     return (*func)(table);
229 }
230 
231 #ifdef HAVE_LONG_LONG
232 EXPORT(PY_LONG_LONG) _testfunc_q_bhilfdq(signed char b, short h, int i, long l, float f,
233                                      double d, PY_LONG_LONG q)
234 {
235     return (PY_LONG_LONG)(b + h + i + l + f + d + q);
236 }
237 
238 EXPORT(PY_LONG_LONG) _testfunc_q_bhilfd(signed char b, short h, int i, long l, float f, double d)
239 {
240     return (PY_LONG_LONG)(b + h + i + l + f + d);
241 }
242 
243 EXPORT(int) _testfunc_callback_i_if(int value, int (*func)(int))
244 {
245     int sum = 0;
246     while (value != 0) {
247         sum += func(value);
248         value /= 2;
249     }
250     return sum;
251 }
252 
253 EXPORT(PY_LONG_LONG) _testfunc_callback_q_qf(PY_LONG_LONG value,
254                                              PY_LONG_LONG (*func)(PY_LONG_LONG))
255 {
256     PY_LONG_LONG sum = 0;
257 
258     while (value != 0) {
259         sum += func(value);
260         value /= 2;
261     }
262     return sum;
263 }
264 
265 #endif
266 
267 typedef struct {
268     char *name;
269     char *value;
270 } SPAM;
271 
272 typedef struct {
273     char *name;
274     int num_spams;
275     SPAM *spams;
276 } EGG;
277 
278 SPAM my_spams[2] = {
279     { "name1", "value1" },
280     { "name2", "value2" },
281 };
282 
283 EGG my_eggs[1] = {
284     { "first egg", 1, my_spams }
285 };
286 
287 EXPORT(int) getSPAMANDEGGS(EGG **eggs)
288 {
289     *eggs = my_eggs;
290     return 1;
291 }
292 
293 typedef struct tagpoint {
294     int x;
295     int y;
296 } point;
297 
298 EXPORT(int) _testfunc_byval(point in, point *pout)
299 {
300     if (pout) {
301         pout->x = in.x;
302         pout->y = in.y;
303     }
304     return in.x + in.y;
305 }
306 
307 EXPORT (int) an_integer = 42;
308 
309 EXPORT(int) get_an_integer(void)
310 {
311     return an_integer;
312 }
313 
314 EXPORT(double)
315 integrate(double a, double b, double (*f)(double), long nstep)
316 {
317     double x, sum=0.0, dx=(b-a)/(double)nstep;
318     for(x=a+0.5*dx; (b-x)*(x-a)>0.0; x+=dx)
319         sum += f(x);
320     return sum/(double)nstep;
321 }
322 
323 typedef struct {
324     void (*initialize)(void *(*)(int), void(*)(void *));
325 } xxx_library;
326 
327 static void _xxx_init(void *(*Xalloc)(int), void (*Xfree)(void *))
328 {
329     void *ptr;
330 
331     printf("_xxx_init got %p %p\n", Xalloc, Xfree);
332     printf("calling\n");
333     ptr = Xalloc(32);
334     Xfree(ptr);
335     printf("calls done, ptr was %p\n", ptr);
336 }
337 
338 xxx_library _xxx_lib = {
339     _xxx_init
340 };
341 
342 EXPORT(xxx_library) *library_get(void)
343 {
344     return &_xxx_lib;
345 }
346 
347 #ifdef MS_WIN32
348 /* See Don Box (german), pp 79ff. */
349 EXPORT(void) GetString(BSTR *pbstr)
350 {
351     *pbstr = SysAllocString(L"Goodbye!");
352 }
353 #endif
354 
355 /*
356  * Some do-nothing functions, for speed tests
357  */
358 PyObject *py_func_si(PyObject *self, PyObject *args)
359 {
360     char *name;
361     int i;
362     if (!PyArg_ParseTuple(args, "si", &name, &i))
363         return NULL;
364     Py_INCREF(Py_None);
365     return Py_None;
366 }
367 
368 EXPORT(void) _py_func_si(char *s, int i)
369 {
370 }
371 
372 PyObject *py_func(PyObject *self, PyObject *args)
373 {
374     Py_INCREF(Py_None);
375     return Py_None;
376 }
377 
378 EXPORT(void) _py_func(void)
379 {
380 }
381 
382 EXPORT(PY_LONG_LONG) last_tf_arg_s;
383 EXPORT(unsigned PY_LONG_LONG) last_tf_arg_u;
384 
385 struct BITS {
386     int A: 1, B:2, C:3, D:4, E: 5, F: 6, G: 7, H: 8, I: 9;
387     short M: 1, N: 2, O: 3, P: 4, Q: 5, R: 6, S: 7;
388 };
389 
390 DL_EXPORT(void) set_bitfields(struct BITS *bits, char name, int value)
391 {
392     switch (name) {
393     case 'A': bits->A = value; break;
394     case 'B': bits->B = value; break;
395     case 'C': bits->C = value; break;
396     case 'D': bits->D = value; break;
397     case 'E': bits->E = value; break;
398     case 'F': bits->F = value; break;
399     case 'G': bits->G = value; break;
400     case 'H': bits->H = value; break;
401     case 'I': bits->I = value; break;
402 
403     case 'M': bits->M = value; break;
404     case 'N': bits->N = value; break;
405     case 'O': bits->O = value; break;
406     case 'P': bits->P = value; break;
407     case 'Q': bits->Q = value; break;
408     case 'R': bits->R = value; break;
409     case 'S': bits->S = value; break;
410     }
411 }
412 
413 DL_EXPORT(int) unpack_bitfields(struct BITS *bits, char name)
414 {
415     switch (name) {
416     case 'A': return bits->A;
417     case 'B': return bits->B;
418     case 'C': return bits->C;
419     case 'D': return bits->D;
420     case 'E': return bits->E;
421     case 'F': return bits->F;
422     case 'G': return bits->G;
423     case 'H': return bits->H;
424     case 'I': return bits->I;
425 
426     case 'M': return bits->M;
427     case 'N': return bits->N;
428     case 'O': return bits->O;
429     case 'P': return bits->P;
430     case 'Q': return bits->Q;
431     case 'R': return bits->R;
432     case 'S': return bits->S;
433     }
434     return 0;
435 }
436 
437 static PyMethodDef module_methods[] = {
438 /*      {"get_last_tf_arg_s", get_last_tf_arg_s, METH_NOARGS},
439     {"get_last_tf_arg_u", get_last_tf_arg_u, METH_NOARGS},
440 */
441     {"func_si", py_func_si, METH_VARARGS},
442     {"func", py_func, METH_NOARGS},
443     { NULL, NULL, 0, NULL},
444 };
445 
446 #define S last_tf_arg_s = (PY_LONG_LONG)c
447 #define U last_tf_arg_u = (unsigned PY_LONG_LONG)c
448 
449 EXPORT(signed char) tf_b(signed char c) { S; return c/3; }
450 EXPORT(unsigned char) tf_B(unsigned char c) { U; return c/3; }
451 EXPORT(short) tf_h(short c) { S; return c/3; }
452 EXPORT(unsigned short) tf_H(unsigned short c) { U; return c/3; }
453 EXPORT(int) tf_i(int c) { S; return c/3; }
454 EXPORT(unsigned int) tf_I(unsigned int c) { U; return c/3; }
455 EXPORT(long) tf_l(long c) { S; return c/3; }
456 EXPORT(unsigned long) tf_L(unsigned long c) { U; return c/3; }
457 EXPORT(PY_LONG_LONG) tf_q(PY_LONG_LONG c) { S; return c/3; }
458 EXPORT(unsigned PY_LONG_LONG) tf_Q(unsigned PY_LONG_LONG c) { U; return c/3; }
459 EXPORT(float) tf_f(float c) { S; return c/3; }
460 EXPORT(double) tf_d(double c) { S; return c/3; }
461 EXPORT(long double) tf_D(long double c) { S; return c/3; }
462 
463 #ifdef MS_WIN32
464 EXPORT(signed char) __stdcall s_tf_b(signed char c) { S; return c/3; }
465 EXPORT(unsigned char) __stdcall s_tf_B(unsigned char c) { U; return c/3; }
466 EXPORT(short) __stdcall s_tf_h(short c) { S; return c/3; }
467 EXPORT(unsigned short) __stdcall s_tf_H(unsigned short c) { U; return c/3; }
468 EXPORT(int) __stdcall s_tf_i(int c) { S; return c/3; }
469 EXPORT(unsigned int) __stdcall s_tf_I(unsigned int c) { U; return c/3; }
470 EXPORT(long) __stdcall s_tf_l(long c) { S; return c/3; }
471 EXPORT(unsigned long) __stdcall s_tf_L(unsigned long c) { U; return c/3; }
472 EXPORT(PY_LONG_LONG) __stdcall s_tf_q(PY_LONG_LONG c) { S; return c/3; }
473 EXPORT(unsigned PY_LONG_LONG) __stdcall s_tf_Q(unsigned PY_LONG_LONG c) { U; return c/3; }
474 EXPORT(float) __stdcall s_tf_f(float c) { S; return c/3; }
475 EXPORT(double) __stdcall s_tf_d(double c) { S; return c/3; }
476 EXPORT(long double) __stdcall s_tf_D(long double c) { S; return c/3; }
477 #endif
478 /*******/
479 
480 EXPORT(signed char) tf_bb(signed char x, signed char c) { S; return c/3; }
481 EXPORT(unsigned char) tf_bB(signed char x, unsigned char c) { U; return c/3; }
482 EXPORT(short) tf_bh(signed char x, short c) { S; return c/3; }
483 EXPORT(unsigned short) tf_bH(signed char x, unsigned short c) { U; return c/3; }
484 EXPORT(int) tf_bi(signed char x, int c) { S; return c/3; }
485 EXPORT(unsigned int) tf_bI(signed char x, unsigned int c) { U; return c/3; }
486 EXPORT(long) tf_bl(signed char x, long c) { S; return c/3; }
487 EXPORT(unsigned long) tf_bL(signed char x, unsigned long c) { U; return c/3; }
488 EXPORT(PY_LONG_LONG) tf_bq(signed char x, PY_LONG_LONG c) { S; return c/3; }
489 EXPORT(unsigned PY_LONG_LONG) tf_bQ(signed char x, unsigned PY_LONG_LONG c) { U; return c/3; }
490 EXPORT(float) tf_bf(signed char x, float c) { S; return c/3; }
491 EXPORT(double) tf_bd(signed char x, double c) { S; return c/3; }
492 EXPORT(long double) tf_bD(signed char x, long double c) { S; return c/3; }
493 EXPORT(void) tv_i(int c) { S; return; }
494 
495 #ifdef MS_WIN32
496 EXPORT(signed char) __stdcall s_tf_bb(signed char x, signed char c) { S; return c/3; }
497 EXPORT(unsigned char) __stdcall s_tf_bB(signed char x, unsigned char c) { U; return c/3; }
498 EXPORT(short) __stdcall s_tf_bh(signed char x, short c) { S; return c/3; }
499 EXPORT(unsigned short) __stdcall s_tf_bH(signed char x, unsigned short c) { U; return c/3; }
500 EXPORT(int) __stdcall s_tf_bi(signed char x, int c) { S; return c/3; }
501 EXPORT(unsigned int) __stdcall s_tf_bI(signed char x, unsigned int c) { U; return c/3; }
502 EXPORT(long) __stdcall s_tf_bl(signed char x, long c) { S; return c/3; }
503 EXPORT(unsigned long) __stdcall s_tf_bL(signed char x, unsigned long c) { U; return c/3; }
504 EXPORT(PY_LONG_LONG) __stdcall s_tf_bq(signed char x, PY_LONG_LONG c) { S; return c/3; }
505 EXPORT(unsigned PY_LONG_LONG) __stdcall s_tf_bQ(signed char x, unsigned PY_LONG_LONG c) { U; return c/3; }
506 EXPORT(float) __stdcall s_tf_bf(signed char x, float c) { S; return c/3; }
507 EXPORT(double) __stdcall s_tf_bd(signed char x, double c) { S; return c/3; }
508 EXPORT(long double) __stdcall s_tf_bD(signed char x, long double c) { S; return c/3; }
509 EXPORT(void) __stdcall s_tv_i(int c) { S; return; }
510 #endif
511 
512 /********/
513 
514 #ifndef MS_WIN32
515 
516 typedef struct {
517     long x;
518     long y;
519 } POINT;
520 
521 typedef struct {
522     long left;
523     long top;
524     long right;
525     long bottom;
526 } RECT;
527 
528 #endif
529 
530 EXPORT(int) PointInRect(RECT *prc, POINT pt)
531 {
532     if (pt.x < prc->left)
533         return 0;
534     if (pt.x > prc->right)
535         return 0;
536     if (pt.y < prc->top)
537         return 0;
538     if (pt.y > prc->bottom)
539         return 0;
540     return 1;
541 }
542 
543 typedef struct {
544     short x;
545     short y;
546 } S2H;
547 
548 EXPORT(S2H) ret_2h_func(S2H inp)
549 {
550     inp.x *= 2;
551     inp.y *= 3;
552     return inp;
553 }
554 
555 typedef struct {
556     int a, b, c, d, e, f, g, h;
557 } S8I;
558 
559 EXPORT(S8I) ret_8i_func(S8I inp)
560 {
561     inp.a *= 2;
562     inp.b *= 3;
563     inp.c *= 4;
564     inp.d *= 5;
565     inp.e *= 6;
566     inp.f *= 7;
567     inp.g *= 8;
568     inp.h *= 9;
569     return inp;
570 }
571 
572 EXPORT(int) GetRectangle(int flag, RECT *prect)
573 {
574     if (flag == 0)
575         return 0;
576     prect->left = (int)flag;
577     prect->top = (int)flag + 1;
578     prect->right = (int)flag + 2;
579     prect->bottom = (int)flag + 3;
580     return 1;
581 }
582 
583 EXPORT(void) TwoOutArgs(int a, int *pi, int b, int *pj)
584 {
585     *pi += a;
586     *pj += b;
587 }
588 
589 #ifdef MS_WIN32
590 EXPORT(S2H) __stdcall s_ret_2h_func(S2H inp) { return ret_2h_func(inp); }
591 EXPORT(S8I) __stdcall s_ret_8i_func(S8I inp) { return ret_8i_func(inp); }
592 #endif
593 
594 #ifdef MS_WIN32
595 /* Should port this */
596 #include <stdlib.h>
597 #include <search.h>
598 
599 EXPORT (HRESULT) KeepObject(IUnknown *punk)
600 {
601     static IUnknown *pobj;
602     if (punk)
603         punk->lpVtbl->AddRef(punk);
604     if (pobj)
605         pobj->lpVtbl->Release(pobj);
606     pobj = punk;
607     return S_OK;
608 }
609 
610 #endif
611 
612 DL_EXPORT(void)
613 init_ctypes_test(void)
614 {
615     Py_InitModule("_ctypes_test", module_methods);
616 }