text
stringlengths
0
601k
let py_test_int t = Test . check t " Python int to OCaml int " ( fun ( ) -> ( !$ Int 99999 ) |> Object . to_int ) 99999 ; Test . check t " Python int to OCaml string " ( fun ( ) -> ( !$ Int 123 ) |> Object . to_string ) " 123 "
let py_test_string t = Test . check t " Python string to OCaml string " ( fun ( ) -> ( !$ String " testing abc 123 " ) |> Object . to_string ) " testing abc 123 "
let py_test_list t = let l = ( !$ List [ Int 1 ; Int 2 ; Int 3 ] ) in let l ' = Object . to_list Object . to_int l in List . iteri ( fun i x -> Test . check t " Python check list " ( fun ( ) -> x ) ( i + 1 ) ) l '
let py_test_tuple t = let l = ( !$ Tuple [ | Int 1 ; Int 2 ; Int 3 ] ) | in let l ' = Object . to_list Object . to_int l in List . iteri ( fun i x -> Test . check t " Python check tuple " ( fun ( ) -> x ) ( i + 1 ) ) l '
let py_test_dict_data = [ | " a " ; " b " ; " c " ] |
let py_test_dict t = let d = ( !$ Dict [ String " a " , Int 1 ; String " b " , Int 2 ; String " c " , Int 3 ; ] ) in let d ' = PyDict . items Object . to_string Object . to_int d in let d ' = List . sort ( fun ( _k , v ) ( _k ' , v ' ) -> compare v v ' ) d ' in List . iteri ( fun i ( k , v ) -> Test . check t " Python ...
let py_test_getitem_dict t = let d = ( !$ Dict [ String " a " , Int 1 ; String " b " , Int 2 ; String " b " , Int 3 ; ] ) in Test . check t " Getitem a " ( fun ( ) -> Object . get_item_s d " a " |> Object . to_int ) 1 ; Test . check t " Getitem b " ( fun ( ) -> Object . get_item d ( PyUnicode . create " b " ) |> Object...
let py_test_type t = List . iter ( fun ( name , py , expected_type ) -> let pyobject = !$ py in let type_name = match PyType . type_name pyobject with | None -> assert false | Some type_name -> type_name in Test . check t ( " Type " ^ name ) ( fun ( ) -> type_name ) name ; Test . check t ( " Type " ^ name ) ( fun ( ) -...
let sort_keys = function | Dict l -> Dict ( List . sort ( fun ( k , _ ) ( k ' , _ ) -> match ( k , k ' ) with | String a , String b -> String . compare a b | Int a , String b -> String . compare ( string_of_int a ) b | String a , Int b -> String . compare a ( string_of_int b ) | _ , _ -> - 1 ) l ) | x -> x
let py_test_of_object t = List . iteri ( fun i py -> let py ' = !$ py |> of_object in Test . check t ( " of_object " ^ string_of_int i ) ( fun ( ) -> sort_keys py ) ( sort_keys py ' ) ) [ String " foobar " ; Int 42 ; Dict [ Int 1337 , Float 3 . 1415 ; String " key " , Int 42 ] ; List [ Tuple [ | Int 2 ; Int 7 ; Int 1 ;...
let py_test_iter t = let l = ( !$ List [ Int 1 ; Int 2 ; Int 3 ; ] ) in let i = PyIter . get l in let o = PyIter . next i in let _ = Test . check t " Python check iter 1 " ( fun ( ) -> Object . to_int o ) 1 in let o = PyIter . next i in let _ = Test . check t " Python check iter 2 " ( fun ( ) -> Object . to_int o ) 2 i...
let py_test_call t = let python_list = PyList . create [ ] in let result = Object . call_method python_list " append " ~ args [ :| to_object ( Int 42 ) ] | in assert Object . ( is_none result ) ; let result = Object . call_method python_list " append " ~ args [ :| to_object ( String " foo " ) ] | in assert Object . ( i...
let py_test_buffer t = let a = [ ' a ' ; ' b ' ; ' c ' ; ' d ' ; ' e ' ; ' f ' ; ' g ' ] in let b = PyByteArray . from_list a in let _ = PyByteArray . set b 0 ' z ' in let c = PyBuffer . create ~ readonly : false b in let _ = PyBuffer . set c 1 ' y ' in Test . check t " Python check byte array " ( fun ( ) -> PyByteArra...
let py_test_wrap t = let sum_fn = Object . get_item_s ( builtins ( ) ) " sum " in let sum = PyWrap . ( python_fn ( list int @-> returning int ) sum_fn ) in let v = sum [ 1 ; 2 ; 3 ; 4 ] , sum [ 1 ; 2 ; 3 ; 4 ; 5 ; 6 ] in Test . check t " Wrap python fn " ( fun ( ) -> v ) ( 10 , 21 ) ; let sum = PyWrap . ( python_fn ( l...
let py_test_thread_state t = let a0 = PyThreadState . get ( ) in let _ = exec " a = 10 " in let a1 = new_interpreter ( ) in let _ = PyThreadState . swap a1 in let _ = exec " a = 5 " in let _ = Test . check t " New thread state " ( fun ( ) -> eval " a " |> Object . to_int ) 5 in let _ = PyThreadState . swap a0 in Test ....
let py_test_gc t = List . iter ( fun to_python_fn -> let array = Array . init 1000 ( fun i -> i * i ) in let tuple = ( !$ to_python_fn ( Array . map ( fun i -> Int i ) array ) ) in Gc . full_major ( ) ; let array ' = Object . to_array Object . to_int tuple in Test . check t " Python gc test " ( fun ( ) -> array ) array...
let py_test_numpy t = if Numpy . is_available ( ) then ( let np = PyModule . import " numpy " in let version = Numpy . get_version ( ) in Test . check t " numpy version test " ( fun ( ) -> version > 0 ) true ; let zeros = np . $ String " zeros " $ [ Int 5 ] in let bigarray = Numpy . to_bigarray zeros Float64 in Bigarra...
let py_test_errors t = let v = Py . to_object ( Int ( - 1 ) ) |> Py . Object . to_int in Test . check t " error can return - 1 " ( fun ( ) -> v ) ( - 1 ) ; let ok = try let _ = Py . to_object ( String " aa " ) |> Py . Object . to_int in false with _ -> true in Test . check t " error string to int " ( fun ( ) -> ok ) tr...
let simple = [ py_test_int ; py_test_string ; py_test_list ; py_test_tuple ; py_test_dict ; py_test_getitem_dict ; py_test_type ; py_test_of_object ; py_test_iter ; py_test_call ; py_test_buffer ; py_test_wrap ; py_test_thread_state ; py_test_gc ; py_test_numpy ; py_test_errors ; ]
let _ = let t = Test . start ( ) in let ( ) = Test . all t simple in Test . finish t
let type_name obj = let ptr = P . Object . to_c_ptr obj |> Ctypes . from_voidp Init . _Py_object in if Ctypes . is_null ptr then None else let ob_type = Ctypes . getf ( Ctypes . ( ) !@ ptr ) Init . ob_type in if Ctypes . is_null ob_type then None else Some ( Ctypes . getf ( Ctypes . ( ) !@ ob_type ) Init . tp_name )
let tp_flags obj = let ptr = P . Object . to_c_ptr obj |> Ctypes . from_voidp Init . _Py_object in if Ctypes . is_null ptr then None else let ob_type = Ctypes . getf ( Ctypes . ( ) !@ ptr ) Init . ob_type in if Ctypes . is_null ob_type then None else let tp_flags = Ctypes . getf ( Ctypes . ( ) !@ ob_type ) Init . tp_fl...
let has_flag ' tp_flags ~ flag_id = match tp_flags with | None -> false | Some tp_flags -> ( tp_flags land ( 1 lsl flag_id ) ) <> 0
let has_flag obj ~ flag_id = tp_flags obj |> has_flag ' ~ flag_id
let long_subclass = has_flag ~ flag_id : 24
let list_subclass = has_flag ~ flag_id : 25
let tuple_subclass = has_flag ~ flag_id : 26
let bytes_subclass = has_flag ~ flag_id : 27
let unicode_subclass = has_flag ~ flag_id : 28
let dict_subclass = has_flag ~ flag_id : 29
let base_exc_subclass = has_flag ~ flag_id : 30
let type_subclass = has_flag ~ flag_id : 31
let is_float obj = let ptr = P . Object . to_c_ptr obj |> Ctypes . from_voidp Init . _Py_object in if Ctypes . is_null ptr then false else let ob_type = Ctypes . getf ( Ctypes . ( ) !@ ptr ) Init . ob_type in Ctypes . ptr_compare ob_type Init . _PyFloat_Type = 0
type t = | Null | None | Long | List | Tuple | Bytes | Unicode | Dict | Base_exc | Type | Float
let get obj = if P . Object . is_null obj then [ Null ] else if P . Object . is_none obj then [ None ] else let tp_flags = tp_flags obj in let maybe_add v ~ flag_id acc = if has_flag ' tp_flags ~ flag_id then v :: acc else acc in ( if is_float obj then [ Float ] else [ ] ) |> maybe_add Long ~ flag_id : 24 |> maybe_add ...
let rec of_object obj : P . t = match get obj with | [ Null ] | [ None ] -> Nil | [ Float ] -> Float ( P . Object . to_float obj ) | [ Long ] -> Int ( P . Object . to_int obj ) | [ List ] -> List ( P . Object . to_list of_object obj ) | [ Tuple ] -> Tuple ( P . Object . to_array of_object obj ) | [ Bytes ] -> Bytes ( P...
module W = struct type ' a t = { wrap : ' a -> P . pyobject ; unwrap_exn : P . pyobject -> ' a ; name : string } end
let un w = w . W . unwrap_exn
type ' a t = | Apply : ' a W . t * ' b t -> ( ' a -> ' b ) t | Return : ' a W . t -> ' a t
let rec to_string : type a . a t -> string = function | Apply ( w , t ) -> w . W . name ^ " -> " ^ to_string t | Return w -> w . W . name
let length t = let rec loop : type a . a t -> int -> int = fun t acc -> match t with | Apply ( _ , t ) -> loop t ( acc + 1 ) | Return _ -> acc in loop t 0
let returning w = Return w
let ( ) @-> w t = Apply ( w , t )
let python_fn t pyobject = let rec loop : type a . a t -> P . t list -> a = fun t acc_args -> match t with | Apply ( w , t ) -> ( fun x -> loop t ( P . Ptr ( w . W . wrap x ) :: acc_args ) ) | Return w -> un w ( P . run pyobject acc_args ? kwargs : None ) in loop t [ ]
let id x = x
let ocaml_fn t fn args = let args = P . Object . to_array id args in if length t <> Array . length args then Printf . sprintf " expected % d arguments , got % d " ( length t ) ( Array . length args ) |> failwith ; let rec loop : type a . a t -> a -> index : int -> P . pyobject = fun t fn ~ index -> match t with | Apply...
module W_impl = struct let none = let unwrap_exn pyobject = if P . Object . is_none pyobject then ( ) else failwith " not none " in { W . wrap = P . Object . none ; unwrap_exn ; name = " unit " } let bool = { W . wrap = P . Object . from_bool ; unwrap_exn = P . Object . to_bool ; name = " bool " } let int = { W . wrap ...
type typ = Int | Int32 | Int64 | Float | String | Blob | Bool
type modifier = Option | List
type input = ( name * name ) * typ * modifier list
type output = name * typ * modifier list
type param = In of input | Out of output
let str2typ = function | " d " -> Int | " n " -> Int32 | " L " -> Int64 | " f " -> Float | " s " -> String | " S " -> Blob | " b " -> Bool | _ -> failwith " Invalid type "
let typ2ocamlstr = function | Int -> " int " | Int32 -> " int32 " | Int64 -> " int64 " | Float -> " float " | String | Blob -> " string " | Bool -> " bool "
let mod2ocamlstr = function | Option -> " option " | List -> " list "
let paramtype2str ( _ , typ , mods ) = let typ = typ2ocamlstr typ in typ
let param2type ( _ , typ , _ ) = typ
let name ( ( name , _ , _ ) : input ) = name
let process str = let escrgx = Re_pcre . regexp { ( ' [ ' ] ' ) } |^*| in let esc_list = ref [ ] in let esc_str = " < GENSQLITE_PRESERVED " > in let esc_subst substrings = let mtch = Re . get substrings 0 in esc_list := mtch :: ! esc_list ; esc_str in let escaped = Re . replace ~ f : esc_subst escrgx str in esc_list :=...
let dbg = ref true
type tok = | Op of string | ILit of int | SLit of int * string | Sym of int
let bufferize f = let buf = ref None in ( fun ( ) -> match ! buf with | Some x -> buf := None ; x | None -> f ( ) ) , ( fun x -> assert ( ! buf = None ) None ; buf := Some x ) x
let getch , ungetch = bufferize ( fun ( ) -> input_char ! inch ) inch
let peekch ( ) = let ch = getch ( ) in ungetch ch ; ch
let addsym , symstr , symitr = let symtab = Array . make 100 " " and syms = ref 0 in let rec find s n = if n >= ! syms then ( incr syms ; n ) n else if symtab ( . n ) n = s then n else find s ( n + 1 ) 1 in ( fun s -> let sid = find s 0 in symtab ( . sid ) sid <- s ; sid ) sid , ( fun n -> assert ( n < ! syms ) syms ; ...
let glo = Bytes . make 0x1000 ' \ x00 '
let next = let s = Bytes . create 100 in let getq ( ) = match getch ( ) with | ' \\ ' when peekch ( ) = ' n ' -> ignore ( getch ( ) ) ; ' \ n ' | c -> c in let isid = function | ' a ' . . ' z ' | ' A ' . . ' Z ' | ' _ ' -> true | _ -> false in let rec id n ch = Bytes . set s n ch ; if isid ( peekch ( ) ) then id ( n + ...
let next , unnext = bufferize next
let nextis t = let nt = next ( ) in unnext nt ; t = nt
let obuf = Bytes . make 0x100000 ' \ x00 '
let rec out x = if x <> 0 then begin out ( x / 0x100 ) 0x100 ; Bytes . set obuf ( ! opos ) opos ( Char . chr ( x land 0xff ) 0xff ) 0xff ; incr opos end
let le n x = for i = 0 to n / 8 - 1 do let byte = ( x lsr ( i * 8 ) 8 ) 8 land 0xff in Bytes . set obuf ( ! opos ) opos ( Char . chr byte ) byte ; incr opos done
let get32 l = Char . code ( Bytes . get obuf l ) l + Char . code ( Bytes . get obuf ( l + 1 ) 1 ) 1 * 0x100 + Char . code ( Bytes . get obuf ( l + 2 ) 2 ) 2 * 0x10000 + Char . code ( Bytes . get obuf ( l + 3 ) 3 ) 3 * 0x1000000
let rec patch rel loc n = assert ( n < 0x100000000 ) 0x100000000 ; if loc <> 0 then begin let i = ! opos in let loc ' = get32 loc in let x = if rel then n - ( loc + 4 ) 4 else n in if ! dbg then Printf . eprintf " patching at % d to % d ( n =% d ) d \ n " loc x n ; opos := loc ; le 32 x ; patch rel loc ' n ; opos := i ...
let load r n = out ( 0xb8 + r ) r ; le 32 n
let cmp n = load 0 0 ; out ( 0x0f92c0 + ( n lsl 8 ) 8 ) 8
let test n l = out 0x4885c0 ; out ( 0x0f84 + n ) n ; let loc = ! opos in le 32 l ; loc
let align = ref 0
let push r = incr align ; if r < 8 then out ( 0x50 + r ) r else out ( 0x4150 + r - 8 ) 8 decr align ; if r < 8 then out ( 0x58 + r ) r else out ( 0x4158 + r - 8 ) 8
type lvpatch = | Mov of int | Del of int
type lvty = | Int | Chr
let lval = ref ( Mov 0 , Int ) Int
let patchlval ( ) = match fst ! lval with | Mov n -> Bytes . set obuf ( ! opos - n ) n ( ' \ x8d ' ) x8d ' | Del n -> opos := ! opos - n
let read = function | Int -> out 0x488b ; le 8 0 ; lval := ( Del 3 , Int ) Int | Chr -> out 0x480fb6 ; le 8 0 ; lval := ( Del 4 , Chr ) Chr
type globref = { loc : int ; va : int }
let globs = Array . make 100 { loc = 0 ; va = - 1 }
let lvls = [ ( " " , * 0 ) 0 ; ( " " , / 0 ) 0 ; ( " " , % 0 ) 0 ; ( " " , + 1 ) 1 ; ( " " , - 1 ) 1 ; ( " " , << 2 ) 2 ; ( " " , >> 2 ) 2 ; ( " " , < 3 ) 3 ; ( " " , <= 3 ) 3 ; ( " " , > 3 ) 3 ; ( " " , >= 3 ) 3 ; ( " " , == 4 ) 4 ; ( " " , != 4 ) 4 ; ( " " , & 5 ) 5 ; ( " " , ^ 6 ) 6 ; ( " " , | 7 ) 7 ; ( " " , && 8 ...
type ins = | Bin of int list | Cmp of int
let inss = [ ( " " , * Bin [ 0x480fafc1 ] 0x480fafc1 ) 0x480fafc1 ; ( " " , / Bin [ 0x4891 ; 0x4899 ; 0x48f7f9 ] 0x48f7f9 ) 0x48f7f9 ; ( " " , % Bin [ 0x4891 ; 0x4899 ; 0x48f7f9 ; 0x4892 ] 0x4892 ) 0x4892 ; ( " " , + Bin [ 0x4801c8 ] 0x4801c8 ) 0x4801c8 ; ( " " , - Bin [ 0x4891 ; 0x4829c8 ] 0x4829c8 ) 0x4829c8 ; ( " " ...
let tokint = Sym ( addsym " int ) "
let rec binary stk lvl = if lvl = - 1 then unary stk else let lvlof o = if not ( List . mem_assoc o lvls ) lvls then - 1 else List . assoc o lvls in let rec fold ( ) = match next ( ) with | Op o when lvlof o = lvl -> push 0 ; binary stk ( lvl - 1 ) 1 ; pop 1 ; begin match List . assoc o inss with | Bin ops -> List . it...
let rec decl g n stk = match next ( ) with | t when t = tokint -> let top = match stk with | ( _ , i ) i :: _ -> i | _ -> 0 in let rec vars n stk = while nextis ( Op ) " " * do ignore ( next ( ) ) done ; if nextis ( Op ) " ; " then ( n , stk ) stk else match next ( ) with | Sym s -> let n ' = n + 1 in let stk ' = if g ...
let retl = ref 0
let rec stmt brk stk = let pexpr stk = ignore ( next ( ) ) ; expr stk ; ignore ( next ( ) ) in match next ( ) with | t when t = tokif -> pexpr stk ; let loc = test 0 0 in stmt brk stk ; let loc = if not ( nextis tokelse ) tokelse then loc else begin ignore ( next ( ) ) ; out 0xe9 ; let l = ! opos in le 32 0 ; patch tru...
let rec top ( ) = if not ( nextis ( Op " EOF ) ) " ! then if nextis tokint then ( ignore ( decl true 0 [ ] ) ; top ( ) ) else match next ( ) with | Sym f -> let g = globs ( . f ) f in if g . va >= 0 then failwith " symbol defined twice " ; globs ( . f ) f <- { g with va = ! opos } ; let regs = [ 7 ; 6 ; 2 ; 1 ; 8 ; 9 ]...
let elfhdr = Bytes . of_string @@ String . concat " " [ " \ x7fELF \ x02 \ x01 \ x01 \ x00 " ; " \ x00 \ x00 \ x00 \ x00 \ x00 \ x00 \ x00 \ x00 " ; " \ x02 \ x00 " ; " \ x3e \ x00 " ; " \ x01 \ x00 \ x00 \ x00 " ; " \ x00 \ x00 \ x00 \ x00 \ x00 \ x00 \ x00 \ x00 " ; " \ x40 \ x00 \ x00 \ x00 \ x00 \ x00 \ x00 \ x00 "...
let elfphdr ty off sz align = le 32 ty ; le 32 7 ; le 64 off ; le 64 ( off + base ) base ; le 64 ( off + base ) base ; le 64 sz ; le 64 sz ; le 64 align
let elfgen outf = let entry = ! opos in let main = addsym " main " in let gmain = globs ( . main ) main in out 0x488b3c24 ; out 0x488d742408 ; out 0x48b8 ; le 64 gmain . loc ; globs ( . main ) main <- { gmain with loc = ! opos - 8 } ; out 0xffd0 ; out 0x89c7 ; load 0 0x3c ; out 0x0f05 ; let off = textoff + ! gpos in le...
let main ( ) = let doone c stk = opos := 0 ; c stk ; print_bytes ( Bytes . sub obuf 0 ! opos ) opos in let ppsym = function | Op s -> Printf . printf " Operator ' % s ' \ n " s | ILit i -> Printf . printf " Int literal % d \ n " i | SLit ( _ , s ) s -> Printf . printf " Str literal % S \ n " s | Sym i -> Printf . print...
let _ = main ( )