PageRenderTime 580ms CodeModel.GetById 141ms app.highlight 196ms RepoModel.GetById 137ms app.codeStats 0ms

/Modules/_ctypes/_ctypes_test.c

http://unladen-swallow.googlecode.com/
C | 602 lines | 481 code | 91 blank | 30 comment | 22 complexity | bbca305b02ed9784e226e04f4bb59191 MD5 | raw file
  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
 28EXPORT(void)testfunc_array(int values[4])
 29{
 30	printf("testfunc_array %d %d %d %d\n",
 31	       values[0],
 32	       values[1],
 33	       values[2],
 34	       values[3]);
 35}
 36
 37EXPORT(long double)testfunc_Ddd(double a, double b)
 38{
 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;
 43}
 44
 45EXPORT(long double)testfunc_DDD(long double a, long double b)
 46{
 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;
 51}
 52
 53EXPORT(int)testfunc_iii(int a, int b)
 54{
 55	int result = a * b;
 56	printf("testfunc_iii(%p, %p)\n", &a, &b);
 57	return result;
 58}
 59
 60EXPORT(int)myprintf(char *fmt, ...)
 61{
 62	int result;
 63	va_list argptr;
 64	va_start(argptr, fmt);
 65	result = vprintf(fmt, argptr);
 66	va_end(argptr);
 67	return result;
 68}
 69
 70EXPORT(char *)my_strtok(char *token, const char *delim)
 71{
 72	return strtok(token, delim);
 73}
 74
 75EXPORT(char *)my_strchr(const char *s, int c)
 76{
 77	return strchr(s, c);
 78}
 79
 80
 81EXPORT(double) my_sqrt(double a)
 82{
 83	return sqrt(a);
 84}
 85
 86EXPORT(void) my_qsort(void *base, size_t num, size_t width, int(*compare)(const void*, const void*))
 87{
 88	qsort(base, num, width, compare);
 89}
 90
 91EXPORT(int *) _testfunc_ai8(int a[8])
 92{
 93	return a;
 94}
 95
 96EXPORT(void) _testfunc_v(int a, int b, int *presult)
 97{
 98	*presult = a + b;
 99}
100
101EXPORT(int) _testfunc_i_bhilfd(signed char b, short h, int i, long l, float f, double d)
102{
103/*	printf("_testfunc_i_bhilfd got %d %d %d %ld %f %f\n",
104	       b, h, i, l, f, d);
105*/
106	return (int)(b + h + i + l + f + d);
107}
108
109EXPORT(float) _testfunc_f_bhilfd(signed char b, short h, int i, long l, float f, double d)
110{
111/*	printf("_testfunc_f_bhilfd got %d %d %d %ld %f %f\n",
112	       b, h, i, l, f, d);
113*/
114	return (float)(b + h + i + l + f + d);
115}
116
117EXPORT(double) _testfunc_d_bhilfd(signed char b, short h, int i, long l, float f, double d)
118{
119/*	printf("_testfunc_d_bhilfd got %d %d %d %ld %f %f\n",
120	       b, h, i, l, f, d);
121*/
122	return (double)(b + h + i + l + f + d);
123}
124
125EXPORT(long double) _testfunc_D_bhilfD(signed char b, short h, int i, long l, float f, long double d)
126{
127/*	printf("_testfunc_d_bhilfd got %d %d %d %ld %f %f\n",
128	       b, h, i, l, f, d);
129*/
130	return (long double)(b + h + i + l + f + d);
131}
132
133EXPORT(char *) _testfunc_p_p(void *s)
134{
135	return (char *)s;
136}
137
138EXPORT(void *) _testfunc_c_p_p(int *argcp, char **argv)
139{
140	return argv[(*argcp)-1];
141}
142
143EXPORT(void *) get_strchr(void)
144{
145	return (void *)strchr;
146}
147
148EXPORT(char *) my_strdup(char *src)
149{
150	char *dst = (char *)malloc(strlen(src)+1);
151	if (!dst)
152		return NULL;
153	strcpy(dst, src);
154	return dst;
155}
156
157EXPORT(void)my_free(void *ptr)
158{
159	free(ptr);
160}
161
162#ifdef HAVE_WCHAR_H
163EXPORT(wchar_t *) my_wcsdup(wchar_t *src)
164{
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;
171}
172
173EXPORT(size_t) my_wcslen(wchar_t *src)
174{
175	return wcslen(src);
176}
177#endif
178
179#ifndef MS_WIN32
180# ifndef __stdcall
181#  define __stdcall /* */
182# endif
183#endif
184
185typedef struct {
186	int (*c)(int, int);
187	int (__stdcall *s)(int, int);
188} FUNCS;
189
190EXPORT(int) _testfunc_callfuncp(FUNCS *fp)
191{
192	fp->c(1, 2);
193	fp->s(3, 4);
194	return 0;
195}
196
197EXPORT(int) _testfunc_deref_pointer(int *pi)
198{
199	return *pi;
200}
201
202#ifdef MS_WIN32
203EXPORT(int) _testfunc_piunk(IUnknown FAR *piunk)
204{
205	piunk->lpVtbl->AddRef(piunk);
206	return piunk->lpVtbl->Release(piunk);
207}
208#endif
209
210EXPORT(int) _testfunc_callback_with_pointer(int (*func)(int *))
211{
212	int table[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
213
214	return (*func)(table);
215}
216
217#ifdef HAVE_LONG_LONG
218EXPORT(PY_LONG_LONG) _testfunc_q_bhilfdq(signed char b, short h, int i, long l, float f,
219				     double d, PY_LONG_LONG q)
220{
221	return (PY_LONG_LONG)(b + h + i + l + f + d + q);
222}
223
224EXPORT(PY_LONG_LONG) _testfunc_q_bhilfd(signed char b, short h, int i, long l, float f, double d)
225{
226	return (PY_LONG_LONG)(b + h + i + l + f + d);
227}
228
229EXPORT(int) _testfunc_callback_i_if(int value, int (*func)(int))
230{
231	int sum = 0;
232	while (value != 0) {
233		sum += func(value);
234		value /= 2;
235	}
236	return sum;
237}
238
239EXPORT(PY_LONG_LONG) _testfunc_callback_q_qf(PY_LONG_LONG value,
240					     PY_LONG_LONG (*func)(PY_LONG_LONG))
241{
242	PY_LONG_LONG sum = 0;
243
244	while (value != 0) {
245		sum += func(value);
246		value /= 2;
247	}
248	return sum;
249}
250
251#endif
252
253typedef struct {
254	char *name;
255	char *value;
256} SPAM;
257
258typedef struct {
259	char *name;
260	int num_spams;
261	SPAM *spams;
262} EGG;
263
264SPAM my_spams[2] = {
265	{ "name1", "value1" },
266	{ "name2", "value2" },
267};
268
269EGG my_eggs[1] = {
270	{ "first egg", 1, my_spams }
271};
272
273EXPORT(int) getSPAMANDEGGS(EGG **eggs)
274{
275	*eggs = my_eggs;
276	return 1;
277}
278
279typedef struct tagpoint {
280	int x;
281	int y;
282} point;
283
284EXPORT(int) _testfunc_byval(point in, point *pout)
285{
286	if (pout) {
287		pout->x = in.x;
288		pout->y = in.y;
289	}
290	return in.x + in.y;
291}
292
293EXPORT (int) an_integer = 42;
294
295EXPORT(int) get_an_integer(void)
296{
297	return an_integer;
298}
299
300EXPORT(double)
301integrate(double a, double b, double (*f)(double), long nstep)
302{
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;
307}
308
309typedef struct {
310	void (*initialize)(void *(*)(int), void(*)(void *));
311} xxx_library;
312
313static void _xxx_init(void *(*Xalloc)(int), void (*Xfree)(void *))
314{
315	void *ptr;
316	
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);
322}
323
324xxx_library _xxx_lib = {
325	_xxx_init
326};
327
328EXPORT(xxx_library) *library_get(void)
329{
330	return &_xxx_lib;
331}
332
333#ifdef MS_WIN32
334/* See Don Box (german), pp 79ff. */
335EXPORT(void) GetString(BSTR *pbstr)
336{
337	*pbstr = SysAllocString(L"Goodbye!");
338}
339#endif
340
341/*
342 * Some do-nothing functions, for speed tests
343 */
344PyObject *py_func_si(PyObject *self, PyObject *args)
345{
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;
352}
353
354EXPORT(void) _py_func_si(char *s, int i)
355{
356}
357
358PyObject *py_func(PyObject *self, PyObject *args)
359{
360	Py_INCREF(Py_None);
361	return Py_None;
362}
363
364EXPORT(void) _py_func(void)
365{
366}
367
368EXPORT(PY_LONG_LONG) last_tf_arg_s;
369EXPORT(unsigned PY_LONG_LONG) last_tf_arg_u;
370
371struct 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;
374};
375
376DL_EXPORT(void) set_bitfields(struct BITS *bits, char name, int value)
377{
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;
388
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;
396	}
397}
398
399DL_EXPORT(int) unpack_bitfields(struct BITS *bits, char name)
400{
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;
411
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;
419	}
420	return 0;
421}
422
423static 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},
426*/
427	{"func_si", py_func_si, METH_VARARGS},
428	{"func", py_func, METH_NOARGS},
429	{ NULL, NULL, 0, NULL},
430};
431
432#define S last_tf_arg_s = (PY_LONG_LONG)c
433#define U last_tf_arg_u = (unsigned PY_LONG_LONG)c
434
435EXPORT(signed char) tf_b(signed char c) { S; return c/3; }
436EXPORT(unsigned char) tf_B(unsigned char c) { U; return c/3; }
437EXPORT(short) tf_h(short c) { S; return c/3; }
438EXPORT(unsigned short) tf_H(unsigned short c) { U; return c/3; }
439EXPORT(int) tf_i(int c) { S; return c/3; }
440EXPORT(unsigned int) tf_I(unsigned int c) { U; return c/3; }
441EXPORT(long) tf_l(long c) { S; return c/3; }
442EXPORT(unsigned long) tf_L(unsigned long c) { U; return c/3; }
443EXPORT(PY_LONG_LONG) tf_q(PY_LONG_LONG c) { S; return c/3; }
444EXPORT(unsigned PY_LONG_LONG) tf_Q(unsigned PY_LONG_LONG c) { U; return c/3; }
445EXPORT(float) tf_f(float c) { S; return c/3; }
446EXPORT(double) tf_d(double c) { S; return c/3; }
447EXPORT(long double) tf_D(long double c) { S; return c/3; }
448
449#ifdef MS_WIN32
450EXPORT(signed char) __stdcall s_tf_b(signed char c) { S; return c/3; }
451EXPORT(unsigned char) __stdcall s_tf_B(unsigned char c) { U; return c/3; }
452EXPORT(short) __stdcall s_tf_h(short c) { S; return c/3; }
453EXPORT(unsigned short) __stdcall s_tf_H(unsigned short c) { U; return c/3; }
454EXPORT(int) __stdcall s_tf_i(int c) { S; return c/3; }
455EXPORT(unsigned int) __stdcall s_tf_I(unsigned int c) { U; return c/3; }
456EXPORT(long) __stdcall s_tf_l(long c) { S; return c/3; }
457EXPORT(unsigned long) __stdcall s_tf_L(unsigned long c) { U; return c/3; }
458EXPORT(PY_LONG_LONG) __stdcall s_tf_q(PY_LONG_LONG c) { S; return c/3; }
459EXPORT(unsigned PY_LONG_LONG) __stdcall s_tf_Q(unsigned PY_LONG_LONG c) { U; return c/3; }
460EXPORT(float) __stdcall s_tf_f(float c) { S; return c/3; }
461EXPORT(double) __stdcall s_tf_d(double c) { S; return c/3; }
462EXPORT(long double) __stdcall s_tf_D(long double c) { S; return c/3; }
463#endif
464/*******/
465
466EXPORT(signed char) tf_bb(signed char x, signed char c) { S; return c/3; }
467EXPORT(unsigned char) tf_bB(signed char x, unsigned char c) { U; return c/3; }
468EXPORT(short) tf_bh(signed char x, short c) { S; return c/3; }
469EXPORT(unsigned short) tf_bH(signed char x, unsigned short c) { U; return c/3; }
470EXPORT(int) tf_bi(signed char x, int c) { S; return c/3; }
471EXPORT(unsigned int) tf_bI(signed char x, unsigned int c) { U; return c/3; }
472EXPORT(long) tf_bl(signed char x, long c) { S; return c/3; }
473EXPORT(unsigned long) tf_bL(signed char x, unsigned long c) { U; return c/3; }
474EXPORT(PY_LONG_LONG) tf_bq(signed char x, PY_LONG_LONG c) { S; return c/3; }
475EXPORT(unsigned PY_LONG_LONG) tf_bQ(signed char x, unsigned PY_LONG_LONG c) { U; return c/3; }
476EXPORT(float) tf_bf(signed char x, float c) { S; return c/3; }
477EXPORT(double) tf_bd(signed char x, double c) { S; return c/3; }
478EXPORT(long double) tf_bD(signed char x, long double c) { S; return c/3; }
479EXPORT(void) tv_i(int c) { S; return; }
480
481#ifdef MS_WIN32
482EXPORT(signed char) __stdcall s_tf_bb(signed char x, signed char c) { S; return c/3; }
483EXPORT(unsigned char) __stdcall s_tf_bB(signed char x, unsigned char c) { U; return c/3; }
484EXPORT(short) __stdcall s_tf_bh(signed char x, short c) { S; return c/3; }
485EXPORT(unsigned short) __stdcall s_tf_bH(signed char x, unsigned short c) { U; return c/3; }
486EXPORT(int) __stdcall s_tf_bi(signed char x, int c) { S; return c/3; }
487EXPORT(unsigned int) __stdcall s_tf_bI(signed char x, unsigned int c) { U; return c/3; }
488EXPORT(long) __stdcall s_tf_bl(signed char x, long c) { S; return c/3; }
489EXPORT(unsigned long) __stdcall s_tf_bL(signed char x, unsigned long c) { U; return c/3; }
490EXPORT(PY_LONG_LONG) __stdcall s_tf_bq(signed char x, PY_LONG_LONG c) { S; return c/3; }
491EXPORT(unsigned PY_LONG_LONG) __stdcall s_tf_bQ(signed char x, unsigned PY_LONG_LONG c) { U; return c/3; }
492EXPORT(float) __stdcall s_tf_bf(signed char x, float c) { S; return c/3; }
493EXPORT(double) __stdcall s_tf_bd(signed char x, double c) { S; return c/3; }
494EXPORT(long double) __stdcall s_tf_bD(signed char x, long double c) { S; return c/3; }
495EXPORT(void) __stdcall s_tv_i(int c) { S; return; }
496#endif
497
498/********/
499 
500#ifndef MS_WIN32
501
502typedef struct {
503	long x;
504	long y;
505} POINT;
506
507typedef struct {
508	long left;
509	long top;
510	long right;
511	long bottom;
512} RECT;
513
514#endif
515
516EXPORT(int) PointInRect(RECT *prc, POINT pt)
517{
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;
527}
528
529typedef struct {
530	short x;
531	short y;
532} S2H;
533
534EXPORT(S2H) ret_2h_func(S2H inp)
535{
536	inp.x *= 2;
537	inp.y *= 3;
538	return inp;
539}
540
541typedef struct {
542	int a, b, c, d, e, f, g, h;
543} S8I;
544
545EXPORT(S8I) ret_8i_func(S8I inp)
546{
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;
556}
557
558EXPORT(int) GetRectangle(int flag, RECT *prect)
559{
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;
567}
568
569EXPORT(void) TwoOutArgs(int a, int *pi, int b, int *pj)
570{
571	*pi += a;
572	*pj += b;
573}
574
575#ifdef MS_WIN32
576EXPORT(S2H) __stdcall s_ret_2h_func(S2H inp) { return ret_2h_func(inp); }
577EXPORT(S8I) __stdcall s_ret_8i_func(S8I inp) { return ret_8i_func(inp); }
578#endif
579
580#ifdef MS_WIN32
581/* Should port this */
582#include <stdlib.h>
583#include <search.h>
584
585EXPORT (HRESULT) KeepObject(IUnknown *punk)
586{
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;
594}
595
596#endif
597
598DL_EXPORT(void)
599init_ctypes_test(void)
600{
601	Py_InitModule("_ctypes_test", module_methods);
602}