Commits

camlspotter committed 509b1bd

cleanup after a long sleep

  • Participants
  • Parent commits e51352a

Comments (0)

Files changed (18)

     string
     tuple
     type
+    float
 
 %.idl: %.idl.in idlconvert.pl
     ./idlconvert.pl $< > $@
   rm -f $(filter-proper-targets $(ls R, .))
 
 MyOCamlPackageExtras[] = opycaml_python.o from_python_c.o
-MyOCamlPackage(opycaml, utils type autoapi api OOApiauto OOApi, auto_stubs api_ml, -lpython$(PY_VERSION) -L$(PREFIX)/lib/ocaml $(L_CAMLIDL_FINDLIB) -lcamlidl)
+MyOCamlPackage(opycaml, utils type autoapi api xapi OOApiauto OOApi, auto_stubs api_ml, -lpython$(PY_VERSION) -L$(PREFIX)/lib/ocaml $(L_CAMLIDL_FINDLIB) -lcamlidl)
 
 opycaml_python.o: opycaml.cmx libopycaml.a dllopycaml.so
 	$(OCamlOpt) -output-obj -o $@ opycaml.cmx -cclib -lopycaml -ccopt -L$(OCAML_WHERE) $(CCOPT_L_CAMLIDL_FINDLIB) -ccopt -Wl,-rpath,$(OCAML_WHERE) -cclib -lpython$(PY_VERSION) -cclib -lcamlidl
 OCAMLFLAGS = -g 
 
 #|Warnings not to be printed separted with space
-OCAMLWARNING_IGNORE[] = 4 9
+OCAMLWARNING_IGNORE[] = 4 9 40 42 44 45
 
 #|Warnings treated as non errors
-OCAMLWARNING_NON_ERROR[] = 4 9
+OCAMLWARNING_NON_ERROR[] = 4 9 40 42 44 45
 
 #|If it is false, warnings do not stop the compilation even if they are listed in OCAMLWARNING_NON_ERROR
-#If you are in developing something, turn it ture!
+#If you are in developing something, turn it to true!
 OCAMLWARNING_STOP_AT_ERROR_WARNING=false
 
 #|The maximum warning value.
 # This is the maximum warning ID NOT of the current OCaml compiler,
 #                                BUT of the OCaml compiler used by the author.
-OCAMLWARNING_MAX= 39
+OCAMLWARNING_MAX= 45
 
 #|Function to build warning switches
 OCAMLWARNING_MAKE()=
     	Any other function in OPyCaml must be called after [initialize ()].
         *)
     
-      val debug : string -> [>_Object] t -> unit
+      val debug : [>_Object] t -> string -> unit
         (** debug print of objects *) 
     
       val main : string list -> int
         end
       ;;
 
-      let debug name o =
+      let debug o name =
         let cnt = refcnt o in
         let repr = String.asString (Object.repr o) in
         let address = address o in
     include X
 
     (* copied from Base *)       
-    let debug o s = Base.debug s o
+    let debug = Base.debug
     let address = Base.address
     let decref = Base.decref
     let incref = Base.incref
     include X
   end
 
+  module Float = Float
+
   module Class = struct
     let isSubclass = Object.isSubclass
   end
-  
+
+  module Pervasives : sig
+    val of_int : int -> _Int t
+    val of_float : float -> _Float t
+    val of_string : string -> _String t
+  end = struct
+    let of_int = Int.fromLong
+    let of_float = Float.fromDouble
+    let of_string = String.fromString
+  end
+  include Pervasives
 end
 
 (* Name space hack: Modules whose names do not collide with the OCaml
 module Object = Object
 module Err = Err
 module Int = Int
+module Float = Float
 module Class = Class
 
 let _ = Base.initialize ()
 #include "dict.idl"
 #include "tuple.idl"
 #include "high.idl"
+#include "float.idl"
 

File examples/OMakefile

+include ../OMyMakefile
+
 USE_OCAMLFIND=true
 
 OCAMLPACKS = opycaml
 
-.DEFAULT: $(OCamlProgram test, test)
-.DEFAULT: $(OCamlProgram module_new, module_new)
-.DEFAULT: $(OCamlProgram testoo, testoo)
-
-.PHONY: clean
-clean:
-  rm -f $(filter-proper-targets $(ls R, .))
+MyOCamlProgram(test, test)
+MyOCamlProgram(module_new, module_new)
+MyOCamlProgram(testoo, testoo)

File examples/OMakeroot

-########################################################################
-# Permission is hereby granted, free of charge, to any person
-# obtaining a copy of this file, to deal in the File without
-# restriction, including without limitation the rights to use,
-# copy, modify, merge, publish, distribute, sublicense, and/or
-# sell copies of the File, and to permit persons to whom the
-# File is furnished to do so, subject to the following condition:
-#
-# THE FILE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
-# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
-# OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
-# IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
-# DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
-# OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE FILE OR
-# THE USE OR OTHER DEALINGS IN THE FILE.
-
-########################################################################
-# The standard OMakeroot file.
-# You will not normally need to modify this file.
-# By default, your changes should be placed in the
-# OMakefile in this directory.
-#
-# If you decide to modify this file, note that it uses exactly
-# the same syntax as the OMakefile.
-#
-
-#
-# Include the standard installed configuration files.
-# Any of these can be deleted if you are not using them,
-# but you probably want to keep the Common file.
-#
 open build/C
 open build/OCaml
 open build/LaTeX
 
-#
-# The command-line variables are defined *after* the
-# standard configuration has been loaded.
-#
 DefineCommandVars()
 
-#
-# Include the OMakefile in this directory.
-#
 .SUBDIRS: .

File examples/README.txt

+You need to install ( ゚∀゚)o彡°O'PyCaml to build these examples.
+

File examples/matplotlib/OMakefile

-USE_OCAMLFIND=true
-
+include ../../OMyMakefile
 OCAMLPACKS = opycaml
 
-OCAMLFLAGS += -annot -I .. -w A-26-31-10
-
-.DEFAULT: $(OCamlProgram finance_demo, finance_demo)
-
-.PHONY: clean
-clean:
-  rm -f $(filter-proper-targets $(ls R, .))
+MyOCamlProgram(finance_demo, finance_demo)
+MyOCamlProgram(finance_demo_oo, finance_demo_oo)

File examples/matplotlib/finance_demo.ml

-open Opycaml.Api
+module Api = Opycaml.Api
+open Api
+open Opycaml.Xapi
 
 let debug = Object.debug
-let (%) o = Dict.getItemString (Dict.coerce o)
-let call o = Object.call (Callable.coerce o)
-let (!:>) = Object.obj
-
-let dict_of_alist kvs =
-  let d = Dict.new_ () in
-  List.iter (fun (k,v) -> Dict.setItem d k v) kvs;
-  d
-
-let kwd_call o lst = 
-  let kwd = lst |> List.map (fun (s,o) -> Py.String.fromString s, o) |> dict_of_alist in
-  Object.call (Callable.coerce o) ~kwd []
-
-let import_module name = Module.getDict (Import.importModule name)
 
 let main () =
   (* from pylab import * *)
   (* import datetime *)
   let datetime = import_module "datetime" in
 
-  let date = call (datetime % "date") in
+  let date = call_obj (datetime % "date") in
 
   (* date1 = datetime.date( 2004, 2, 1) *)
-  let date1 = date [ Int.fromLong 2004; Int.fromLong 2; Int.fromLong 1 ] in
+  let date1 = date [ Py.of_int 2004; Py.of_int 2; Py.of_int 1 ] in
   debug date1 "date1";
 
-  let date2 = date [ Int.fromLong 2004; Int.fromLong 4; Int.fromLong 12 ] in
+  let date2 = date [ Py.of_int 2004; Py.of_int 4; Py.of_int 12 ] in
   debug date2 "date2";
 
   begin
   debug monday "monday";
 
   let dayLocator = dates % "DayLocator" in
-  debug dayLocator "DayLocator";
+  debug dayLocator "DayLocator"; 
 
   (* Class can be called as a function for New *)
-  let dayLocator_f = call (dates % "DayLocator") in
+  let dayLocator_f = call_obj (dates % "DayLocator") in
 
   let alldays = dayLocator_f [] in
   debug alldays "alldays";
 
-  let weekdayLocator = call (dates % "WeekdayLocator") in
+  let weekdayLocator = call_obj (dates % "WeekdayLocator") in
 
   let mondays = weekdayLocator [ monday ] in
 
-  let dateFormatter = call (dates % "DateFormatter") in
+  let dateFormatter = call_obj (dates % "DateFormatter") in
 
-  let weekFormatter = dateFormatter [ Py.String.fromString "%b %d" ] in
+  let weekFormatter = dateFormatter [ Py.of_string "%b %d" ] in
   debug weekFormatter "weekFormatter";
 
-  let dayFormatter = dateFormatter [ Py.String.fromString "%d" ] in
+  let dayFormatter = dateFormatter [ Py.of_string "%d" ] in
   debug dayFormatter "dayFormatter";
 
-  let quotes_historical_yahoo = call (finance % "quotes_historical_yahoo") in
+  let quotes_historical_yahoo = call_obj (finance % "quotes_historical_yahoo") in
   debug (finance % "quotes_historical_yahoo") "quotes_historical_yahoo";
  
   let quotes = 
     try
-      quotes_historical_yahoo [ !:> (Py.String.fromString "INTC");
+      quotes_historical_yahoo [ !:> (Py.of_string "INTC");
                                 date1;
                                 date2 ]
     with
-    | Opycaml.Api.Error (ty,detail) -> 
+    | Api.Error (ty,detail) -> 
         Format.eprintf "%s: %s@." (Object.str_string ty) (Object.str_string detail); raise Exit
   in
 
   debug quotes "quotes";
 
-  let fig = call (pylab % "figure") [] in
+  let fig = call_obj (pylab % "figure") [] |> Dict.coerce in
   debug fig "fig";
 
   (* CR jfuruse: warnings for unit cases. lousy... *)
-  kwd_call (fig % "subplots_adjust") [ "bottom", Py.Float.fromDouble 0.2];
-  let ax = call (fig % "add_subplot") [ Int.fromLong 111 ] in
+  call_obj_ (fig % "subplots_adjust") ~kwd:(dict_of_klist [ "bottom", Py.of_float 0.2]) [];
+  let ax = call_obj (fig % "add_subplot") [ Py.of_int 111 ] |> Dict.coerce in
   debug ax "ax";
 
-  call (ax % "xaxis" % "set_major_locator") [ mondays ];
-  call (ax % "xaxis" % "set_minor_locator") [ alldays ];
-  call (ax % "xaxis" % "set_major_formatter") [ weekFormatter ];
-  (* call (ax % "xaxis" % "set_minor_formatter") [ dayFormatter ]; *)
+  call_obj_ (ax % "xaxis" %! "set_major_locator") [ mondays ];
+  call_obj_ (ax % "xaxis" %! "set_minor_locator") [ alldays ];
+  call_obj_ (ax % "xaxis" %! "set_major_formatter") [ weekFormatter ];
+  (* call_obj (ax % "xaxis" % "set_minor_formatter") [ dayFormatter ]; *)
 
   (* #plot_day_summary(ax, quotes, ticksize=3) *)
-  kwd_call (finance % "candlestick") [ "", ax; "", quotes; "width", !:> (Py.Float.fromDouble 0.6) ];
+  call_obj_ (finance % "candlestick") ~kwd:(dict_of_klist [ "", !:> ax; "", !:> quotes; "width", !:> (Py.of_float 0.6) ]) [];
 
-  call (ax % "xaxis_date") [];
-  call (ax % "autoscale_view") [];
+  call_obj_ (ax % "xaxis_date") [];
+  call_obj_ (ax % "autoscale_view") [];
   
-  kwd_call (pylab % "setp")
-    [ "", call (call (pylab % "gca") [] % "get_xticklabels") []; 
-      "rotation", !:> (Int.fromLong 45); 
-      "horizontalalignment", !:> (Py.String.fromString "right") ];
+  call_obj_ (pylab % "setp")
+    ~kwd: (dict_of_klist [ "", call_obj (call_obj (pylab % "gca") [] %! "get_xticklabels") []; 
+                           "rotation", !:> (Py.of_int 45); 
+                           "horizontalalignment", !:> (Py.of_string "right") ])
+    [];
 
-  call (pylab % "show") [];
-
-  ()
+  call_obj_ (pylab % "show") []
 
 let () = 
   try main () with
-  | Opycaml.Api.Error(ty, detail) -> 
+  | Api.Error(ty, detail) -> 
       prerr_endline "Expected type error!";
       debug ty "type";
       debug detail "detail";

File float.idl.in

+PyFloatObject_p_noincr  PyFloat_FromDouble(double d);
+// Return Python float from C double.
+
+double PyFloat_AsDouble(PyObject_p o);
+
+double PyFloat_AS_DOUBLE(PyObject_p o);
+// Extract C double from Python float.  The macro version trades safety for speed.

File js_of_ocaml/OMakefile

-USE_OCAMLFIND=true
+include ../OMyMakefile
 
 OCAMLPACKS = opycaml
 
 OCAMLDEPFLAGS += -syntax camlp4o -package js_of_ocaml.syntax
 OCAMLPPFLAGS += -syntax camlp4o -package js_of_ocaml.syntax
-OCAMLFLAGS += -annot
 
-.DEFAULT: $(OCamlProgram test, py_of_ocaml opy_of_ocaml test)
-.DEFAULT: $(OCamlProgram testoo, py_of_ocaml opy_of_ocaml testoo)
-
-.PHONY: clean
-clean:
-  rm -f $(filter-proper-targets $(ls R, .))
+MyOCamlProgram(test, py_of_ocaml opy_of_ocaml test)
+MyOCamlProgram(testoo, py_of_ocaml opy_of_ocaml testoo)
+MyOCamlProgram(finance_demo_oo, py_of_ocaml opy_of_ocaml finance_demo_oo)
+# MyOCamlProgram(testoo2, py_of_ocaml opy_of_ocaml testoo2)

File js_of_ocaml/opy_of_ocaml.ml

   type 'a meth
 end
 
-let import_module name = dict (Import.importModule name)#getDict
+let import_module name = Js.dict (Import.importModule name)#getDict
     print "module Unicode = struct end\n";
     print "module List = struct end\n";
     print "module Slice = struct end\n";
-    print "module Float = struct end\n";
     print "module Long = struct end\n";
 }
 

File oowrap/oowrap.ml

       | _ -> assert false
 
     (* [> _Cls ] t -> ...   =>    < _Cls : _Cls t; .. > -> ... *)
+    (* ... -> [> _Cls ] t -> ...   =>    < _Cls : _Cls t; .. > -> ... 
+       only when _ t occurs once
+    *)
     let wrap_in v ty0 =
 
       (* escape label *)

File tests/OMakefile

 %.cmx: ../opycaml.cmxa
 %.cmo: ../opycaml.cma
 
-.DEFAULT: $(OCamlProgram from_string_iteration, from_string_iteration)
-.DEFAULT: $(OCamlProgram set_closure_string, set_closure_string)
+MyOCamlProgram(from_string_iteration, from_string_iteration)
+MyOCamlProgram(set_closure_string, set_closure_string)

File tests/from_string_iteration.ml

 
 let f n = 
   let o = Py.String.fromString "hello world" in
-  if n mod 1000 = 0 then Base.debug "o" o;
+  if n mod 1000 = 0 then Base.debug o "o";
   if n mod 10000 = 0 then Gc.print_stat stderr
 ;;
 
+(* Extended API which may be useful and should be integrated into Api *)
+
+open Api
+
+let (%) = Dict.getItemString
+let (%!) x = Dict.getItemString (Dict.coerce x)
+
+let (!:>) = Object.obj
+
+let call_obj o = Object.call (Callable.coerce o)
+
+let call_obj_ o ?kwd xs = Object.call (Callable.coerce o) ?kwd xs |> ignore
+
+let dict_of_alist kvs =
+  let d = Dict.new_ () in
+  List.iter (fun (k,v) -> Dict.setItem d k v) kvs;
+  d
+  
+let dict_of_klist kvs =
+  kvs |> List.map (fun (s,o) -> Py.String.fromString s, o) |> dict_of_alist
+
+let import_module name = Module.getDict (Import.importModule name)
+(** Useful functions which might be moved to api.ml *)
+
+open Type
+
+val ( % ) : [> _Dict ] t -> string -> _Object t
+(** getItemString *)
+
+val ( %! ) : [> _Object ] t -> string -> _Object t
+(** Dict.coerce + getItemString *)
+
+val ( !:> ) : [> _Object ] t -> _Object t
+(** Forget Python object class *)
+
+val call_obj : [> _Object ] t -> ?kwd:[> _Dict ] t -> [> _Object ] t list -> _Object t
+(** Dynamically typed function call *)
+
+val call_obj_ : [> _Object ] t -> ?kwd:[> _Dict ] t -> [> _Object ] t list -> unit
+(** Dynamically typed function call *)
+
+val dict_of_alist : ([> _Object ] t * [> _Object ] t) list -> _Dict t
+(** Building a Dict from an assoc list via API: Very slow *)
+
+val dict_of_klist : (string * [> _Object ] t) list -> _Dict t
+(** Building a Dict from a "keyword list" via API. Very slow. *) 
+
+val import_module : string -> _Dict t
+(** Import a module. Ex. [import_module "pylab"] *)
+