-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathpy.mli
2236 lines (1746 loc) · 86.1 KB
/
py.mli
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
(** OCaml Interface for Python. *)
(** Call [initialize ()] first. *)
val initialize: ?library_name:string -> ?interpreter:string -> ?version:int ->
?minor:int -> ?verbose:bool -> ?debug_build:bool -> ?python_sigint:bool ->
unit -> unit
(** [initialize ~interpreter ~version ~minor ~verbose ~debug_build ()] finds
and loads the Python library.
This function should be called before any other functions, except
if explicitely mentioned.
If [library_name] is given, it is used as the path for the library to
be loaded: in this case, version parameters are ignored.
If [library_name] is not given, the library is searched as described
below.
[version] should specify the major version number of Python (2 or 3).
[minor] should specify the minor version number.
If no version number is given, the version of Python is determined by the
output of the shell command [python --version].
If an [interpreter] executable name is given, this executable is
used in place of [python] in the previous command line.
The library is searched by
using [pkg-config] if available, by considering system paths, and
in the directory [../lib] relatively to the directory where the
[python] executable is. If the library has been statically linked
with the executable, it will be used.
When [verbose] is [true] (default: [false]), library filenames that are
tried to be loaded are printed on standard error.
[debug_build] specifies whether the Python library is a debug build:
if the argument is left unspecified, debug build is detected
automatically.
If [python_sigint] is [true] (default: [false]), the function let
[pythonlib] take handle on [sigint], preventing programs from
being interrupted by [Ctrl+C]. When [python_sigint] is [false]
(the default), the previous signal behavior of [sigint] is restored after
the library has been loaded (so, [Ctrl+C] will still interrupt the
program, unless this behavior was changed elsewhere). *)
val finalize: unit -> unit
(** [finalize ()] unloads the library. No other functions except
[initialize ()] should be called afterwards. *)
val on_finalize: (unit -> unit) -> unit
(** [on_finalize f] registers [f ()] to be executed when [finalize] is
executed. *)
val is_initialized: unit -> bool
(** [is_initialized ()] returns [true] if the library is initialized
([initialize ()] has been called and [finalize ()] has not been
called afterwards). *)
val is_debug_build: unit -> bool
(** [is_debug_build ()] returns [true] if the library is a debug build. *)
val get_library_filename: unit -> string option
(** [get_library_filename ()] returns [Some filename] where [filename] is the
path to the Python library that has been loaded, or [None] if no Python
library has been loaded (for example, if the library has been statically
linked with the executable). *)
val version: unit -> string
(** [version ()] returns the version of the Python library. E.g. ["3.5.1"]. *)
val version_major: unit -> int
(** [version_major ()] returns the major number (the first component) of the
version of the Python library, either [2] or [3]. *)
val version_minor: unit -> int
(** [version_minor ()] returns the minor number (the second component) of the
version of the Python library. *)
val version_pair: unit -> int * int
(** [version_pair ()] returns the major and the minor numbers of the
version of the Python library. *)
type compare = Pytypes.compare = LT | LE | EQ | NE | GT | GE
(** Either a filename or a channel.
Channels suppose that the same C runtime has been used to compile both the
Python library and the OCaml runtime.
Warning: using channels is unsafe if runtimes differ (can lead to
segmentation fault).*)
type 'a file = 'a Pytypes.file = Filename of string | Channel of 'a
val check_error: unit -> unit
(** General functions to handle Python values *)
module Object: sig
type t = Pytypes.pyobject
(** The type of a Python value.
Structural comparison of values of type [Py.Object.t] rely on
Python comparison of underlying values. That is to say, if [u] and [v]
are two values of type [Py.Object.t], and by abuse of notations, if we
denote also [u] and [v] their respective value in Python, we have [u = v]
in OCaml if and only if [u == v] in Python, and [u < v] in OCaml if and
only if [u < v] in Python, etc.
Moreover, there are five values which are handled specially:
- {!val:Py.null}: the value [NULL] used in the Python API for error case
- {!val:Py.none}: the value [None];
- {!val:Py.Bool.t}: the value [True];
- {!val:Py.Bool.f}: the value [False];
- {!val:Py.Tuple.empty}: the value [()].
These values are guaranteed to be unique, so that the physical equality
can be used to compare against their definitions: for instance, a value
[v] of type [Py.Object.t] is [None] if and only if [v == Py.none].
*)
val del_attr: t -> t -> unit
(** Wrapper for
{{:https://docs.python.org/3/c-api/object.html#c.PyObject_DelAttr} PyObject_DelAttr} *)
val del_attr_string: t -> string -> unit
(** Wrapper for
{{:https://docs.python.org/3/c-api/object.html#c.PyObject_DelAttrString} PyObject_DelAttrString} *)
val del_item: t -> t -> unit
(** Wrapper for
{{:https://docs.python.org/3/c-api/object.html#c.PyObject_DelItem} PyObject_DelItem} *)
val del_item_string: t -> string -> unit
(** Wrapper for
{{:https://docs.python.org/3/c-api/object.html#c.PyObject_DelItemString} PyObject_DelItemString} *)
val get_attr: t -> t -> t option
(** Wrapper for
{{:https://docs.python.org/3/c-api/object.html#c.PyObject_GetAttr} PyObject_GetAttr} *)
val find_attr: t -> t -> t
(** Equivalent to {!get_attr} but raises a [Not_found] exception in
case of failure. *)
val find_attr_err: t -> t -> t
(** Equivalent to {!get_attr} but raises a Python exception in
case of failure. *)
val find_attr_opt: t -> t -> t option
(** Alias for {!get_attr}. *)
val get_attr_string: t -> string -> t option
(** Wrapper for
{{:https://docs.python.org/3/c-api/object.html#c.PyObject_GetAttrString} PyObject_GetAttrString} *)
val find_attr_string: t -> string -> t
(** Equivalent to {!get_attr_string} but raises a [Not_found] exception in
case of failure. *)
val find_attr_string_err: t -> string -> t
(** Equivalent to {!get_attr_string} but raises a Python exception in
case of failure. *)
val find_attr_string_opt: t -> string -> t option
(** Alias for {!get_attr_string}. *)
val get_item: t -> t -> t option
(** Wrapper for
{{:https://docs.python.org/3/c-api/object.html#c.PyObject_GetItem} PyObject_GetItem} *)
val find: t -> t -> t
(** Equivalent to {!get_item} but raises a [Not_found] exception in
case of failure. *)
val find_err: t -> t -> t
(** Equivalent to {!get_item} but raises a Python exception in
case of failure. *)
val find_opt: t -> t -> t option
(** Alias for {!get_item}. *)
val get_item_string: t -> string -> t option
(** [get_item_string o key] returns the element corresponding to the object
[key] or [None] on failure. *)
val find_string: t -> string -> t
(** Equivalent to {!get_item_string} but raises a [Not_found] exception in
case of failure. *)
val find_string_err: t -> string -> t
(** Equivalent to {!get_item_string} but raises a Python exception in
case of failure. *)
val find_string_opt: t -> string -> t option
(** Alias for {!get_item_string}. *)
val get_iter: t -> t
(** Wrapper for
{{:https://docs.python.org/3/c-api/object.html#c.PyObject_GetIter} PyObject_GetIter} *)
val get_type: t -> t
(** Wrapper for
{{:https://docs.python.org/3/c-api/object.html#c.PyObject_GetType} PyObject_GetType} *)
val has_attr: t -> t -> bool
(** Wrapper for
{{:https://docs.python.org/3/c-api/object.html#c.PyObject_HasAttr} PyObject_HasAttr} *)
val has_attr_string: t -> string -> bool
(** Wrapper for
{{:https://docs.python.org/3/c-api/object.html#c.PyObject_HasAttrString} PyObject_HasAttrString} *)
val hash: t -> int64
(** Wrapper for
{{:https://docs.python.org/3/c-api/object.html#c.PyObject_Hash} PyObject_Hash} *)
val is_true: t -> bool
(** Wrapper for
{{:https://docs.python.org/3/c-api/object.html#c.PyObject_IsTrue} PyObject_IsTrue} *)
val not: t -> bool
(** Wrapper for
{{:https://docs.python.org/3/c-api/object.html#c.PyObject_Not} PyObject_Not} *)
val is_instance: t -> t -> bool
(** Wrapper for
{{:https://docs.python.org/3/c-api/object.html#c.PyObject_IsInstance} PyObject_IsInstance} *)
val is_subclass: t -> t -> bool
(** Wrapper for
{{:https://docs.python.org/3/c-api/object.html#c.PyObject_IsSubclass} PyObject_IsSubclass} *)
val print: t -> out_channel file -> unit
(** Wrapper for
{{:https://docs.python.org/3/c-api/object.html#c.PyObject_Print} PyObject_Print} *)
val repr: t -> t
(** Wrapper for
{{:https://docs.python.org/3/c-api/object.html#c.PyObject_Repr} PyObject_Repr} *)
val rich_compare: t -> t -> compare -> t
(** Wrapper for
{{:https://docs.python.org/3/c-api/object.html#c.PyObject_RichCompare} PyObject_RichCompare} *)
val rich_compare_bool: t -> t -> compare -> bool
(** Wrapper for
{{:https://docs.python.org/3/c-api/object.html#c.PyObject_RichCompareBool} PyObject_RichCompareBool} *)
val set_attr: t -> t -> t -> unit
(** Wrapper for
{{:https://docs.python.org/3/c-api/object.html#c.PyObject_SetAttr} PyObject_SetAttr} *)
val set_attr_string: t -> string -> t -> unit
(** Wrapper for
{{:https://docs.python.org/3/c-api/object.html#c.PyObject_SetAttrString} PyObject_SetAttrString} *)
val set_item: t -> t -> t -> unit
(** Wrapper for
{{:https://docs.python.org/3/c-api/object.html#c.PyObject_SetItem} PyObject_SetItem} *)
val set_item_string: t -> string -> t -> unit
(** Wrapper for
{{:https://docs.python.org/3/c-api/object.html#c.PyObject_SetItemString} PyObject_SetItemString} *)
val str: t -> t
(** Wrapper for
{{:https://docs.python.org/3/c-api/object.html#c.PyObject_Str} PyObject_Str} *)
val string_of_repr: t -> string
(** [string_of_repr o] returns the string [repr o].
We have
[Py.Object.to_string o = Py.String.to_string (Py.Object.repr o)]. *)
val to_string: t -> string
(** [to_string o] returns the string [str o].
We have
[Py.Object.to_string o = Py.String.to_string (Py.Object.str o)]. *)
val as_char_buffer: t -> string
(** Wrapper for
{{:https://docs.python.org/3/c-api/objbuffer.html#c.PyObject_AsCharBuffer} PyObject_AsCharBuffer} *)
val as_read_buffer: t -> string
(** Wrapper for
{{:https://docs.python.org/3/c-api/objbuffer.html#c.PyObject_AsReadBuffer} PyObject_AsReadBuffer} *)
val as_write_buffer: t -> string
(** Wrapper for
{{:https://docs.python.org/3/c-api/objbuffer.html#c.PyObject_AsWriteBuffer} PyObject_AsWriteBuffer} *)
val reference_count: t -> int
(** [reference_count o] returns the number of references to the Python
object [o]. *)
val format: Format.formatter -> t -> unit
(** [Py.Object.format fmt v] is equivalent to
[Format.pp_print_string fmt (Py.Object.to_string v)].
Can be used as printer for the top-level:
[#install_printer Py.Object.format]. *)
val format_repr: Format.formatter -> t -> unit
(** [Py.Object.format_repr fmt v] is equivalent to
[Format.pp_print_string fmt (Py.Object.string_of_repr v)].
Can be used as printer for the top-level:
[#install_printer Py.Object.format_repr]. *)
val call_function_obj_args: t -> t array -> t
(** Wrapper for
{{:https://docs.python.org/3/c-api/object.html#c.PyObject_CallFunctionObjArgs} PyObject_CallFunctionObjArgs} *)
val call_method_obj_args: t -> t -> t array -> t
(** Wrapper for
{{:https://docs.python.org/3/c-api/object.html#c.PyObject_CallMethodObjArgs} PyObject_CallMethodObjArgs} *)
val call_method: t -> string -> t array -> t
(** [Py.Object.call_method o m args] is equivalent to
[Py.Object.call_method_obj_args o (Py.String.of_string m) args]. *)
val call: t -> t -> t -> t
(** Wrapper for
{{:https://docs.python.org/3/c-api/object.html#c.PyObject_Call} PyObject_Call} *)
val size: t -> int
(** Wrapper for
{{:https://docs.python.org/3/c-api/object.html#c.PyObject_Size} PyObject_Size} *)
val dir: t -> t
(** Wrapper for
{{:https://docs.python.org/3/c-api/object.html#c.PyObject_Dir} PyObject_Dir} *)
end
exception E of Object.t * Object.t
(** [E (errtype, errvalue)] is a Python error.
[errtype] is the type of the exception.
[errvalue] is the value. *)
val null: Object.t
(** The value [NULL] of the C Python API. [null] is useful for calling
directly the functions of {!Pywrappers} module.
The value should not appear when using the functions of the [Py] module.
This value is guaranteed to be the unique value associated to [NULL]. *)
val is_null: Object.t -> bool
(** [Py.is_null v] is true if and only if [v] is [NULL].
Since [Py.none] is guaranteed to be the unique value associated to [NULL],
[Py.is_null v] is equivalent to [v == Py.null]. *)
val check_not_null: Object.t -> Object.t
(** [check_not_null v] checks that [v] is not [null] and returns [v].
Raises the current Python error as exception otherwise. *)
val none: Object.t
(** The value [None] of Python.
This value is guaranteed to be the unique value associated to [None]. *)
val is_none: Object.t -> bool
(** [Py.is_none v] is true if and only if [v] is [None].
Since [Py.none] is guaranteed to be the unique value associated to [None],
[Py.is_none v] is equivalent to [v == Py.none]. *)
val set_program_name: string -> unit
(** Sets the program name (by default, [Sys.argv.(0)]).
The function can be called before [initialize ()] and the value is preserved
from one initialization to the other. *)
val set_python_home: string -> unit
(** Sets the path of the Python home.
The function can be called before [initialize ()] and the value is preserved
from one initialization to the other. *)
val add_python_path: string -> unit
(** Adds a path to Python search path.
The function can be called before [initialize ()] and the value is preserved
from one initialization to the other. *)
val get_program_name: unit -> string
(** Gets the program name (by default, [Sys.argv.(0)]).
The function can be called before [initialize ()]. *)
val get_python_home: unit -> string
(** Gets the path of the Python home.
The function can be called before [initialize ()]. *)
val get_program_full_path: unit -> string
(** Wrapper for
{{: https://docs.python.org/3/c-api/init.html#Py_GetProgramFullPath} Py_GetProgramFullPath}. *)
val get_prefix: unit -> string
(** Wrapper for
{{: https://docs.python.org/3/c-api/init.html#Py_GetPrefix} Py_GetPrefix}. *)
val get_exec_prefix: unit -> string
(** Wrapper for
{{: https://docs.python.org/3/c-api/init.html#Py_GetExecPrefix} Py_GetExecPrefix}. *)
val get_path: unit -> string
(** Wrapper for
{{: https://docs.python.org/3/c-api/init.html#Py_GetPath} Py_GetPath}. *)
val get_version: unit -> string
(** Wrapper for
{{: https://docs.python.org/3/c-api/init.html#Py_GetVersion} Py_GetVersion}. *)
val get_platform: unit -> string
(** Wrapper for
{{: https://docs.python.org/3/c-api/init.html#Py_GetPlatform} Py_GetPlatform}. *)
val get_copyright: unit -> string
(** Wrapper for
{{: https://docs.python.org/3/c-api/init.html#Py_GetCopyright} Py_GetCopyright}. *)
val get_compiler: unit -> string
(** Wrapper for
{{: https://docs.python.org/3/c-api/init.html#Py_GetCompiler} Py_GetCompiler}. *)
val get_build_info: unit -> string
(** Wrapper for
{{: https://docs.python.org/3/c-api/init.html#Py_GetBuildInfo} Py_GetBuildInfo}. *)
(** Interface for Python values of type [Bool]. *)
module Bool: sig
val t: Object.t
(** The Python value [True].
This value is guaranteed to be the unique value associated to [True]. *)
val is_true: Object.t -> bool
(** [Py.is_true v] is true if and only if [v] is [True].
Since [Py.Bool.t] is guaranteed to be the unique value associated to [True],
[Py.is_true v] is equivalent to [v == Py.t]. *)
val f: Object.t
(** The Python value [False].
This value is guaranteed to be the unique value associated to [False]. *)
val is_false: Object.t -> bool
(** [Py.is_false v] is true if and only if [v] is [False].
Since [Py.Bool.f] is guaranteed to be the unique value associated to [False],
[Py.is_false f] is equivalent to [v == Py.f]. *)
val check: Object.t -> bool
(** [check v] returns [true] if [v = t] or [v = f]. *)
val of_bool: bool -> Object.t
(** [of_bool b] returns [t] if [b = true], and [f] if [b = false]. *)
val to_bool: Object.t -> bool
(** [to_bool b] returns [true] if [b = t], and [false] if [b = f].
[Failure] is raised if [b] is neither [t] nor [f]. *)
end
(** Interface for Python values of type [Callable]. *)
module Callable: sig
val check: Object.t -> bool
(** [check v] returns [true] if [v] is callable.
Wrapper for
{{: https://docs.python.org/3/c-api/object.html#c.PyCallable_Check} PyCallable_Check}. *)
val handle_errors : ('a -> Object.t) -> 'a -> Object.t
(** [handle_errors f x] calls [f x] and returns its result if the call
succeeds. If [f x] raises a Python exception
([Py.E (errtype, errvalue)] or [Py.Err (errtype, msg)]),
this exception is raised as a Python exception
(via {!Err.set_object} or {!Err.set_error} respectively). *)
val of_function_as_tuple: ?name:string -> ?docstring:string -> (Object.t -> Object.t) ->
Object.t
(** [of_function_as_tuple f] returns a Python callable object that calls the
function [f].
Arguments are passed as a tuple.
If [f] raises a Python exception
([Py.E (errtype, errvalue)] or [Py.Err (errtype, msg)]),
this exception is raised as a Python exception
(via {!Err.set_object} or {!Err.set_error} respectively).
If [f] raises any other exception, this exception bypasses the Python
interpreter. *)
val of_function_as_tuple_and_dict: ?name:string -> ?docstring:string ->
(Object.t -> Object.t -> Object.t) -> Object.t
(** [of_function_as_tuple_and_dict f] returns a Python callable object that
calls the function [f].
Arguments are passed as a tuple and a dictionary of keywords. *)
val of_function: ?name:string -> ?docstring:string -> (Object.t array -> Object.t) -> Object.t
(** Equivalent to {!of_function_as_tuple} but with an array of Python objects
instead of a tuple for passing arguments. *)
val of_function_with_keywords: ?name:string -> ?docstring:string ->
(Object.t array -> Object.t -> Object.t) -> Object.t
(** Equivalent to {!of_function_as_tuple_and_dict} but with an array of
Python objects instead of a tuple for passing arguments.
The dictionary of keywords is passed as such as it is more efficient
to access arguments with ``Py.Dict.find_string``, rather than using
``List.assoc`` with an associative list. *)
val to_function_as_tuple: Object.t -> Object.t -> Object.t
(** [to_function_as_tuple c] returns a function [f] such that
[f args] calls the Python callable [c] with the Python tuple [args]
as arguments. *)
val to_function_as_tuple_and_dict: Object.t -> Object.t -> Object.t ->
Object.t
(** [to_function_as_tuple_and_dict c] returns a function [f] such that
[f args dict] calls the Python callable [c] with the Python tuple [args]
and the dictionary of keywords [dict] as arguments. *)
val to_function: Object.t -> Object.t array -> Object.t
(** Equivalent to {!to_function_as_tuple} but with an array of
Python objects instead of a tuple for passing arguments. *)
val to_function_with_keywords: Object.t -> Object.t array ->
(string * Object.t) list -> Object.t
(** Equivalent to {!to_function_as_tuple_and_dict} but with an array of
Python objects instead of a tuple and an associative list instead of a
dictionary for passing arguments. *)
end
(** Embedding of OCaml values in Python. *)
module Capsule: sig
type 'a t = {
wrap : 'a -> Object.t;
unwrap : Object.t -> 'a;
}
val check: Object.t -> bool
(** [check v] returns [true] if [v] contains an OCaml value. *)
val create: string -> 'a t
(** For a given type ['a], [create s] returns a pair [{ wrap; unwrap }].
[wrap v] transforms the value [v] of type 'a to an opaque Python object.
[unwrap w] transforms the opaque Python object [w] previously obtained
with [wrap v] into the original OCaml value [v],
such that [unwrap (wrap v) = v].
[Failure _] is raised if a wrapper has already been generated for a type
of the same name. *)
val make: string -> ('a -> Object.t) * (Object.t -> 'a)
(** Same as {!val:create}, but returns a plain pair instead of a record. *)
val type_of: Object.t -> string
(** [type_of w] returns the type string associated to the opaque Python
object [w]. *)
val is_valid: Object.t -> string -> bool
(** Wrapper for
{{: https://docs.python.org/3/c-api/capsule.html#c.PyCapsule_IsValid} PyCapsule_IsValid}.
OCaml capsules have the name ["ocaml-capsule"].
We have [check v = is_valid v "ocaml-capsule"]. *)
val unsafe_wrap_value: 'a -> Object.t
(** [unsafe_wrap_value v] transforms the value [v] to an opaque Python
object. *)
val unsafe_unwrap_value: Object.t -> 'a
(** [unsafe_unwrap_value v] transforms the opaque Python object [w]
previously obtained with [unsafe_wrap_value v] into the original OCaml
value [v]. *)
end
(** Defining a new class type *)
module Class: sig
val init: ?parents:(Object.t list) -> ?fields:((string * Object.t) list) ->
?methods:((string * Object.t) list) ->
string -> Object.t
(** [init ~parents ~fields ~methods classname] Returns a new class type.
@param parents list of base classes (default: [[]]).
@param fields associative list for field values (default : [[]]).
@param methods associative list for method closures
(default : [[]]). *)
end
(** Interface for Python values of type [Long]. *)
module Long: sig
val check: Object.t -> bool
(** [check o] returns [true] if [o] is a Python long. *)
val of_int64: int64 -> Object.t
(** [of_int i] returns the Python long with the value [i].
Wrapper for
{{: https://docs.python.org/3/c-api/long.html#c.PyLong_FromLong} PyLong_FromLong}. *)
val to_int64: Object.t -> int64
(** [to_int o] takes a Python long [o] as arguments
and returns the corresponding 64-bit integer value.
A Python exception ([Py.E _]) is raised if [o] is not a long.
Wrapper for
{{: https://docs.python.org/3/c-api/long.html#c.PyLong_AsLong} PyLong_AsLong}. *)
val of_int: int -> Object.t
(** [of_int i] returns the Python long with the value [i].
We have [of_int i = of_int64 (Int64.of_int i)]. *)
val to_int: Object.t -> int
(** [to_int o] takes a Python long [o] as arguments
and returns the corresponding integer value.
A Python exception ([Py.E _]) is raised if [o] is not a long.
We have [to_int o = Int64.to_int (to_int 64 o)]. *)
val from_string: string -> int -> Object.t * int
(** [from_string s base] parses [s] as a number written in [base] and
returns [(o, l)] where [o] is the Python long which has been read,
and [l] is the number of characters that has been parsed.
Wrapper for
{{: https://docs.python.org/3/c-api/long.html#c.PyLong_FromString} PyLong_FromString}. *)
val of_string: ?base:int -> string -> Object.t
(** [of_string ?base s] parses [s] and returns the Python long that has
been read. By default, [base] is [0]: the radix is determined based
on the leading characters of [s]. *)
val to_string: Object.t -> string
(** Synonym for [Py.Object.to_string]. *)
end
(** Interface for Python values of type [Int] if Python 2, [Long] if Python 3. *)
module Int: sig
val check: Object.t -> bool
(** [check o] returns [true] if [o] is a Python int. *)
val of_int64: int64 -> Object.t
(** [of_int i] returns the Python int with the value [i].
Wrapper for
{{: https://docs.python.org/2/c-api/int.html#c.PyInt_FromLong} PyInt_FromLong}. *)
val to_int64: Object.t -> int64
(** [to_int o] takes a Python int [o] as arguments
and returns the corresponding 64-bit integer value.
A Python exception ([Py.E _]) is raised if [o] is not a long.
Wrapper for
{{: https://docs.python.org/2/c-api/int.html#c.PyInt_AsLong} PyInt_AsLong}. *)
val of_int: int -> Object.t
(** [of_int i] returns the Python int with the value [i].
We have [of_int i = of_int64 (Int64.of_int i)]. *)
val to_int: Object.t -> int
(** [to_int o] takes a Python int [o] as arguments
and returns the corresponding integer value.
A Python exception ([Py.E _]) is raised if [o] is not a long.
We have [to_int o = Int64.to_int (to_int 64 o)]. *)
val of_string: ?base:int -> string -> Object.t
(** Synonym for [Py.Long.of_string]. *)
val to_string: Object.t -> string
(** Synonym for [Py.Long.to_string]. *)
end
(** Interface for Python values of type [Dict]. *)
module Dict: sig
val check: Object.t -> bool
(** [check o] returns [true] if [o] is a Python dictionary. *)
val clear: Object.t -> unit
(** Wrapper for
{{:https://docs.python.org/3/c-api/dict.html#c.PyDict_Clear} PyDict_Clear} *)
val copy: Object.t -> Object.t
(** Wrapper for
{{:https://docs.python.org/3/c-api/dict.html#c.PyDict_Copy} PyDict_Copy} *)
val create: unit -> Object.t
(** Wrapper for
{{:https://docs.python.org/3/c-api/dict.html#c.PyDict_New} PyDict_New} *)
val del_item: Object.t -> Object.t -> unit
(** Wrapper for
{{:https://docs.python.org/3/c-api/dict.html#c.PyDict_DelItem} PyDict_DelItem} *)
val del_item_string: Object.t -> string -> unit
(** Wrapper for
{{:https://docs.python.org/3/c-api/dict.html#c.PyDict_DelItemString} PyDict_DelItemString} *)
val get_item: Object.t -> Object.t -> Object.t option
(** Wrapper for
{{:https://docs.python.org/3/c-api/dict.html#c.PyDict_GetItem} PyDict_GetItem} *)
val find: Object.t -> Object.t -> Object.t
(** [find p key] returns the object from Python dictionary [p] which has a key
[key]. Equivalent to {!get_item} but [find] raises [Not_found] if the
key [key] is not present. *)
val find_opt: Object.t -> Object.t -> Object.t option
(** Alias for {!get_item}. *)
val get_item_string: Object.t -> string -> Object.t option
(** Wrapper for
{{:https://docs.python.org/3/c-api/dict.html#c.PyDict_GetItemString} PyDict_GetItemString} *)
val find_string: Object.t -> string -> Object.t
(** [find_string p key] returns the object from Python dictionary [p]
which has a key [key]. Equivalent to {!get_item_string} but [find_string]
raises [Not_found] if the key [key] is not present. *)
val find_string_opt: Object.t -> string -> Object.t option
(** Alias for {!get_item_string}. *)
val keys: Object.t -> Object.t
(** Wrapper for
{{:https://docs.python.org/3/c-api/dict.html#c.PyDict_Keys} PyDict_Keys} *)
val items: Object.t -> Object.t
(** Wrapper for
{{:https://docs.python.org/3/c-api/dict.html#c.PyDict_Items} PyDict_Items} *)
val set_item: Object.t -> Object.t -> Object.t -> unit
(** Wrapper for
{{:https://docs.python.org/3/c-api/dict.html#c.PyDict_SetItem} PyDict_SetItem} *)
val set_item_string: Object.t -> string -> Object.t -> unit
(** Wrapper for
{{:https://docs.python.org/3/c-api/dict.html#c.PyDict_SetItemString} PyDict_SetItemString} *)
val size: Object.t -> int
(** Wrapper for
{{:https://docs.python.org/3/c-api/dict.html#c.PyDict_Size} PyDict_Size} *)
val values: Object.t -> Object.t
(** Wrapper for
{{:https://docs.python.org/3/c-api/dict.html#c.PyDict_Clear} PyDict_Str} *)
val iter: (Object.t -> Object.t -> unit) -> Object.t -> unit
(** [iter f dict] applies [f key value] for each pair [(key, value)]
in the Python dictionary [dict]. *)
val fold: (Object.t -> Object.t -> 'a -> 'a) -> Object.t -> 'a -> 'a
(** [fold f dict v] returns [f key1 value1 (... (f keyn valuen dict))]
where [(key1, value1)], ..., [(keyn, valuen)] are the bindings of
the Python dictionary [dict]. *)
val for_all: (Object.t -> Object.t -> bool) -> Object.t -> bool
(** [for_all p dict] checks whether all the bindings [(key, value)] of the
Python dictionary [dict] satisfy the predicate [p key value]. *)
val exists: (Object.t -> Object.t -> bool) -> Object.t -> bool
(** [for_all p dict] checks that there is at least one binding [(key, value)]
among those of the Python dictionary [dict] that satisfies the predicate
[p key value]. *)
val to_bindings: Object.t -> (Object.t * Object.t) list
(** [to_bindings o] returns all the pairs [(key, value)] in the Python
dictionary [o]. *)
val to_bindings_map: (Object.t -> 'a) -> (Object.t -> 'b) -> Object.t ->
('a * 'b) list
(** [to_bindings_map fkey fvalue o] returns all the pairs
[(fkey key, fvalue value)] in the Python dictionary [o]. *)
val to_bindings_string: Object.t -> (string * Object.t) list
(** [to_bindings_string o] returns all the pairs [(key, value)] in the Python
dictionary [o]. *)
val to_bindings_seq: Object.t -> (Object.t * Object.t) Stdcompat.Seq.t
(** [to_bindings_seq o] returns the ephemeral sequence of all the pairs
(key, value) in the Python dictionary [o]. *)
val to_bindings_seq_map: (Object.t -> 'a) -> (Object.t -> 'b) -> Object.t ->
('a * 'b) Stdcompat.Seq.t
(** [to_bindings_seq_map fkey fvalue o] returns the ephemeral sequence of all
the pairs (fkey key, fvalue value) in the Python dictionary [o]. *)
val to_bindings_string_seq: Object.t -> (string * Object.t) Stdcompat.Seq.t
(** [to_bindings_string_seq o] returns the ephemeral sequence of all the pairs
(key, value) in the Python dictionary [o]. *)
val of_bindings: (Object.t * Object.t) list -> Object.t
(** [of_bindings b] returns then Python dictionary mapping all the pairs
[(key, value)] in [b]. *)
val of_bindings_map: ('a -> Object.t) -> ('b -> Object.t) -> ('a * 'b) list
-> Object.t
(** [of_bindings_map fkey fvalue b] returns then Python dictionary mapping
all the pairs [(fkey key, fvalue value)] in [b]. *)
val of_bindings_string: (string * Object.t) list -> Object.t
(** [of_bindings_string b] returns then Python dictionary mapping all the
pairs [(key, value)] in [b]. *)
val singleton: Object.t -> Object.t -> Object.t
(** [singleton key value] returns the one-element Python dictionary that maps
[key] to [value] *)
val singleton_string: string -> Object.t -> Object.t
(** [singleton key value] returns the one-element Python dictionary that maps
[key] to [value] *)
end
(** Interface for Python values of type [Set]. *)
module Set: sig
val check: Object.t -> bool
(** [check o] returns [true] if [o] is a Python set. *)
val add: Object.t -> Object.t -> unit
(** Wrapper for
{{:https://docs.python.org/3/c-api/set.html#c.PySet_Add} PySet_Add} *)
val clear: Object.t -> unit
(** Wrapper for
{{:https://docs.python.org/3/c-api/set.html#c.PySet_Clear} PySet_Clear} *)
val contains: Object.t -> Object.t -> bool
(** Wrapper for
{{:https://docs.python.org/3/c-api/set.html#c.PySet_Contains} PySet_Contains} *)
val copy: Object.t -> Object.t
(** Wrapper for
{{:https://docs.python.org/3/c-api/set.html#c.PySet_New} PySet_New} *)
val create: unit -> Object.t
(** Wrapper for
{{:https://docs.python.org/3/c-api/set.html#c.PySet_New} PySet_New} *)
val discard: Object.t -> Object.t -> unit
(** Wrapper for
{{:https://docs.python.org/3/c-api/set.html#c.PySet_Discard} PySet_Discard} *)
val size: Object.t -> int
(** Wrapper for
{{:https://docs.python.org/3/c-api/set.html#c.PySet_Size} PySet_Size} *)
val to_list: Object.t -> Object.t list
(** [to_list o] returns the list of all elements in Python set [o]. *)
val to_list_map: (Object.t -> 'a) -> Object.t -> 'a list
(** [to_list_map f o] returns the list of [f v] for all elements v in
Python set [o]. *)
val of_list: Object.t list -> Object.t
(** [of_list l] returns then Python set containing all elements from [l]. *)
val of_list_map: ('a -> Object.t) -> 'a list -> Object.t
(** [of_list_map f l] returns then Python set containing [f e] for any
[e] from [l]. *)
end
module Err: sig
type t =
Exception
| StandardError
| ArithmeticError
| LookupError
| AssertionError
| AttributeError
| EOFError
| EnvironmentError
| FloatingPointError
| IOError
| ImportError
| IndexError
| KeyError
| KeyboardInterrupt
| MemoryError
| NameError
| NotImplementedError
| OSError
| OverflowError
| ReferenceError
| RuntimeError
| SyntaxError
| SystemExit
| TypeError
| ValueError
| ZeroDivisionError
| StopIteration
val clear: unit -> unit
(** Wrapper for
{{:https://docs.python.org/3/c-api/exceptions.html#c.PyErr_Clear} PyErr_Clear} *)
val exception_matches: Object.t -> bool
(** Wrapper for
{{:https://docs.python.org/3/c-api/exceptions.html#c.PyErr_ExceptionMatches} PyErr_ExceptionMatches} *)
val fetch: unit -> (Object.t * Object.t * Object.t) option
(** Wrapper for
{{:https://docs.python.org/3/c-api/exceptions.html#c.PyErr_Fetch} PyErr_Fetch}.
*)
val fetched: unit -> (Object.t * Object.t * Object.t) option
(** Exception fetched when {!Py.E} has been raised. *)
val given_exception_matches: Object.t -> Object.t -> bool
(** Wrapper for
{{:https://docs.python.org/3/c-api/exceptions.html#c.PyErr_GivenExceptionMatches} PyErr_GivenExceptionMatches} *)
val occurred: unit -> Object.t option
(** Wrapper for
{{:https://docs.python.org/3/c-api/exceptions.html#c.PyErr_Occurred} PyErr_Occurred} *)
val print: unit -> unit
(** Wrapper for
{{:https://docs.python.org/3/c-api/exceptions.html#c.PyErr_Print} PyErr_Print} *)
val print_ex: int -> unit
(** Wrapper for
{{:https://docs.python.org/3/c-api/exceptions.html#c.PyErr_PrintEx} PyErr_PrintEx} *)
val restore: Object.t -> Object.t -> Object.t -> unit
(** Wrapper for
{{:https://docs.python.org/3/c-api/exceptions.html#c.PyErr_Restore} PyErr_Restore} *)
val restore_tuple: Object.t * Object.t * Object.t -> unit
(** [restore_tuple (ptype, pvalue, ptraceback)] is equivalent to
[Py.Err.restore ptype pvalue ptraceback]. *)
val restore_fetch: unit -> unit
(** Restore the exception returned by [Py.Err.fetch ()] and raise
[Failure] if [None]. *)
val restore_fetched: unit -> unit
(** Restore the exception returned by [Py.Err.fetched ()] and raise
[Failure] if [None]. *)
val set_error: t -> string -> unit
(** [set_error e msg] calls [Py.Err.set_string e msg] with a predefined error type.
In a closure/method/callback, it is recommended to raise a [Py.Err _] exception
instead. *)
val set_none: Object.t -> unit
(** Wrapper for
{{:https://docs.python.org/3/c-api/exceptions.html#c.PyErr_SetNone} PyErr_SetNone} *)
val set_string: Object.t -> string -> unit
(** Wrapper for
{{:https://docs.python.org/3/c-api/exceptions.html#c.PyErr_SetString} PyErr_SetString} *)
val set_object: Object.t -> Object.t -> unit
(** Wrapper for
{{:https://docs.python.org/3/c-api/exceptions.html#c.PyErr_SetObject} PyErr_SetObject}.
In a closure/method/callback, it is recommended to raise a [Py.E _] exception
instead. *)
val set_interrupt: unit -> unit
(** Wrapper for
{{:https://docs.python.org/3/c-api/exceptions.html#c.PyErr_SetInterrupt} PyErr_SetInterrupt} *)
val set_interrupt_ex: int -> unit
(** Since Python 3.10. Wrapper for
{{:https://docs.python.org/3/c-api/exceptions.html#c.PyErr_SetInterruptEx} PyErr_SetInterruptEx} *)
end
module Traceback : sig
type frame =
{ filename : string
; function_name : string
; line_number : int
}
val create_frame : frame -> Object.t
type t = frame list
end
exception Err of Err.t * string
(** Represents an exception to be set with {!Err.set_error} in a callback. *)
exception Err_with_traceback of Err.t * string * Traceback.t
(** Represents an exception with traceback information to be set with {!Err.restore}. *)
module Eval: sig
val call_object: Object.t -> Object.t -> Object.t
(** See {{:https://docs.python.org/3.0/extending/extending.html} Extending Python with C or C++} *)
val call_object_with_keywords: Object.t -> Object.t -> Object.t -> Object.t
(** See {{:https://docs.python.org/3.0/extending/extending.html} Extending Python with C or C++} *)
val get_builtins: unit -> Object.t
(** Wrapper for
{{:https://docs.python.org/3/c-api/reflection.html#c.PyEval_GetBuiltins} PyEval_GetBuiltins} *)
val get_globals: unit -> Object.t
(** Wrapper for
{{:https://docs.python.org/3/c-api/reflection.html#c.PyEval_GetGlobals} PyEval_GetGlobals} *)
val get_locals: unit -> Object.t
(** Wrapper for
{{:https://docs.python.org/3/c-api/reflection.html#c.PyEval_GetLocals} PyEval_GetLocals} *)
end
(** Interface for Python values of type [Float]. *)
module Float: sig
val check: Object.t -> bool
(** [check o] returns [true] if [o] is a Python float. *)
val of_float: float -> Object.t
(** [of_float f] returns the Python long with the value [f].
Wrapper for
{{:https://docs.python.org/3/c-api/float.html#c.PyFloat_AsDouble} PyFloat_AsDouble}. *)
val to_float: Object.t -> float
(** [to_float o] returns the floating-point vale stored in [o].
A Python exception ([Py.E _]) is raised if [o] is not a float.
Wrapper for
{{:https://docs.python.org/3/c-api/float.html#c.PyFloat_FromDouble} PyFloat_FromDouble}. *)
end
type optimize = Default | Debug | Normal | RemoveDocstrings
val int_of_optimize : optimize -> int
(** Importing Modules *)
module Import: sig
(* This function has been removed from Python 3.9, and was marked
"for internal use only" before.
val cleanup: unit -> unit
(** Wrapper for
{{:https://docs.python.org/3/c-api/import.html#c.PyImport_Cleanup} PyImport_Cleanup} *)
*)
val add_module: string -> Object.t
(** Wrapper for