package pyast

  1. Overview
  2. Docs

Source file common.ml

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
type num =
  | Int of int
  | Float of float
  | Big_int of (Py.Object.t [@opaque])
  [@@deriving refl]

type object_ = num [@@deriving refl]

let num (obj : Py.Object.t) : num =
  match Py.Type.get obj with
  | Int -> Int (Py.Int.to_int obj)
  | Long -> Int (Py.Long.to_int obj)
  | Float -> Float (Py.Float.to_float obj)
  | unexpected_type ->
      invalid_arg
        (Printf.sprintf "build_num: unexpected type '%s'"
           (Py.Type.name unexpected_type))

let object_ = num

type constant_desc =
  | Ellipsis
  | Bool of bool
  | Num of num
  | Str of string
  [@@deriving refl]

type constant = constant_desc option [@@deriving refl]

type singleton = bool option [@@deriving refl]

let to_option_map (f : Py.Object.t -> 'a) (obj : Py.Object.t) :
    'a option =
  if Py.is_none obj then
    None
  else
    Some (f obj)

let get_class_name (obj : Py.Object.t) : string =
  Py.String.to_string Pyops.(obj.@$("__class__").@$("__name__"))

let constant_desc (obj : Py.Object.t) : constant_desc =
  match Py.Type.get obj with
  | Unknown ->
     begin match get_class_name obj with
     | "ellipsis" -> Ellipsis
     | _ -> invalid_arg "constant_desc"
     end
  | Bytes | Unicode -> Str (Py.String.to_string obj)
  | Bool -> Bool (Py.Bool.to_bool obj)
  | _ -> Num (num obj)

let constant = to_option_map constant_desc

let singleton = to_option_map Py.Bool.to_bool

let option_apply (f : 'a -> 'b) (opt : 'a option) : 'b =
  f (Option.get opt)

let option_bind f x = Option.bind x f

let list_option_bind (f : 'a -> 'b list) (opt : 'a option) : 'b list =
  match opt with
  | None -> []
  | Some v -> f v

let bool_of_int (obj : Py.Object.t) : bool =
  Py.Int.to_int obj <> 0

let get_attr_string (obj : Py.Object.t) (attr : string) : Py.Object.t option =
  let value = Pywrappers.pyobject_getattrstring obj attr in
  if Py.is_null value then
    begin
      Py.Err.clear ();
      None
    end
  else
    Some value

let encoded_slice = "encoded slice"
OCaml

Innovation. Community. Security.