4 This library is free software; you can redistribute it and/or modify
5 it under the terms of the GNU Lesser General Public License as
6 published by the Free Software Foundation; either version 2.1 of the
7 License, or (at your option) any later version.
9 This library is distributed in the hope that it will be useful, but
10 WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
14 You should have received a copy of the GNU Lesser General Public
15 License along with this library; if not, write to the Free Software
16 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
20 Heavy modifications (Bugfixes!) done by T.F.
24 http://mail.python.org/pipermail/doc-sig/2001-July/001956.html
26 for a discussion of result value stealing and a list of functions where
27 this becomes relevant.
29 Further modifications by Barry Schwartz.
34 #include <caml/mlvalues.h>
35 #include <caml/memory.h>
36 #include <caml/fail.h>
37 #include <caml/callback.h>
38 #include <caml/custom.h>
39 #include <caml/alloc.h>
43 #include "pycaml_stubs.h"
45 #if 3 <= PY_MAJOR_VERSION
47 #if 4 <= PY_MAJOR_VERSION || (PY_MAJOR_VERSION == 3 && 1 <= PY_MINOR_VERSION)
48 #define USE_PYCAPSULE 1
50 #define USE_PYCAPSULE 0
55 static void *xmalloc(size_t size
)
57 void *p
= malloc(size
);
59 fprintf(stderr
, "Virtual memory exhausted\n");
65 #if 3 <= PY_MAJOR_VERSION
67 static wchar_t *copy_to_wide_string(const char *s
)
73 n
= mbstowcs(NULL
, s
, 0) + 1;
74 if (n
!= (size_t) -1) {
75 ws
= xmalloc(n
* sizeof (wchar_t));
81 static size_t multibyte_strlen(const wchar_t *ws
)
83 char buffer
[MB_CUR_MAX
];
89 n
= wcrtomb(NULL
, L
'\0', &ps
); /* Initialize the parse state. */
92 while (size
!= (size_t) -1 && ws
[i
] != L
'\0') {
93 n
= wcrtomb(buffer
, ws
[i
], &ps
);
103 static char *copy_from_wide_string(const wchar_t *ws
)
113 size
= multibyte_strlen(ws
);
114 if (size
!= (size_t) -1) {
115 s
= xmalloc((size
+ 1) * sizeof (char));
116 n
= wcrtomb(NULL
, L
'\0', &ps
); /* Initialize the parse state. */
119 while (ws
[i
] != L
'\0') {
120 n
= wcrtomb(s
+ j
, ws
[i
], &ps
);
129 #endif /* 3 <= PY_MAJOR_VERSION */
131 static void *getcustom( value v
)
133 return *((void **)Data_custom_val(v
));
136 static void pydecref( value v
)
139 /* printf("GC - pydecref obj 0x%08x to refcount=%d\nOBJ=",getcustom(v),((PyObject *)getcustom(v))->ob_refcnt-1);
140 PyObject_Print((PyObject *)getcustom(v),stdout,0);
144 Py_DECREF((PyObject
*)getcustom(v
));
148 #if PY_MAJOR_VERSION <= 2
150 static int pycompare( value v1
, value v2
)
154 if (getcustom(v1
) && !getcustom(v2
))
156 else if (getcustom(v2
) && !getcustom(v1
))
158 else if (!getcustom(v1
) && !getcustom(v2
))
161 PyObject_Cmp((PyObject
*)getcustom(v1
),
162 (PyObject
*)getcustom(v2
), &result
);
166 #else /* PY_MAJOR_VERSION <= 2 */
168 static int pycompare(value v1
, value v2
)
172 if (getcustom(v1
) && !getcustom(v2
))
174 else if (getcustom(v2
) && !getcustom(v1
))
176 else if (!getcustom(v1
) && !getcustom(v2
))
178 else if (1 == PyObject_RichCompareBool((PyObject
*) getcustom(v1
),
179 (PyObject
*) getcustom(v2
), Py_EQ
))
181 else if (1 == PyObject_RichCompareBool((PyObject
*) getcustom(v1
),
182 (PyObject
*) getcustom(v2
), Py_LT
))
184 else if (1 == PyObject_RichCompareBool((PyObject
*) getcustom(v1
),
185 (PyObject
*) getcustom(v2
), Py_GT
))
188 result
= -1; /* Is there a better value to put here? */
193 #endif /* PY_MAJOR_VERSION <= 2 */
195 static long pyhash( value v
)
198 return PyObject_Hash((PyObject
*)getcustom(v
));
203 static unsigned long pydeserialize( void *dst
)
208 struct custom_operations pyops
=
214 custom_serialize_default
,
218 struct custom_operations fnops
=
228 static value
pywrap(PyObject
*obj
)
236 v
= caml_alloc_custom( &pyops
, sizeof( PyObject
* ), 100, 30000000 );
237 *((PyObject
**)Data_custom_val(v
)) = obj
;
241 /* T.F.: we may want to pywrap in such a way that we steal the reference: */
242 static value
pywrap_steal( PyObject
*obj
)
247 v
= caml_alloc_custom( &pyops
, sizeof( PyObject
* ), 100, 30000000 );
248 *((PyObject
**)Data_custom_val(v
)) = obj
;
255 return *((PyObject
**)Data_custom_val(v
));
261 caml_destructor(PyObject
*v
, const char *capsule_name
)
263 value
*valptr
= (value
*) PyCapsule_GetPointer(v
, capsule_name
);
264 caml_remove_global_root(valptr
);
269 camldestr(PyObject
*v
)
271 caml_destructor(v
, "caml-other");
275 camldestr_pill(PyObject
*v
)
277 caml_destructor(v
, "caml-pill");
280 #else /* USE_PYCAPSULE */
285 value
*valptr
= (value
*) v
;
286 /* printf("DDD camlwrap remove_global_root(0x%08x)\n",valptr);fflush(stdout); */
287 caml_remove_global_root(valptr
);
292 camldestr_pill(void *v
, void *unused_dummy_receiving_ocamlpill_token
)
294 value
*valptr
= (value
*) v
;
295 /* printf("DDD camlwrap remove_global_root(0x%08x)\n",valptr);fflush(stdout); */
296 caml_remove_global_root(valptr
);
300 #endif /* USE_PYCAPSULE */
302 /* T.F. Extension: the pill token is a subtle hack:
304 One problem is that, as it seems, there are
305 some python objects around which would be regarded as
306 being of OtherType in original PyCaml.
308 As these are opaque, we do not really have a good way
309 to discern them from OCaml pills, and passing such an
310 opaque value (which cannot be investigated systematically)
311 where an ocaml pill is expected is bound to result
312 in crashes (which we want to avoid).
314 How to get rid of this? We somehow have to be able to properly
315 identify OCaml Pills and extend the python types with CamlType.
317 We do this by using the PyCObject_Check to determine c-object type,
318 and abuse the closure parameter that is passed on to the destructor
319 (which can be queried independently, and actually is not used by our
320 destructor) as a token designating OCaml pills.
322 Barry Schwartz: PyCapsule has a name field that can be used for
323 identification, in place of the PyCObject hack described above.
327 static const char *ocamlpill_token
= "CAML";
331 camlwrap(value val
, void *aux_str
, int size
)
333 value
*v
= (value
*) xmalloc(sizeof(value
) + size
);
335 memcpy((void *)v
+ sizeof(value
), aux_str
, size
);
336 caml_register_global_root(v
);
337 /* printf("DDD camlwrap caml_register_global_root(0x%08x)\n",v);fflush(stdout); */
339 return PyCapsule_New(v
, "caml-other", camldestr
);
341 return PyCObject_FromVoidPtr(v
, camldestr
);
346 camlwrap_pill(value val
, void *aux_str
, int size
)
348 value
*v
= (value
*) xmalloc(sizeof(value
) + size
);
350 memcpy((void *)v
+ sizeof(value
), aux_str
, size
);
351 caml_register_global_root(v
);
353 return PyCapsule_New(v
, "caml-pill", camldestr_pill
);
355 return PyCObject_FromVoidPtrAndDesc(v
, (void*)ocamlpill_token
, camldestr_pill
);
361 caml_aux(PyObject
*obj
)
364 value
*v
= (value
*) PyCapsule_GetPointer(obj
, "caml-other");
366 value
*v
= (value
*) PyCObject_AsVoidPtr(obj
);
368 return (void *) v
+ sizeof(value
);
372 PyObject *pycall_callback_buggy( PyObject *obj, PyObject *args ) {
376 if( !PyCObject_Check(obj) ) {
380 v = (value *)PyCObject_AsVoidPtr( obj );
381 out = caml_callback(*v,pywrap(args));
382 return pyunwrap(out);
386 /* T.F.: - I think the definition above is flawed...
387 Looking at the definitions of OCAML macros in memory.h,
388 this is how I suppose it should work:
391 PyObject
*pycall_callback( PyObject
*obj
, PyObject
*args
)
394 CAMLlocal3(ml_out
, ml_func
, ml_args
);
398 void *p
= PyCapsule_GetPointer(obj
, "caml-other");
404 ml_func
= * (value
*) p
;
406 if (!PyCObject_Check(obj
))
412 ml_func
= * (value
*) PyCObject_AsVoidPtr(obj
);
414 ml_args
= pywrap(args
);
415 ml_out
= caml_callback(ml_func
, ml_args
);
416 out
= pyunwrap(ml_out
);
418 The result which we have now is borrowed - most probably,
419 there is only one reference to it which says
420 "I am reachable through the ML heap".
421 We have to properly transfer ownership, and hence
422 see that we own that reference:
425 CAMLreturnT(PyObject
*, out
);
428 /*-----------------------------------------------------------------------*/
430 static FILE *make_FILE(int fd_int
)
434 fd_duplicate
= dup(fd_int
);
435 return fdopen(fd_duplicate
, "r+");
438 /*-----------------------------------------------------------------------*/
440 value
pynull(value unit
)
443 CAMLreturn(pywrap(0));
446 value
pynone(value unit
)
449 CAMLreturn(pywrap(Py_None
));
452 value
py_true(value unit
)
455 CAMLreturn(pywrap(Py_True
));
458 value
py_false(value unit
)
461 CAMLreturn(pywrap(Py_False
));
464 /*-----------------------------------------------------------------------*/
466 #define Type1(func) \
467 CAMLprim value func##_wrapper(value unit) \
471 CAMLreturn(Val_unit); \
478 Type1(PyImport_Cleanup
)
480 /*-----------------------------------------------------------------------*/
482 #define Type2(func) \
483 CAMLprim value func##_wrapper(value obj) \
487 func(Int_val(obj)); \
488 CAMLreturn(Val_unit); \
494 /*-----------------------------------------------------------------------*/
496 #if PY_MAJOR_VERSION <= 2
498 #define Type3(func) \
499 CAMLprim value func##_wrapper(value obj) \
503 func(String_val(obj)); \
504 CAMLreturn(Val_unit); \
509 #define Type3(func) \
510 CAMLprim value func##_wrapper(value obj) \
514 char *s = String_val(obj); \
515 wchar_t *ws = copy_to_wide_string(s); \
518 CAMLreturn(Val_unit); \
523 Type3(Py_SetProgramName
)
524 Type3(Py_SetPythonHome
)
526 /*-----------------------------------------------------------------------*/
528 #define Type4(func) \
529 CAMLprim value func##_wrapper(value unit) \
533 int result = func(); \
534 CAMLreturn(Val_int(result)); \
537 Type4(Py_IsInitialized
)
539 #if PY_MAJOR_VERSION <= 2
540 Type4(PyEval_GetRestricted
)
543 /*-----------------------------------------------------------------------*/
545 #define Type5(func) \
546 CAMLprim value func##_wrapper(value obj) \
550 int result = func(String_val(obj)); \
551 CAMLreturn(Val_int(result)); \
554 Type5(PyRun_SimpleString
)
555 Type5(PyImport_ImportFrozenModule
)
557 /*-----------------------------------------------------------------------*/
559 /* Perhaps these should take a wrapped (FILE*) as argument instead of
560 * an integer file descriptor. */
562 #define Type6(func) \
563 CAMLprim value func##_wrapper(value py_args) \
565 CAMLparam1(py_args); \
567 FILE *f = make_FILE(Int_val(Field(py_args, 0))); \
568 int result = func(f, String_val(Field(py_args, 1))); \
570 CAMLreturn(Val_int(result)); \
574 Type6(PyRun_SimpleFile
)
575 Type6(PyRun_InteractiveOne
)
576 Type6(PyRun_InteractiveLoop
)
577 Type6(Py_FdIsInteractive
)
579 /*-----------------------------------------------------------------------*/
581 /* Perhaps these should take a wrapped (FILE*) as argument instead of
582 * an integer file descriptor. */
584 #define Type7(func) \
585 CAMLprim value func##_wrapper(value py_args) \
587 CAMLparam1(py_args); \
589 FILE *f = make_FILE(Int_val(Field(py_args, 0))); \
590 int result = func(f, \
591 String_val(Field(py_args, 1)), \
592 Int_val(Field(py_args, 2))); \
594 CAMLreturn(Val_int(result)); \
597 Type7(PyRun_AnyFileEx
)
598 Type7(PyRun_SimpleFileEx
)
600 /*-----------------------------------------------------------------------*/
602 #define Type8(func) \
603 CAMLprim value func##_wrapper(value unit) \
606 CAMLreturn(caml_copy_string(func())); \
609 #if PY_MAJOR_VERSION <= 2
612 #define Type8a(func) \
613 CAMLprim value func##_wrapper(value unit) \
616 CAMLlocal1(string); \
622 string = pynull(Val_unit); \
625 s = copy_from_wide_string(ws); \
627 string = pynull(Val_unit); \
629 string = caml_copy_string(s); \
631 CAMLreturn(string); \
636 Type8(Py_GetPlatform
)
637 Type8(Py_GetCopyright
)
638 Type8(Py_GetCompiler
)
639 Type8(Py_GetBuildInfo
)
641 Type8a(Py_GetProgramName
)
642 Type8a(Py_GetPythonHome
)
643 Type8a(Py_GetProgramFullPath
)
645 Type8a(Py_GetExecPrefix
)
648 /*-----------------------------------------------------------------------*/
650 #define Type9(func, wrap_obj) \
651 CAMLprim value func##_wrapper(value py_args) \
653 CAMLparam1(py_args); \
655 PyObject *new_obj = func(String_val(Field(py_args, 0)), \
656 Int_val(Field(py_args, 1)), \
657 pyunwrap(Field(py_args, 2)), \
658 pyunwrap(Field(py_args, 3))); \
659 CAMLreturn(wrap_obj(new_obj)); \
662 Type9(PyRun_String
, pywrap_steal
)
664 /*-----------------------------------------------------------------------*/
666 /* Perhaps these should take a wrapped (FILE*) as argument instead of
667 * an integer file descriptor. */
669 #define Type10(func, wrap_obj) \
670 CAMLprim value func##_wrapper(value py_args) \
672 CAMLparam1(py_args); \
674 FILE *f = make_FILE(Int_val(Field(py_args, 0))); \
675 PyObject *new_obj = func(f, \
676 String_val(Field(py_args, 1)), \
677 Int_val(Field(py_args, 2)), \
678 pyunwrap(Field(py_args, 3)), \
679 pyunwrap(Field(py_args, 4))); \
681 CAMLreturn(wrap_obj(new_obj)); \
684 Type10(PyRun_File
, pywrap_steal
)
686 /*-----------------------------------------------------------------------*/
688 /* Perhaps these should take a wrapped (FILE*) as argument instead of
689 * an integer file descriptor. */
691 #define Type11(func, wrap_obj) \
692 CAMLprim value func##_wrapper(value py_args) \
694 CAMLparam1(py_args); \
696 FILE *f = make_FILE(Int_val(Field(py_args, 0))); \
697 PyObject *new_obj = func(f, \
698 String_val(Field(py_args, 1)), \
699 Int_val(Field(py_args, 2)), \
700 pyunwrap(Field(py_args, 3)), \
701 pyunwrap(Field(py_args, 4)), \
702 Int_val(Field(py_args, 5))); \
704 CAMLreturn(wrap_obj(new_obj)); \
707 Type11(PyRun_FileEx
, pywrap_steal
)
709 /*-----------------------------------------------------------------------*/
711 #define Type12(func, wrap_obj) \
712 CAMLprim value func##_wrapper(value py_args) \
714 CAMLparam1(py_args); \
716 PyObject *new_obj = func(String_val(Field(py_args, 0)), \
717 String_val(Field(py_args, 1)), \
718 Int_val(Field(py_args, 2))); \
719 CAMLreturn(wrap_obj(new_obj)); \
722 Type12(Py_CompileString
, pywrap_steal
)
724 /*-----------------------------------------------------------------------*/
726 /* Perhaps these should take a wrapped (FILE*) as argument instead of
727 * an integer file descriptor. */
729 #define Type13(func) \
730 CAMLprim value func##_wrapper(value py_args) \
732 CAMLparam1(py_args); \
734 FILE *f = make_FILE(Int_val(Field(py_args, 1))); \
735 int result = func(pyunwrap(Field(py_args, 0)), \
737 Int_val(Field(py_args, 2))); \
739 CAMLreturn(Val_int(result)); \
742 Type13(PyObject_Print
)
744 /*-----------------------------------------------------------------------*/
746 #define Type_GetSlice(func, wrap_obj) \
747 CAMLprim value func##_wrapper(value py_args) \
749 CAMLparam1(py_args); \
751 PyObject *new_obj = func(pyunwrap(Field(py_args, 0)), \
752 Int_val(Field(py_args, 1)), \
753 Int_val(Field(py_args, 2))); \
754 CAMLreturn(wrap_obj(new_obj)); \
757 Type_GetSlice(PySequence_GetSlice
, pywrap_steal
)
758 Type_GetSlice(PyTuple_GetSlice
, pywrap_steal
)
760 /*-----------------------------------------------------------------------*/
762 #define Type14(func, wrap_obj) \
763 CAMLprim value func##_wrapper(value obj) \
767 PyObject *new_obj = func(pyunwrap(obj)); \
768 CAMLreturn(wrap_obj(new_obj)); \
771 #if PY_MAJOR_VERSION <= 2
772 #define Type14a(func, substitute, wrap_obj) Type14(func, wrap_obj)
774 #define Type14a(func, substitute, wrap_obj) \
775 CAMLprim value func##_wrapper(value obj) \
779 PyObject *new_obj = substitute(pyunwrap(obj)); \
780 CAMLreturn(wrap_obj(new_obj)); \
784 Type14(PyMethod_Function
, pywrap
)
785 Type14(PyMethod_Self
, pywrap
)
786 Type14(PyModule_GetDict
, pywrap
)
788 #if PY_MAJOR_VERSION <= 2
789 Type14(PyMethod_Class
, pywrap
)
792 Type14(PyUnicode_AsUTF8String
, pywrap_steal
)
793 Type14(PyUnicode_AsUTF16String
, pywrap_steal
)
794 Type14(PyUnicode_AsUTF32String
, pywrap_steal
)
795 Type14(PyObject_Repr
, pywrap_steal
)
796 Type14(PyImport_ReloadModule
, pywrap_steal
)
797 Type14(PyImport_Import
, pywrap_steal
)
798 Type14(PyObject_Str
, pywrap_steal
)
799 Type14(PyObject_Type
, pywrap_steal
)
800 Type14(PyDict_Keys
, pywrap_steal
)
801 Type14(PyDict_Values
, pywrap_steal
)
802 Type14(PyDict_Items
, pywrap_steal
)
803 Type14(PyDict_Copy
, pywrap_steal
)
804 Type14(PySequence_Tuple
, pywrap_steal
)
805 Type14(PySequence_List
, pywrap_steal
)
806 Type14(PyNumber_Long
, pywrap_steal
)
807 Type14(PyNumber_Float
, pywrap_steal
)
808 Type14(PyNumber_Negative
, pywrap_steal
)
809 Type14(PyNumber_Positive
, pywrap_steal
)
810 Type14(PyNumber_Absolute
, pywrap_steal
)
811 Type14(PyNumber_Invert
, pywrap_steal
)
812 Type14(PyIter_Next
, pywrap_steal
)
814 Type14a(PyObject_Unicode
, PyObject_Str
, pywrap_steal
)
815 Type14a(PyNumber_Int
, PyNumber_Long
, pywrap_steal
)
817 /*-----------------------------------------------------------------------*/
819 #define Type15(func, wrap_obj) \
820 CAMLprim value func##_wrapper(value py_args) \
822 CAMLparam1(py_args); \
824 PyObject *new_obj = func(pyunwrap(Field(py_args, 0)), \
825 pyunwrap(Field(py_args, 1)), \
826 Int_val(Field(py_args, 2))); \
827 CAMLreturn(wrap_obj(new_obj)); \
830 Type15(PyObject_RichCompare
, pywrap_steal
);
832 /*-----------------------------------------------------------------------*/
834 #define Type16(func, wrap_obj) \
835 CAMLprim value func##_wrapper(value py_args) \
837 CAMLparam1(py_args); \
839 PyObject *new_obj = func(pyunwrap(Field(py_args, 0)), \
840 String_val(Field(py_args, 1))); \
841 CAMLreturn(wrap_obj(new_obj)); \
844 Type16(PyDict_GetItemString
, pywrap
)
846 Type16(PyObject_GetAttrString
, pywrap_steal
)
847 Type16(PySequence_Fast
, pywrap_steal
)
848 Type16(PyMapping_GetItemString
, pywrap_steal
)
850 /*-----------------------------------------------------------------------*/
852 #define Type17(func, wrap_obj) \
853 CAMLprim value func##_wrapper(value py_args) \
855 CAMLparam1(py_args); \
857 PyObject *new_obj = func(pyunwrap(Field(py_args, 0)), \
858 pyunwrap(Field(py_args, 1))); \
859 CAMLreturn(wrap_obj(new_obj)); \
862 Type17(PyDict_GetItem
, pywrap
)
864 Type17(PyEval_CallObject
, pywrap_steal
)
866 #if PY_MAJOR_VERSION <= 2
867 Type17(PyBytes_Format
, pywrap_steal
)
870 #if PY_MAJOR_VERSION <= 2
871 Type17(PyInstance_NewRaw
, pywrap_steal
)
874 Type17(PySequence_Concat
, pywrap_steal
)
875 Type17(PySequence_InPlaceConcat
, pywrap_steal
)
877 Type17(PyObject_GetAttr
, pywrap_steal
)
878 Type17(PyObject_GetItem
, pywrap_steal
)
880 Type17(PyNumber_Add
, pywrap_steal
)
881 Type17(PyNumber_Subtract
, pywrap_steal
)
882 Type17(PyNumber_Multiply
, pywrap_steal
)
883 Type17(PyNumber_Remainder
, pywrap_steal
)
884 Type17(PyNumber_Divmod
, pywrap_steal
)
885 Type17(PyNumber_TrueDivide
, pywrap_steal
)
886 Type17(PyNumber_FloorDivide
, pywrap_steal
)
888 #if PY_MAJOR_VERSION <= 2
889 Type17(PyNumber_Divide
, pywrap_steal
)
892 Type17(PyNumber_Lshift
, pywrap_steal
)
893 Type17(PyNumber_Rshift
, pywrap_steal
)
894 Type17(PyNumber_And
, pywrap_steal
)
895 Type17(PyNumber_Xor
, pywrap_steal
)
896 Type17(PyNumber_Or
, pywrap_steal
)
898 Type17(PyNumber_InPlaceAdd
, pywrap_steal
)
899 Type17(PyNumber_InPlaceSubtract
, pywrap_steal
)
900 Type17(PyNumber_InPlaceMultiply
, pywrap_steal
)
901 Type17(PyNumber_InPlaceTrueDivide
, pywrap_steal
)
902 Type17(PyNumber_InPlaceFloorDivide
, pywrap_steal
)
903 Type17(PyNumber_InPlaceRemainder
, pywrap_steal
)
904 Type17(PyNumber_InPlaceLshift
, pywrap_steal
)
905 Type17(PyNumber_InPlaceRshift
, pywrap_steal
)
906 Type17(PyNumber_InPlaceAnd
, pywrap_steal
)
907 Type17(PyNumber_InPlaceXor
, pywrap_steal
)
908 Type17(PyNumber_InPlaceOr
, pywrap_steal
)
910 #if PY_MAJOR_VERSION <= 2
911 Type17(PyNumber_InPlaceDivide
, pywrap_steal
)
914 /*-----------------------------------------------------------------------*/
916 #define Type18(func) \
917 CAMLprim value func##_wrapper(value obj) \
921 int result = func(pyunwrap(obj)); \
922 CAMLreturn(Val_int(result)); \
925 Type18(PyObject_IsTrue
)
927 Type18(PyCallable_Check
)
931 Type18(PyErr_ExceptionMatches
)
932 Type18(PyObject_Size
)
933 Type18(PyNumber_Check
)
934 Type18(PySequence_Check
)
935 Type18(PySequence_Size
)
936 Type18(PySequence_Length
)
937 Type18(PyMapping_Check
)
938 Type18(PyMapping_Size
)
939 Type18(PyMapping_Length
)
941 Type18(PyUnicode_GetSize
)
943 /*-----------------------------------------------------------------------*/
945 #define Type19(func) \
946 CAMLprim value func##_wrapper(value py_args) \
948 CAMLparam1(py_args); \
950 int result = func(pyunwrap(Field(py_args, 0)), \
951 pyunwrap(Field(py_args, 1))); \
952 CAMLreturn(Val_int(result)); \
955 Type19(PyObject_HasAttr
)
956 Type19(PyObject_DelItem
)
957 Type19(PyDict_DelItem
)
958 Type19(PyErr_GivenExceptionMatches
)
959 Type19(PySequence_Count
)
960 Type19(PySequence_Contains
)
961 Type19(PySequence_In
)
962 Type19(PySequence_Index
)
963 Type19(PyMapping_HasKey
)
965 #if PY_MAJOR_VERSION <= 2
966 Type19(PyObject_Compare
)
969 /*-----------------------------------------------------------------------*/
971 #define Type20(func) \
972 CAMLprim value func##_wrapper(value py_args) \
974 CAMLparam1(py_args); \
976 int result = func(pyunwrap(Field(py_args, 0)), \
977 pyunwrap(Field(py_args, 1)), \
978 Int_val(Field(py_args, 2))); \
979 CAMLreturn(Val_int(result)); \
982 Type20(PyObject_RichCompareBool
)
984 /*-----------------------------------------------------------------------*/
986 #define Type21(func) \
987 CAMLprim value func##_wrapper(value py_args) \
989 CAMLparam1(py_args); \
991 int result = func(pyunwrap(Field(py_args, 0)), \
992 String_val(Field(py_args, 1)), \
993 pyunwrap(Field(py_args, 2))); \
994 CAMLreturn(Val_int(result)); \
997 Type21(PyObject_SetAttrString
)
998 Type21(PyDict_SetItemString
)
999 Type21(PyMapping_SetItemString
)
1001 /*-----------------------------------------------------------------------*/
1003 #define Type22(func) \
1004 CAMLprim value func##_wrapper(value py_args) \
1006 CAMLparam1(py_args); \
1008 int result = func(pyunwrap(Field(py_args, 0)), \
1009 String_val(Field(py_args, 1))); \
1010 CAMLreturn(Val_int(result)); \
1013 Type22(PyMapping_HasKeyString
)
1014 Type22(PyObject_HasAttrString
)
1015 Type22(PyDict_DelItemString
)
1017 /*-----------------------------------------------------------------------*/
1020 Type23 not implemented:
1025 /*-----------------------------------------------------------------------*/
1027 #define Type24(func) \
1028 CAMLprim value func##_wrapper(value py_args) \
1030 CAMLparam1(py_args); \
1032 int result = func(pyunwrap(Field(py_args, 0)), \
1033 pyunwrap(Field(py_args, 1)), \
1034 pyunwrap(Field(py_args, 2))); \
1035 CAMLreturn(Val_int(result)); \
1038 Type24(PyObject_SetAttr
)
1039 Type24(PyObject_SetItem
)
1040 Type24(PyDict_SetItem
)
1042 /*-----------------------------------------------------------------------*/
1044 #define Type25(func) \
1045 CAMLprim value func##_wrapper(value obj) \
1048 CAMLreturn(copy_int64(func(pyunwrap(obj)))); \
1051 #if PY_MAJOR_VERSION <= 2
1052 #define Type25a(func, substitute) Type25(func)
1054 #define Type25a(func, substitute) \
1055 CAMLprim value func##_wrapper(value obj) \
1058 CAMLreturn(copy_int64(substitute(pyunwrap(obj)))); \
1062 Type25(PyObject_Hash
)
1063 Type25a(PyInt_AsLong
, PyLong_AsLong
)
1065 /*-----------------------------------------------------------------------*/
1067 #define Type26(func, byte_type) \
1068 CAMLprim value func##_wrapper(value obj) \
1071 CAMLlocal1(string); \
1073 byte_type *s = func(pyunwrap(obj)); \
1075 string = pynull(Val_unit); \
1077 string = caml_copy_string(s); \
1078 CAMLreturn(string); \
1081 Type26(PyBytes_AsString
, char)
1082 Type26(PyModule_GetName
, const char)
1083 Type26(PyModule_GetFilename
, const char)
1085 /*-----------------------------------------------------------------------*/
1087 #define Type28(func, wrap_obj) \
1088 CAMLprim value func##_wrapper(value obj) \
1092 PyObject *result = func(String_val(obj)); \
1093 CAMLreturn(wrap_obj(result)); \
1096 Type28(PyImport_AddModule
, pywrap
)
1098 Type28(PyBytes_FromString
, pywrap_steal
)
1099 Type28(PyUnicode_FromString
, pywrap_steal
)
1100 Type28(PyModule_New
, pywrap_steal
)
1101 Type28(PyImport_ImportModule
, pywrap_steal
)
1103 /*-----------------------------------------------------------------------*/
1105 #define Type29(func, wrap_obj) \
1106 CAMLprim value func##_wrapper(value unit) \
1110 PyObject *result = func(); \
1111 CAMLreturn(wrap_obj(result)); \
1114 Type29(PyErr_Occurred
, pywrap
)
1115 Type29(PyImport_GetModuleDict
, pywrap
)
1116 Type29(PyEval_GetBuiltins
, pywrap
)
1117 Type29(PyEval_GetGlobals
, pywrap
)
1118 Type29(PyEval_GetLocals
, pywrap
)
1119 /* Type29(PyEval_GetFrame, pywrap) -- FIX: Should return wrapped (PyFrameObject*), not wrapped (Object*). */
1121 Type29(PyDict_New
, pywrap_steal
)
1123 /*-----------------------------------------------------------------------*/
1125 #define Type30(func) \
1126 CAMLprim value func##_wrapper(value obj) \
1130 func(pyunwrap(obj)); \
1131 CAMLreturn(Val_unit); \
1134 Type30(PyDict_Clear
)
1135 Type30(PyErr_SetNone
)
1137 /*-----------------------------------------------------------------------*/
1140 Type31 -- currently not implemented:
1144 /*-----------------------------------------------------------------------*/
1146 #define Type34(func, wrap_obj) \
1147 CAMLprim value func##_wrapper(value obj) \
1151 PyObject *new_obj = func(Int64_val(obj)); \
1152 CAMLreturn(wrap_obj(new_obj)); \
1155 #if PY_MAJOR_VERSION <= 2
1157 #define Type34a(func, substitute, wrap_obj) Type34(func, wrap_obj)
1161 #define Type34a(func, substitute, wrap_obj) \
1162 CAMLprim value func##_wrapper(value obj) \
1166 PyObject *new_obj = substitute(Int64_val(obj)); \
1167 CAMLreturn(wrap_obj(new_obj)); \
1172 Type34a(PyInt_FromLong
, PyLong_FromLong
, pywrap_steal
)
1174 /*-----------------------------------------------------------------------*/
1176 #define Type35(func) \
1177 CAMLprim value func##_wrapper(value unit) \
1180 CAMLreturn(copy_int64(func())); \
1183 #if PY_MAJOR_VERSION <= 2
1184 Type35(PyInt_GetMax
)
1187 Type35(PyImport_GetMagicNumber
)
1189 /*-----------------------------------------------------------------------*/
1191 #define Type36(func, wrap_obj) \
1192 CAMLprim value func##_wrapper(value obj) \
1196 PyObject *new_obj = func(Double_val(obj)); \
1197 CAMLreturn(wrap_obj(new_obj)); \
1200 Type36(PyFloat_FromDouble
, pywrap_steal
)
1202 /*-----------------------------------------------------------------------*/
1204 #define Type37(func) \
1205 CAMLprim value func##_wrapper(value obj) \
1208 CAMLreturn(copy_double(func(pyunwrap(obj)))); \
1211 Type37(PyFloat_AsDouble
)
1213 /*-----------------------------------------------------------------------*/
1215 #define Type39(func, wrap_obj) \
1216 CAMLprim value func##_wrapper(value obj) \
1220 PyObject *new_obj = func(Int_val(obj)); \
1221 CAMLreturn(wrap_obj(new_obj)); \
1224 Type39(PyTuple_New
, pywrap_steal
);
1226 /*-----------------------------------------------------------------------*/
1228 #define Type40(func, wrap_obj) \
1229 CAMLprim value func##_wrapper(value py_args) \
1231 CAMLparam1(py_args); \
1233 PyObject *new_obj = func(pyunwrap(Field(py_args, 0)), \
1234 Int_val(Field(py_args, 1))); \
1235 CAMLreturn(wrap_obj(new_obj)); \
1238 Type40(PyTuple_GetItem
, pywrap
)
1240 Type40(PySequence_InPlaceRepeat
, pywrap_steal
)
1241 Type40(PySequence_Repeat
, pywrap_steal
)
1242 Type40(PySequence_GetItem
, pywrap_steal
)
1244 /*-----------------------------------------------------------------------*/
1246 #define Type40b(func) \
1247 CAMLprim value func##_wrapper(value py_args) \
1249 CAMLparam1(py_args); \
1251 int result = func(pyunwrap(Field(py_args, 0)), \
1252 Int_val(Field(py_args, 1))); \
1253 CAMLreturn(Val_int(result)); \
1256 Type40b(PySequence_DelItem
)
1258 /*-----------------------------------------------------------------------*/
1260 /* |do_steal| here means: "do we steal the arg[2] reference (which is
1261 an OCaml reference)?" If our function is stealing, we first have
1262 to get a reference. */
1264 #define Type41(func, do_steal) \
1265 CAMLprim value func##_wrapper(value py_args) \
1267 CAMLparam1(py_args); \
1269 PyObject *x = pyunwrap(Field(py_args, 2)); \
1272 CAMLreturn(Val_int(func(pyunwrap(Field(py_args, 0)), \
1273 Int_val(Field(py_args, 1)), \
1278 Type41(PySequence_SetItem
, 0)
1280 Type41(PyTuple_SetItem
, 1)
1282 /*-----------------------------------------------------------------------*/
1284 #define Type42(func, wrap_obj) \
1285 CAMLprim value func##_wrapper(value py_args) \
1287 CAMLparam1(py_args); \
1289 PyObject *new_obj = func(pyunwrap(Field(py_args, 0)), \
1290 pyunwrap(Field(py_args, 1)), \
1291 pyunwrap(Field(py_args, 2))); \
1292 CAMLreturn(wrap_obj(new_obj)); \
1295 #if PY_MAJOR_VERSION <= 2
1296 #define Type42a Type42
1298 #define Type42a(func, wrap_obj) \
1299 CAMLprim value func##_wrapper(value py_args) \
1301 CAMLparam1(py_args); \
1303 PyObject *new_obj = func(pyunwrap(Field(py_args, 0)), \
1304 pyunwrap(Field(py_args, 2))); \
1305 CAMLreturn(wrap_obj(new_obj)); \
1309 Type42(PySlice_New
, pywrap_steal
)
1310 Type42(PyEval_CallObjectWithKeywords
, pywrap_steal
)
1311 Type42(PyNumber_Power
, pywrap_steal
)
1312 Type42(PyNumber_InPlacePower
, pywrap_steal
)
1314 #if PY_MAJOR_VERSION <= 2
1315 Type42(PyClass_New
, pywrap_steal
)
1316 Type42(PyInstance_New
, pywrap_steal
)
1318 /* Calls the builtin-function: type(name,bases,dict), with the
1319 * name of the class, tuples of parent names, and dictionary
1320 * with initializations of fields.
1322 CAMLprim value
PyClass_New_wrapper(value py_args
)
1324 CAMLparam1(py_args
);
1325 PyObject
*bases
= pyunwrap(Field(py_args
, 0));
1326 PyObject
*dict
= pyunwrap(Field(py_args
, 1));
1327 PyObject
*name
= pyunwrap(Field(py_args
, 2));
1329 PyObject_CallFunctionObjArgs
1330 ( (PyObject
*) &PyType_Type
, name
, bases
, dict
, NULL
);
1331 CAMLreturn(pywrap_steal(new_obj
));
1335 Type42a(PyMethod_New
, pywrap_steal
)
1337 /*-----------------------------------------------------------------------*/
1340 Type43(PySlice_GetIndices) <-- Not supported in this version.
1343 /*-----------------------------------------------------------------------*/
1345 #define Type45(func) \
1346 CAMLprim value func##_wrapper(value py_args) \
1348 CAMLparam1(py_args); \
1350 func(pyunwrap(Field(py_args, 0)), \
1351 pyunwrap(Field(py_args, 1))); \
1352 CAMLreturn(Val_unit); \
1355 Type45(PyErr_SetObject
)
1357 /*-----------------------------------------------------------------------*/
1359 #define Type46(func) \
1360 CAMLprim value func##_wrapper(value py_args) \
1362 CAMLparam1(py_args); \
1364 func(pyunwrap(Field(py_args, 0)), \
1365 String_val(Field(py_args, 1))); \
1366 CAMLreturn(Val_unit); \
1369 Type46(PyErr_SetString
)
1371 /*-----------------------------------------------------------------------*/
1373 #define Type47(func, wrap_obj) \
1374 CAMLprim value func##_wrapper(value py_args) \
1376 CAMLparam1(py_args); \
1377 CAMLlocal1(result); \
1379 PyObject *obj1 = pyunwrap(Field(py_args, 0)); \
1380 PyObject *obj2 = pyunwrap(Field(py_args, 1)); \
1381 PyObject *obj3 = pyunwrap(Field(py_args, 2)); \
1382 func(&obj1, &obj2, &obj3); \
1383 result = caml_alloc_tuple(3); \
1384 Store_field(result, 0, wrap_obj(obj1)); \
1385 Store_field(result, 1, wrap_obj(obj2)); \
1386 Store_field(result, 2, wrap_obj(obj3)); \
1387 CAMLreturn(result); \
1390 Type47(PyErr_Fetch
, pywrap_steal
)
1391 Type47(PyErr_NormalizeException
, pywrap_steal
)
1393 /*-----------------------------------------------------------------------*/
1395 #define Type48(func) \
1396 CAMLprim value func##_wrapper(value py_args) \
1398 CAMLparam1(py_args); \
1400 func(pyunwrap(Field(py_args, 0)), \
1401 pyunwrap(Field(py_args, 1)), \
1402 pyunwrap(Field(py_args, 2))); \
1403 CAMLreturn(Val_unit); \
1406 Type48(PyErr_Restore
)
1408 /*-----------------------------------------------------------------------*/
1411 #define Type49(func, wrap_obj) \
1412 CAMLprim value func##_wrapper(value py_args) \
1414 CAMLparam1(py_args); \
1416 PyObject *result = func(String_val(Field(py_args, 0)), \
1417 pyunwrap(Field(py_args, 1))); \
1418 CAMLreturn(wrap_obj(result)); \
1421 Type49(PyImport_ExecCodeModule
, pywrap_steal
)
1423 /*-----------------------------------------------------------------------*/
1425 #define Type50(func, wrap_obj) \
1426 CAMLprim value func##_wrapper(value py_args) \
1428 CAMLparam1(py_args); \
1430 PyObject *result = func(String_val(Field(py_args, 0)), \
1431 pyunwrap(Field(py_args, 1)), \
1432 String_val(Field(py_args, 2))); \
1433 CAMLreturn(wrap_obj(result)); \
1436 Type50(PyImport_ExecCodeModuleEx
, pywrap_steal
)
1438 /*-----------------------------------------------------------------------*/
1440 #define Type51(func, wrap_obj) \
1441 CAMLprim value func##_wrapper(value py_args) \
1443 CAMLparam1(py_args); \
1445 PyObject *result = func(String_val(Field(py_args, 0)), \
1446 pyunwrap(Field(py_args, 1)), \
1447 pyunwrap(Field(py_args, 2)), \
1448 pyunwrap(Field(py_args, 3))); \
1449 CAMLreturn(wrap_obj(result)); \
1452 Type51(PyImport_ImportModuleEx
, pywrap_steal
)
1454 /*-----------------------------------------------------------------------*/
1456 #define Type52(func, byte_type) \
1457 CAMLprim value func##_wrapper(value obj) \
1460 CAMLlocal1(string); \
1463 byte_type *buffer; \
1464 Py_ssize_t length; \
1466 return_val = func(pyunwrap(obj), &buffer, &length); \
1467 if (return_val == -1) { \
1468 string = pynull(Val_unit); \
1470 string = caml_alloc_string(length); \
1471 memcpy(String_val(string), buffer, length); \
1473 CAMLreturn(string); \
1476 Type52(PyBytes_AsStringAndSize
, char)
1477 Type52(PyObject_AsCharBuffer
, const char)
1478 Type52(PyObject_AsReadBuffer
, const void)
1479 Type52(PyObject_AsWriteBuffer
, void)
1481 /*-----------------------------------------------------------------------*/
1483 #define Type53(func) \
1484 CAMLprim value func##_wrapper(value py_args) \
1486 CAMLparam1(py_args); \
1488 int result = func(pyunwrap(Field(py_args, 0)), \
1489 Int_val(Field(py_args, 1)), \
1490 Int_val(Field(py_args, 2)), \
1491 pyunwrap(Field(py_args, 3))); \
1492 CAMLreturn(Val_int(result)); \
1495 Type53(PySequence_SetSlice
)
1497 /*-----------------------------------------------------------------------*/
1499 #define Type54(func) \
1500 CAMLprim value func##_wrapper(value py_args) \
1502 CAMLparam1(py_args); \
1504 int result = func(pyunwrap(Field(py_args, 0)), \
1505 Int_val(Field(py_args, 1)), \
1506 Int_val(Field(py_args, 2))); \
1507 CAMLreturn(Val_int(result)); \
1510 Type54(PySequence_DelSlice
)
1512 /*-----------------------------------------------------------------------*/
1514 #define TypeUTF8Decoder(func, wrap_obj) \
1515 CAMLprim value func##_wrapper(value py_args) \
1517 CAMLparam1(py_args); \
1521 char *utf8 = String_val(Field(py_args, 0)); \
1522 Py_ssize_t utf8_length = caml_string_length(Field(py_args, 0)); \
1524 if (Field(py_args, 1) == Val_int(0)) \
1525 result = func(utf8, utf8_length, NULL); \
1527 result = func(utf8, utf8_length, \
1528 String_val(Field(Field(py_args, 1), 0))); \
1529 CAMLreturn(wrap_obj(result)); \
1532 TypeUTF8Decoder(PyUnicode_DecodeUTF8
, pywrap_steal
)
1534 /*-----------------------------------------------------------------------*/
1536 #define TypeUTF16Decoder(func, wrap_obj) \
1537 CAMLprim value func##_wrapper(value py_args) \
1539 CAMLparam1(py_args); \
1544 char *s = String_val(Field(py_args, 0)); \
1545 Py_ssize_t s_length = caml_string_length(Field(py_args, 0)); \
1547 if (Field(py_args, 1) == Val_int(0)) \
1550 errors = String_val(Field(Field(py_args, 1), 0)); \
1552 if (Field(py_args, 2) == Val_int(0)) \
1553 result = func(s, s_length, errors, NULL); \
1555 int byteorder = Int_val(Field(Field(py_args, 2), 0)); \
1556 result = func(s, s_length, errors, &byteorder); \
1559 CAMLreturn(wrap_obj(result)); \
1562 TypeUTF16Decoder(PyUnicode_DecodeUTF16
, pywrap_steal
)
1563 TypeUTF16Decoder(PyUnicode_DecodeUTF32
, pywrap_steal
)
1565 /*-----------------------------------------------------------------------*/
1567 CAMLprim value
PyUnicode_FromUnicode_wrapper(value closure
, value length
)
1569 CAMLparam2(closure
, length
);
1570 CAMLlocal2(index
, val
); /* We need named intermediate values for
1571 * garbage collection. */
1573 Py_ssize_t len
= Int_val(length
);
1577 result
= PyUnicode_FromUnicode(NULL
, len
);
1578 if (result
!= NULL
) {
1579 Py_UNICODE
*p
= PyUnicode_AS_UNICODE(result
);
1580 for (i
= 0; i
< len
; i
++) {
1582 val
= caml_callback(closure
, index
);
1583 p
[i
] = Int_val(val
) & 0x7FFFFFFF;
1587 CAMLreturn(pywrap_steal(result
));
1590 CAMLprim value
PyUnicode_AsUnicode_wrapper(value uni
)
1595 PyObject
*py_uni
= pyunwrap(uni
);
1596 Py_UNICODE
*p
= PyUnicode_AsUnicode(py_uni
);
1599 Py_ssize_t len
= PyUnicode_GET_SIZE(py_uni
);
1602 result
= caml_alloc(len
, 0);
1603 for (i
= 0; i
< len
; i
++)
1604 Store_field(result
, i
, Val_int(p
[i
]));
1610 /*-----------------------------------------------------------------------*/
1612 /* Value -> Pyobject */
1614 value
pywrapvalue( value cb
) {
1616 CAMLreturn(pywrap_steal(camlwrap(cb
,NULL
,0)));
1617 /* T.F.: camlwrap already gives us a reference. We steal that. */
1620 /* For pills, we use an extension: */
1621 value
pywrapvalue_pill( value cb
) {
1623 CAMLreturn(pywrap_steal(camlwrap_pill(cb
,NULL
,0)));
1627 value
pycaml_seterror(value ml_err
,value ml_str
)
1629 CAMLparam2(ml_err
,ml_str
);
1633 nr_err
=Int_val(ml_err
);
1637 err
=PyExc_Exception
;
1640 #if PY_MAJOR_VERSION <= 2
1641 err
=PyExc_StandardError
;
1644 /* PyExc_StandardError is obsolete. Maybe it would be better
1645 * to raise an OCaml exception. */
1646 err
=PyExc_Exception
;
1650 err
=PyExc_ArithmeticError
;
1653 err
=PyExc_LookupError
;
1656 err
=PyExc_AssertionError
;
1659 err
=PyExc_AttributeError
;
1665 err
=PyExc_EnvironmentError
;
1668 err
=PyExc_FloatingPointError
;
1674 err
=PyExc_ImportError
;
1677 err
=PyExc_IndexError
;
1683 err
=PyExc_KeyboardInterrupt
;
1686 err
=PyExc_MemoryError
;
1689 err
=PyExc_NameError
;
1692 err
=PyExc_NotImplementedError
;
1698 err
=PyExc_OverflowError
;
1701 err
=PyExc_ReferenceError
;
1704 err
=PyExc_RuntimeError
;
1707 err
=PyExc_SyntaxError
;
1710 err
=PyExc_SystemExit
;
1713 err
=PyExc_TypeError
;
1716 err
=PyExc_ValueError
;
1719 err
=PyExc_ZeroDivisionError
;
1722 /* Maybe it would be better here to raise an OCaml
1724 #if PY_MAJOR_VERSION <= 2
1725 err
=PyExc_StandardError
;
1727 err
=PyExc_Exception
;
1731 PyErr_SetString(err
,String_val(ml_str
));
1732 CAMLreturn(Val_unit
);
1735 value
pyunwrapvalue(value cb
)
1740 v
= (value
*) PyCapsule_GetPointer(pyunwrap(cb
), "caml-pill");
1742 v
= (value
*) PyCapsule_GetPointer(pyunwrap(cb
), "caml-other");
1744 v
= (value
*) PyCObject_AsVoidPtr(pyunwrap(cb
));
1749 /* Create the function table */
1751 typedef struct _python_func_table
{
1756 } python_func_table
;
1758 int PyRun_SimpleString_(const char *command
)
1760 return PyRun_SimpleStringFlags(command
, NULL
);
1763 int PyRun_SimpleFile_(FILE *fp
, const char *filename
)
1765 return PyRun_SimpleFileExFlags(fp
, filename
, 0, NULL
);
1768 int PyRun_AnyFile_(FILE *fp
, const char *filename
)
1770 return PyRun_AnyFileExFlags(fp
, filename
, 0, NULL
);
1773 int PyRun_InteractiveOne_(FILE *fp
, const char *filename
)
1775 return PyRun_InteractiveOneFlags(fp
, filename
, NULL
);
1778 int PyRun_InteractiveLoop_(FILE *fp
, const char *filename
)
1780 return PyRun_InteractiveLoopFlags(fp
, filename
, NULL
);
1783 int PyRun_AnyFileEx_(FILE *fp
, const char *filename
, int closeit
)
1785 return PyRun_AnyFileExFlags(fp
, filename
, closeit
, NULL
);
1788 int PyRun_SimpleFileEx_(FILE *fp
, const char *filename
, int closeit
)
1790 return PyRun_SimpleFileExFlags(fp
, filename
, closeit
, NULL
);
1793 PyObject
* PyRun_String_(const char *str
, int start
, PyObject
*globals
, PyObject
*locals
)
1795 return PyRun_StringFlags(str
, start
, globals
, locals
, NULL
);
1798 PyObject
* PyRun_File_(FILE *fp
, const char *filename
, int start
, PyObject
*globals
, PyObject
*locals
)
1800 return PyRun_FileExFlags(fp
, filename
, start
, globals
, locals
, 0, NULL
);
1803 PyObject
* PyRun_FileEx_(FILE *fp
, const char *filename
, int start
, PyObject
*globals
, PyObject
*locals
, int closeit
)
1805 return PyRun_FileExFlags(fp
, filename
, start
, globals
, locals
, closeit
, NULL
);
1808 PyObject
* Py_CompileString_(const char *str
, const char *filename
, int start
)
1810 return Py_CompileStringFlags(str
, filename
, start
, NULL
);
1813 PyObject
* PyImport_ImportModuleEx_(char *name
, PyObject
*globals
, PyObject
*locals
, PyObject
*fromlist
)
1815 return PyImport_ImportModuleLevel(name
, globals
, locals
, fromlist
, -1);
1818 enum PycamlTypeLabels
{
1835 EitherStringType
, /* Signifies that either of BytesType or UnicodeType is allowed. */
1836 CamlpillSubtype
, /* Signifies that only the particular Camlpill variety is allowed. */
1837 AnyType
/* Allow any python object. */
1840 value
pytype( value obj
) {
1842 PyObject
*pobj
= pyunwrap( obj
);
1843 if( !pobj
) CAMLreturn(NullType
);
1844 else if( PyTuple_Check( pobj
) ) CAMLreturn(Val_int(TupleType
));
1845 else if( PyBytes_Check( pobj
) ) CAMLreturn(Val_int(BytesType
));
1846 else if( PyUnicode_Check( pobj
) ) CAMLreturn(Val_int(UnicodeType
));
1847 else if( PyBool_Check( pobj
) ) CAMLreturn(Val_int(BoolType
));
1848 #if PY_MAJOR_VERSION <= 2
1849 else if( PyInt_Check( pobj
) ) CAMLreturn(Val_int(IntType
));
1851 else if( PyLong_Check( pobj
) ) CAMLreturn(Val_int(IntType
));
1853 else if( PyFloat_Check( pobj
) ) CAMLreturn(Val_int(FloatType
));
1854 else if( PyList_Check( pobj
) ) CAMLreturn(Val_int(ListType
));
1855 else if( pobj
== Py_None
) CAMLreturn(Val_int(NoneType
));
1856 else if( PyCallable_Check( pobj
) ) CAMLreturn(Val_int(CallableType
));
1857 else if( PyModule_Check( pobj
) ) CAMLreturn(Val_int(ModuleType
));
1858 #if PY_MAJOR_VERSION <= 2
1859 else if( PyClass_Check( pobj
) ) CAMLreturn(Val_int(ClassType
));
1861 else if( PyType_Check( pobj
) ) CAMLreturn(Val_int(TypeType
));
1862 else if( PyDict_Check( pobj
) ) CAMLreturn(Val_int(DictType
));
1864 else if (PyCapsule_IsValid(pobj
, "caml-pill"))
1865 CAMLreturn(Val_int(CamlpillType
));
1866 #else /* USE_PYCAPSULE */
1867 else if (PyCObject_Check(pobj
))
1869 void *desc
= PyCObject_GetDesc(pobj
);
1870 if (desc
== (void *) ocamlpill_token
)
1871 CAMLreturn(Val_int(CamlpillType
));
1873 CAMLreturn(Val_int(OtherType
));
1875 #endif /* USE_PYCAPSULE */
1877 CAMLreturn(Val_int(OtherType
));
1880 value
pytuple_fromarray( value array
) {
1882 PyObject
*tuple
= PyTuple_New(Wosize_val(array
));
1886 for( i
= 0; i
< Wosize_val(array
); i
++ )
1889 entry
=pyunwrap(Field(array
,i
));
1891 entry's reference count was increased by one because it is visible
1892 from within OCaml (and OCaml's GC will take care of decreasing
1893 this again upon finalization. But now, we do add another use to
1894 entry. So, we have to increase its reference count manually:
1896 Note that even if tuple contained some python value before
1897 (which it does not), we do not have to Py_DECREF the
1898 reference count on the old entry, as the PyTuple_SetItem/PyList_SetItem
1899 does this of its own! Nice, isn't it?
1902 x
= PyTuple_SetItem(tuple
,i
,entry
);
1905 CAMLreturn(pywrap_steal(tuple
));
1908 value
pytuple_toarray( value array
) {
1910 PyObject
*obj
= pyunwrap(array
);
1914 rv
= caml_alloc_tuple(PySequence_Size(obj
));
1915 /* XXX T.F.: actually, using caml_alloc_tuple to get an array is not overly aesthetic... */
1917 for (i
= 0; i
< PySequence_Size(obj
); i
++)
1918 Store_field(rv
, i
, pywrap_steal(PySequence_GetItem(obj
, i
)));
1923 value
pywrap_closure( value closure
) {
1924 CAMLparam1(closure
);
1927 PyMethodDef
*ml_def
;
1928 ml
.ml_name
= "anonymous_closure";
1929 ml
.ml_meth
= pycall_callback
;
1931 ml
.ml_doc
= "Anonymous closure";
1932 obj
= camlwrap(closure
, &ml
, sizeof(ml
));
1933 ml_def
= (PyMethodDef
*) caml_aux(obj
);
1934 CAMLreturn(pywrap_steal(PyCFunction_New(ml_def
, obj
)));
1938 value pymodule_initmodule( value name, value funclist ) { ... }
1939 Removed by T.F., as this piece of code seemed quite buggy....
1942 /* -- T.F. Extensions -- */
1945 In case of "You used it the wrong way" or "this should not happen",
1946 we want to be able to just bailout from python into ocaml with an
1950 Note: this is not being used, as we decided to do error handling the
1951 other way round: python is in charge, ocaml lies "beneath the surface",
1952 so we effectively always bailout into python.
1956 static int pycaml_raise_error(int type
, char *message
)
1959 ex
=caml_alloc_tuple(3);
1960 Store_field(ex
,0,Val_int(type
));
1961 Store_field(ex
,1,caml_copy_string(message
));
1962 raise_with_arg(*caml_named_value("ocaml_exn_pycaml"),ex
);
1966 /* This is just an adjusted copy of pytuple_fromarray. */
1967 value
pylist_fromarray( value array
) {
1969 PyObject
*list
= PyList_New(Wosize_val(array
));
1973 for( i
= 0; i
< Wosize_val(array
); i
++ )
1976 entry
=pyunwrap(Field(array
,i
));
1977 /* T.F.: See pytuple_fromarray code comments! */
1979 x
= PyList_SetItem(list
,i
,entry
);
1981 CAMLreturn(pywrap_steal(list
));
1984 /* We also need it the other way round */
1985 value
pylist_toarray( value pylist
) {
1987 PyObject
*obj
= pyunwrap(pylist
);
1991 rv
= caml_alloc_tuple( PySequence_Size(obj
) );
1993 len
=PySequence_Size(obj
);
1995 for( i
= 0; i
< len
; i
++ )
1996 Store_field(rv
,i
,pywrap_steal(PySequence_GetItem(obj
,i
)));
2001 value
pylist_set( value pylist
, value index
, value v
) {
2002 CAMLparam3(pylist
,index
,v
);
2003 PyObject
*list
, *new_entry
;
2005 list
= pyunwrap(pylist
);
2006 new_entry
=pyunwrap(v
);
2007 Py_INCREF(new_entry
);
2008 PyList_SetItem(list
,Int_val(index
),new_entry
);
2010 CAMLreturn(Val_unit
);
2013 value
pylist_get( value pylist
, value index
) {
2014 CAMLparam2(pylist
,index
);
2015 PyObject
*list
= pyunwrap(pylist
);
2017 /* T.F.: According to the Python docs, we own the reference produced by
2018 PySequence_GetItem. Hence, we have to steal that reference...
2020 CAMLreturn(pywrap_steal(PySequence_GetItem(list
,Int_val(index
))));
2025 /* It's nice to have this variant of pywrap_closure */
2026 value
pywrap_closure_docstring(value docstring
, value closure
) {
2027 CAMLparam2(docstring
, closure
);
2030 PyMethodDef
*ml_def
;
2031 ml
.ml_name
= "anonymous_closure";
2032 ml
.ml_meth
= pycall_callback
;
2034 ml
.ml_doc
= String_val(docstring
);
2035 obj
= camlwrap(closure
,&ml
,sizeof(ml
));
2036 ml_def
= (PyMethodDef
*) caml_aux(obj
);
2037 CAMLreturn(pywrap_steal(PyCFunction_New(ml_def
,obj
)));
2040 /* Using pyrun_interactiveloop the way it was in the original code
2041 may work on some systems, but then just by chance. We have to
2042 do this in a cleaner way:
2044 value
pycaml_prompt(value ml_unit
) {
2045 CAMLparam1(ml_unit
);
2047 PyRun_InteractiveLoop(stdin
,"<stdin>");
2049 CAMLreturn(Val_unit
);
2053 /* The function below is highly useful for debugging! */
2054 value
pyrefcount(value pyobj
) {
2056 PyObject
*obj
= pyunwrap(pyobj
);
2058 CAMLreturn(Val_int(obj
->ob_refcnt
));