3 * This software is covered under the GNU lesser general public license
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"
17 static void *getcustom( value v
) { return *((void **)Data_custom_val(v
)); }
19 static void pydecref( value v
) {
20 if( getcustom(v
) ) { Py_DECREF((PyObject
*)getcustom(v
)); }
23 static int pycompare( value v1
, value v2
) {
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
);
33 static long pyhash( value v
) {
34 if( getcustom(v
) ) return PyObject_Hash((PyObject
*)getcustom(v
));
38 static unsigned long pydeserialize( void *dst
) {
42 struct custom_operations pyops
= {
47 custom_serialize_default
,
51 struct custom_operations fnops
= {
60 value
pywrap( PyObject
*obj
) {
65 v
= alloc_custom( &pyops
, sizeof( PyObject
* ), 100, 100000 );
66 *((PyObject
**)Data_custom_val(v
)) = obj
;
70 value
funcwrap( void *obj
) {
73 v
= alloc_custom( &fnops
, sizeof( void * ), 100, 100000 );
74 *((void **)Data_custom_val(v
)) = obj
;
78 PyObject
*pyunwrap( value v
) {
79 return *((PyObject
**)Data_custom_val(v
));
82 static void camldestr( void *v
) {
83 value
*valptr
= (value
*)v
;
84 remove_global_root(valptr
);
88 PyObject
*camlwrap( value val
, void *aux_str
, int size
) {
89 value
*v
= (value
*)malloc(sizeof(value
) + size
);
91 memcpy((void *)v
+sizeof(value
),aux_str
,size
);
92 register_global_root(v
);
93 return PyCObject_FromVoidPtr(v
,camldestr
);
96 void *caml_aux( PyObject
*obj
) {
97 value
*v
= (value
*)PyCObject_AsVoidPtr( obj
);
98 return (void *)v
+sizeof(value
);
101 PyObject
*pycall_callback( PyObject
*obj
, PyObject
*args
) {
105 if( !PyCObject_Check(obj
) ) {
109 v
= (value
*)PyCObject_AsVoidPtr( obj
);
110 out
= callback(*v
,pywrap(args
));
111 return pyunwrap(out
);
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
);
169 value
pygencall( value format
, value arg
) {
170 CAMLparam2(format
,arg
);
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));
187 CAMLreturn(Val_unit
);
190 ((type_2
)func
)(Int_val(arg
));
191 CAMLreturn(Val_unit
);
194 ((type_3
)func
)(String_val(arg
));
195 CAMLreturn(Val_unit
);
198 CAMLreturn(Int_val(((type_4
)func
)()));
201 CAMLreturn(Int_val(((type_5
)func
)
208 fd
= dup(Int_val(Field(arg
,0)));
212 rv
= Val_int(((type_6
)func
)
213 (f
,String_val(Field(arg
,1))));
217 rv
= Val_int(((type_7
)func
)
219 String_val(Field(arg
,1)),
220 Int_val(Field(arg
,2))));
224 rv
= pywrap(((type_10
)func
)
226 String_val(Field(arg
,1)),
227 Int_val(Field(arg
,2)),
228 pyunwrap(Field(arg
,3)),
229 pyunwrap(Field(arg
,4))));
233 rv
= pywrap(((type_11
)func
)
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))));
247 CAMLreturn(copy_string(((type_8
)func
)()));
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)))));
257 CAMLreturn(pywrap(((type_12
)func
)
258 (String_val(Field(arg
,0)),
259 String_val(Field(arg
,1)),
260 Int_val(Field(arg
,2)))));
263 fd
= dup(Int_val(Field(arg
,1)));
265 rv
= Val_int(((type_13
)func
)
266 (pyunwrap(Field(arg
,0)),
268 Int_val(Field(arg
,2))));
273 CAMLreturn(pywrap(((type_14
)func
)(pyunwrap(arg
))));
276 CAMLreturn(pywrap(((type_15
)func
)
277 (pyunwrap(Field(arg
,0)),
278 pyunwrap(Field(arg
,1)),Int_val(Field(arg
,2)))));
281 CAMLreturn(pywrap(((type_16
)func
)
282 (pyunwrap(Field(arg
,0)),
283 String_val(Field(arg
,1)))));
286 CAMLreturn(pywrap(((type_17
)func
)
287 (pyunwrap(Field(arg
,0)),pyunwrap(Field(arg
,1)))));
290 CAMLreturn(Val_int(((type_18
)func
)(pyunwrap(arg
))));
293 CAMLreturn(Val_int(((type_19
)func
)
294 (pyunwrap(Field(arg
,0)),
295 pyunwrap(Field(arg
,1)))));
298 CAMLreturn(Val_int(((type_20
)func
)
299 (pyunwrap(Field(arg
,0)),
300 pyunwrap(Field(arg
,1)),
301 Int_val(Field(arg
,2)))));
304 CAMLreturn(Val_int(((type_21
)func
)
305 (pyunwrap(Field(arg
,0)),
306 String_val(Field(arg
,1)),
307 pyunwrap(Field(arg
,2)))));
310 CAMLreturn(Val_int(((type_22
)func
)
311 (pyunwrap(Field(arg
,0)),
312 String_val(Field(arg
,1)))));
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);
320 Field(rv
,0) = pywrap(ob1
);
321 Field(rv
,1) = pywrap(ob2
);
326 CAMLreturn(Int_val(((type_24
)func
)
327 (pyunwrap(Field(arg
,0)),
328 pyunwrap(Field(arg
,1)),
329 pyunwrap(Field(arg
,2)))));
332 CAMLreturn(copy_int64(((type_25
)func
)(pyunwrap(arg
))));
335 CAMLreturn(copy_string(((type_26
)func
)(pyunwrap(arg
))));
338 ob1
= pyunwrap(Field(arg
,0));
339 ((type_27
)func
)(&ob1
,pyunwrap(Field(arg
,1)));
340 CAMLreturn(pywrap(ob1
));
343 CAMLreturn(pywrap(((type_28
)func
)(String_val(arg
))));
346 CAMLreturn(pywrap(((type_29
)func
)()));
349 ((type_30
)func
)(pyunwrap(arg
));
350 CAMLreturn(Val_unit
);
353 x
= Int_val(Field(arg
,1));
354 ret_int
= ((type_31
)func
)
355 (pyunwrap(Field(arg
,0)),
358 if( !ret_int
) CAMLreturn((value
)1);
361 Field(rv
,0) = pywrap(ob1
);
362 Field(rv
,1) = pywrap(ob2
);
363 Field(rv
,2) = Val_int(x
);
367 /* case 32: string -> int */
369 /* case 33: unicode ... need to do something coherent */
372 CAMLreturn(pywrap(((type_34
)func
)(Int64_val(arg
))));
375 CAMLreturn(copy_int64(((type_35
)func
)()));
378 CAMLreturn(pywrap(((type_36
)func
)(Double_val(arg
))));
381 CAMLreturn(copy_double(((type_37
)func
)
384 /* case 38: string -> float */
387 CAMLreturn(pywrap(((type_39
)func
)(Int_val(arg
))));
390 CAMLreturn(pywrap(((type_40
)func
)
391 (pyunwrap(Field(arg
,0)),
392 Int_val(Field(arg
,1)))));
395 CAMLreturn(Val_int(((type_41
)func
)
396 (pyunwrap(Field(arg
,0)),
397 Int_val(Field(arg
,1)),
398 pyunwrap(Field(arg
,2)))));
401 CAMLreturn(pywrap(((type_42
)func
)
402 (pyunwrap(Field(arg
,0)),
403 pyunwrap(Field(arg
,1)),
404 pyunwrap(Field(arg
,2)))));
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);
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)))));
432 (pyunwrap(Field(arg
,0)),
433 pyunwrap(Field(arg
,1)));
434 CAMLreturn(Val_unit
);
438 (pyunwrap(Field(arg
,0)),String_val(Field(arg
,1)));
439 CAMLreturn(Val_unit
);
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
);
447 Field(rv
,0) = pywrap(ob1
);
448 Field(rv
,1) = pywrap(ob2
);
449 Field(rv
,2) = pywrap(ob3
);
454 (pyunwrap(Field(arg
,0)),
455 pyunwrap(Field(arg
,1)),
456 pyunwrap(Field(arg
,2)));
457 CAMLreturn(Val_unit
);
460 CAMLreturn(pywrap(((type_49
)func
)
461 (pyunwrap(Field(arg
,0)),
462 String_val(Field(arg
,1)))));
465 CAMLreturn(pywrap(((type_50
)func
)
466 (String_val(Field(arg
,0)),
467 pyunwrap(Field(arg
,1)),
468 String_val(Field(arg
,2)))));
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)))));
478 ((type_52
)func
)(pyunwrap(arg
),&rvs
,&ret_int
);
479 rv
= copy_string(rvs
);
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)))));
490 CAMLreturn(Val_int(((type_54
)func
)
491 (pyunwrap(Field(arg
,0)),
492 Int_val(Field(arg
,1)),
493 Int_val(Field(arg
,2)))));
499 #ifdef DONT_COMPILE_THIS
501 DL_IMPORT(void) Py_Initialize(void);
502 DL_IMPORT(void) Py_Finalize(void);
503 DL_IMPORT(void) PyErr_Print(void);
505 DL_IMPORT(void) Py_Exit(int);
506 DL_IMPORT(void) PyErr_PrintEx(int);
508 DL_IMPORT(void) Py_SetProgramName(char *);
509 DL_IMPORT(void) Py_SetPythonHome(char *);
511 DL_IMPORT(int) Py_IsInitialized(void);
513 DL_IMPORT(int) PyRun_SimpleString(char *);
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 *);
521 DL_IMPORT(int) PyRun_AnyFileEx(FILE *, char *, int);
522 DL_IMPORT(int) PyRun_SimpleFileEx(FILE *, char *, int);
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);
536 DL_IMPORT(PyObject
*) PyRun_String(char*, int, PyObject
*, PyObject
*);
538 DL_IMPORT(PyObject
*) PyRun_File(FILE *, char*, int, PyObject
*, PyObject
*);
540 DL_IMPORT(PyObject
*) PyRun_FileEx(FILE *, char*, int, PyObject
*, PyObject
*, int );
542 DL_IMPORT(PyObject
*) Py_CompileString(char*, char*, int); /* FUNCTION 30 */
544 /* Generic operations on objects */
546 extern DL_IMPORT(int) PyObject_Print(PyObject
*, FILE *, int);
548 extern DL_IMPORT(PyObject
*) PyObject_Repr(PyObject
*);
549 extern DL_IMPORT(PyObject
*) PyObject_Str(PyObject
*);
550 extern DL_IMPORT(PyObject
*) PyObject_Unicode(PyObject
*);
552 extern DL_IMPORT(PyObject
*) PyObject_RichCompare(PyObject
*, PyObject
*, int);
554 extern DL_IMPORT(PyObject
*) PyObject_GetAttrString(PyObject
*, char *);
556 extern DL_IMPORT(PyObject
*) PyObject_GetAttr(PyObject
*, PyObject
*);
557 extern DL_IMPORT(PyObject
*) PyObject_CallObject(PyObject
*, PyObject
*);
559 extern DL_IMPORT(int) PyObject_IsTrue(PyObject
*);
560 extern DL_IMPORT(int) PyObject_Not(PyObject
*);
561 extern DL_IMPORT(int) PyCallable_Check(PyObject
*);
563 extern DL_IMPORT(int) PyObject_Compare(PyObject
*, PyObject
*);
564 extern DL_IMPORT(int) PyObject_HasAttr(PyObject
*, PyObject
*);
566 extern DL_IMPORT(int) PyObject_RichCompareBool(PyObject
*, PyObject
*, int);
568 extern DL_IMPORT(int) PyObject_SetAttrString(PyObject
*, char *, PyObject
*);
570 extern DL_IMPORT(int) PyObject_HasAttrString(PyObject
*, char *);
572 extern DL_IMPORT(int) PyNumber_Coerce(PyObject
**, PyObject
**);
573 extern DL_IMPORT(int) PyNumber_CoerceEx(PyObject
**, PyObject
**);
575 extern DL_IMPORT(int) PyObject_SetAttr(PyObject
*, PyObject
*, PyObject
*);
577 extern DL_IMPORT(long) PyObject_Hash(PyObject
*);
581 extern DL_IMPORT(int) PyString_Size(PyObject
*);
583 extern DL_IMPORT(char *) PyString_AsString(PyObject
*);
585 extern DL_IMPORT(void) PyString_Concat(PyObject
**, PyObject
*);
586 extern DL_IMPORT(void) PyString_ConcatAndDel(PyObject
**, PyObject
*);
588 extern DL_IMPORT(PyObject
*) PyString_FromString(const char *);
590 extern DL_IMPORT(PyObject
*) PyString_Format(PyObject
*, PyObject
*);
594 extern DL_IMPORT(PyObject
*) PyDict_New(void);
596 extern DL_IMPORT(PyObject
*) PyDict_GetItem(PyObject
*mp
, PyObject
*key
);
598 extern DL_IMPORT(int) PyDict_SetItem(PyObject
*mp
, PyObject
*key
, PyObject
*item
);
600 extern DL_IMPORT(int) PyDict_DelItem(PyObject
*mp
, PyObject
*key
);
602 extern DL_IMPORT(void) PyDict_Clear(PyObject
*mp
);
604 extern DL_IMPORT(int) PyDict_Next
605 (PyObject
*mp
, int *pos
, PyObject
**key
, PyObject
**value
);
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
);
612 extern DL_IMPORT(int) PyDict_Size(PyObject
*mp
);
614 extern DL_IMPORT(PyObject
*) PyDict_GetItemString(PyObject
*dp
, char *key
);
616 extern DL_IMPORT(int) PyDict_DelItemString(PyObject
*dp
, char *key
);
618 extern DL_IMPORT(int) PyDict_SetItemString(PyObject
*dp
, char *key
, PyObject
*item
);
622 extern DL_IMPORT(PyObject
*) PyInt_FromString(char*, char**, int);
624 extern DL_IMPORT(PyObject
*) PyInt_FromUnicode(Py_UNICODE
*, int, int);
626 extern DL_IMPORT(PyObject
*) PyInt_FromLong(long);
628 extern DL_IMPORT(long) PyInt_AsLong(PyObject
*);
630 extern DL_IMPORT(long) PyInt_GetMax(void);
633 extern DL_IMPORT(PyObject
*) PyLong_FromLong(long);
635 extern DL_IMPORT(PyObject
*) PyLong_FromDouble(double);
637 extern DL_IMPORT(long) PyLong_AsLong(PyObject
*);
641 extern DL_IMPORT(PyObject
*) PyFloat_FromDouble(double);
643 extern DL_IMPORT(double) PyFloat_AsDouble(PyObject
*);
647 extern DL_IMPORT(PyObject
*) PyModule_New(char *);
649 extern DL_IMPORT(PyObject
*) PyModule_GetDict(PyObject
*);
651 extern DL_IMPORT(char *) PyModule_GetName(PyObject
*);
652 extern DL_IMPORT(char *) PyModule_GetFilename(PyObject
*);
655 extern DL_IMPORT(PyObject
*) PyTuple_New(int size
);
657 extern DL_IMPORT(int) PyTuple_Size(PyObject
*);
658 extern DL_IMPORT(int) PyTuple_Check(PyObject
*);
660 extern DL_IMPORT(PyObject
*) PyTuple_GetItem(PyObject
*, int);
662 extern DL_IMPORT(int) PyTuple_SetItem(PyObject
*, int, PyObject
*);
664 extern DL_IMPORT(PyObject
*) PyTuple_GetSlice(PyObject
*, int, int);
667 DL_IMPORT(PyObject
*) PySlice_New(PyObject
* start
, PyObject
* stop
, PyObject
* step
);
669 DL_IMPORT(int) PySlice_GetIndices(PySliceObject
*r
, int length
, int *start
, int *stop
, int *step
);
671 DL_IMPORT(PyObject
*) PyRange_New(long, long, long, int);
673 /* Error handling definitions */
676 DL_IMPORT(void) PyErr_SetNone(PyObject
*);
678 DL_IMPORT(void) PyErr_SetObject(PyObject
*, PyObject
*);
680 DL_IMPORT(void) PyErr_SetString(PyObject
*, const char *);
682 DL_IMPORT(PyObject
*) PyErr_Occurred(void);
684 DL_IMPORT(void) PyErr_Clear(void);
686 DL_IMPORT(void) PyErr_Fetch(PyObject
**, PyObject
**, PyObject
**);
688 DL_IMPORT(void) PyErr_Restore(PyObject
*, PyObject
*, PyObject
*);
690 /* Error testing and normalization */
692 DL_IMPORT(int) PyErr_GivenExceptionMatches(PyObject
*, PyObject
*);
694 DL_IMPORT(int) PyErr_ExceptionMatches(PyObject
*);
696 DL_IMPORT(void) PyErr_NormalizeException(PyObject
**, PyObject
**, PyObject
**);
700 extern DL_IMPORT(PyObject
*) PyClass_New(PyObject
*, PyObject
*, PyObject
*);
702 extern DL_IMPORT(PyObject
*) PyInstance_New(PyObject
*, PyObject
*,
705 extern DL_IMPORT(PyObject
*) PyInstance_NewRaw(PyObject
*, PyObject
*);
707 extern DL_IMPORT(PyObject
*) PyMethod_New(PyObject
*, PyObject
*, PyObject
*);
709 extern DL_IMPORT(PyObject
*) PyMethod_Function(PyObject
*);
710 extern DL_IMPORT(PyObject
*) PyMethod_Self(PyObject
*);
711 extern DL_IMPORT(PyObject
*) PyMethod_Class(PyObject
*);
715 extern DL_IMPORT(PyObject
*) PyModule_New(char *);
717 extern DL_IMPORT(PyObject
*) PyModule_GetDict(PyObject
*);
719 extern DL_IMPORT(char *) PyModule_GetName(PyObject
*);
720 extern DL_IMPORT(char *) PyModule_GetFilename(PyObject
*);
722 DL_IMPORT(long) PyImport_GetMagicNumber(void);
724 DL_IMPORT(PyObject
*) PyImport_ExecCodeModule(char *name
, PyObject
*co
);
726 DL_IMPORT(PyObject
*) PyImport_ExecCodeModuleEx(char *name
, PyObject
*co
, char *pathname
);
728 DL_IMPORT(PyObject
*) PyImport_GetModuleDict(void);
730 DL_IMPORT(PyObject
*) PyImport_AddModule(char *name
);
731 DL_IMPORT(PyObject
*) PyImport_ImportModule(char *name
);
733 DL_IMPORT(PyObject
*) PyImport_ImportModuleEx(char *name
, PyObject
*globals
, PyObject
*locals
, PyObject
*fromlist
);
735 DL_IMPORT(PyObject
*) PyImport_Import(PyObject
*name
);
737 DL_IMPORT(PyObject
*) PyImport_ReloadModule(PyObject
*m
);
739 DL_IMPORT(void) PyImport_Cleanup(void);
741 DL_IMPORT(int) PyImport_ImportFrozenModule(char *);
743 /* Interface to random parts in ceval.c */
745 DL_IMPORT(PyObject
*) PyEval_CallObjectWithKeywords(PyObject
*, PyObject
*, PyObject
*);
748 // dropped in Python 2.7
749 DL_IMPORT(PyObject
*) PyEval_CallObject(PyObject
*, PyObject
*);
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);
758 DL_IMPORT(int) PyEval_GetRestricted(void);
762 DL_IMPORT(PyObject
*) PyObject_Type(PyObject
*o
);
764 DL_IMPORT(int) PyObject_Size(PyObject
*o
);
766 DL_IMPORT(PyObject
*) PyObject_GetItem(PyObject
*o
, PyObject
*key
);
768 DL_IMPORT(int) PyObject_SetItem(PyObject
*o
, PyObject
*key
, PyObject
*v
);
770 DL_IMPORT(int) PyObject_DelItem(PyObject
*o
, PyObject
*key
);
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
);
776 DL_IMPORT(int) PyNumber_Check(PyObject
*o
);
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
);
785 DL_IMPORT(PyObject
*) PyNumber_Power(PyObject
*o1
, PyObject
*o2
,PyObject
*o3
);
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
);
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
);
798 DL_IMPORT(PyObject
*) PyNumber_Int(PyObject
*o
);
799 DL_IMPORT(PyObject
*) PyNumber_Long(PyObject
*o
);
800 DL_IMPORT(PyObject
*) PyNumber_Float(PyObject
*o
);
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
);
813 DL_IMPORT(PyObject
*) PyNumber_InPlacePower(PyObject
*o1
, PyObject
*o2
,PyObject
*o3
);
815 DL_IMPORT(int) PySequence_Check(PyObject
*o
);
816 DL_IMPORT(int) PySequence_Size(PyObject
*o
);
817 DL_IMPORT(int) PySequence_Length(PyObject
*o
);
819 DL_IMPORT(PyObject
*) PySequence_Concat(PyObject
*o1
, PyObject
*o2
);
821 DL_IMPORT(PyObject
*) PySequence_Repeat(PyObject
*o
, int count
);
822 DL_IMPORT(PyObject
*) PySequence_GetItem(PyObject
*o
, int i
);
824 DL_IMPORT(PyObject
*) PySequence_GetSlice(PyObject
*o
, int i1
, int i2
);
826 DL_IMPORT(int) PySequence_SetItem(PyObject
*o
, int i
, PyObject
*v
);
828 DL_IMPORT(int) PySequence_DelItem(PyObject
*o
, int i
);
830 DL_IMPORT(int) PySequence_SetSlice(PyObject
*o
, int i1
, int i2
, PyObject
*v
);
832 DL_IMPORT(int) PySequence_DelSlice(PyObject
*o
, int i1
, int i2
);
834 DL_IMPORT(PyObject
*) PySequence_Tuple(PyObject
*o
);
835 DL_IMPORT(PyObject
*) PySequence_List(PyObject
*o
);
837 DL_IMPORT(PyObject
*) PySequence_Fast(PyObject
*o
, const char* m
);
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
);
844 DL_IMPORT(PyObject
*) PySequence_InPlaceConcat(PyObject
*o1
, PyObject
*o2
);
846 DL_IMPORT(PyObject
*) PySequence_InPlaceRepeat(PyObject
*o
, int count
);
848 DL_IMPORT(int) PyMapping_Check(PyObject
*o
);
849 DL_IMPORT(int) PyMapping_Size(PyObject
*o
);
850 DL_IMPORT(int) PyMapping_Length(PyObject
*o
);
852 DL_IMPORT(int) PyMapping_HasKeyString(PyObject
*o
, char *key
);
854 DL_IMPORT(int) PyMapping_HasKey(PyObject
*o
, PyObject
*key
);
856 DL_IMPORT(PyObject
*) PyMapping_GetItemString(PyObject
*o
, char *key
);
858 DL_IMPORT(int) PyMapping_SetItemString(PyObject
*o
, char *key
, PyObject
*value
);
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*);
889 #endif//DONT_COMPILE_THIS
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
); }
898 #define PyRun_AnyFile PyRun_AnyFile_redo
899 int PyRun_AnyFile_redo(FILE* fp
, const char* filename
) { return PyRun_AnyFileExFlags(fp
, filename
, 0, NULL
); }
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
); }
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
); }
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
); }
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
); }
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
); }
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
); }
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
); }
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
); }
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
); }
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 */
943 int PyTuple_Check(PyObject
* op
) { return PyObject_TypeCheck(op
, &PyTuple_Type
); }
946 /* Value -> Pyobject */
948 value
pywrapvalue( value cb
) {
950 CAMLreturn(pywrap(camlwrap(cb
,NULL
,0)));
953 value
pyunwrapvalue( value cb
) {
956 v
= (value
*)PyCObject_AsVoidPtr( pyunwrap(cb
) );
960 /* Create the function table */
962 typedef struct _python_func_table
{
968 python_func_table the_python_func_table
[] = {
970 { (void *)Py_Initialize
, 1, "Py_Initialize" },
971 { (void *)Py_Finalize
, 1, "Py_Finalize" },
972 { (void *)PyErr_Print
, 1, "PyErr_Print" },
974 { (void *)Py_Exit
, 2, "Py_Exit" },
975 { (void *)PyErr_PrintEx
, 2, "PyErr_PrintEx" },
977 { (void *)Py_SetProgramName
, 3, "Py_SetProgramName" },
978 { (void *)Py_SetPythonHome
, 3, "Py_SetPythonHome" },
980 { (void *)Py_IsInitialized
, 4, "Py_IsInitialized" },
982 { (void *)PyRun_SimpleString
, 5, "PyRun_SimpleString" },
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" },
990 { (void *)PyRun_AnyFileEx
, 7, "PyRun_AnyFileEx" },
991 { (void *)PyRun_SimpleFileEx
, 7, "PyRun_SimpleFileEx" },
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" },
1005 { (void *)PyRun_String
, 9, "PyRun_String" },
1007 { (void *)PyRun_File
, 10, "PyRun_File" },
1009 { (void *)PyRun_FileEx
, 11, "PyRun_FileEx" },
1011 { (void *)Py_CompileString
, 12, "Py_CompileString" },
1015 { (void *)PyObject_Print
, 13, "PyObject_Print" },
1017 { (void *)PyObject_Repr
, 14, "PyObject_Repr" },
1018 { (void *)PyObject_Str
, 14, "PyObject_Str" },
1019 { (void *)PyObject_Unicode
, 14, "PyObject_Unicode" },
1021 { (void *)PyObject_RichCompare
, 15, "PyObject_RichCompare" },
1023 { (void *)PyObject_GetAttrString
, 16, "PyObject_GetAttrString" },
1025 { (void *)PyObject_GetAttr
, 17, "PyObject_GetAttr" },
1026 { (void *)PyObject_CallObject
, 17, "PyObject_CallObject" },
1028 { (void *)PyObject_IsTrue
, 18, "PyObject_IsTrue" },
1029 { (void *)PyObject_Not
, 18, "PyObject_Not" },
1030 { (void *)PyCallable_Check
, 18, "PyCallable_Check" },
1032 { (void *)PyObject_Compare
, 19, "PyObject_Compare" },
1033 { (void *)PyObject_HasAttr
, 19, "PyObject_HasAttr" },
1035 { (void *)PyObject_RichCompareBool
, 20, "PyObject_RichCompareBool" },
1037 { (void *)PyObject_SetAttrString
, 21, "PyObject_SetAttrString" },
1039 { (void *)PyObject_HasAttrString
, 22, "PyObject_HasAttrString" },
1041 { (void *)PyNumber_Coerce
, 23, "PyNumber_Coerce" },
1042 { (void *)PyNumber_CoerceEx
, 23, "PyNumber_CoerceEx" },
1044 { (void *)PyObject_SetAttr
, 24, "PyObject_SetAttr" },
1046 { (void *)PyObject_Hash
, 25, "PyObject_Hash" },
1050 { (void *)PyString_Size
, 18, "PyString_Size" },
1052 { (void *)PyString_AsString
, 26, "PyString_AsString" },
1054 { (void *)PyString_Concat
, 27, "PyString_Concat" },
1055 { (void *)PyString_ConcatAndDel
, 27, "PyString_ConcatAndDel" },
1057 { (void *)PyString_FromString
, 28, "PyString_FromString" },
1059 { (void *)PyString_Format
, 17, "PyString_Format" },
1063 { (void *)PyDict_New
, 29, "PyDict_New" },
1065 { (void *)PyDict_GetItem
, 17, "PyDict_GetItem" },
1067 { (void *)PyDict_SetItem
, 24, "PyDict_SetItem" },
1069 { (void *)PyDict_DelItem
, 19, "PyDict_DelItem" },
1071 { (void *)PyDict_Clear
, 30, "PyDict_Clear" },
1073 { (void *)PyDict_Next
, 31, "PyDict_Next" },
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" },
1080 { (void *)PyDict_Size
, 18, "PyDict_Size" },
1082 { (void *)PyDict_GetItemString
, 16, "PyDict_GetItemString" },
1084 { (void *)PyDict_DelItemString
, 22, "PyDict_DelItemString" },
1086 { (void *)PyDict_SetItemString
, 21, "PyDict_SetItemString" },
1090 { (void *)PyInt_FromLong
, 34, "PyInt_FromLong" },
1092 { (void *)PyInt_AsLong
, 25, "PyInt_AsLong" },
1094 { (void *)PyInt_GetMax
, 35, "PyInt_GetMax" },
1098 { (void *)PyFloat_FromDouble
, 36, "PyFloat_FromDouble" },
1100 { (void *)PyFloat_AsDouble
, 37, "PyFloat_AsDouble" },
1104 { (void *)PyModule_New
, 28, "PyModule_New" },
1106 { (void *)PyModule_GetDict
, 14, "PyModule_GetDict" },
1108 { (void *)PyModule_GetName
, 26, "PyModule_GetName" },
1109 { (void *)PyModule_GetFilename
, 26, "PyModule_GetFilename" },
1112 { (void *)PyTuple_New
, 39, "PyTuple_New" },
1114 { (void *)PyTuple_Size
, 18, "PyTuple_Size" },
1115 { (void *)PyTuple_Check
, 18, "PyTuple_Check" },
1117 { (void *)PyTuple_GetItem
, 40, "PyTuple_GetItem" },
1119 { (void *)PyTuple_SetItem
, 41, "PyTuple_SetItem" },
1121 { (void *)PyTuple_GetSlice
, 13, "PyTuple_GetSlice" },
1124 { (void *)PySlice_New
, 42, "PySlice_New" },
1126 { (void *)PySlice_GetIndices
, 43, "PySlice_GetIndices" },
1128 { (void *)PyRange_New
, 44, "PyRange_New" },
1130 /* Error handling definitions */
1133 { (void *)PyErr_SetNone
, 30, "PyErr_SetNone" },
1135 { (void *)PyErr_SetObject
, 45, "PyErr_SetObject" },
1137 { (void *)PyErr_SetString
, 46, "PyErr_SetString" },
1139 { (void *)PyErr_Occurred
, 29, "PyErr_Occurred" },
1141 { (void *)PyErr_Clear
, 1, "PyErr_Clear" },
1143 { (void *)PyErr_Fetch
, 47, "PyErr_Fetch" },
1145 { (void *)PyErr_Restore
, 48, "PyErr_Restore" },
1147 /* Error testing and normalization */
1149 { (void *)PyErr_GivenExceptionMatches
, 19, "PyErr_GivenExceptionMatches" },
1151 { (void *)PyErr_ExceptionMatches
, 18, "PyErr_ExceptionMatches" },
1153 { (void *)PyErr_NormalizeException
, 47, "PyErr_NormalizeException" },
1157 { (void *)PyClass_New
, 42, "PyClass_New" },
1159 { (void *)PyInstance_New
, 42, "PyInstance_New" },
1162 { (void *)PyInstance_NewRaw
, 17, "PyInstance_NewRaw" },
1164 { (void *)PyMethod_New
, 42, "PyMethod_New" },
1166 { (void *)PyMethod_Function
, 14, "PyMethod_Function" },
1167 { (void *)PyMethod_Self
, 14, "PyMethod_Self" },
1168 { (void *)PyMethod_Class
, 14, "PyMethod_Class" },
1172 { (void *)PyModule_New
, 28, "PyModule_New" },
1174 { (void *)PyModule_GetDict
, 14, "PyModule_GetDict" },
1176 { (void *)PyModule_GetName
, 26, "PyModule_GetName" },
1177 { (void *)PyModule_GetFilename
, 26, "PyModule_GetFilename" },
1179 { (void *)PyImport_GetMagicNumber
, 35, "PyImport_GetMagicNumber" },
1181 { (void *)PyImport_ExecCodeModule
, 49, "PyImport_ExecCodeModule" },
1183 { (void *)PyImport_ExecCodeModuleEx
, 50, "PyImport_ExecCodeModuleEx" },
1185 { (void *)PyImport_GetModuleDict
, 29, "PyImport_GetModuleDict" },
1187 { (void *)PyImport_AddModule
, 28, "PyImport_AddModule" },
1188 { (void *)PyImport_ImportModule
, 28, "PyImport_ImportModule" },
1191 /* In Python 2.6, this because a #define so we cannot take
1192 * the address of the function. - RWMJ.
1194 { (void *)PyImport_ImportModuleEx
, 51, "PyImport_ImportModuleEx" },
1197 { (void *)PyImport_Import
, 28, "PyImport_Import" },
1199 { (void *)PyImport_ReloadModule
, 14, "PyImport_ReloadModule" },
1201 { (void *)PyImport_Cleanup
, 1, "PyImport_Cleanup" },
1203 { (void *)PyImport_ImportFrozenModule
, 5, "PyImport_ImportFrozenModule" },
1205 /* Interface to random parts in ceval.c */
1207 { (void *)PyEval_CallObjectWithKeywords
, 42, "PyEval_CallObjectWithKeywords" },
1210 // dropped in Python 2.7
1211 { (void *)PyEval_CallObject
, 17, "PyEval_CallObject" },
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" },
1220 { (void *)PyEval_GetRestricted
, 4, "PyEval_GetRestricted" },
1222 /* Abstract layer */
1224 { (void *)PyObject_Type
, 14, "PyObject_Type" },
1226 { (void *)PyObject_Size
, 18, "PyObject_Size" },
1228 { (void *)PyObject_GetItem
, 17, "PyObject_GetItem" },
1230 { (void *)PyObject_SetItem
, 24, "PyObject_SetItem" },
1232 { (void *)PyObject_DelItem
, 17, "PyObject_DelItem" },
1234 { (void *)PyObject_AsCharBuffer
, 52, "PyObject_AsCharBuffer" },
1235 { (void *)PyObject_AsReadBuffer
, 52, "PyObject_AsReadBuffer" },
1236 { (void *)PyObject_AsWriteBuffer
, 52, "PyObject_AsWriteBuffer" },
1238 { (void *)PyNumber_Check
, 18, "PyNumber_Check" },
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" },
1247 { (void *)PyNumber_Power
, 42, "PyNumber_Power" },
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" },
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" },
1260 { (void *)PyNumber_Int
, 14, "PyNumber_Int" },
1261 { (void *)PyNumber_Long
, 14, "PyNumber_Long" },
1262 { (void *)PyNumber_Float
, 14, "PyNumber_Float" },
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" },
1275 { (void *)PyNumber_InPlacePower
, 42, "PyNumber_InPlacePower" },
1277 { (void *)PySequence_Check
, 18, "PySequence_Check" },
1278 { (void *)PySequence_Size
, 18, "PySequence_Size" },
1279 { (void *)PySequence_Length
, 18, "PySequence_Length" },
1281 { (void *)PySequence_Concat
, 17, "PySequence_Concat" },
1283 { (void *)PySequence_Repeat
, 40, "PySequence_Repeat" },
1284 { (void *)PySequence_GetItem
, 40, "PySequence_GetItem" },
1286 { (void *)PySequence_GetSlice
, 13, "PySequence_GetSlice" },
1288 { (void *)PySequence_SetItem
, 41, "PySequence_SetItem" },
1290 { (void *)PySequence_DelItem
, 20, "PySequence_DelItem" },
1292 { (void *)PySequence_SetSlice
, 53, "PySequence_SetSlice" },
1294 { (void *)PySequence_DelSlice
, 54, "PySequence_DelSlice" },
1296 { (void *)PySequence_Tuple
, 14, "PySequence_Tuple" },
1297 { (void *)PySequence_List
, 14, "PySequence_List" },
1299 { (void *)PySequence_Fast
, 16, "PySequence_Fast" },
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" },
1306 { (void *)PySequence_InPlaceConcat
, 17, "PySequence_InPlaceConcat" },
1308 { (void *)PySequence_InPlaceRepeat
, 22, "PySequence_InPlaceRepeat" },
1310 { (void *)PyMapping_Check
, 18, "PyMapping_Check" },
1311 { (void *)PyMapping_Size
, 18, "PyMapping_Size" },
1312 { (void *)PyMapping_Length
, 18, "PyMapping_Length" },
1314 { (void *)PyMapping_HasKeyString
, 16, "PyMapping_HasKeyString" },
1316 { (void *)PyMapping_HasKey
, 19, "PyMapping_HasKey" },
1318 { (void *)PyMapping_GetItemString
, 16, "PyMapping_GetItemString" },
1320 { (void *)PyMapping_SetItemString
, 41, "PyMapping_SetItemString" },
1326 value
pygetfuncarray( value unit
) {
1328 CAMLlocal2(retv
,tuplev
);
1332 for( i
= 0; the_python_func_table
[i
].func
; i
++ ) ; total_funcs
= i
;
1334 retv
= alloc(total_funcs
,0);
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
);
1347 enum PycamlTypeLabels
{
1363 value
pytype( value 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
));
1381 value
pynull( value unit
) {
1383 CAMLreturn(pywrap(NULL
));
1386 value
pynone( value unit
) {
1388 CAMLreturn(pywrap(Py_None
));
1391 value
pytuple_fromarray( value array
) {
1393 PyObject
*tuple
= PyTuple_New(Wosize_val(array
));
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
);
1404 CAMLreturn(pywrap(tuple
));
1407 value
pytuple_toarray( value array
) {
1409 PyObject
*obj
= pyunwrap(array
);
1413 rv
= alloc_tuple( PySequence_Size(obj
) );
1415 for( i
= 0; i
< PySequence_Size(obj
); i
++ )
1416 Store_field(rv
,i
,pywrap(PySequence_GetItem(obj
,i
)));
1421 value
pywrap_closure( value closure
) {
1422 CAMLparam1(closure
);
1425 PyMethodDef
*ml_def
;
1426 ml
.ml_name
= "anonymous_closure";
1427 ml
.ml_meth
= pycall_callback
;
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
)));
1435 value
pymodule_initmodule( value name
, value funclist
) {
1436 CAMLparam2(name
,funclist
);
1438 PyMethodDef
*methods
= malloc( sizeof( PyMethodDef
) *
1439 Wosize_val(funclist
) );
1442 PyImport_AddModule( String_val(name
) );
1444 for( i
= 0; i
< Wosize_val(funclist
); i
++ ) {
1445 item
= Field(funclist
,i
);
1446 methods
[i
].ml_name
= String_val(Field(item
,0));
1448 methods
[i
].ml_meth
= pywrap_closure(Field(item
,1));
1450 methods
[i
].ml_meth
= (void *)(uintptr_t)pywrap_closure(Field(item
,1));
1452 methods
[i
].ml_flags
= Int_val(Field(item
,2));
1453 methods
[i
].ml_doc
= String_val(Field(item
,3));
1456 Py_InitModule( String_val(name
), methods
);
1460 CAMLreturn(Val_unit
);
1463 value
pycaml_setargs(value argv
) {
1467 cargv
[0] = String_val(argv
);
1469 PySys_SetArgv(1, cargv
);
1474 value
pytrue( value unit
) {
1476 CAMLreturn(pywrap(Py_True
));
1479 value
pyfalse(value unit
) {
1481 CAMLreturn(pywrap(Py_False
));