1. camlspotter
  2. opycaml

Commits

camlspotter  committed 8e935f2

python setup.py

  • Participants
  • Parent commits de35bfb
  • Branches default

Comments (0)

Files changed (5)

File OMakefile

View file
  • Ignore whitespace
 
 MyOCamlPackedLibrary(opycaml, utils type autoapi api, auto_stubs api_ml, -lpython2.6 -L$(PREFIX)/lib/ocaml -lcamlidl)
 
-.DEFAULT: opycaml.cma opycaml.cmxa
+opycaml_python.o: opycaml.cmx libopycaml.a dllopycaml.so
+	$(OCamlOpt) -output-obj -o $@ opycaml.cmx -cclib -lopycaml -ccopt -L$(OCAML_WHERE) -ccopt -Wl,-rpath,$(OCAML_WHERE) -cclib -lpython2.6 -cclib -lcamlidl 
+
+.DEFAULT: opycaml.cma opycaml.cmxa opycaml_python.o
 
 OCAML_LIBS += opycaml
 
 .DEFAULT: $(OCamlProgram test_bug1, test_bug1)
 
 .PHONY: install
-install: opycaml.cma opycaml.cmxa
-	$(OCAMLFIND) install opycaml opycaml.cmi opycaml.cma opycaml.cmxa opycaml.a dllopycaml.so libopycaml.a autoapi.ml api.ml META
+install: opycaml.cma opycaml.cmxa libopycaml_python.so
+	$(OCAMLFIND) install opycaml opycaml.cmi opycaml.cma opycaml.cmxa opycaml.a dllopycaml.so libopycaml.a autoapi.ml api.ml META libopycaml_python.so

File api.ml

View file
  • Ignore whitespace
   	| Some f -> try Ok (f self_option args) with e -> Error (Printexc.to_string e)
     ;;
   
-    let initialize () = OCaml.Callback.register "( ゚∀゚)o彡°O'PyCaml callback" callback
+    external python_init : unit -> unit = "opycaml_init_callback"
+
+    let initialize () = 
+      OCaml.Callback.register "( ゚∀゚)o彡°O'PyCaml callback" callback;
+      python_init ()
   end
   
   module Base : sig

File api_ml.c

View file
  • Ignore whitespace
     caml_raise_with_args(*opycaml_exc, 2, vs); 
 }
 
-// custom value
-
-static void pydecref( value v ) {
-    PyObject *py = PyObject_val(v);
-    if( py ) { Py_DECREF(py); }
-}
-
-static int pycompare( value v1, value v2 ) {
-    int result;
-    PyObject *p1 = PyObject_val(v1);
-    PyObject *p2 = PyObject_val(v2);
-    if( p1 && !p2 ) return -1;
-    if( p2 && !p1 ) return 1;
-    if( !p1 && !p2 ) return 0;
-    PyObject_Cmp(p1, p2, &result);
-    return result;
-}
-
-static long pyhash( value v ) {
-    PyObject *p = PyObject_val(v);
-    if(p) return PyObject_Hash(p);
-    else return 0;
-}
-
-// CR jfuruse: TODO!
-static unsigned long pydeserialize( void *dst ) {
-    return 0;
-}
-
-struct custom_operations pyops = {
-    "PythonObject",
-    pydecref,
-    pycompare,
-    pyhash,
-    custom_serialize_default,
-    pydeserialize
-};
-
-struct custom_operations fnops = {
-    "FuncPointer",
-    NULL,
-    NULL,
-    NULL,
-    NULL,
-    NULL
-};
-
 // apis for refcnts
 
 value opycaml_refcnt(value obj){
 
 void opycaml_decref(value obj){
     PyObject *o = PyObject_val(obj);
-    Py_DECREF(o);
+    if( o ) Py_DECREF(o);
     return;
 }
 
     return caml_copy_nativeint((int)o);
 }
 
+// custom value
+
+static int opycaml_compare( value v1, value v2 ) {
+    int result;
+    PyObject *p1 = PyObject_val(v1);
+    PyObject *p2 = PyObject_val(v2);
+    if( p1 && !p2 ) return -1;
+    if( p2 && !p1 ) return 1;
+    if( !p1 && !p2 ) return 0;
+    PyObject_Cmp(p1, p2, &result);
+    return result;
+}
+
+static long opycaml_hash( value v ) {
+    PyObject *p = PyObject_val(v);
+    if(p) return PyObject_Hash(p);
+    else return 0;
+}
+
+// CR jfuruse: TODO!
+static unsigned long opycaml_deserialize( void *dst ) {
+    return 0;
+}
+
+struct custom_operations pyops = {
+    "PythonObject",
+    opycaml_decref,
+    opycaml_compare,
+    opycaml_hash,
+    custom_serialize_default,
+    opycaml_deserialize
+};
+
+// struct custom_operations fnops = {
+//     "FuncPointer",
+//     NULL,
+//     NULL,
+//     NULL,
+//     NULL,
+//     NULL
+// };
+
 // val <-> PyObject conversion 
 
 value Val_PyObject( PyObject *obj, int incr ) {
     CAMLreturn(Val_int(res));
 }
 
-// Callbacks from pycaml. Not investigated yet. 
-
-value funcwrap( void *obj ) {
-    CAMLparam0();
-    CAMLlocal1(v);
-    v = alloc_custom( &fnops, sizeof( void * ), 100, 100000 );
-    *((void **)Data_custom_val(v)) = obj;
-    CAMLreturn(v);
-}
-
-static void camldestr( void *v ) {
-    value *valptr = (value *)v;
-    remove_global_root(valptr);
-    free( v );
-}
-
-PyObject *camlwrap( value val, void *aux_str, int size ) {
-    // v stores value + aux_str
-    value *v = (value *)malloc(sizeof(value) + size);
-    *v = val;
-    memcpy((void *)v+sizeof(value),aux_str,size);
-    register_global_root(v); // Prevent val from being GCed
-    return PyCObject_FromVoidPtr(v,camldestr);
-}
-
-// Get Caml value in Python and return its aux_str.
-void *caml_aux( PyObject *obj ) {
-    value *v = (value *)PyCObject_AsVoidPtr( obj );
-    return (void *)v+sizeof(value);
-}
-
-PyObject *pycall_callback( PyObject *obj, PyObject *args ) {
-    value out;
-    value *v;
-    
-    // If the obj is strange, returns None
-    if( !PyCObject_Check(obj) ) {
-        Py_INCREF(Py_None);
-        return Py_None;
-    }
-    // obj as caml value
-    v = (value *)PyCObject_AsVoidPtr( obj );
-    out = callback(*v, Val_PyObject(args, 1)); // takes only 1 argument
-    return PyObject_val(out);
-}
-
 PyObject *opycaml_callback( PyObject *self, PyObject *args_tuple) {
     value out;
 
         return NULL;
     }
 }
+
+static PyMethodDef OPyCamlMethods[] = {
+    { "ocaml", opycaml_callback, METH_VARARGS, "Call registered OCaml functions" },
+    { NULL, NULL, 0, NULL }
+};
+
+PyMODINIT_FUNC initopycaml(void)
+{
+    (void) Py_InitModule("opycaml", OPyCamlMethods);
+}
+
+CAMLprim void opycaml_init_callback(void)
+{
+    initopycaml();
+}

File python/opycaml_ext.c

View file
  • Ignore whitespace
+// dummy

File python/setup.py

View file
  • Ignore whitespace
+from distutils.core import setup, Extension
+
+module1 = Extension('opycaml',
+                    sources = ['opycaml_ext.c'],
+                    libraries = ['asmrun', 'camlidl'],
+                    extra_objects = ['../opycaml_python.o', '../api_ml.o', '../auto_stubs.o'],
+                    library_dirs = ['/home/jfuruse/.share/prefix/lib/ocaml', '/usr/lib/ocaml'])
+
+setup ( name = 'OPyCaml',
+        version = '1.0',
+        description = 'O\'PyCaml',
+        ext_modules = [module1] )
+
+