Coccinelle release 0.2.5-rc4
[bpt/coccinelle.git] / pycaml / pycaml_ml.c
1 /*
2 * (C) arty 2002
3 * This software is covered under the GNU lesser general public license
4 */
5
6 #include "Python.h"
7 #include "caml/mlvalues.h"
8 #include "caml/memory.h"
9 #include "caml/callback.h"
10 #include "caml/custom.h"
11 #include "caml/alloc.h"
12 #include <unistd.h>
13
14 #define true 1
15 #define false 0
16
17 static void *getcustom( value v ) { return *((void **)Data_custom_val(v)); }
18
19 static void pydecref( value v ) {
20 if( getcustom(v) ) { Py_DECREF((PyObject *)getcustom(v)); }
21 }
22
23 static int pycompare( value v1, value v2 ) {
24 int result;
25 if( getcustom(v1) && !getcustom(v2) ) return -1;
26 if( getcustom(v2) && !getcustom(v1) ) return 1;
27 if( !getcustom(v1) && !getcustom(v2) ) return 0;
28 PyObject_Cmp((PyObject *)getcustom(v1),
29 (PyObject *)getcustom(v2),&result);
30 return result;
31 }
32
33 static long pyhash( value v ) {
34 if( getcustom(v) ) return PyObject_Hash((PyObject *)getcustom(v));
35 else return 0;
36 }
37
38 static unsigned long pydeserialize( void *dst ) {
39 return 0;
40 }
41
42 struct custom_operations pyops = {
43 "PythonObject",
44 pydecref,
45 pycompare,
46 pyhash,
47 custom_serialize_default,
48 pydeserialize
49 };
50
51 struct custom_operations fnops = {
52 "FuncPointer",
53 NULL,
54 NULL,
55 NULL,
56 NULL,
57 NULL
58 };
59
60 value pywrap( PyObject *obj ) {
61 CAMLparam0();
62 CAMLlocal1(v);
63 if( obj )
64 Py_INCREF(obj);
65 v = alloc_custom( &pyops, sizeof( PyObject * ), 100, 100000 );
66 *((PyObject **)Data_custom_val(v)) = obj;
67 CAMLreturn(v);
68 }
69
70 value funcwrap( void *obj ) {
71 CAMLparam0();
72 CAMLlocal1(v);
73 v = alloc_custom( &fnops, sizeof( void * ), 100, 100000 );
74 *((void **)Data_custom_val(v)) = obj;
75 CAMLreturn(v);
76 }
77
78 PyObject *pyunwrap( value v ) {
79 return *((PyObject **)Data_custom_val(v));
80 }
81
82 static void camldestr( void *v ) {
83 value *valptr = (value *)v;
84 remove_global_root(valptr);
85 free( v );
86 }
87
88 PyObject *camlwrap( value val, void *aux_str, int size ) {
89 value *v = (value *)malloc(sizeof(value) + size);
90 *v = val;
91 memcpy((void *)v+sizeof(value),aux_str,size);
92 register_global_root(v);
93 return PyCObject_FromVoidPtr(v,camldestr);
94 }
95
96 void *caml_aux( PyObject *obj ) {
97 value *v = (value *)PyCObject_AsVoidPtr( obj );
98 return (void *)v+sizeof(value);
99 }
100
101 PyObject *pycall_callback( PyObject *obj, PyObject *args ) {
102 value out;
103 value *v;
104
105 if( !PyCObject_Check(obj) ) {
106 Py_INCREF(Py_None);
107 return Py_None;
108 }
109 v = (value *)PyCObject_AsVoidPtr( obj );
110 out = callback(*v,pywrap(args));
111 return pyunwrap(out);
112 }
113
114 typedef void (*type_1)( void );
115 typedef void (*type_2)( int );
116 typedef void (*type_3)( char * );
117 typedef int (*type_4)( void );
118 typedef int (*type_5)( char * );
119 typedef int (*type_6)( FILE *, char * );
120 typedef int (*type_7)( FILE *, char *, int );
121 typedef char *(*type_8)( void );
122 typedef PyObject* (*type_9)(char*, int, PyObject *, PyObject *);
123 typedef PyObject* (*type_10)(FILE *, char*, int, PyObject *, PyObject *);
124 typedef PyObject* (*type_11)(FILE *, char*, int, PyObject *, PyObject *, int );
125 typedef PyObject* (*type_12)(char*, char*, int);
126 typedef int (*type_13)(PyObject *, FILE *, int);
127 typedef PyObject * (*type_14)(PyObject *);
128 typedef PyObject * (*type_15)(PyObject *, PyObject *, int);
129 typedef PyObject * (*type_16)(PyObject *, char *);
130 typedef PyObject * (*type_17)(PyObject *, PyObject *);
131 typedef int (*type_18)(PyObject *);
132 typedef int (*type_19)(PyObject *, PyObject *);
133 typedef int (*type_20)(PyObject *, PyObject *, int);
134 typedef int (*type_21)(PyObject *, char *, PyObject *);
135 typedef int (*type_22)(PyObject *, char *);
136 typedef int (*type_23)(PyObject **, PyObject **);
137 typedef int (*type_24)(PyObject *, PyObject *, PyObject *);
138 typedef long (*type_25)(PyObject *);
139 typedef char *(*type_26)(PyObject *);
140 typedef void (*type_27)(PyObject **, PyObject *);
141 typedef PyObject *(*type_28)(char *);
142 typedef PyObject *(*type_29)(void);
143 typedef void (*type_30)(PyObject *);
144 typedef int (*type_31)(PyObject *, int *, PyObject **, PyObject **);
145 typedef PyObject *(*type_32)(char*, char**, int);
146 typedef PyObject *(*type_33)(Py_UNICODE*, int, int);
147 typedef PyObject *(*type_34)(long);
148 typedef long (*type_35)(void);
149 typedef PyObject *(*type_36)(double);
150 typedef double (*type_37)(PyObject *);
151 typedef PyObject *(*type_38)(PyObject*, char**);
152 typedef PyObject *(*type_39)(int size);
153 typedef PyObject *(*type_40)(PyObject *, int);
154 typedef int (*type_41)(PyObject *, int, PyObject *);
155 typedef PyObject *(*type_42)(PyObject* start, PyObject* stop, PyObject* step);
156 typedef int (*type_43)(PySliceObject *r, int length, int *start, int *stop, int *step);
157 typedef PyObject *(*type_44)(long, long, long, int);
158 typedef void (*type_45)(PyObject *, PyObject *);
159 typedef void (*type_46)(PyObject *, const char *);
160 typedef void (*type_47)(PyObject **, PyObject **, PyObject **);
161 typedef void (*type_48)(PyObject *, PyObject *, PyObject *);
162 typedef PyObject *(*type_49)(PyObject *, char *);
163 typedef PyObject *(*type_50)(char *,PyObject *,char *);
164 typedef PyObject *(*type_51)(char *,PyObject *,PyObject *,PyObject *);
165 typedef int (*type_52)(PyObject *obj,const char **buffer,int *buffer_len);
166 typedef int (*type_53)(PyObject *o, int i1, int i2, PyObject *v);
167 typedef int (*type_54)(PyObject *o, int i1, int i2);
168
169 value pygencall( value format, value arg ) {
170 CAMLparam2(format,arg);
171 CAMLlocal1(rv);
172 FILE *f;
173 int fd;
174 int x;
175 int ret_int;
176 const char *rvs;
177 int fmt = Int_val(Field(format,1));
178 PyObject *ob1,*ob2,*ob3;
179 void *func = getcustom(Field(format,0));
180 int reflect = Int_val(Field(format,2));
181
182 rv = Val_unit;
183
184 switch( fmt ) {
185 case 1:
186 ((type_1)func)();
187 CAMLreturn(Val_unit);
188
189 case 2:
190 ((type_2)func)(Int_val(arg));
191 CAMLreturn(Val_unit);
192
193 case 3:
194 ((type_3)func)(String_val(arg));
195 CAMLreturn(Val_unit);
196
197 case 4:
198 CAMLreturn(Int_val(((type_4)func)()));
199
200 case 5:
201 CAMLreturn(Int_val(((type_5)func)
202 (String_val(arg))));
203
204 case 6:
205 case 7:
206 case 10:
207 case 11:
208 fd = dup(Int_val(Field(arg,0)));
209 f = fdopen(fd,"r+");
210 switch( fmt ) {
211 case 6:
212 rv = Val_int(((type_6)func)
213 (f,String_val(Field(arg,1))));
214 break;
215
216 case 7:
217 rv = Val_int(((type_7)func)
218 (f,
219 String_val(Field(arg,1)),
220 Int_val(Field(arg,2))));
221 break;
222
223 case 10:
224 rv = pywrap(((type_10)func)
225 (f,
226 String_val(Field(arg,1)),
227 Int_val(Field(arg,2)),
228 pyunwrap(Field(arg,3)),
229 pyunwrap(Field(arg,4))));
230 break;
231
232 case 11:
233 rv = pywrap(((type_11)func)
234 (f,
235 String_val(Field(arg,1)),
236 Int_val(Field(arg,2)),
237 pyunwrap(Field(arg,3)),
238 pyunwrap(Field(arg,4)),
239 Int_val(Field(arg,5))));
240 break;
241 }
242
243 fclose( f );
244 CAMLreturn( rv );
245
246 case 8:
247 CAMLreturn(copy_string(((type_8)func)()));
248
249 case 9:
250 CAMLreturn(pywrap(((type_9)func)
251 (String_val(Field(arg,0)),
252 Int_val(Field(arg,1)),
253 pyunwrap(Field(arg,2)),
254 pyunwrap(Field(arg,3)))));
255
256 case 12:
257 CAMLreturn(pywrap(((type_12)func)
258 (String_val(Field(arg,0)),
259 String_val(Field(arg,1)),
260 Int_val(Field(arg,2)))));
261
262 case 13:
263 fd = dup(Int_val(Field(arg,1)));
264 f = fdopen(fd,"r+");
265 rv = Val_int(((type_13)func)
266 (pyunwrap(Field(arg,0)),
267 f,
268 Int_val(Field(arg,2))));
269 fclose( f );
270 break;
271
272 case 14:
273 CAMLreturn(pywrap(((type_14)func)(pyunwrap(arg))));
274
275 case 15:
276 CAMLreturn(pywrap(((type_15)func)
277 (pyunwrap(Field(arg,0)),
278 pyunwrap(Field(arg,1)),Int_val(Field(arg,2)))));
279
280 case 16:
281 CAMLreturn(pywrap(((type_16)func)
282 (pyunwrap(Field(arg,0)),
283 String_val(Field(arg,1)))));
284
285 case 17:
286 CAMLreturn(pywrap(((type_17)func)
287 (pyunwrap(Field(arg,0)),pyunwrap(Field(arg,1)))));
288
289 case 18:
290 CAMLreturn(Val_int(((type_18)func)(pyunwrap(arg))));
291
292 case 19:
293 CAMLreturn(Val_int(((type_19)func)
294 (pyunwrap(Field(arg,0)),
295 pyunwrap(Field(arg,1)))));
296
297 case 20:
298 CAMLreturn(Val_int(((type_20)func)
299 (pyunwrap(Field(arg,0)),
300 pyunwrap(Field(arg,1)),
301 Int_val(Field(arg,2)))));
302
303 case 21:
304 CAMLreturn(Val_int(((type_21)func)
305 (pyunwrap(Field(arg,0)),
306 String_val(Field(arg,1)),
307 pyunwrap(Field(arg,2)))));
308
309 case 22:
310 CAMLreturn(Val_int(((type_22)func)
311 (pyunwrap(Field(arg,0)),
312 String_val(Field(arg,1)))));
313
314 case 23:
315 ob1 = pyunwrap(Field(arg,0)); ob2 = pyunwrap(Field(arg,1));
316 ret_int = ((type_23)func)( &ob1, &ob2 );
317 if( ret_int == -1 ) CAMLreturn((value)1);
318 else {
319 rv = alloc_tuple(2);
320 Field(rv,0) = pywrap(ob1);
321 Field(rv,1) = pywrap(ob2);
322 CAMLreturn(rv);
323 }
324
325 case 24:
326 CAMLreturn(Int_val(((type_24)func)
327 (pyunwrap(Field(arg,0)),
328 pyunwrap(Field(arg,1)),
329 pyunwrap(Field(arg,2)))));
330
331 case 25:
332 CAMLreturn(copy_int64(((type_25)func)(pyunwrap(arg))));
333
334 case 26:
335 CAMLreturn(copy_string(((type_26)func)(pyunwrap(arg))));
336
337 case 27:
338 ob1 = pyunwrap(Field(arg,0));
339 ((type_27)func)(&ob1,pyunwrap(Field(arg,1)));
340 CAMLreturn(pywrap(ob1));
341
342 case 28:
343 CAMLreturn(pywrap(((type_28)func)(String_val(arg))));
344
345 case 29:
346 CAMLreturn(pywrap(((type_29)func)()));
347
348 case 30:
349 ((type_30)func)(pyunwrap(arg));
350 CAMLreturn(Val_unit);
351
352 case 31:
353 x = Int_val(Field(arg,1));
354 ret_int = ((type_31)func)
355 (pyunwrap(Field(arg,0)),
356 &x,
357 &ob1, &ob2);
358 if( !ret_int ) CAMLreturn((value)1);
359 else {
360 rv = alloc_tuple(3);
361 Field(rv,0) = pywrap(ob1);
362 Field(rv,1) = pywrap(ob2);
363 Field(rv,2) = Val_int(x);
364 CAMLreturn(rv);
365 }
366
367 /* case 32: string -> int */
368
369 /* case 33: unicode ... need to do something coherent */
370
371 case 34:
372 CAMLreturn(pywrap(((type_34)func)(Int64_val(arg))));
373
374 case 35:
375 CAMLreturn(copy_int64(((type_35)func)()));
376
377 case 36:
378 CAMLreturn(pywrap(((type_36)func)(Double_val(arg))));
379
380 case 37:
381 CAMLreturn(copy_double(((type_37)func)
382 (pyunwrap(arg))));
383
384 /* case 38: string -> float */
385
386 case 39:
387 CAMLreturn(pywrap(((type_39)func)(Int_val(arg))));
388
389 case 40:
390 CAMLreturn(pywrap(((type_40)func)
391 (pyunwrap(Field(arg,0)),
392 Int_val(Field(arg,1)))));
393
394 case 41:
395 CAMLreturn(Val_int(((type_41)func)
396 (pyunwrap(Field(arg,0)),
397 Int_val(Field(arg,1)),
398 pyunwrap(Field(arg,2)))));
399
400 case 42:
401 CAMLreturn(pywrap(((type_42)func)
402 (pyunwrap(Field(arg,0)),
403 pyunwrap(Field(arg,1)),
404 pyunwrap(Field(arg,2)))));
405
406 case 43: {
407 int start,end,step;
408
409 ret_int = ((type_43)func)
410 ((PySliceObject *)pyunwrap(Field(arg,0)),
411 Int_val(Field(arg,1)),
412 &start, &end, &step);
413 if( !ret_int ) CAMLreturn((value)1);
414 else {
415 rv = alloc_tuple(3);
416 Field(rv,0) = start;
417 Field(rv,1) = end;
418 Field(rv,2) = step;
419 CAMLreturn(rv);
420 }
421 }
422
423 case 44:
424 CAMLreturn(pywrap(((type_44)func)
425 (Int_val(Field(arg,0)),
426 Int_val(Field(arg,1)),
427 Int_val(Field(arg,2)),
428 Int_val(Field(arg,3)))));
429
430 case 45:
431 ((type_45)func)
432 (pyunwrap(Field(arg,0)),
433 pyunwrap(Field(arg,1)));
434 CAMLreturn(Val_unit);
435
436 case 46:
437 ((type_46)func)
438 (pyunwrap(Field(arg,0)),String_val(Field(arg,1)));
439 CAMLreturn(Val_unit);
440
441 case 47:
442 ob1 = pyunwrap(Field(arg,0));
443 ob2 = pyunwrap(Field(arg,1));
444 ob3 = pyunwrap(Field(arg,2));
445 ((type_47)func)(&ob1,&ob2,&ob3);
446 rv = alloc_tuple(3);
447 Field(rv,0) = pywrap(ob1);
448 Field(rv,1) = pywrap(ob2);
449 Field(rv,2) = pywrap(ob3);
450 CAMLreturn(rv);
451
452 case 48:
453 ((type_48)func)
454 (pyunwrap(Field(arg,0)),
455 pyunwrap(Field(arg,1)),
456 pyunwrap(Field(arg,2)));
457 CAMLreturn(Val_unit);
458
459 case 49:
460 CAMLreturn(pywrap(((type_49)func)
461 (pyunwrap(Field(arg,0)),
462 String_val(Field(arg,1)))));
463
464 case 50:
465 CAMLreturn(pywrap(((type_50)func)
466 (String_val(Field(arg,0)),
467 pyunwrap(Field(arg,1)),
468 String_val(Field(arg,2)))));
469
470 case 51:
471 CAMLreturn(pywrap(((type_51)func)
472 (String_val(Field(arg,0)),
473 pyunwrap(Field(arg,1)),
474 pyunwrap(Field(arg,2)),
475 pyunwrap(Field(arg,3)))));
476
477 case 52:
478 ((type_52)func)(pyunwrap(arg),&rvs,&ret_int);
479 rv = copy_string(rvs);
480 CAMLreturn(rv);
481
482 case 53:
483 CAMLreturn(Val_int(((type_53)func)
484 (pyunwrap(Field(arg,0)),
485 Int_val(Field(arg,1)),
486 Int_val(Field(arg,2)),
487 pyunwrap(Field(arg,3)))));
488
489 case 54:
490 CAMLreturn(Val_int(((type_54)func)
491 (pyunwrap(Field(arg,0)),
492 Int_val(Field(arg,1)),
493 Int_val(Field(arg,2)))));
494 }
495
496 CAMLreturn(rv);
497 }
498
499 #ifdef DONT_COMPILE_THIS
500 /* 1 */
501 DL_IMPORT(void) Py_Initialize(void);
502 DL_IMPORT(void) Py_Finalize(void);
503 DL_IMPORT(void) PyErr_Print(void);
504 /* 2 */
505 DL_IMPORT(void) Py_Exit(int);
506 DL_IMPORT(void) PyErr_PrintEx(int);
507 /* 3 */
508 DL_IMPORT(void) Py_SetProgramName(char *);
509 DL_IMPORT(void) Py_SetPythonHome(char *);
510 /* 4 */
511 DL_IMPORT(int) Py_IsInitialized(void);
512 /* 5 */
513 DL_IMPORT(int) PyRun_SimpleString(char *);
514 /* 6 */
515 DL_IMPORT(int) PyRun_AnyFile(FILE *, char *);
516 DL_IMPORT(int) PyRun_SimpleFile(FILE *, char *);
517 DL_IMPORT(int) PyRun_InteractiveOne(FILE *, char *);
518 DL_IMPORT(int) PyRun_InteractiveLoop(FILE *, char *);
519 DL_IMPORT(int) Py_FdIsInteractive(FILE *, char *);
520 /* 7 */
521 DL_IMPORT(int) PyRun_AnyFileEx(FILE *, char *, int);
522 DL_IMPORT(int) PyRun_SimpleFileEx(FILE *, char *, int);
523 /* 8 */
524 DL_IMPORT(char*) Py_GetProgramName(void);
525 DL_IMPORT(char*) Py_GetPythonHome(void);
526 DL_IMPORT(char*) Py_GetProgramFullPath(void);
527 DL_IMPORT(char*) Py_GetPrefix(void);
528 DL_IMPORT(char*) Py_GetExecPrefix(void);
529 DL_IMPORT(char*) Py_GetPath(void);
530 DL_IMPORT(char*) Py_GetVersion(void);
531 DL_IMPORT(char*) Py_GetPlatform(void);
532 DL_IMPORT(char*) Py_GetCopyright(void);
533 DL_IMPORT(char*) Py_GetCompiler(void);
534 DL_IMPORT(char*) Py_GetBuildInfo(void);
535 /* 9 */
536 DL_IMPORT(PyObject*) PyRun_String(char*, int, PyObject *, PyObject *);
537 /* 10 */
538 DL_IMPORT(PyObject*) PyRun_File(FILE *, char*, int, PyObject *, PyObject *);
539 /* 11 */
540 DL_IMPORT(PyObject*) PyRun_FileEx(FILE *, char*, int, PyObject *, PyObject *, int );
541 /* 12 */
542 DL_IMPORT(PyObject*) Py_CompileString(char*, char*, int); /* FUNCTION 30 */
543
544 /* Generic operations on objects */
545 /* 13 */
546 extern DL_IMPORT(int) PyObject_Print(PyObject *, FILE *, int);
547 /* 14 */
548 extern DL_IMPORT(PyObject *) PyObject_Repr(PyObject *);
549 extern DL_IMPORT(PyObject *) PyObject_Str(PyObject *);
550 extern DL_IMPORT(PyObject *) PyObject_Unicode(PyObject *);
551 /* 15 */
552 extern DL_IMPORT(PyObject *) PyObject_RichCompare(PyObject *, PyObject *, int);
553 /* 16 */
554 extern DL_IMPORT(PyObject *) PyObject_GetAttrString(PyObject *, char *);
555 /* 17 */
556 extern DL_IMPORT(PyObject *) PyObject_GetAttr(PyObject *, PyObject *);
557 extern DL_IMPORT(PyObject *) PyObject_CallObject(PyObject *, PyObject *);
558 /* 18 */
559 extern DL_IMPORT(int) PyObject_IsTrue(PyObject *);
560 extern DL_IMPORT(int) PyObject_Not(PyObject *);
561 extern DL_IMPORT(int) PyCallable_Check(PyObject *);
562 /* 19 */
563 extern DL_IMPORT(int) PyObject_Compare(PyObject *, PyObject *);
564 extern DL_IMPORT(int) PyObject_HasAttr(PyObject *, PyObject *);
565 /* 20 */
566 extern DL_IMPORT(int) PyObject_RichCompareBool(PyObject *, PyObject *, int);
567 /* 21 */
568 extern DL_IMPORT(int) PyObject_SetAttrString(PyObject *, char *, PyObject *);
569 /* 22 */
570 extern DL_IMPORT(int) PyObject_HasAttrString(PyObject *, char *);
571 /* 23 */
572 extern DL_IMPORT(int) PyNumber_Coerce(PyObject **, PyObject **);
573 extern DL_IMPORT(int) PyNumber_CoerceEx(PyObject **, PyObject **);
574 /* 24 */
575 extern DL_IMPORT(int) PyObject_SetAttr(PyObject *, PyObject *, PyObject *);
576 /* 25 */
577 extern DL_IMPORT(long) PyObject_Hash(PyObject *);
578
579 /* Strings */
580 /* 18 */
581 extern DL_IMPORT(int) PyString_Size(PyObject *);
582 /* 26 */
583 extern DL_IMPORT(char *) PyString_AsString(PyObject *);
584 /* 27 */
585 extern DL_IMPORT(void) PyString_Concat(PyObject **, PyObject *);
586 extern DL_IMPORT(void) PyString_ConcatAndDel(PyObject **, PyObject *);
587 /* 28 */
588 extern DL_IMPORT(PyObject *) PyString_FromString(const char *);
589 /* 17 */
590 extern DL_IMPORT(PyObject *) PyString_Format(PyObject *, PyObject *);
591
592 /* Dictionaries */
593 /* 29 */
594 extern DL_IMPORT(PyObject *) PyDict_New(void);
595 /* 17 */
596 extern DL_IMPORT(PyObject *) PyDict_GetItem(PyObject *mp, PyObject *key);
597 /* 24 */
598 extern DL_IMPORT(int) PyDict_SetItem(PyObject *mp, PyObject *key, PyObject *item);
599 /* 19 */
600 extern DL_IMPORT(int) PyDict_DelItem(PyObject *mp, PyObject *key);
601 /* 30 */
602 extern DL_IMPORT(void) PyDict_Clear(PyObject *mp);
603 /* 31 */
604 extern DL_IMPORT(int) PyDict_Next
605 (PyObject *mp, int *pos, PyObject **key, PyObject **value);
606 /* 14 */
607 extern DL_IMPORT(PyObject *) PyDict_Keys(PyObject *mp);
608 extern DL_IMPORT(PyObject *) PyDict_Values(PyObject *mp);
609 extern DL_IMPORT(PyObject *) PyDict_Items(PyObject *mp);
610 extern DL_IMPORT(PyObject *) PyDict_Copy(PyObject *mp);
611 /* 18 */
612 extern DL_IMPORT(int) PyDict_Size(PyObject *mp);
613 /* 16 */
614 extern DL_IMPORT(PyObject *) PyDict_GetItemString(PyObject *dp, char *key);
615 /* 22 */
616 extern DL_IMPORT(int) PyDict_DelItemString(PyObject *dp, char *key);
617 /* 21 */
618 extern DL_IMPORT(int) PyDict_SetItemString(PyObject *dp, char *key, PyObject *item);
619
620 /* Integer */
621 /* 32 */
622 extern DL_IMPORT(PyObject *) PyInt_FromString(char*, char**, int);
623 /* 33 */
624 extern DL_IMPORT(PyObject *) PyInt_FromUnicode(Py_UNICODE*, int, int);
625 /* 34 */
626 extern DL_IMPORT(PyObject *) PyInt_FromLong(long);
627 /* 25 */
628 extern DL_IMPORT(long) PyInt_AsLong(PyObject *);
629 /* 35 */
630 extern DL_IMPORT(long) PyInt_GetMax(void);
631 /* Long */
632 /* 34 */
633 extern DL_IMPORT(PyObject *) PyLong_FromLong(long);
634 /* 36 */
635 extern DL_IMPORT(PyObject *) PyLong_FromDouble(double);
636 /* 25 */
637 extern DL_IMPORT(long) PyLong_AsLong(PyObject *);
638
639 /* Float */
640 /* 36 */
641 extern DL_IMPORT(PyObject *) PyFloat_FromDouble(double);
642 /* 37 */
643 extern DL_IMPORT(double) PyFloat_AsDouble(PyObject *);
644
645 /* Modules */
646 /* 28 */
647 extern DL_IMPORT(PyObject *) PyModule_New(char *);
648 /* 14 */
649 extern DL_IMPORT(PyObject *) PyModule_GetDict(PyObject *);
650 /* 26 */
651 extern DL_IMPORT(char *) PyModule_GetName(PyObject *);
652 extern DL_IMPORT(char *) PyModule_GetFilename(PyObject *);
653
654 /* 39 */
655 extern DL_IMPORT(PyObject *) PyTuple_New(int size);
656 /* 18 */
657 extern DL_IMPORT(int) PyTuple_Size(PyObject *);
658 extern DL_IMPORT(int) PyTuple_Check(PyObject *);
659 /* 40 */
660 extern DL_IMPORT(PyObject *) PyTuple_GetItem(PyObject *, int);
661 /* 41 */
662 extern DL_IMPORT(int) PyTuple_SetItem(PyObject *, int, PyObject *);
663 /* 13 */
664 extern DL_IMPORT(PyObject *) PyTuple_GetSlice(PyObject *, int, int);
665
666 /* 42 */
667 DL_IMPORT(PyObject *) PySlice_New(PyObject* start, PyObject* stop, PyObject* step);
668 /* 43 */
669 DL_IMPORT(int) PySlice_GetIndices(PySliceObject *r, int length, int *start, int *stop, int *step);
670 /* 44 */
671 DL_IMPORT(PyObject *) PyRange_New(long, long, long, int);
672
673 /* Error handling definitions */
674
675 /* 30 */
676 DL_IMPORT(void) PyErr_SetNone(PyObject *);
677 /* 45 */
678 DL_IMPORT(void) PyErr_SetObject(PyObject *, PyObject *);
679 /* 46 */
680 DL_IMPORT(void) PyErr_SetString(PyObject *, const char *);
681 /* 29 */
682 DL_IMPORT(PyObject *) PyErr_Occurred(void);
683 /* 1 */
684 DL_IMPORT(void) PyErr_Clear(void);
685 /* 47 */
686 DL_IMPORT(void) PyErr_Fetch(PyObject **, PyObject **, PyObject **);
687 /* 48 */
688 DL_IMPORT(void) PyErr_Restore(PyObject *, PyObject *, PyObject *);
689
690 /* Error testing and normalization */
691 /* 19 */
692 DL_IMPORT(int) PyErr_GivenExceptionMatches(PyObject *, PyObject *);
693 /* 18 */
694 DL_IMPORT(int) PyErr_ExceptionMatches(PyObject *);
695 /* 47 */
696 DL_IMPORT(void) PyErr_NormalizeException(PyObject**, PyObject**, PyObject**);
697
698 /* Classes */
699 /* 42 */
700 extern DL_IMPORT(PyObject *) PyClass_New(PyObject *, PyObject *, PyObject *);
701 /* 42 */
702 extern DL_IMPORT(PyObject *) PyInstance_New(PyObject *, PyObject *,
703 PyObject *);
704 /* 17 */
705 extern DL_IMPORT(PyObject *) PyInstance_NewRaw(PyObject *, PyObject *);
706 /* 42 */
707 extern DL_IMPORT(PyObject *) PyMethod_New(PyObject *, PyObject *, PyObject *);
708 /* 14 */
709 extern DL_IMPORT(PyObject *) PyMethod_Function(PyObject *);
710 extern DL_IMPORT(PyObject *) PyMethod_Self(PyObject *);
711 extern DL_IMPORT(PyObject *) PyMethod_Class(PyObject *);
712
713 /* Module */
714 /* 28 */
715 extern DL_IMPORT(PyObject *) PyModule_New(char *);
716 /* 14 */
717 extern DL_IMPORT(PyObject *) PyModule_GetDict(PyObject *);
718 /* 26 */
719 extern DL_IMPORT(char *) PyModule_GetName(PyObject *);
720 extern DL_IMPORT(char *) PyModule_GetFilename(PyObject *);
721 /* 35 */
722 DL_IMPORT(long) PyImport_GetMagicNumber(void);
723 /* 49 */
724 DL_IMPORT(PyObject *) PyImport_ExecCodeModule(char *name, PyObject *co);
725 /* 50 */
726 DL_IMPORT(PyObject *) PyImport_ExecCodeModuleEx(char *name, PyObject *co, char *pathname);
727 /* 29 */
728 DL_IMPORT(PyObject *) PyImport_GetModuleDict(void);
729 /* 28 */
730 DL_IMPORT(PyObject *) PyImport_AddModule(char *name);
731 DL_IMPORT(PyObject *) PyImport_ImportModule(char *name);
732 /* 51 */
733 DL_IMPORT(PyObject *) PyImport_ImportModuleEx(char *name, PyObject *globals, PyObject *locals, PyObject *fromlist);
734 /* 28 */
735 DL_IMPORT(PyObject *) PyImport_Import(PyObject *name);
736 /* 14 */
737 DL_IMPORT(PyObject *) PyImport_ReloadModule(PyObject *m);
738 /* 1 */
739 DL_IMPORT(void) PyImport_Cleanup(void);
740 /* 5 */
741 DL_IMPORT(int) PyImport_ImportFrozenModule(char *);
742
743 /* Interface to random parts in ceval.c */
744 /* 42 */
745 DL_IMPORT(PyObject *) PyEval_CallObjectWithKeywords(PyObject *, PyObject *, PyObject *);
746 /* 17 */
747 #if 0
748 // dropped in Python 2.7
749 DL_IMPORT(PyObject *) PyEval_CallObject(PyObject *, PyObject *);
750 #endif
751
752 /* 29 */
753 DL_IMPORT(PyObject *) PyEval_GetBuiltins(void);
754 DL_IMPORT(PyObject *) PyEval_GetGlobals(void);
755 DL_IMPORT(PyObject *) PyEval_GetLocals(void);
756 DL_IMPORT(PyObject *) PyEval_GetFrame(void);
757 /* 4 */
758 DL_IMPORT(int) PyEval_GetRestricted(void);
759
760 /* Abstract layer */
761 /* 14 */
762 DL_IMPORT(PyObject *) PyObject_Type(PyObject *o);
763 /* 18 */
764 DL_IMPORT(int) PyObject_Size(PyObject *o);
765 /* 17 */
766 DL_IMPORT(PyObject *) PyObject_GetItem(PyObject *o, PyObject *key);
767 /* 24 */
768 DL_IMPORT(int) PyObject_SetItem(PyObject *o, PyObject *key, PyObject *v);
769 /* 17 */
770 DL_IMPORT(int) PyObject_DelItem(PyObject *o, PyObject *key);
771 /* 52 */
772 DL_IMPORT(int) PyObject_AsCharBuffer(PyObject *obj,const char **buffer,int *buffer_len);
773 DL_IMPORT(int) PyObject_AsReadBuffer(PyObject *obj,const void **buffer,int *buffer_len);
774 DL_IMPORT(int) PyObject_AsWriteBuffer(PyObject *obj,void **buffer,int *buffer_len);
775 /* 18 */
776 DL_IMPORT(int) PyNumber_Check(PyObject *o);
777 /* 17 */
778 DL_IMPORT(PyObject *) PyNumber_Add(PyObject *o1, PyObject *o2);
779 DL_IMPORT(PyObject *) PyNumber_Subtract(PyObject *o1, PyObject *o2);
780 DL_IMPORT(PyObject *) PyNumber_Multiply(PyObject *o1, PyObject *o2);
781 DL_IMPORT(PyObject *) PyNumber_Divide(PyObject *o1, PyObject *o2);
782 DL_IMPORT(PyObject *) PyNumber_Remainder(PyObject *o1, PyObject *o2);
783 DL_IMPORT(PyObject *) PyNumber_Divmod(PyObject *o1, PyObject *o2);
784 /* 42 */
785 DL_IMPORT(PyObject *) PyNumber_Power(PyObject *o1, PyObject *o2,PyObject *o3);
786 /* 14 */
787 DL_IMPORT(PyObject *) PyNumber_Negative(PyObject *o);
788 DL_IMPORT(PyObject *) PyNumber_Positive(PyObject *o);
789 DL_IMPORT(PyObject *) PyNumber_Absolute(PyObject *o);
790 DL_IMPORT(PyObject *) PyNumber_Invert(PyObject *o);
791 /* 17 */
792 DL_IMPORT(PyObject *) PyNumber_Lshift(PyObject *o1, PyObject *o2);
793 DL_IMPORT(PyObject *) PyNumber_Rshift(PyObject *o1, PyObject *o2);
794 DL_IMPORT(PyObject *) PyNumber_And(PyObject *o1, PyObject *o2);
795 DL_IMPORT(PyObject *) PyNumber_Xor(PyObject *o1, PyObject *o2);
796 DL_IMPORT(PyObject *) PyNumber_Or(PyObject *o1, PyObject *o2);
797 /* 14 */
798 DL_IMPORT(PyObject *) PyNumber_Int(PyObject *o);
799 DL_IMPORT(PyObject *) PyNumber_Long(PyObject *o);
800 DL_IMPORT(PyObject *) PyNumber_Float(PyObject *o);
801 /* 17 */
802 DL_IMPORT(PyObject *) PyNumber_InPlaceAdd(PyObject *o1, PyObject *o2);
803 DL_IMPORT(PyObject *) PyNumber_InPlaceSubtract(PyObject *o1, PyObject *o2);
804 DL_IMPORT(PyObject *) PyNumber_InPlaceMultiply(PyObject *o1, PyObject *o2);
805 DL_IMPORT(PyObject *) PyNumber_InPlaceDivide(PyObject *o1, PyObject *o2);
806 DL_IMPORT(PyObject *) PyNumber_InPlaceRemainder(PyObject *o1, PyObject *o2);
807 DL_IMPORT(PyObject *) PyNumber_InPlaceLshift(PyObject *o1, PyObject *o2);
808 DL_IMPORT(PyObject *) PyNumber_InPlaceRshift(PyObject *o1, PyObject *o2);
809 DL_IMPORT(PyObject *) PyNumber_InPlaceAnd(PyObject *o1, PyObject *o2);
810 DL_IMPORT(PyObject *) PyNumber_InPlaceXor(PyObject *o1, PyObject *o2);
811 DL_IMPORT(PyObject *) PyNumber_InPlaceOr(PyObject *o1, PyObject *o2);
812 /* 42 */
813 DL_IMPORT(PyObject *) PyNumber_InPlacePower(PyObject *o1, PyObject *o2,PyObject *o3);
814 /* 18 */
815 DL_IMPORT(int) PySequence_Check(PyObject *o);
816 DL_IMPORT(int) PySequence_Size(PyObject *o);
817 DL_IMPORT(int) PySequence_Length(PyObject *o);
818 /* 17 */
819 DL_IMPORT(PyObject *) PySequence_Concat(PyObject *o1, PyObject *o2);
820 /* 40 */
821 DL_IMPORT(PyObject *) PySequence_Repeat(PyObject *o, int count);
822 DL_IMPORT(PyObject *) PySequence_GetItem(PyObject *o, int i);
823 /* 13 */
824 DL_IMPORT(PyObject *) PySequence_GetSlice(PyObject *o, int i1, int i2);
825 /* 41 */
826 DL_IMPORT(int) PySequence_SetItem(PyObject *o, int i, PyObject *v);
827 /* 20 */
828 DL_IMPORT(int) PySequence_DelItem(PyObject *o, int i);
829 /* 53 */
830 DL_IMPORT(int) PySequence_SetSlice(PyObject *o, int i1, int i2, PyObject *v);
831 /* 54 */
832 DL_IMPORT(int) PySequence_DelSlice(PyObject *o, int i1, int i2);
833 /* 14 */
834 DL_IMPORT(PyObject *) PySequence_Tuple(PyObject *o);
835 DL_IMPORT(PyObject *) PySequence_List(PyObject *o);
836 /* 16 */
837 DL_IMPORT(PyObject *) PySequence_Fast(PyObject *o, const char* m);
838 /* 19 */
839 DL_IMPORT(int) PySequence_Count(PyObject *o, PyObject *value);
840 DL_IMPORT(int) PySequence_Contains(PyObject *o, PyObject *value);
841 DL_IMPORT(int) PySequence_In(PyObject *o, PyObject *value);
842 DL_IMPORT(int) PySequence_Index(PyObject *o, PyObject *value);
843 /* 17 */
844 DL_IMPORT(PyObject *) PySequence_InPlaceConcat(PyObject *o1, PyObject *o2);
845 /* 22 */
846 DL_IMPORT(PyObject *) PySequence_InPlaceRepeat(PyObject *o, int count);
847 /* 18 */
848 DL_IMPORT(int) PyMapping_Check(PyObject *o);
849 DL_IMPORT(int) PyMapping_Size(PyObject *o);
850 DL_IMPORT(int) PyMapping_Length(PyObject *o);
851 /* 16 */
852 DL_IMPORT(int) PyMapping_HasKeyString(PyObject *o, char *key);
853 /* 19 */
854 DL_IMPORT(int) PyMapping_HasKey(PyObject *o, PyObject *key);
855 /* 16 */
856 DL_IMPORT(PyObject *) PyMapping_GetItemString(PyObject *o, char *key);
857 /* 41 */
858 DL_IMPORT(int) PyMapping_SetItemString(PyObject *o, char *key, PyObject *value);
859
860
861 #ifdef MAYBE_RUN
862 DL_IMPORT(void) init_exceptions(void);
863 DL_IMPORT(void) fini_exceptions(void);
864 DL_IMPORT(void) _PyImport_Init(void);
865 DL_IMPORT(void) _PyImport_Fini(void);
866 DL_IMPORT(void) PyMethod_Fini(void);
867 DL_IMPORT(void) PyFrame_Fini(void);
868 DL_IMPORT(void) PyCFunction_Fini(void);
869 DL_IMPORT(void) PyTuple_Fini(void);
870 DL_IMPORT(void) PyString_Fini(void);
871 DL_IMPORT(void) PyInt_Fini(void);
872 DL_IMPORT(void) PyFloat_Fini(void);
873 DL_IMPORT(PyObject*) _PyBuiltin_Init(void);
874 DL_IMPORT(PyObject*) _PySys_Init(void);
875 DL_IMPORT(struct symtable *) Py_SymtableString(char*, char*, int);
876 DL_IMPORT(struct _node *) PyParser_SimpleParseString(char*, int);
877 DL_IMPORT(struct _node *) PyParser_SimpleParseFile(FILE *, char*, int);
878 DL_IMPORT(int) Py_AtExit(void (*func)(void));
879 DL_IMPORT(void) Py_EndInterpreter(PyThreadState *);
880 DL_IMPORT(PyThreadState *) Py_NewInterpreter(void);
881 DL_IMPORT(int) (*PyOS_InputHook)(void);
882 DL_IMPORT(int) PyOS_CheckStack(void);
883 DL_IMPORT(char) *(*PyOS_ReadlineFunctionPointer)(char*);
884 DL_IMPORT(PyOS_sighandler_t) PyOS_getsig(int);
885 DL_IMPORT(PyOS_sighandler_t) PyOS_setsig(int, PyOS_sighandler_t);
886 DL_IMPORT(void) PyOS_FiniInterrupts(void);
887 DL_IMPORT(char*) PyOS_Readline(char*);
888 #endif//MAYBE_RUN
889 #endif//DONT_COMPILE_THIS
890
891 /* HST FIXUP */
892 #if (PY_MAJOR_VERSION >= 2 && PY_MINOR_VERSION > 4)
893 #undef PyRun_SimpleString
894 #define PyRun_SimpleString PyRun_SimpleString_redo
895 int PyRun_SimpleString_redo(const char* command) { return PyRun_SimpleStringFlags(command, NULL); }
896
897 #undef PyRun_AnyFile
898 #define PyRun_AnyFile PyRun_AnyFile_redo
899 int PyRun_AnyFile_redo(FILE* fp, const char* filename) { return PyRun_AnyFileExFlags(fp, filename, 0, NULL); }
900
901 #undef PyRun_SimpleFile
902 #define PyRun_SimpleFile PyRun_SimpleFile_redo
903 int PyRun_SimpleFile_redo(FILE* fp, const char* filename) { return PyRun_SimpleFileExFlags(fp, filename, 0, NULL); }
904
905 #undef PyRun_InteractiveOne
906 #define PyRun_InteractiveOne PyRun_InteractiveOne_redo
907 int PyRun_InteractiveOne_redo(FILE* fp, const char* filename) { return PyRun_InteractiveOneFlags(fp, filename, NULL); }
908
909 #undef PyRun_InteractiveLoop
910 #define PyRun_InteractiveLoop PyRun_InteractiveLoop_redo
911 int PyRun_InteractiveLoop_redo(FILE* fp, const char* filename) { return PyRun_InteractiveLoopFlags(fp, filename, NULL); }
912
913 #undef PyRun_AnyFileEx
914 #define PyRun_AnyFileEx PyRun_AnyFileEx_redo
915 int PyRun_AnyFileEx_redo(FILE* fp, const char* filename, int closeit) { return PyRun_AnyFileExFlags(fp, filename, closeit, NULL); }
916
917 #undef PyRun_SimpleFileEx
918 #define PyRun_SimpleFileEx PyRun_SimpleFileEx_redo
919 int PyRun_SimpleFileEx_redo(FILE* fp, const char* filename, int closeit) { return PyRun_SimpleFileExFlags(fp, filename, closeit, NULL); }
920
921 #undef PyRun_String
922 #define PyRun_String PyRun_String_redo
923 PyObject* PyRun_String_redo(const char* str, int start, PyObject* globals, PyObject* locals) { return PyRun_StringFlags(str, start, globals, locals, NULL); }
924
925 #undef PyRun_File
926 #define PyRun_File PyRun_File_redo
927 PyObject* PyRun_File_redo(FILE* fp, const char* filename, int start, PyObject* globals, PyObject* locals) { return PyRun_FileExFlags(fp, filename, start, globals, locals, 0, NULL); }
928
929 #undef PyRun_FileEx
930 #define PyRun_FileEx PyRun_FileEx_redo
931 PyObject* PyRun_FileEx_redo(FILE* fp, const char* filename, int start, PyObject* globals, PyObject* locals, int closeit) { return PyRun_FileExFlags(fp, filename, start, globals, locals, closeit, NULL); }
932
933 #undef Py_CompileString
934 #define Py_CompileString Py_CompileString_redo
935 PyObject* Py_CompileString_redo(const char* str, const char* filename, int start) { return Py_CompileStringFlags(str, filename, start, NULL); }
936
937 #undef PyRange_New
938 #define PyRange_New PyRange_New_redo
939 PyObject* PyRange_New_redo(PyObject* start, PyObject* stop, PyObject* step) { return PyObject_CallFunction((PyObject*)&PyRange_Type, "lll", start, stop, step); }
940 #endif /* PYTHON 2.4 */
941
942 #undef PyTuple_Check
943 int PyTuple_Check(PyObject* op) { return PyObject_TypeCheck(op, &PyTuple_Type); }
944 /* END HST FIXUP */
945
946 /* Value -> Pyobject */
947
948 value pywrapvalue( value cb ) {
949 CAMLparam1(cb);
950 CAMLreturn(pywrap(camlwrap(cb,NULL,0)));
951 }
952
953 value pyunwrapvalue( value cb ) {
954 CAMLparam1(cb);
955 value *v;
956 v = (value *)PyCObject_AsVoidPtr( pyunwrap(cb) );
957 CAMLreturn(*v);
958 }
959
960 /* Create the function table */
961
962 typedef struct _python_func_table {
963 void *func;
964 int format;
965 char *desc;
966 } python_func_table;
967
968 python_func_table the_python_func_table[] = {
969 /* 1 */
970 { (void *)Py_Initialize, 1, "Py_Initialize" },
971 { (void *)Py_Finalize, 1, "Py_Finalize" },
972 { (void *)PyErr_Print, 1, "PyErr_Print" },
973 /* 2 */
974 { (void *)Py_Exit, 2, "Py_Exit" },
975 { (void *)PyErr_PrintEx, 2, "PyErr_PrintEx" },
976 /* 3 */
977 { (void *)Py_SetProgramName, 3, "Py_SetProgramName" },
978 { (void *)Py_SetPythonHome, 3, "Py_SetPythonHome" },
979 /* 4 */
980 { (void *)Py_IsInitialized, 4, "Py_IsInitialized" },
981 /* 5 */
982 { (void *)PyRun_SimpleString, 5, "PyRun_SimpleString" },
983 /* 6 */
984 { (void *)PyRun_AnyFile, 6, "PyRun_AnyFile" },
985 { (void *)PyRun_SimpleFile, 6, "PyRun_SimpleFile" },
986 { (void *)PyRun_InteractiveOne, 6, "PyRun_InteractiveOne" },
987 { (void *)PyRun_InteractiveLoop, 6, "PyRun_InteractiveLoop" },
988 { (void *)Py_FdIsInteractive, 6, "Py_FdIsInteractive" },
989 /* 7 */
990 { (void *)PyRun_AnyFileEx, 7, "PyRun_AnyFileEx" },
991 { (void *)PyRun_SimpleFileEx, 7, "PyRun_SimpleFileEx" },
992 /* 8 */
993 { (void *)Py_GetProgramName, 8, "Py_GetProgramName" },
994 { (void *)Py_GetPythonHome, 8, "Py_GetPythonHome" },
995 { (void *)Py_GetProgramFullPath, 8, "Py_GetProgramFullPath" },
996 { (void *)Py_GetPrefix, 8, "Py_GetPrefix" },
997 { (void *)Py_GetExecPrefix, 8, "Py_GetExecPrefix" },
998 { (void *)Py_GetPath, 8, "Py_GetPath" },
999 { (void *)Py_GetVersion, 8, "Py_GetVersion" },
1000 { (void *)Py_GetPlatform, 8, "Py_GetPlatform" },
1001 { (void *)Py_GetCopyright, 8, "Py_GetCopyright" },
1002 { (void *)Py_GetCompiler, 8, "Py_GetCompiler" },
1003 { (void *)Py_GetBuildInfo, 8, "Py_GetBuildInfo" },
1004 /* 9 */
1005 { (void *)PyRun_String, 9, "PyRun_String" },
1006 /* 10 */
1007 { (void *)PyRun_File, 10, "PyRun_File" },
1008 /* 11 */
1009 { (void *)PyRun_FileEx, 11, "PyRun_FileEx" },
1010 /* 12 */
1011 { (void *)Py_CompileString, 12, "Py_CompileString" },
1012
1013 /* Object */
1014 /* 13 */
1015 { (void *)PyObject_Print, 13, "PyObject_Print" },
1016 /* 14 */
1017 { (void *)PyObject_Repr, 14, "PyObject_Repr" },
1018 { (void *)PyObject_Str, 14, "PyObject_Str" },
1019 { (void *)PyObject_Unicode, 14, "PyObject_Unicode" },
1020 /* 15 */
1021 { (void *)PyObject_RichCompare, 15, "PyObject_RichCompare" },
1022 /* 16 */
1023 { (void *)PyObject_GetAttrString, 16, "PyObject_GetAttrString" },
1024 /* 17 */
1025 { (void *)PyObject_GetAttr, 17, "PyObject_GetAttr" },
1026 { (void *)PyObject_CallObject, 17, "PyObject_CallObject" },
1027 /* 18 */
1028 { (void *)PyObject_IsTrue, 18, "PyObject_IsTrue" },
1029 { (void *)PyObject_Not, 18, "PyObject_Not" },
1030 { (void *)PyCallable_Check, 18, "PyCallable_Check" },
1031 /* 19 */
1032 { (void *)PyObject_Compare, 19, "PyObject_Compare" },
1033 { (void *)PyObject_HasAttr, 19, "PyObject_HasAttr" },
1034 /* 20 */
1035 { (void *)PyObject_RichCompareBool, 20, "PyObject_RichCompareBool" },
1036 /* 21 */
1037 { (void *)PyObject_SetAttrString, 21, "PyObject_SetAttrString" },
1038 /* 22 */
1039 { (void *)PyObject_HasAttrString, 22, "PyObject_HasAttrString" },
1040 /* 23 */
1041 { (void *)PyNumber_Coerce, 23, "PyNumber_Coerce" },
1042 { (void *)PyNumber_CoerceEx, 23, "PyNumber_CoerceEx" },
1043 /* 24 */
1044 { (void *)PyObject_SetAttr, 24, "PyObject_SetAttr" },
1045 /* 25 */
1046 { (void *)PyObject_Hash, 25, "PyObject_Hash" },
1047
1048 /* Strings */
1049 /* 18 */
1050 { (void *)PyString_Size, 18, "PyString_Size" },
1051 /* 26 */
1052 { (void *)PyString_AsString, 26, "PyString_AsString" },
1053 /* 27 */
1054 { (void *)PyString_Concat, 27, "PyString_Concat" },
1055 { (void *)PyString_ConcatAndDel, 27, "PyString_ConcatAndDel" },
1056 /* 28 */
1057 { (void *)PyString_FromString, 28, "PyString_FromString" },
1058 /* 17 */
1059 { (void *)PyString_Format, 17, "PyString_Format" },
1060
1061 /* Dictionaries */
1062 /* 29 */
1063 { (void *)PyDict_New, 29, "PyDict_New" },
1064 /* 17 */
1065 { (void *)PyDict_GetItem, 17, "PyDict_GetItem" },
1066 /* 24 */
1067 { (void *)PyDict_SetItem, 24, "PyDict_SetItem" },
1068 /* 19 */
1069 { (void *)PyDict_DelItem, 19, "PyDict_DelItem" },
1070 /* 30 */
1071 { (void *)PyDict_Clear, 30, "PyDict_Clear" },
1072 /* 31 */
1073 { (void *)PyDict_Next, 31, "PyDict_Next" },
1074 /* 14 */
1075 { (void *)PyDict_Keys, 14, "PyDict_Keys" },
1076 { (void *)PyDict_Values, 14, "PyDict_Values" },
1077 { (void *)PyDict_Items, 14, "PyDict_Items" },
1078 { (void *)PyDict_Copy, 14, "PyDict_Copy" },
1079 /* 18 */
1080 { (void *)PyDict_Size, 18, "PyDict_Size" },
1081 /* 16 */
1082 { (void *)PyDict_GetItemString, 16, "PyDict_GetItemString" },
1083 /* 22 */
1084 { (void *)PyDict_DelItemString, 22, "PyDict_DelItemString" },
1085 /* 21 */
1086 { (void *)PyDict_SetItemString, 21, "PyDict_SetItemString" },
1087
1088 /* Integer */
1089 /* 34 */
1090 { (void *)PyInt_FromLong, 34, "PyInt_FromLong" },
1091 /* 25 */
1092 { (void *)PyInt_AsLong, 25, "PyInt_AsLong" },
1093 /* 35 */
1094 { (void *)PyInt_GetMax, 35, "PyInt_GetMax" },
1095
1096 /* Float */
1097 /* 36 */
1098 { (void *)PyFloat_FromDouble, 36, "PyFloat_FromDouble" },
1099 /* 37 */
1100 { (void *)PyFloat_AsDouble, 37, "PyFloat_AsDouble" },
1101
1102 /* Modules */
1103 /* 28 */
1104 { (void *)PyModule_New, 28, "PyModule_New" },
1105 /* 14 */
1106 { (void *)PyModule_GetDict, 14, "PyModule_GetDict" },
1107 /* 26 */
1108 { (void *)PyModule_GetName, 26, "PyModule_GetName" },
1109 { (void *)PyModule_GetFilename, 26, "PyModule_GetFilename" },
1110
1111 /* 39 */
1112 { (void *)PyTuple_New, 39, "PyTuple_New" },
1113 /* 18 */
1114 { (void *)PyTuple_Size, 18, "PyTuple_Size" },
1115 { (void *)PyTuple_Check, 18, "PyTuple_Check" },
1116 /* 40 */
1117 { (void *)PyTuple_GetItem, 40, "PyTuple_GetItem" },
1118 /* 41 */
1119 { (void *)PyTuple_SetItem, 41, "PyTuple_SetItem" },
1120 /* 13 */
1121 { (void *)PyTuple_GetSlice, 13, "PyTuple_GetSlice" },
1122
1123 /* 42 */
1124 { (void *)PySlice_New, 42, "PySlice_New" },
1125 /* 43 */
1126 { (void *)PySlice_GetIndices, 43, "PySlice_GetIndices" },
1127 /* 44 */
1128 { (void *)PyRange_New, 44, "PyRange_New" },
1129
1130 /* Error handling definitions */
1131
1132 /* 30 */
1133 { (void *)PyErr_SetNone, 30, "PyErr_SetNone" },
1134 /* 45 */
1135 { (void *)PyErr_SetObject, 45, "PyErr_SetObject" },
1136 /* 46 */
1137 { (void *)PyErr_SetString, 46, "PyErr_SetString" },
1138 /* 29 */
1139 { (void *)PyErr_Occurred, 29, "PyErr_Occurred" },
1140 /* 1 */
1141 { (void *)PyErr_Clear, 1, "PyErr_Clear" },
1142 /* 47 */
1143 { (void *)PyErr_Fetch, 47, "PyErr_Fetch" },
1144 /* 48 */
1145 { (void *)PyErr_Restore, 48, "PyErr_Restore" },
1146
1147 /* Error testing and normalization */
1148 /* 19 */
1149 { (void *)PyErr_GivenExceptionMatches, 19, "PyErr_GivenExceptionMatches" },
1150 /* 18 */
1151 { (void *)PyErr_ExceptionMatches, 18, "PyErr_ExceptionMatches" },
1152 /* 47 */
1153 { (void *)PyErr_NormalizeException, 47, "PyErr_NormalizeException" },
1154
1155 /* Classes */
1156 /* 42 */
1157 { (void *)PyClass_New, 42, "PyClass_New" },
1158 /* 42 */
1159 { (void *)PyInstance_New, 42, "PyInstance_New" },
1160
1161 /* 17 */
1162 { (void *)PyInstance_NewRaw, 17, "PyInstance_NewRaw" },
1163 /* 42 */
1164 { (void *)PyMethod_New, 42, "PyMethod_New" },
1165 /* 14 */
1166 { (void *)PyMethod_Function, 14, "PyMethod_Function" },
1167 { (void *)PyMethod_Self, 14, "PyMethod_Self" },
1168 { (void *)PyMethod_Class, 14, "PyMethod_Class" },
1169
1170 /* Module */
1171 /* 28 */
1172 { (void *)PyModule_New, 28, "PyModule_New" },
1173 /* 14 */
1174 { (void *)PyModule_GetDict, 14, "PyModule_GetDict" },
1175 /* 26 */
1176 { (void *)PyModule_GetName, 26, "PyModule_GetName" },
1177 { (void *)PyModule_GetFilename, 26, "PyModule_GetFilename" },
1178 /* 35 */
1179 { (void *)PyImport_GetMagicNumber, 35, "PyImport_GetMagicNumber" },
1180 /* 49 */
1181 { (void *)PyImport_ExecCodeModule, 49, "PyImport_ExecCodeModule" },
1182 /* 50 */
1183 { (void *)PyImport_ExecCodeModuleEx, 50, "PyImport_ExecCodeModuleEx" },
1184 /* 29 */
1185 { (void *)PyImport_GetModuleDict, 29, "PyImport_GetModuleDict" },
1186 /* 28 */
1187 { (void *)PyImport_AddModule, 28, "PyImport_AddModule" },
1188 { (void *)PyImport_ImportModule, 28, "PyImport_ImportModule" },
1189 /* 51 */
1190 #if 0
1191 /* In Python 2.6, this because a #define so we cannot take
1192 * the address of the function. - RWMJ.
1193 */
1194 { (void *)PyImport_ImportModuleEx, 51, "PyImport_ImportModuleEx" },
1195 #endif
1196 /* 28 */
1197 { (void *)PyImport_Import, 28, "PyImport_Import" },
1198 /* 14 */
1199 { (void *)PyImport_ReloadModule, 14, "PyImport_ReloadModule" },
1200 /* 1 */
1201 { (void *)PyImport_Cleanup, 1, "PyImport_Cleanup" },
1202 /* 5 */
1203 { (void *)PyImport_ImportFrozenModule, 5, "PyImport_ImportFrozenModule" },
1204
1205 /* Interface to random parts in ceval.c */
1206 /* 42 */
1207 { (void *)PyEval_CallObjectWithKeywords, 42, "PyEval_CallObjectWithKeywords" },
1208 /* 17 */
1209 #if 0
1210 // dropped in Python 2.7
1211 { (void *)PyEval_CallObject, 17, "PyEval_CallObject" },
1212 #endif
1213
1214 /* 29 */
1215 { (void *)PyEval_GetBuiltins, 29, "PyEval_GetBuiltins" },
1216 { (void *)PyEval_GetGlobals, 29, "PyEval_GetGlobals" },
1217 { (void *)PyEval_GetLocals, 29, "PyEval_GetLocals" },
1218 { (void *)PyEval_GetFrame, 29, "PyEval_GetFrame" },
1219 /* 4 */
1220 { (void *)PyEval_GetRestricted, 4, "PyEval_GetRestricted" },
1221
1222 /* Abstract layer */
1223 /* 14 */
1224 { (void *)PyObject_Type, 14, "PyObject_Type" },
1225 /* 18 */
1226 { (void *)PyObject_Size, 18, "PyObject_Size" },
1227 /* 17 */
1228 { (void *)PyObject_GetItem, 17, "PyObject_GetItem" },
1229 /* 24 */
1230 { (void *)PyObject_SetItem, 24, "PyObject_SetItem" },
1231 /* 17 */
1232 { (void *)PyObject_DelItem, 17, "PyObject_DelItem" },
1233 /* 52 */
1234 { (void *)PyObject_AsCharBuffer, 52, "PyObject_AsCharBuffer" },
1235 { (void *)PyObject_AsReadBuffer, 52, "PyObject_AsReadBuffer" },
1236 { (void *)PyObject_AsWriteBuffer, 52, "PyObject_AsWriteBuffer" },
1237 /* 18 */
1238 { (void *)PyNumber_Check, 18, "PyNumber_Check" },
1239 /* 17 */
1240 { (void *)PyNumber_Add, 17, "PyNumber_Add" },
1241 { (void *)PyNumber_Subtract, 17, "PyNumber_Subtract" },
1242 { (void *)PyNumber_Multiply, 17, "PyNumber_Multiply" },
1243 { (void *)PyNumber_Divide, 17, "PyNumber_Divide" },
1244 { (void *)PyNumber_Remainder, 17, "PyNumber_Remainder" },
1245 { (void *)PyNumber_Divmod, 17, "PyNumber_Divmod" },
1246 /* 42 */
1247 { (void *)PyNumber_Power, 42, "PyNumber_Power" },
1248 /* 14 */
1249 { (void *)PyNumber_Negative, 14, "PyNumber_Negative" },
1250 { (void *)PyNumber_Positive, 14, "PyNumber_Positive" },
1251 { (void *)PyNumber_Absolute, 14, "PyNumber_Absolute" },
1252 { (void *)PyNumber_Invert, 14, "PyNumber_Invert" },
1253 /* 17 */
1254 { (void *)PyNumber_Lshift, 17, "PyNumber_Lshift" },
1255 { (void *)PyNumber_Rshift, 17, "PyNumber_Rshift" },
1256 { (void *)PyNumber_And, 17, "PyNumber_And" },
1257 { (void *)PyNumber_Xor, 17, "PyNumber_Xor" },
1258 { (void *)PyNumber_Or, 17, "PyNumber_Or" },
1259 /* 14 */
1260 { (void *)PyNumber_Int, 14, "PyNumber_Int" },
1261 { (void *)PyNumber_Long, 14, "PyNumber_Long" },
1262 { (void *)PyNumber_Float, 14, "PyNumber_Float" },
1263 /* 17 */
1264 { (void *)PyNumber_InPlaceAdd, 17, "PyNumber_InPlaceAdd" },
1265 { (void *)PyNumber_InPlaceSubtract, 17, "PyNumber_InPlaceSubtract" },
1266 { (void *)PyNumber_InPlaceMultiply, 17, "PyNumber_InPlaceMultiply" },
1267 { (void *)PyNumber_InPlaceDivide, 17, "PyNumber_InPlaceDivide" },
1268 { (void *)PyNumber_InPlaceRemainder, 17, "PyNumber_InPlaceRemainder" },
1269 { (void *)PyNumber_InPlaceLshift, 17, "PyNumber_InPlaceLshift" },
1270 { (void *)PyNumber_InPlaceRshift, 17, "PyNumber_InPlaceRshift" },
1271 { (void *)PyNumber_InPlaceAnd, 17, "PyNumber_InPlaceAnd" },
1272 { (void *)PyNumber_InPlaceXor, 17, "PyNumber_InPlaceXor" },
1273 { (void *)PyNumber_InPlaceOr, 17, "PyNumber_InPlaceOr" },
1274 /* 42 */
1275 { (void *)PyNumber_InPlacePower, 42, "PyNumber_InPlacePower" },
1276 /* 18 */
1277 { (void *)PySequence_Check, 18, "PySequence_Check" },
1278 { (void *)PySequence_Size, 18, "PySequence_Size" },
1279 { (void *)PySequence_Length, 18, "PySequence_Length" },
1280 /* 17 */
1281 { (void *)PySequence_Concat, 17, "PySequence_Concat" },
1282 /* 40 */
1283 { (void *)PySequence_Repeat, 40, "PySequence_Repeat" },
1284 { (void *)PySequence_GetItem, 40, "PySequence_GetItem" },
1285 /* 13 */
1286 { (void *)PySequence_GetSlice, 13, "PySequence_GetSlice" },
1287 /* 41 */
1288 { (void *)PySequence_SetItem, 41, "PySequence_SetItem" },
1289 /* 20 */
1290 { (void *)PySequence_DelItem, 20, "PySequence_DelItem" },
1291 /* 53 */
1292 { (void *)PySequence_SetSlice, 53, "PySequence_SetSlice" },
1293 /* 54 */
1294 { (void *)PySequence_DelSlice, 54, "PySequence_DelSlice" },
1295 /* 14 */
1296 { (void *)PySequence_Tuple, 14, "PySequence_Tuple" },
1297 { (void *)PySequence_List, 14, "PySequence_List" },
1298 /* 16 */
1299 { (void *)PySequence_Fast, 16, "PySequence_Fast" },
1300 /* 19 */
1301 { (void *)PySequence_Count, 19, "PySequence_Count" },
1302 { (void *)PySequence_Contains, 19, "PySequence_Contains" },
1303 { (void *)PySequence_In, 19, "PySequence_In" },
1304 { (void *)PySequence_Index, 19, "PySequence_Index" },
1305 /* 17 */
1306 { (void *)PySequence_InPlaceConcat, 17, "PySequence_InPlaceConcat" },
1307 /* 22 */
1308 { (void *)PySequence_InPlaceRepeat, 22, "PySequence_InPlaceRepeat" },
1309 /* 18 */
1310 { (void *)PyMapping_Check, 18, "PyMapping_Check" },
1311 { (void *)PyMapping_Size, 18, "PyMapping_Size" },
1312 { (void *)PyMapping_Length, 18, "PyMapping_Length" },
1313 /* 16 */
1314 { (void *)PyMapping_HasKeyString, 16, "PyMapping_HasKeyString" },
1315 /* 19 */
1316 { (void *)PyMapping_HasKey, 19, "PyMapping_HasKey" },
1317 /* 16 */
1318 { (void *)PyMapping_GetItemString, 16, "PyMapping_GetItemString" },
1319 /* 41 */
1320 { (void *)PyMapping_SetItemString, 41, "PyMapping_SetItemString" },
1321
1322 /* End */
1323 { NULL, 0 }
1324 };
1325
1326 value pygetfuncarray( value unit ) {
1327 CAMLparam1(unit);
1328 CAMLlocal2(retv,tuplev);
1329 int i;
1330 int total_funcs;
1331
1332 for( i = 0; the_python_func_table[i].func; i++ ) ; total_funcs = i;
1333
1334 retv = alloc(total_funcs,0);
1335
1336 for( i = 0; i < total_funcs; i++ ) {
1337 tuplev = alloc_tuple( 3 );
1338 Store_field(tuplev,0,funcwrap((void *)the_python_func_table[i].func));
1339 Store_field(tuplev,1,Val_int(the_python_func_table[i].format));
1340 Store_field(tuplev,2,Val_int(i));
1341 Store_field(retv,i,tuplev);
1342 }
1343
1344 CAMLreturn(retv);
1345 }
1346
1347 enum PycamlTypeLabels {
1348 TupleType = 0,
1349 StringType,
1350 IntType,
1351 FloatType,
1352 ListType,
1353 NoneType,
1354 CallableType,
1355 ModuleType,
1356 ClassType,
1357 TypeType,
1358 DictType,
1359 NullType,
1360 OtherType
1361 };
1362
1363 value pytype( value obj ) {
1364 CAMLparam1(obj);
1365 PyObject *pobj = pyunwrap( obj );
1366 if( !pobj ) CAMLreturn(NullType);
1367 else if( PyTuple_Check( pobj ) ) CAMLreturn(Val_int(TupleType));
1368 else if( PyString_Check( pobj ) ) CAMLreturn(Val_int(StringType));
1369 else if( PyInt_Check( pobj ) ) CAMLreturn(Val_int(IntType));
1370 else if( PyFloat_Check( pobj ) ) CAMLreturn(Val_int(FloatType));
1371 else if( PyList_Check( pobj ) ) CAMLreturn(Val_int(ListType));
1372 else if( pobj == Py_None ) CAMLreturn(Val_int(NoneType));
1373 else if( PyCallable_Check( pobj ) ) CAMLreturn(Val_int(CallableType));
1374 else if( PyModule_Check( pobj ) ) CAMLreturn(Val_int(ModuleType));
1375 else if( PyClass_Check( pobj ) ) CAMLreturn(Val_int(ClassType));
1376 else if( PyType_Check( pobj ) ) CAMLreturn(Val_int(TypeType));
1377 else if( PyDict_Check( pobj ) ) CAMLreturn(Val_int(DictType));
1378 else CAMLreturn(Val_int(OtherType));
1379 }
1380
1381 value pynull( value unit ) {
1382 CAMLparam1(unit);
1383 CAMLreturn(pywrap(NULL));
1384 }
1385
1386 value pynone( value unit ) {
1387 CAMLparam1(unit);
1388 CAMLreturn(pywrap(Py_None));
1389 }
1390
1391 value pytuple_fromarray( value array ) {
1392 CAMLparam1(array);
1393 PyObject *tuple = PyTuple_New(Wosize_val(array));
1394 PyObject *elt;
1395 int i;
1396 int x;
1397
1398 for( i = 0; i < Wosize_val(array); i++ ) {
1399 elt = pyunwrap(Field(array,i));
1400 Py_INCREF(elt); /* PyTuple_SetItem will steal a reference */
1401 x = PyTuple_SetItem(tuple,i,elt);
1402 }
1403
1404 CAMLreturn(pywrap(tuple));
1405 }
1406
1407 value pytuple_toarray( value array ) {
1408 CAMLparam1(array);
1409 PyObject *obj = pyunwrap(array);
1410 int i;
1411 CAMLlocal1(rv);
1412
1413 rv = alloc_tuple( PySequence_Size(obj) );
1414
1415 for( i = 0; i < PySequence_Size(obj); i++ )
1416 Store_field(rv,i,pywrap(PySequence_GetItem(obj,i)));
1417
1418 CAMLreturn(rv);
1419 }
1420
1421 value pywrap_closure( value closure ) {
1422 CAMLparam1(closure);
1423 PyMethodDef ml;
1424 PyObject *obj;
1425 PyMethodDef *ml_def;
1426 ml.ml_name = "anonymous_closure";
1427 ml.ml_meth = pycall_callback;
1428 ml.ml_flags = 1;
1429 ml.ml_doc = "Anonymous closure";
1430 obj = camlwrap(closure,&ml,sizeof(ml));
1431 ml_def = (PyMethodDef *)caml_aux(obj);
1432 CAMLreturn(pywrap(PyCFunction_New(ml_def,obj)));
1433 }
1434
1435 value pymodule_initmodule( value name, value funclist ) {
1436 CAMLparam2(name,funclist);
1437 int i;
1438 PyMethodDef *methods = malloc( sizeof( PyMethodDef ) *
1439 Wosize_val(funclist) );
1440 CAMLlocal1(item);
1441
1442 PyImport_AddModule( String_val(name) );
1443
1444 for( i = 0; i < Wosize_val(funclist); i++ ) {
1445 item = Field(funclist,i);
1446 methods[i].ml_name = String_val(Field(item,0));
1447 #ifdef PYTHON24
1448 methods[i].ml_meth = pywrap_closure(Field(item,1));
1449 #else
1450 methods[i].ml_meth = (void *)(uintptr_t)pywrap_closure(Field(item,1));
1451 #endif
1452 methods[i].ml_flags = Int_val(Field(item,2));
1453 methods[i].ml_doc = String_val(Field(item,3));
1454 }
1455
1456 Py_InitModule( String_val(name), methods );
1457
1458 free( methods );
1459
1460 CAMLreturn(Val_unit);
1461 }
1462
1463 value pycaml_setargs(value argv) {
1464 CAMLparam1(argv);
1465 char* cargv[1];
1466
1467 cargv[0] = String_val(argv);
1468
1469 PySys_SetArgv(1, cargv);
1470
1471 CAMLreturn0;
1472 }
1473
1474 value pytrue( value unit ) {
1475 CAMLparam1(unit);
1476 CAMLreturn(pywrap(Py_True));
1477 }
1478
1479 value pyfalse(value unit) {
1480 CAMLparam1(unit);
1481 CAMLreturn(pywrap(Py_False));
1482 }