Skip to content

Commit

Permalink
[inferpython] bugfix IMPORT_STAR
Browse files Browse the repository at this point in the history
Summary: this opcode does not push anything on the stack.

Reviewed By: skcho

Differential Revision:
D65056888

Privacy Context Container: L1208441

fbshipit-source-id: b0cd09806c1a180ddf9641769c92c958ca99892a
  • Loading branch information
davidpichardie authored and facebook-github-bot committed Oct 29, 2024
1 parent 76e1f26 commit 324d444
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 9 deletions.
9 changes: 4 additions & 5 deletions infer/src/python/PyIR.ml
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,6 @@ module BuiltinCaller = struct
| FormatFn of FormatFunction.t
| CallFunctionEx (** [CALL_FUNCTION_EX] *)
| Inplace of BinaryOp.t
| ImportStar
| Binary of BinaryOp.t
| Unary of UnaryOp.t
| Compare of CompareOp.t
Expand Down Expand Up @@ -333,8 +332,6 @@ module BuiltinCaller = struct
sprintf "$FormatFn.%s" (FormatFunction.to_string fn)
| CallFunctionEx ->
"$CallFunctionEx"
| ImportStar ->
sprintf "$ImportStar"
| Binary op ->
let op = BinaryOp.to_string op in
sprintf "$Binary.%s" op
Expand Down Expand Up @@ -646,6 +643,7 @@ module Stmt = struct
| Delete of ScopedIdent.t (** [DELETE_FAST] & cie *)
| DeleteDeref of {name: Ident.t; slot: int} (** [DELETE_DEREF] *)
| DeleteAttr of {exp: Exp.t; attr: Ident.t}
| ImportStar of Exp.t
| GenStart of {kind: gen_kind}
| SetupAnnotations

Expand Down Expand Up @@ -676,6 +674,8 @@ module Stmt = struct
F.fprintf fmt "$DeleteDeref[%d,\"%a\")" slot Ident.pp name
| DeleteAttr {exp; attr} ->
F.fprintf fmt "$DeleteAttr(%a, %a)" Exp.pp exp Ident.pp attr
| ImportStar exp ->
F.fprintf fmt "$ImportStart(%a)" Exp.pp exp
| GenStart {kind} ->
let kind =
match kind with
Expand Down Expand Up @@ -1805,8 +1805,7 @@ let parse_bytecode st ({FFI.Code.co_consts; co_names; co_varnames} as code)
Ok (st, None)
| "IMPORT_STAR" ->
let* module_object, st = State.pop_and_cast st in
let* id, st = call_builtin_function st ImportStar [module_object] in
let st = State.push st (Exp.Temp id) in
let st = State.push_stmt st (ImportStar module_object) in
Ok (st, None)
| "IMPORT_FROM" ->
let name = co_names.(arg) |> Ident.mk in
Expand Down
2 changes: 1 addition & 1 deletion infer/src/python/PyIR.mli
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,6 @@ module BuiltinCaller : sig
| FormatFn of FormatFunction.t
| CallFunctionEx (** [CALL_FUNCTION_EX] *)
| Inplace of BinaryOp.t
| ImportStar
| Binary of BinaryOp.t
| Unary of UnaryOp.t
| Compare of CompareOp.t
Expand Down Expand Up @@ -196,6 +195,7 @@ module Stmt : sig
| Delete of ScopedIdent.t
| DeleteDeref of {name: Ident.t; slot: int} (** [DELETE_DEREF] *)
| DeleteAttr of {exp: Exp.t; attr: Ident.t}
| ImportStar of Exp.t
| GenStart of {kind: gen_kind}
| SetupAnnotations
end
Expand Down
4 changes: 2 additions & 2 deletions infer/src/python/PyIR2Textual.ml
Original file line number Diff line number Diff line change
Expand Up @@ -276,8 +276,6 @@ let builtin_name builtin =
"py_call_function_ex"
| Inplace op ->
F.asprintf "py_inplace_%s" (binary_op_name op)
| ImportStar ->
"py_import_star"
| Binary op ->
F.asprintf "py_binary_%s" (binary_op_name op)
| Unary op ->
Expand Down Expand Up @@ -396,6 +394,8 @@ let of_stmt loc stmt : Textual.Instr.t =
Let {id= None; exp= call_builtin "py_delete_attr" [of_exp exp; exp_of_ident_str attr]; loc}
| SetupAnnotations ->
Let {id= None; exp= call_builtin "py_setup_annotations" []; loc}
| ImportStar exp ->
Let {id= None; exp= call_builtin "py_import_star" [of_exp exp]; loc}
| GenStart {kind} ->
let kind =
match kind with
Expand Down
1 change: 1 addition & 0 deletions infer/src/python/PyIRExec.ml
Original file line number Diff line number Diff line change
Expand Up @@ -431,6 +431,7 @@ let run_files modules =
| DeleteAttr _
| StoreDeref _
| SetupAnnotations
| ImportStar _
| GenStart _ ->
todo "exec_stmt"
in
Expand Down
33 changes: 32 additions & 1 deletion infer/src/python/unit/PyIRTestImport.ml
Original file line number Diff line number Diff line change
Expand Up @@ -609,5 +609,36 @@ from foo import *
function toplevel():
b0:
n0 <- $ImportName(foo, $BuildTuple("*"), 0)
n1 <- $ImportStar(n0, None)
$ImportStart(n0)
return None |}]


let%expect_test _ =
let source =
{|
if test():
from mod import *
try:
pass
except Exception as error:
pass
|}
in
PyIR.test source ;
[%expect
{|
module dummy:

function toplevel():
b0:
n0 <- TOPLEVEL[test]
n1 <- $Call(n0, None)
if n1 then jmp b1 else jmp b2

b1:
n2 <- $ImportName(mod, $BuildTuple("*"), 0)
$ImportStart(n2)
jmp b2

b2:
return None |}]

0 comments on commit 324d444

Please sign in to comment.