Library
Module
Module type
Parameter
Class
Class type
Embedding Python into OCaml.
(C) arty 2002
This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
A Derivative of Art Yerkes' 2002 Pycaml module.
Modifications (C) 2005 Dr. Thomas Fischbacher, Giuliano Bordignon, Dr. Hans Fangohr, SES, University of Southampton
More modifications are by Barry Schwartz. Copyright (C) 2009 Barry Schwartz.
Adapted for py.ml by Thierry Martinez. Copyright (C) 2016 Thierry Martinez.
The original code is available in Debian as package "pycaml".
For various reasons, we hijacked it so that we can easily both fix bugs and extend it. This is permitted by the Pycaml license (the GNU LGPL).
Note: the layout and hierarchical structure of the documentation could need some more work.
type pyobject = Py.Object.t
Python objects are wrapped up within OCaml as entities of type pyobject
.
The following types are slightly esoteric; normally, users of this module should not have any need to access them.
type funcent = funcptr * int * int * bool
As Python is a dynamically typed language, pyobject
values may represent entities of very different nature. The pytype
function maps a pyobject to its type, or rather, a selection of types that have been made known to OCaml. The default for "unknown" values is OtherType.
Note (for advanced users only): This in particular holds for PyCObject
values, which are used at present to wrap up OCaml values opaquely within Python values: at present, these appear to be of type OtherType
, but there might be good reason to change this in the future.
type pyerror_type =
| Pyerr_Exception
| Pyerr_StandardError
| Pyerr_ArithmeticError
| Pyerr_LookupError
| Pyerr_AssertionError
| Pyerr_AttributeError
| Pyerr_EOFError
| Pyerr_EnvironmentError
| Pyerr_FloatingPointError
| Pyerr_IOError
| Pyerr_ImportError
| Pyerr_IndexError
| Pyerr_KeyError
| Pyerr_KeyboardInterrupt
| Pyerr_MemoryError
| Pyerr_NameError
| Pyerr_NotImplementedError
| Pyerr_OSError
| Pyerr_OverflowError
| Pyerr_ReferenceError
| Pyerr_RuntimeError
| Pyerr_SyntaxError
| Pyerr_SystemExit
| Pyerr_TypeError
| Pyerr_ValueError
| Pyerr_ZeroDivisionError
exception Pycaml_exn of pyerror_type * string
val pytype : pyobject -> pyobject_type
Also note the existence of pytype_name
, which maps python types to human-readable strings.
The Python interpreter has to be initialized, which is done via py_initialize
. Note that this module does call this function automatically when it is initialized itself, so the end user does not have to worry about this.
Note that Python initialization seems to be idempotent, so there should not be any problems if one starts up a python interpreter first, and then loads a shared object via Python's foreign function interface which itself initializes OCaml and pycaml. (Note: However, this still needs more testing!)
There is a collection of functions from the original Pycaml module which are not-too-well-documented. For some of them, there are examples available, and often, one can guess what they are supposed to do from their name and type. (Admittedly, this is a quite unsatisfactory state of affairs, but on the other hand, as it turns out, we will have to use only very few of them. So for now, if there is a question, look at the source, or ask t.fischbacher\@soton.ac.uk
.)
In order not to clutter the Pycaml documentation with a block of unreadable code, they have been moved to the last section.
The Pycaml functions pywrap_value
and pyunwrap_value
are elementary low-level primitives to make opaque Python values that hold OCaml values. As Python is a dynamically typed language, and OCaml is a statically typed language, and both achieve safety in a somewhat misaligned way, this interface may be considered as dangerous. In fact, it allows one to break OCaml type safety by mapping a statically typed value to a dynamically typed Python value and back.
This means that a Python user handing a wrapped-up ocaml value of a different type than expected over to an OCaml callback may crash the system.
This module provides an extension to the original Pycaml which will have added checks that prevent precisely such a situation and therefore is safer. Note however, that at the moment, it is only foolproof if one does not mix this up with other PyCObject
Python values. (Presumably, it can be tightened up by introducing a new primitive Python type PyCamlObject
. TODO.)
val pywrap_value : 'a -> pyobject
val pyunwrap_value : pyobject -> 'a
val py_repr : pyobject -> string
Map an OCaml array of Python values to a Python list and vice versa. (This was just missing.)
val pyrefcount : pyobject -> int
While the functions in ocaml.* should not be made visible to end users directly, it may nevertheless be helpful to be able to set docstrings on them.
val pytype_name : pyobject_type -> string
Return a name-string for an Ocaml Python-Object-Type value. Used mainly for debugging and in error messages.
val python_last_value : unit -> pyobject
Return the last value that was computed interactively at the Python prompt
val py_true : unit -> pyobject
val py_false : unit -> pyobject
val py_is_true : pyobject -> bool
val register_for_python : (string * pyobject) array -> unit
A convenient function to make a collection of pyobject
values (which usually will be OCaml callbacks) known to Python in one go. The strings give the names under which the corresponding values should appear in Python's "ocaml
" module, which Pycaml will add to Python and automatically import
on the Python side.
Note that as a convention, one must not register names that start with the string "example_
" or "sys_
", as those are reserved for internal use by Pycaml.
val register_pre_functions_for_python :
(string * (string -> pyobject)) array ->
unit
val float_array_to_python : float array -> pyobject
val int_array_to_python : int array -> pyobject
These functions provides a quick and convenient way to pass a simple array of numbers to Python. Note that neither on the OCaml nor on the Python side, the special data structure for efficient manipulation of large numerical arrays is used (OCaml: bigarray, Python: numarray). Rather, this just maps ordinary arrays.
This little helper creates a nested float array python structure that is supposed to represent a multi-indexed tensor, plus a function to set tensor entries.
val py_float_list_as_array :
?error_label:string ->
?length:int ->
pyobject ->
float array
val py_number_list_as_float_array :
?error_label:string ->
?length:int ->
pyobject ->
float array
val py_int_list_as_array :
?error_label:string ->
?length:int ->
pyobject ->
int array
val py_string_list_as_array :
?error_label:string ->
?length:int ->
pyobject ->
string array
val py_float_list_list_as_array :
?error_label:string ->
?length_outer:int ->
?length_inner:int ->
pyobject ->
float array array
val py_number_list_list_as_float_array :
?error_label:string ->
?length_outer:int ->
?length_inner:int ->
pyobject ->
float array array
val py_int_list_list_as_array :
?error_label:string ->
?length_outer:int ->
?length_inner:int ->
pyobject ->
int array array
val py_string_list_list_as_array :
?error_label:string ->
?length_outer:int ->
?length_inner:int ->
pyobject ->
string array array
val unpythonizing_function :
?name:string ->
?catch_weird_exceptions:bool ->
?extra_guards:(pyobject -> string option) array ->
?expect_tuple:bool ->
pyobject_type array ->
(pyobject array -> 'a) ->
pyobject ->
'a
val pythonize_string : string -> pyobject
val unpythonize_string : pyobject -> string
val python_interfaced_function :
?name:string ->
?catch_weird_exceptions:bool ->
?doc:string ->
?extra_guards:(pyobject -> string option) array ->
pyobject_type array ->
(pyobject array -> pyobject) ->
pyobject
This helper simplifies the creation of OCaml callbacks that can be registered in Python's "ocaml
" module.
First argument: An array of pyobject_type
Python types.
Second argument: A "body" function B mapping an OCaml array of Python values to a Python return value.
Optional argument: An array of extra checks to be performed on the arguments, one by one, returning an optional error message.
The body function (as well as the optional checks) will be wrapped up in code that first checks for the correct number and the specified Python types of arguments, so B can rely on the n'th entry of its Python argument array being of the Python type specified in the n'th position of the type array.
XXX Note: we need examples in the documentation!
val python_pre_interfaced_function :
?catch_weird_exceptions:bool ->
?doc:string ->
?extra_guards:(pyobject -> string option) array ->
pyobject_type array ->
(pyobject array -> pyobject) ->
string ->
pyobject
Sometimes, we want to manipulate complicated structures via Python which are implemented in OCaml, and about whose interna only OCaml should know and have to worry. So, all that one can do from Python is to place such values in containers (tuples, lists) and retrieve them back, pass them around, and hand them over to OCaml callbacks.
In order to ensure type safety, we have to extend OCaml by a primitive dynamic type system for Python-wrapped OCaml values. This is based on the following assumptions:
Thus, before one can opaquely wrap up OCaml values in "ocamlpills" for Python, one has to register a type name with Pycaml. From the Python side, the function ocaml.sys_ocamlpill_type(x)
will map the ocamlpill x
to the registered type string.
py.ml: Pill types are not required to be registered. This function does nothing and is provided for compatibility only.
val ocamlpill_type_of : pyobject -> string
Given an ocamlpill type name (which was registered before using register_ocamlpill_type
), as well as a witness of the type in question in form of a prototypical OCaml value, make a function that maps other OCaml values of the same type as the prototype to Python ocamlpills. This function is exported to python as ocaml.sys_ocamlpill_type
.
Note: a simple type system hack is used to ensure that the wrapper function generated can only be applied to OCaml values of the proper type. One major drawback of this is that presumably, the prototypical object provided cannot be garbage collected until the wrapper function is. (A clever compiler might be able to figure out how to get rid of that, though.)
XXX Provide example code!
val check_pill_type :
?position:'a ->
?exn_name:string ->
string ->
pyobject ->
unit
py.ml: the signature has been changed from string -> 'a -> ('a -> pyobject) * (pyobject -> 'b)
to string -> 'a -> ('a -> pyobject) * (pyobject -> 'a)
. The second argument is ignored and the function calls Py.Capsule.make
. Applying the function twice to the same type name raises a failure (Failure _
).
py.ml: the signature has been changed from string -> 'a -> ('a -> pyobject) * (pyobject -> 'b)
to string -> 'a -> ('a -> pyobject) * (pyobject -> 'a)
. The second argument is ignored and the function calls Py.Capsule.make
. Applying the function twice to the same type name raises a failure (Failure _
).
Also, we want to be able to pass optional arguments from python to OCaml. The convention which we use for now is as follows:
We then need ocaml functions that make it convenient to handle the automatic unpacking of such values. (XXX Note: we need examples in the documentation that show how to use this!)
val guarded_pyint_asint : pyobject -> int
val guarded_pyfloat_asfloat : pyobject -> float
val guarded_pynumber_asfloat : pyobject -> float
val guarded_pybytes_asstring : pyobject -> string
val ocamlpill_hard_unwrap : pyobject -> 'a
This is semi-internal - It should only be used for writing other convenience type applicators that have their own way of doing the checking.
This function allows us to set Python's sys.argv
A convenience function for just letting the Python interpreter evaluate a block of Python code.
One may use python_eval "execfile(...)"
to load Python code into the interpreter. This function provides a slightly nicer way to do the same.
Note 1: Internally, this uses python_eval
. The int
return value is ignored, however.
Note 2: As we do not bother to properly escape quotation marks, this will not work as supposed on filenames containing double quotes. (Yes, this is a bug and should better be fixed!)
Start the ipython toplevel. Note: for still unknown reasons, this does not seem to be 100% reliable, and especially seems to fail in many situations where Pycaml.ipython()
is called not from the OCaml toplevel. May be some crazy terminal handling bug.
Addition: 23/01/2006 fangohr:
On Mac OS X, one of the problems is that there are often several Python installations. (One is provided by Apple, but usually a fink or Darwinport installation is actually meant to use.) For fink-python (the binary installed in /sw/bin, it helps to set the shell environment variable PYTHONHOME=/sw .
Then the call to ipython works fine.
All functions which are made visible from OCaml to python by means of register_for_python
go into the Python module ocaml
. Usually, one wants to place low-level interface functions there and build higher levels of abstraction on the python side on top of it which are more convenient (maybe object-oriented) to the Python end user. So, the user of a Python library that uses OCaml callbacks internally should (ideally) never notice the existence of the ocaml
Python module.
The following names are pre-registered in the ocaml
module. Note that they all start with the reserved prefixes sys_
or example_
.
sys_ocamlpill_type
: Function that maps an OCaml pill to a type string, so that Python can find out what a given pill is supposed to be. (The OCaml function name is ocamlpill_type_of
.)sys_python
: Function that starts a recursive Python toplevel. This may seem strange at first, but actually is highly useful e.g. for providing some interactive control deep inside a contrived function during debugging. Return value is the value computed last on the recursive python command prompt.example_test_interface
: Function that just prints a test string.example_the_answer
: The number "42", put in the ocaml
module by OCaml.example_make_powers
: A function mapping an integer n
and a float p
to the array
[|1.0**p,2.0**p,...,(float_of_int n)**p|]
.
example_hypotenuse
: A function mapping two floatingpoint values x,y
to sqrt(x**2+y**2)
.It is instructive to have a look at the pycaml source providing the example_
entries to see how one can publish other constants and functions to Python.
The implementations of example_make_powers
and example_hypotenuse
demonstrate how to use python_interfaced_function
:
let _py_make_powers = python_interfaced_function ~extra_guards: [|(fun py_len -> let len = pyint_asint py_len in if len < 0 then Some "Negative Length" else None); (fun _ -> None); (* This check never fails *) |] [|IntType;FloatType|] (fun py_args -> let len = pyint_asint py_args.(0) and pow = pyfloat_asdouble py_args.(1) in float_array_to_python (Array.init len (fun n -> let nn = float_of_int (n+1) in nn**pow))) and _py_hypotenuse_2d = python_interfaced_function [|FloatType;FloatType|] (fun py_args -> let x = pyfloat_asdouble py_args.(0) and y = pyfloat_asdouble py_args.(1) in pyfloat_fromdouble (sqrt(x*.x+.y*.y))) in register_for_python [|("example_make_powers", _py_make_powers); ("example_hypotenuse", _py_hypotenuse_2d); |] ;;
val py_compilestring : (string * string * int) -> pyobject
val pyobject_print : (pyobject * int * int) -> int
val pyobject_istrue : pyobject -> int
val pyobject_not : pyobject -> int
val pycallable_check : pyobject -> int
val pyobject_hasattrstring : (pyobject * string) -> int
val pyobject_hash : pyobject -> int64
val pybytes_size : pyobject -> int
val pystring_size : pyobject -> int
val pybytes_asstring : pyobject -> string
val pystring_asstring : pyobject -> string
val pybytes_asstringandsize : pyobject -> string
val pystring_asstringandsize : pyobject -> string
val pybytes_fromstring : string -> pyobject
val pystring_fromstring : string -> pyobject
val pyunicode_decodeutf8 : (string * string option) -> pyobject
val pyunicode_decodeutf16 : (string * string option * int option) -> pyobject
val pyunicode_decodeutf32 : (string * string option * int option) -> pyobject
val pyunicode_fromunicode : (int -> int) -> int -> pyobject
val pyunicode_asunicode : pyobject -> int array
val pyunicode_getsize : pyobject -> int
val pydict_new : unit -> pyobject
val pydict_clear : pyobject -> unit
val pydict_size : pyobject -> int
val pydict_delitemstring : (pyobject * string) -> int
val pyint_fromlong : int64 -> pyobject
val pyint_aslong : pyobject -> int64
val pyfloat_fromdouble : float -> pyobject
val pyfloat_asdouble : pyobject -> float
val pymodule_new : string -> pyobject
val pymodule_getname : pyobject -> string
val pymodule_getfilename : pyobject -> string
val pytuple_new : int -> pyobject
val pytuple_size : pyobject -> int
py.ml: the result type has been changed from int
to pyobject
.
val pyerr_setnone : pyobject -> unit
val pyerr_setstring : (pyobject * string) -> unit
val pyerr_occurred : unit -> pyobject
val pyerr_exceptionmatches : pyobject -> int
val pyimport_getmoduledict : unit -> pyobject
val pyimport_addmodule : string -> pyobject
val pyimport_importmodule : string -> pyobject
val pyeval_getbuiltins : unit -> pyobject
val pyeval_getglobals : unit -> pyobject
val pyeval_getlocals : unit -> pyobject
val pyobject_size : pyobject -> int
val pyobject_ascharbuffer : pyobject -> string
val pyobject_asreadbuffer : pyobject -> string
val pyobject_aswritebuffer : pyobject -> string
val pynumber_check : pyobject -> int
val pysequence_check : pyobject -> int
val pysequence_size : pyobject -> int
val pysequence_length : pyobject -> int
py.ml: the result type has been changed from int
to pyobject
.
val pysequence_delitem : (pyobject * int) -> int
py.ml: one of the two pyobject
arguments has been removed.
val pysequence_delslice : (pyobject * int * int) -> int
val pymapping_check : pyobject -> int
val pymapping_size : pyobject -> int
val pymapping_length : pyobject -> int
val pymapping_haskeystring : (pyobject * string) -> int
val pyiter_check : pyobject -> int
val pynull : unit -> pyobject
val pynone : unit -> pyobject
val pytuple_empty : pyobject
val pyint_fromint : int -> pyobject
val pyint_asint : pyobject -> int