text
stringlengths
0
601k
let initialize ? library_name ? interpreter ? version ? minor ( ? verbose = false ) false ? debug_build ( ) = if ! initialized then failwith " Py . initialize : already initialized " ; begin match library_name with | Some library_name -> load_library ( Some library_name ) library_name ; | None -> begin try let python_f...
let on_finalize_list = ref [ ]
let on_finalize f = on_finalize_list := f :: ! on_finalize_list
let finalize ( ) = assert_initialized ( ) ; List . iter ( fun f -> f ( ) ) ! on_finalize_list ; Gc . full_major ( ) ; finalize_library ( ) ; uninit_pythonhome ( ) ; uninit_pythonpath ( ) ; initialized := false
let version ( ) = assert_initialized ( ) ; ! version_value
let version_major ( ) = assert_initialized ( ) ; ! version_major_value
let version_minor ( ) = assert_initialized ( ) ; ! version_minor_value
let null = pynull ( )
let is_null v = v == null
let none = pynone ( )
let is_none v = v == none
let fetched_exception = ref None
let python_exception ( ) = let ptype , pvalue , ptraceback = pyerr_fetch_internal ( ) in fetched_exception := Some ( ptype , pvalue , ptraceback ) ptraceback ; raise ( E ( ptype , pvalue ) pvalue ) pvalue
let check_not_null result = if result = null then python_exception ( ) ; result
let check_some s = match s with None -> python_exception ( ) | Some s -> s
let check_error ( ) = if Pywrappers . pyerr_occurred ( ) <> null then python_exception ( )
let check_int result = if result = - 1 then python_exception ( ) else result
let check_int64 result = if result = - 1L then python_exception ( ) else result
let assert_int_success result = if result = - 1 then python_exception ( )
let bool_of_int i = check_int i <> 0
let get_program_name ( ) = if ! initialized then if ! version_major_value <= 2 then Pywrappers . Python2 . py_getprogramname ( ) else Pywrappers . Python3 . py_getprogramname ( ) else ! program_name
let get_python_home ( ) = if ! initialized then if ! version_major_value <= 2 then Pywrappers . Python2 . py_getpythonhome ( ) else Pywrappers . Python3 . py_getpythonhome ( ) else match ! python_home with None -> " " | Some s -> s
let get_program_full_path ( ) = if version_major ( ) <= 2 then Pywrappers . Python2 . py_getprogramfullpath ( ) else Pywrappers . Python3 . py_getprogramfullpath ( )
let get_prefix ( ) = if version_major ( ) <= 2 then Pywrappers . Python2 . py_getprogramfullpath ( ) else Pywrappers . Python3 . py_getprogramfullpath ( )
let get_exec_prefix ( ) = if version_major ( ) <= 2 then Pywrappers . Python2 . py_getexecprefix ( ) else Pywrappers . Python3 . py_getexecprefix ( )
let get_path ( ) = if version_major ( ) <= 2 then Pywrappers . Python2 . py_getpath ( ) else Pywrappers . Python3 . py_getpath ( )
let option result = if result = null then begin check_error ( ) ; None end else Some result
let assert_not_null function_name obj = if is_null obj then invalid_arg ( function_name ^ " : unallowed null argument ) "
module Eval = struct let call_object_with_keywords func arg keyword = assert_not_null " call_object_with_keywords ( call_object_with_keywords , ! _ , _ ) _ " func ; assert_not_null " call_object_with_keywords ( call_object_with_keywords_ , , ! _ ) _ " arg ; check_not_null ( Pywrappers . pyeval_callobjectwithkeywords fu...
let object_repr obj = check_not_null ( Pywrappers . pyobject_repr obj ) obj
module String_ = struct let as_UTF8_string s = assert_not_null " as_UTF8_string " s ; let f = match ucs ( ) with UCS2 -> Pywrappers . UCS2 . pyunicodeucs2_asutf8string | UCS4 -> Pywrappers . UCS4 . pyunicodeucs4_asutf8string | UCSNone -> if ! version_major_value >= 3 then Pywrappers . Python3 . pyunicode_asutf8string e...
module Tuple_ = struct let create size = check_not_null ( Pywrappers . pytuple_new size ) size let set_item s index value = assert_int_success ( Pywrappers . pytuple_setitem s index value ) value let set = set_item let init size f = let result = create size in for index = 0 to size - 1 do let v = f index in assert_not_...
let id x = x
module Dict_ = struct let create ( ) = check_not_null ( Pywrappers . pydict_new ( ) ) let set_item dict key value = assert_not_null " set_item ( set_item , ! _ ) _ " dict ; assert_not_null " set_item ( set_item_ , ) " ! key ; assert_int_success ( Pywrappers . pydict_setitem dict key value ) value let of_bindings_map fk...
module Object_ = struct let call_function_obj_args callable args = check_not_null ( pyobject_callfunctionobjargs callable args ) args end
module Type = struct let is_none v = v == none let none = None type t = Unknown | Bool | Bytes | Callable | Capsule | Closure | Dict | Float | List | Int | Long | Module | None | Null | Tuple | Type | Unicode | Iter | Set external get : pyobject -> t = " pytype " let is_subtype a b = assert_not_null " of_tuple5 ( of_tu...
module Capsule = struct let is_valid v name = pycapsule_isvalid v name <> 0 let check v = is_valid v " ocaml - capsule " let table = Hashtbl . create 17 let ( ) = on_finalize ( fun ( ) -> Hashtbl . clear table ) table external unsafe_wrap_value : ' a -> pyobject = " pyml_wrap_value " external unsafe_unwrap_value : pyob...
module Mapping = struct let check v = Pywrappers . pymapping_check v <> 0 let get_item_string mapping key = option ( Pywrappers . pymapping_getitemstring mapping key ) key let find_string mapping key = Stdcompat . Option . get ( get_item_string mapping key ) key let find_string_opt = get_item_string let has_key mapping...
module Method = struct let create func self cl = assert_not_null " create ( create , ! _ , _ ) _ " func ; assert_not_null " create ( create_ , , ! _ ) _ " self ; assert_not_null " create ( create_ , _ , ) " ! cl ; check_not_null ( Pywrappers . pymethod_new func self cl ) cl let get_function m = assert_not_null " get_fu...
module Bool = struct let t = pytrue ( ) let is_true v = v == t let f = pyfalse ( ) let is_false v = v == f let check v = v = t || v = f let of_bool b = if b then t else f let to_bool v = if v = t then true else if v = f then false else Type . mismatch " True or False " v end
module Float = struct let check o = Type . get o = Type . Float let of_float = Pywrappers . pyfloat_fromdouble let to_float v = let result = Pywrappers . pyfloat_asdouble v in if result = - 1 . 0 then check_error ( ) ; result end
type byteorder = LittleEndian | BigEndian
module String__ = struct include String_ let check_bytes s = Type . get s = Type . Bytes let check_unicode s = Type . get s = Type . Unicode let check s = match Type . get s with Type . Bytes | Type . Unicode -> true | _ -> false let decode_UTF8 ? errors ? size s = let size ' = match size with None -> String . length s...
module Bytes = struct include String__ let of_string s = let len = String . length s in if ! version_major_value >= 3 then check_not_null ( Pywrappers . Python3 . pybytes_fromstringandsize s len ) len else check_not_null ( Pywrappers . Python2 . pystring_fromstringandsize s len ) len let of_bytes s = of_string ( Bytes ...
module Err = struct type t = Exception | StandardError | ArithmeticError | LookupError | AssertionError | AttributeError | EOFError | EnvironmentError | FloatingPointError | IOError | ImportError | IndexError | KeyError | KeyboardInterrupt | MemoryError | NameError | NotImplementedError | OSError | OverflowError | Refe...
module Object = struct include Object_ type t = Pytypes . pyobject let del_item obj item = assert_not_null " del_item ( del_item , ! _ ) _ " obj ; assert_not_null " del_item ( del_item_ , ) " ! item ; assert_int_success ( Pywrappers . pyobject_delitem obj item ) item let del_item_string obj item = assert_int_success ( ...
let exception_printer exn = match exn with E ( ty , value ) value when ! initialized -> Some ( Printf . sprintf " E ( % s , % s ) s " ( Object . to_string ty ) ty ( Object . to_string value ) value ) value | _ -> None
let ( ) = Stdlib_printexc . register_printer exception_printer
module Long = struct let check o = Type . get o = Type . Long let of_int64 v = check_not_null ( Pywrappers . pylong_fromlong v ) v let to_int64 v = let result = Pywrappers . pylong_aslong v in check_error ( ) ; result let of_int v = of_int64 ( Int64 . of_int v ) v let to_int v = Int64 . to_int ( to_int64 v ) v let from...
module Int = struct let check o = Type . get o = Type . Long let of_int64 v = if version_major ( ) >= 3 then Long . of_int64 v else check_not_null ( Pywrappers . Python2 . pyint_fromlong v ) v let to_int64 v = if version_major ( ) >= 3 then Long . to_int64 v else let result = Pywrappers . Python2 . pyint_aslong v in ch...
module Number = struct let absolute v = check_not_null ( Pywrappers . pynumber_absolute v ) v let add v0 v1 = assert_not_null " add ( add , ! _ ) _ " v0 ; assert_not_null " add ( add_ , ) " ! v1 ; check_not_null ( Pywrappers . pynumber_add v0 v1 ) v1 let number_and v0 v1 = assert_not_null " number_and ( number_and , ! ...
module Iter_ = struct let check o = Type . get o = Type . Iter let next i = assert_not_null " next " i ; option ( Pywrappers . pyiter_next i ) i let rec iter f i = match next i with None -> ( ) | Some item -> f item ; iter f i let rec fold_left f v i = match next i with None -> v | Some item -> fold_left f ( f v item )...
let vec_to_seq length get v = let length = length v in let rec aux i ( ) = if i = length then Seq . Nil else let x = get v i in Seq . Cons ( x , aux ( i + 1 ) 1 ) 1 in aux 0
let vec_to_seqi length get v = let length = length v in let rec aux i ( ) = if i = length then Seq . Nil else let x = get v i in Seq . Cons ( ( i , x ) x , aux ( i + 1 ) 1 ) 1 in aux 0
module Sequence = struct let check obj = bool_of_int ( Pywrappers . pysequence_check obj ) obj let concat s s ' = check_not_null ( Pywrappers . pysequence_concat s s ' ) s ' let contains s value = assert_not_null " contains ( contains , ! _ ) _ " s ; assert_not_null " contains ( contains_ , ) " ! value ; bool_of_int ( ...
module Tuple = struct include Sequence include Tuple_ let check o = Type . get o = Type . Tuple let empty = pytuple_empty ( ) let is_empty v = v == empty let get_slice tuple i0 i1 = check_not_null ( Pywrappers . pytuple_getslice tuple i0 i1 ) i1 let size tuple = check_int ( Pywrappers . pytuple_size tuple ) tuple let o...
module Dict = struct include Dict_ let check o = Type . get o = Type . Dict let clear o = assert_not_null " clear " o ; Pywrappers . pydict_clear o let copy v = check_not_null ( Pywrappers . pydict_copy v ) v let del_item dict item = assert_not_null " del_item ( del_item , ! _ ) _ " dict ; assert_not_null " del_item ( ...
module Set = struct let check o = Type . get o = Type . Set let clear o = assert_not_null " clear " o ; Pywrappers . pyset_clear o let copy v = check_not_null ( Pywrappers . pyset_new v ) v let create ( ) = check_not_null ( Pywrappers . pyset_new null ) null let size set = assert_not_null " size " set ; let sz = Pywrap...
module Traceback = struct type frame = { filename : string ; function_name : string ; line_number : int } let create_frame { filename ; function_name ; line_number } = check_not_null ( pyframe_new filename function_name line_number ) line_number type t = frame list let create t = let types_module = check_not_null ( Pyw...
module Callable = struct let check v = Pywrappers . pycallable_check v <> 0 let handle_errors f arg = try f arg with E ( errtype , errvalue ) errvalue -> Err . set_object errtype errvalue ; null | Err ( errtype , msg ) msg | Err_with_traceback ( errtype , msg , [ ] ) -> Err . set_error errtype msg ; null | Err_with_tra...
type optimize = Default | Debug | Normal | RemoveDocstrings
let int_of_optimize opt = match opt with | Default -> - 1 | Debug -> 0 | Normal -> 1 | RemoveDocstrings -> 2
module Import = struct let add_module name = check_not_null ( Pywrappers . pyimport_addmodule name ) name let main ( ) = add_module " __main__ " let builtins ( ) = Object . find_attr_string ( main ( ) ) " __builtins__ " let compile ~ source ~ filename ( ? dont_inherit = false ) false ( ? optimize = Default ) Default mo...
module Module = struct let check o = Type . get o = Type . Module let create name = check_not_null ( Pywrappers . pymodule_new name ) name let get_dict m = assert_not_null " get_dict " m ; check_not_null ( Pywrappers . pymodule_getdict m ) m let get_filename m = assert_not_null " get_filename " m ; check_some ( Pywrapp...
module Class = struct let init ( ? parents = [ ] ) ( ? fields = [ ] ) ( ? methods = [ ] ) classname = if version_major ( ) >= 3 then let methods = List . rev_map ( fun ( name , closure ) closure -> ( name , Pywrappers . Python3 . pyinstancemethod_new closure ) closure ) closure methods in Type . create classname parent...
module Iter = struct include Iter_ let create next = let next_name = if version_major ( ) >= 3 then " __next__ " else " next " in let next ' _args = match next ( ) with None -> raise ( Err ( Err . StopIteration , ) ) " " | Some item -> item in let iter_fn = Callable . of_function ( function | [ ] || -> failwith " __ite...
module List = struct include Sequence let check v = Type . get v = Type . List let create size = check_not_null ( Pywrappers . pylist_new size ) size let size list = assert_not_null " size " list ; check_int ( Pywrappers . pylist_size list ) list let length = size let set_item list index value = assert_int_success ( Py...
module Marshal = struct let version ( ) = let marshal_module = Import . import_module " marshal " in Long . to_int ( Module . get marshal_module " version ) " let read_object_from_file file = let fd = Pytypes . file_map Unix . descr_of_in_channel file in check_not_null ( Pywrappers . pymarshal_readobjectfromfile fd ) f...
module Array = struct let of_indexed_structure getter setter length = let methods = [ " __len__ " , Callable . of_function_as_tuple ( fun _tuple -> Int . of_int length ) length ; " __getitem__ " , Callable . of_function_as_tuple ( fun tuple -> let ( _self , key ) key = Tuple . to_tuple2 tuple in getter ( Long . to_int ...
module Run = struct let any_file file filename = assert_int_success ( Pywrappers . pyrun_anyfileexflags ( Pytypes . file_map Unix . descr_of_in_channel file ) file filename 1 None ) None let file file filename start globals locals = let fd = Pytypes . file_map Unix . descr_of_in_channel file in check_not_null ( Pywrapp...
module Gil = struct type t = int let ensure = Pywrappers . pygilstate_ensure let release = Pywrappers . pygilstate_release let check ( ) = Pywrappers . pygilstate_check ( ) <> 0 let with_lock f = let t = ensure ( ) in Fun . protect f ~ finally ( : fun ( ) -> release t ) t end
let set_argv argv = Module . set ( Module . sys ( ) ) " argv " ( List . of_array_map String . of_string argv ) argv
let last_value ( ) = Module . get ( Module . builtins ( ) ) " _ "
let compile ~ source ~ filename ? dont_inherit ? optimize mode = let mode = match mode with | ` Exec -> File | ` Eval -> Eval | ` Single -> Single in let optimize = Stdcompat . Option . map ( function | ` Default -> Default | ` Debug -> Debug | ` Normal -> Normal | ` RemoveDocstrings -> RemoveDocstrings ) RemoveDocstri...
type pyobject_type = | TupleType | BytesType | UnicodeType | BoolType | IntType | FloatType | ListType | NoneType | CallableType | ModuleType | ClassType | TypeType | DictType | NullType | CamlpillType | OtherType | EitherStringType | CamlpillSubtype of string | AnyType
let pytype_name t = match t with | TupleType -> " Python - Tuple " | BytesType -> " Python - Bytes " | UnicodeType -> " Python - Unicode " | BoolType -> " Python - Bool " | IntType -> " Python - Int " | FloatType -> " Python - Float " | ListType -> " Python - List " | NoneType -> " Python - None " | CallableType -> " P...
let _py_type_of_pyobject_type t = match t with | BoolType -> Py . Type . Bool | BytesType -> Py . Type . Bytes | CallableType -> Py . Type . Callable | CamlpillType | CamlpillSubtype _ -> Py . Type . Capsule | DictType -> Py . Type . Dict | FloatType -> Py . Type . Float | ListType -> Py . Type . List | IntType -> Py ....
let pyobject_type_of_py_type t = match t with Py . Type . Unknown | Py . Type . Iter | Py . Type . Set -> OtherType | Py . Type . Bool -> BoolType | Py . Type . Bytes -> BytesType | Py . Type . Callable -> CallableType | Py . Type . Capsule -> CamlpillType | Py . Type . Closure -> CallableType | Py . Type . Dict -> Dic...
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 | Pye...
let make_pill_wrapping name _instance = Py . Capsule . make name
let py_false ( ) = Py . Bool . f
let py_initialize ( ) = Py . initialize ( )
let py_isinitialized ( ) = if Py . is_initialized ( ) then 1 else 0
let py_true ( ) = Py . Bool . t
let _pycaml_seterror error msg = let error ' = match error with Pyerr_Exception -> Py . Err . Exception | Pyerr_StandardError -> Py . Err . StandardError | Pyerr_ArithmeticError -> Py . Err . ArithmeticError | Pyerr_LookupError -> Py . Err . LookupError | Pyerr_AssertionError -> Py . Err . AssertionError | Pyerr_Attrib...
let int_of_bool b = if b then - 1 else 0
let _pybytes_check v = int_of_bool ( Py . Type . get v = Py . Type . Bytes ) Bytes
let pybytes_format ( fmt , args ) args = Py . String . format fmt args
let pyiter_check v = int_of_bool ( Py . Type . get v = Py . Type . Iter ) Iter
let _pyunicode_check v = int_of_bool ( Py . Type . get v = Py . Type . Unicode ) Unicode
let pyerr_fetch _ = match Py . Err . fetch ( ) with None -> ( Py . null , Py . null , Py . null ) null | Some e -> e
let pyerr_normalizeexception e = e
let pynone ( ) = Py . none
let pynull ( ) = Py . null
let pytype v = pyobject_type_of_py_type ( Py . Type . get v ) v
let register_ocamlpill_types _array = ( )
let pyeval_callobject ( func , arg ) arg = Pywrappers . pyeval_callobjectwithkeywords func arg Py . null
let pyimport_execcodemodule ( obj , s ) s = Pywrappers . pyimport_execcodemodule s obj
let pyimport_importmoduleex ( name , globals , locals , fromlist ) fromlist = Pywrappers . pyimport_importmodulelevel name globals locals fromlist ( - 1 ) 1
let py_compilestringflags ( str , filename , start , flags ) flags = if Py . version_major ( ) <= 2 then py_compilestringflags ( str , filename , start , flags ) flags else py_compilestringexflags ( str , filename , start , flags , - 1 ) 1