1 /*****************************************************************
2 This file should be kept compatible with Python 2.3, see PEP 291.
3 *****************************************************************/
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
20 #if defined(MS_WIN32) || defined(__CYGWIN__)
21 #define EXPORT(x) __declspec(dllexport) x
26 /* some functions handy for testing */
28 EXPORT(void)testfunc_array(int values
[4])
30 printf("testfunc_array %d %d %d %d\n",
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
);
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
);
53 EXPORT(int)testfunc_iii(int a
, int b
)
56 printf("testfunc_iii(%p, %p)\n", &a
, &b
);
60 EXPORT(int)myprintf(char *fmt
, ...)
64 va_start(argptr
, fmt
);
65 result
= vprintf(fmt
, argptr
);
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
)
81 EXPORT(double) my_sqrt(double 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])
96 EXPORT(void) _testfunc_v(int a
, int b
, int *presult
)
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",
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",
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",
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",
130 return (long double)(b
+ h
+ i
+ l
+ f
+ d
);
133 EXPORT(char *) _testfunc_p_p(void *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);
157 EXPORT(void)my_free(void *ptr
)
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));
169 memcpy(ptr
, src
, (len
+1) * sizeof(wchar_t));
173 EXPORT(size_t) my_wcslen(wchar_t *src
)
181 # define __stdcall /* */
187 int (__stdcall
*s
)(int, int);
190 EXPORT(int) _testfunc_callfuncp(FUNCS
*fp
)
197 EXPORT(int) _testfunc_deref_pointer(int *pi
)
203 EXPORT(int) _testfunc_piunk(IUnknown FAR
*piunk
)
205 piunk
->lpVtbl
->AddRef(piunk
);
206 return piunk
->lpVtbl
->Release(piunk
);
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))
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;
265 { "name1", "value1" },
266 { "name2", "value2" },
270 { "first egg", 1, my_spams
}
273 EXPORT(int) getSPAMANDEGGS(EGG
**eggs
)
279 typedef struct tagpoint
{
284 EXPORT(int) _testfunc_byval(point in
, point
*pout
)
293 EXPORT (int) an_integer
= 42;
295 EXPORT(int) get_an_integer(void)
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
)
306 return sum
/(double)nstep
;
310 void (*initialize
)(void *(*)(int), void(*)(void *));
313 static void _xxx_init(void *(*Xalloc
)(int), void (*Xfree
)(void *))
317 printf("_xxx_init got %p %p\n", Xalloc
, Xfree
);
321 printf("calls done, ptr was %p\n", ptr
);
324 xxx_library _xxx_lib
= {
328 EXPORT(xxx_library
) *library_get(void)
334 /* See Don Box (german), pp 79ff. */
335 EXPORT(void) GetString(BSTR
*pbstr
)
337 *pbstr
= SysAllocString(L
"Goodbye!");
342 * Some do-nothing functions, for speed tests
344 PyObject
*py_func_si(PyObject
*self
, PyObject
*args
)
348 if (!PyArg_ParseTuple(args
, "si", &name
, &i
))
354 EXPORT(void) _py_func_si(char *s
, int i
)
358 PyObject
*py_func(PyObject
*self
, PyObject
*args
)
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
;
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
)
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
)
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
;
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; }
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; }
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; }
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; }
516 EXPORT(int) PointInRect(RECT
*prc
, POINT pt
)
518 if (pt
.x
< prc
->left
)
520 if (pt
.x
> prc
->right
)
524 if (pt
.y
> prc
->bottom
)
534 EXPORT(S2H
) ret_2h_func(S2H inp
)
542 int a
, b
, c
, d
, e
, f
, g
, h
;
545 EXPORT(S8I
) ret_8i_func(S8I inp
)
558 EXPORT(int) GetRectangle(int flag
, RECT
*prect
)
562 prect
->left
= (int)flag
;
563 prect
->top
= (int)flag
+ 1;
564 prect
->right
= (int)flag
+ 2;
565 prect
->bottom
= (int)flag
+ 3;
569 EXPORT(void) TwoOutArgs(int a
, int *pi
, int b
, int *pj
)
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
); }
581 /* Should port this */
585 EXPORT (HRESULT
) KeepObject(IUnknown
*punk
)
587 static IUnknown
*pobj
;
589 punk
->lpVtbl
->AddRef(punk
);
591 pobj
->lpVtbl
->Release(pobj
);
599 init_ctypes_test(void)
601 Py_InitModule("_ctypes_test", module_methods
);