Skip to content

Commit

Permalink
[Python] replacement fixes (#3619)
Browse files Browse the repository at this point in the history
* Python replacement fix

* Fix replacement for exn message
  • Loading branch information
dbrattli authored Nov 28, 2023
1 parent 8ee247a commit dc32820
Show file tree
Hide file tree
Showing 6 changed files with 9 additions and 15 deletions.
8 changes: 0 additions & 8 deletions src/Fable.Transforms/Python/Fable2Python.fs
Original file line number Diff line number Diff line change
Expand Up @@ -3082,14 +3082,6 @@ module Util =
// printfn "transformGet: %A" (fableExpr.Type)

match kind with
| Fable.FieldGet { Name = "message" } ->
let func = Expression.name "str"
let left, stmts = com.TransformAsExpr(ctx, fableExpr)
Expression.call (func, [ left ]), stmts
| Fable.FieldGet { Name = "push" } ->
let attr = Identifier("append")
let value, stmts = com.TransformAsExpr(ctx, fableExpr)
Expression.attribute (value = value, attr = attr, ctx = Load), stmts
| Fable.ExprGet(TransformExpr com ctx (prop, stmts)) ->
let expr, stmts' = com.TransformAsExpr(ctx, fableExpr)
let expr, stmts'' = getExpr com ctx range expr prop
Expand Down
5 changes: 3 additions & 2 deletions src/Fable.Transforms/Python/Replacements.fs
Original file line number Diff line number Diff line change
Expand Up @@ -2829,7 +2829,7 @@ let resizeArrays
| "set_Item", Some ar, [ idx; value ] -> setExpr r ar idx value |> Some
| "Add", Some ar, [ arg ] ->
"void ($0)"
|> emitExpr r t [ Helper.InstanceCall(ar, "push", t, [ arg ]) ]
|> emitExpr r t [ Helper.InstanceCall(ar, "append", t, [ arg ]) ]
|> Some
| "Remove", Some ar, [ arg ] ->
let args =
Expand Down Expand Up @@ -4625,7 +4625,8 @@ let exceptions
| ".ctor", _ ->
Helper.ConstructorCall(makeIdentExpr "Exception", t, args, ?loc = r)
|> Some
| "get_Message", Some e -> getFieldWith r t e "message" |> Some
| "get_Message", Some e ->
Helper.GlobalCall("str", t, [ thisArg.Value ], ?loc = r) |> Some
| "get_StackTrace", Some e -> getFieldWith r t e "stack" |> Some
| _ -> None

Expand Down
4 changes: 2 additions & 2 deletions tests/Python/TestApplicative.fs
Original file line number Diff line number Diff line change
Expand Up @@ -1174,7 +1174,7 @@ let ``test Curried function options work`` () =
// See https://github.com/fable-compiler/Fable/issues/1199#issuecomment-347101093
[<Fact>]
let ``test Applying function options works`` () =
Pointful.testFunctionOptions
Pointful.testFunctionOptions ()

[<Fact>]
let ``test Point-free and partial application work`` () = // See #1199
Expand Down Expand Up @@ -1215,7 +1215,7 @@ let ``test Uncurried functions in record fields can be partially applied`` () =
// See https://github.com/fable-compiler/Fable/issues/1199#issuecomment-347190893
[<Fact>]
let ``test Applicative operators work with three-argument functions``() =
Results.testOperatorsWith3Args
Results.testOperatorsWith3Args ()

[<Fact>]
let ``test partialApply works with tuples`` () =
Expand Down
2 changes: 1 addition & 1 deletion tests/Python/TestMisc.fs
Original file line number Diff line number Diff line change
Expand Up @@ -1329,7 +1329,7 @@ let ``test conditional expressions`` () = // See #2782
| U2.Case2 s when s.t1 <> "" -> s.t1
| _ -> ""

equal(test (U2.Case1 "x"), "x")
test (U2.Case1 "x") |> equal "x"

type Æøå =
| Union1 of string
Expand Down
1 change: 1 addition & 0 deletions tests/Python/TestPyInterop.fs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ type Props =

[<Global("Array")>]
type PyArray =
[<Emit("$0.append($1)")>]
abstract push: item: obj -> unit
[<Emit("len($0)")>]
abstract length: int
Expand Down
4 changes: 2 additions & 2 deletions tests/Python/TestSeq.fs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ let ``test Can test untyped enumerables`` () =
sumEnumerable xs |> equal 3
sumEnumerable ys |> equal 7
sumEnumerable zs |> equal -1

[<Fact>]
let ``test Seq.length works`` () =
let xs = [1.; 2.; 3.; 4.]
Expand Down Expand Up @@ -216,7 +216,7 @@ let ``test Seq.find works`` () =
[<Fact>]
let ``test Seq.findIndex works`` () =
let xs = [1.; 2.; 3.; 4.]
xs |> Seq.findIndex ((=) 2.)
xs |> Seq.findIndex ((=) 2.) |> equal 1

[<Fact>]
let ``test Seq.findBack works`` () =
Expand Down

0 comments on commit dc32820

Please sign in to comment.