SourceForge Logo

What is Pycaml?

A library written by which follows the Python/C API as closely as possible, while providing equivalent functionality for objective caml. This is built against python 2.x and Ocaml 3.04.

It is intended to allow users to build native ocaml libraries and use them from python, and alternately, in order to allow ocaml users to benefit from linkable libraries provided for python.

I created this library in order to take advantage of python binding for certain native libraries from ocaml. While it is true that I could have written new interfaces specifically for ocaml, the python interface is sufficient for my needs, and this project was easier.


Python Objective Caml

Why use Pycaml?

There are a few reasons to use pycaml;

Bug fix release 0.82 available. It was pointed out by a nice user that PyTuple_SetItem steals a reference, so reference counts for tuplized values would be off-by-one. Fixed in 0.82.

Note that used to be here: Ocaml 3.07 and Python 2.3 do not conflict, so you can avoid the earlier reported link-time conflict by using current versions of Ocaml and Python.

pycaml uses configure to find the python libraries, etc. Thanks to adonthell for their acinclude.m4.

Get the distribution here:pycaml.tar.gz

Because these are made to closely mirror the python API, the user should become familiar with the python API.

Given Ocaml parameter passing convention, it was convenient to pass multiple arguments as members of a tuple, but single arguments without. Consequently, functions with arity 1, such as pytuple_new are called as

  pytuple_new 3 ;;

And functions with more arguments are called as

  pydict_getitemstring (dict,"keystring") ;;


See The real documentation for detailed documentation.

Sample Application

This is a simple toplevel example for pycaml. This is meant to be the simplest possible example of an Ocaml provided module that has a class with a method. This example might serve as a starting point for custom python toplevels with native code provided by ocaml.
open Pycaml

let foo_bar_print = pywrap_closure (fun x -> print_endline "hi" ; pynone ()) ;;
let sd = pyimport_getmoduledict () ;;
let mx = pymodule_new "CamlModule" ;;
let cd = pydict_new () ;;
let cx = pyclass_new (pynull (), cd, pystring_fromstring "CamlClass") ;;
let cmx = pymethod_new (foo_bar_print,(pynull ()),cx) ;;
let _ = pydict_setitemstring (cd, "CamlMethod", cmx) ;;
let _ = pydict_setitemstring (pymodule_getdict mx, "CamlClass", cx) ;;
let _ = pydict_setitemstring (sd, "CamlModule", mx) ;;
let _ = pyrun_interactiveloop (0,"-") ;;