Skip to content

Commit

Permalink
[infer/py][easy] adding support for the open builtin
Browse files Browse the repository at this point in the history
Summary: Adding a new builtin for `open`, which we'll use in future tests for the `with` statements (because `open` is my personal first source of `with` statements)

Reviewed By: ngorogiannis

Differential Revision: D49369160

fbshipit-source-id: ee56952ca2fce4fc87033b64631b0e7b6a61474d
  • Loading branch information
Vincent Siles authored and facebook-github-bot committed Sep 20, 2023
1 parent f0d9ccd commit 0e5602d
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 3 deletions.
15 changes: 12 additions & 3 deletions infer/src/python/PyBuiltin.ml
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ module Builtin = struct
| CompareOp of Compare.t
[@@deriving compare]

type python = Print | Range [@@deriving compare]
type python = Print | Range | Open [@@deriving compare]

type t = Primitive of primitive | Textual of textual | Python of python [@@deriving compare]
end
Expand All @@ -122,7 +122,7 @@ type builtin = t = Primitive of primitive | Textual of textual | Python of pytho

let textual t = Textual t

let python_to_string = function Print -> "print" | Range -> "range"
let python_to_string = function Print -> "print" | Range -> "range" | Open -> "open"

let to_proc_name = function
| Primitive primitive -> (
Expand Down Expand Up @@ -179,7 +179,15 @@ let to_proc_name = function

(** Lookup a [Python] builtin from its name. *)
let of_string name =
match name with "print" -> Some (Python Print) | "range" -> Some (Python Range) | _ -> None
match name with
| "print" ->
Some (Python Print)
| "range" ->
Some (Python Range)
| "open" ->
Some (Python Open)
| _ ->
None


let annot typ = T.Typ.{typ; attributes= []}
Expand Down Expand Up @@ -330,6 +338,7 @@ module Set = struct

let python_builtins =
[ (Builtin.Print, {formals_types= None; result_type= annotatedObject; used_struct_types= []})
; (Builtin.Open, {formals_types= None; result_type= annotatedObject; used_struct_types= []})
; (Builtin.Range, {formals_types= None; result_type= annotatedObject; used_struct_types= []}) ]


Expand Down
45 changes: 45 additions & 0 deletions infer/src/python/unit/PyTransTest.ml
Original file line number Diff line number Diff line change
Expand Up @@ -2866,3 +2866,48 @@ class Test(unittest.TestCase):
no support for `hasattr` at the moment. Skipping... |}]
end )
let%test_module "with" =
( module struct
let%expect_test _ =
(* No with for this one it's a baseline to support [open] *)
let source = {|
fp = open("foo.txt", "wt")
fp.write("yolo")
|} in
test source ;
[%expect
{|
.source_language = "python"
define dummy.$toplevel() : *PyNone {
#b0:
n0 = $builtins.open($builtins.python_string("foo.txt"), $builtins.python_string("wt"))
store &dummy::fp <- n0:*PyObject
n1:*PyObject = load &dummy::fp
n2 = n1.?.write($builtins.python_string("yolo"))
ret null
}
global dummy::fp: *PyObject
global $python_implicit_names::__name__: *PyString
global $python_implicit_names::__file__: *PyString
declare $builtins.open(...) : *PyObject
declare $builtins.python_tuple(...) : *PyObject
declare $builtins.python_bytes(*Bytes) : *PyBytes
declare $builtins.python_string(*String) : *PyString
declare $builtins.python_bool(int) : *PyBool
declare $builtins.python_float(float) : *PyFloat
declare $builtins.python_int(int) : *PyInt |}]
end )

0 comments on commit 0e5602d

Please sign in to comment.