Legend:
Page
Library
Module
Module type
Parameter
Class
Class type
Source
Source file class_wrapper.ml
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206openBaseopenImportmoduleId:sigtypetvalcreate:unit->tvalto_string:t->stringend=structtypet=intletcreate=letcurrent=ref0infun()->Int.incrcurrent;!current;;letto_string=Int.to_stringendletcontent_field="_content"type'at={wrap:'a->Py.Object.t;unwrap:Py.Object.t->'a;name:string;mutablecls_object:Py.Object.toption}letset_cls_object_exntpyobject=ifOption.is_somet.cls_objectthenPrintf.failwithf"cls_object for %s has already been set"t.name();t.cls_object<-Somepyobject;;moduleInit=structtype'acls='attype'at={fn:'acls->args:pyobjectlist->'a;docstring:stringoption}[@@derivingfields]letcreate?docstringfn={docstring;fn}endmoduleMethod=structtype'acls='attype'afn=|No_keywordsof('acls->self:'a*pyobject->args:pyobjectlist->pyobject)|With_keywordsof('acls->self:'a*pyobject->args:pyobjectlist->keywords:(string,pyobject,String.comparator_witness)Map.t->pyobject)type'at={name:string;fn:'afn;docstring:stringoption}[@@derivingfields]letcreate?docstringnamefn={name;fn=No_keywordsfn;docstring}letcreate_with_keywords?docstringnamefn={name;fn=With_keywordsfn;docstring};;letdefunc?docstringnamefn=letfncls~self~args~keywords=Defunc.apply(fncls~self)(Array.of_listargs)keywordsincreate_with_keywords?docstringnamefn;;endletwrap_capsuletobj=t.wrapobjletunwrap_exntpyobj=letpyobj=matchPy.Object.get_attr_stringpyobjcontent_fieldwith|None->Printf.failwithf"no %s field in object"content_field()|Somecontent->contentinifnot(Py.Capsule.checkpyobj)thenfailwith"not an ocaml capsule";t.unwrappyobj;;letunwraptpyobj=trySome(unwrap_exntpyobj)with|_->None;;letwraptobj=letcls=Option.value_exnt.cls_objectinletpyobject=Py.Object.call_function_obj_argscls[||]inPy.Object.set_attr_stringpyobjectcontent_field(wrap_capsuletobj);pyobject;;letmake?to_string_repr?to_string?eq?initname~methods=letid=Id.create()inlett=letwrap,unwrap=Py.Capsule.make(Printf.sprintf!"%s-%{Id}"nameid)in{wrap;unwrap;cls_object=None;name}inletmethods=letto_string=Option.mapto_string~f:(funfnt~self~args:_->fnt(fstself)|>Py.String.of_string)inletto_string_repr=Option.mapto_string_repr~f:(funfnt~self~args:_->fnt(fstself)|>Py.String.of_string)inletto_string_repr=Option.first_someto_string_reprto_stringinleteq=Option.mapeq~f:(funfnt~self~args->letrhs=matchargswith|[]->failwith"eq with no argument"|_::_::_->Printf.failwithf"eq with %d arguments"(List.lengthargs)()|[rhs]->rhsinfnt(fstself)(unwrap_exntrhs)|>Py.Bool.of_bool)inList.filter_map["__str__",to_string;"__repr__",to_string_repr;"__eq__",eq]~f:(fun(name,fn)->Option.mapfn~f:(funfn->Method.createnamefn))@methodsinletmethods=List.mapmethods~f:(fun{Method.name;fn;docstring}->letfn=letself_and_argsargs=letargs=Array.to_listargsinmatchargswith|[]->failwith"empty input"|p::q->p,qinmatch(fn:_Method.fn)with|No_keywordsfn->Py.Callable.of_function?docstring(funargs->letself,args=self_and_argsargsintryfnt~self:(unwrap_exntself,self)~argswith|Py.Err_aspyerr->raisepyerr|exn->letmsg=Printf.sprintf!"ocaml error %{Exn#mach}"exninraise(Py.Err(ValueError,msg)))|With_keywordsfn->Py.Callable.of_function_with_keywords?docstring(funargskeywords->tryletself,args=self_and_argsargsinletkeywords=Py_module.keywords_of_pythonkeywords|>Or_error.ok_exninfnt~self:(unwrap_exntself,self)~args~keywordswith|Py.Err_aspyerr->raisepyerr|exn->letmsg=Printf.sprintf!"ocaml error %{Exn#mach}"exninraise(Py.Err(ValueError,msg)))inname,fn)inletinit=letfn=letdocstring=Option.bindinit~f:Init.docstringinPy.Callable.of_function_as_tuple?docstring(funtuple->tryletself,args=matchPy.Tuple.to_listtuplewith|[]->failwith"empty input"|p::q->p,qinletcontent=matchinitwith|Someinit->init.fnt~args|>wrap_capsulet|None->Py.noneinPy.Object.set_attr_stringselfcontent_fieldcontent;Py.nonewith|Py.Err_aspyerr->raisepyerr|exn->letmsg=Printf.sprintf!"ocaml error %{Exn#mach}"exninraise(Py.Err(ValueError,msg)))in"__init__",fninletcls_object=Py.Class.initname~fields:[content_field,Py.none]~methods:(init::methods)inset_cls_object_exntcls_object;t;;letregister_in_moduletmodl=Py_module.set_valuemodlt.name(Option.value_exnt.cls_object);;