Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Python] Misc fixes #3540

Merged
merged 5 commits into from
Oct 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
257 changes: 118 additions & 139 deletions poetry.lock

Large diffs are not rendered by default.

13 changes: 6 additions & 7 deletions src/Fable.Transforms/Python/Fable2Python.fs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ module rec Fable.Transforms.Fable2Python

open System
open System.Collections.Generic
open System.Text.RegularExpressions

open Fable
open Fable.AST
Expand Down Expand Up @@ -2224,11 +2225,6 @@ module Util =
// printfn "transformGet: %A" (fableExpr.Type)

match kind with
| Fable.ExprGet (Fable.Value(kind = Fable.StringConstant "length"))
| Fable.FieldGet { Name = "length" } ->
let func = Expression.name "len"
let left, stmts = com.TransformAsExpr(ctx, fableExpr)
Expression.call (func, [ left ]), stmts
| Fable.FieldGet { Name = "message" } ->
let func = Expression.name "str"
let left, stmts = com.TransformAsExpr(ctx, fableExpr)
Expand All @@ -2243,7 +2239,7 @@ module Util =
expr, stmts @ stmts' @ stmts''

| Fable.FieldGet i ->
//printfn "Fable.FieldGet: %A" (fieldName, fableExpr.Type)
// printfn "Fable.FieldGet: %A" (i.Name, fableExpr.Type)
let fieldName = i.Name |> Naming.toSnakeCase // |> Helpers.clean

let fableExpr =
Expand Down Expand Up @@ -3249,6 +3245,9 @@ module Util =

let args = discardUnitArg args

/// Removes `_mut` or `_mut_1` suffix from the identifier name
let cleanName (input: string) = Regex.Replace(input, @"_mut(_\d+)?$", "")

// For Python we need to append the TC-arguments to any declared (arrow) function inside the while-loop of the
// TCO. We will set them as default values to themselves e.g `i=i` to capture the value and not the variable.
let tcArgs, tcDefaults =
Expand All @@ -3257,7 +3256,7 @@ module Util =
tc.Args
|> List.choose (fun arg ->
let (Identifier name) = arg.Arg
let name = name.Substring(0, name.Length - 4)
let name = cleanName name
match name with
| "tupled_arg_m" -> None // Remove these arguments (not sure why)
| _ ->
Expand Down
20 changes: 11 additions & 9 deletions src/Fable.Transforms/Python/Replacements.fs
Original file line number Diff line number Diff line change
Expand Up @@ -1578,7 +1578,7 @@ let strings (com: ICompiler) (ctx: Context) r t (i: CallInfo) (thisArg: Expr opt

let stringModule (com: ICompiler) (ctx: Context) r t (i: CallInfo) (_: Expr option) (args: Expr list) =
match i.CompiledName, args with
| "Length", [ arg ] -> getFieldWith r t arg "length" |> Some
| "Length", [ arg ] -> Helper.GlobalCall("len", t, [ arg ], [ t ], ?loc = r) |> Some
| ("Iterate"
| "IterateIndexed"
| "ForAll"
Expand Down Expand Up @@ -1617,7 +1617,7 @@ let formattableString (com: ICompiler) (ctx: Context) r (t: Type) (i: CallInfo)
| "Create", None, [ str; args ] -> objExpr [ "str", str; "args", args ] |> Some
| "get_Format", Some x, _ -> getFieldWith r t x "str" |> Some
| "get_ArgumentCount", Some x, _ ->
getFieldWith r t (getField x "args") "length"
Helper.GlobalCall("len", t, [ getField x "args" ], [ t ], ?loc = r)
|> Some
| "GetArgument", Some x, [ idx ] ->
getExpr r t (getField x "args") idx
Expand Down Expand Up @@ -1696,7 +1696,7 @@ let resizeArrays (com: ICompiler) (ctx: Context) r (t: Type) (i: CallInfo) (this
match ar.Type with
// Fable translates System.Collections.Generic.List as Array
// TODO: Check also IList?
| Array _ -> getFieldWith r t ar "length" |> Some
| Array _ -> Helper.GlobalCall("len", t, [ ar ], [ t ], ?loc = r) |> Some
| _ ->
Helper.LibCall(com, "util", "count", t, [ ar ], ?loc = r)
|> Some
Expand Down Expand Up @@ -1889,7 +1889,7 @@ let arrayModule (com: ICompiler) (ctx: Context) r (t: Type) (i: CallInfo) (_: Ex

newArray (makeIntConst 0) t |> Some
| "IsEmpty", [ ar ] ->
eq (getFieldWith r (Int32.Number) ar "length") (makeIntConst 0)
eq (Helper.GlobalCall("len", t, [ ar ], [ t ], ?loc = r)) (makeIntConst 0)
|> Some

| "SortInPlaceWith", args ->
Expand Down Expand Up @@ -2480,7 +2480,9 @@ let intrinsicFunctions (com: ICompiler) (ctx: Context) r t (i: CallInfo) (thisAr
[ ar; lower; upper ] ->
let upper =
match upper with
| Value (NewOption (None, _, _), _) -> getExpr None (Int32.Number) ar (makeStrConst "length")
| Value (NewOption (None, _, _), _) ->
Helper.GlobalCall("len", t, [ ar ], [ t ], ?loc = r)
//getExpr None (Int32.Number) ar (makeStrConst "length2")
| _ -> add upper (makeIntConst 1)

Helper.InstanceCall(ar, "slice", t, [ lower; upper ], ?loc = r)
Expand Down Expand Up @@ -3185,10 +3187,10 @@ let regex com (ctx: Context) r t (i: CallInfo) (thisArg: Expr option) (args: Exp
propInt 0 thisArg.Value |> Some
| "get_Length" ->
if isGroup then
propStr "length" thisArg.Value |> Some
Helper.GlobalCall("len", t, [ thisArg.Value ], [ t ], ?loc = r) |> Some
else
propInt 0 thisArg.Value
|> propStr "length"
let prop = propInt 0 thisArg.Value
Helper.GlobalCall("len", t, [ prop ], [ t ], ?loc = r)
|> Some
// Group
| "get_Success" ->
Expand All @@ -3198,7 +3200,7 @@ let regex com (ctx: Context) r t (i: CallInfo) (thisArg: Expr option) (args: Exp
Helper.LibCall(com, "RegExp", "get_item", t, [ thisArg.Value; args.Head ], [ thisArg.Value.Type ], ?loc = r)
|> Some
| "get_Item" -> getExpr r t thisArg.Value args.Head |> Some
| "get_Count" -> propStr "length" thisArg.Value |> Some
| "get_Count" -> Helper.GlobalCall("len", t, [ thisArg.Value ], [ t ], ?loc = r) |> Some
| "GetEnumerator" -> getEnumerator com r t thisArg.Value |> Some
| meth ->
let meth =
Expand Down
1 change: 1 addition & 0 deletions tests/Python/Fable.Tests.Python.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@
<Compile Include="TestUri.fs" />
<Compile Include="TestPyInterop.fs" />

<Compile Include="TestNonRegression.fs" />
<Compile Include="Main.fs" />
</ItemGroup>
</Project>
17 changes: 17 additions & 0 deletions tests/Python/TestNonRegression.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
module Fable.Tests.NonRegression

open Util.Testing

module Issue3496 =
type Class(length: int) =
member x.Length = length
static member StaticLength (length: int) = length
let returnLength (length: int) = length


[<Fact>]
let testLengthPassedToCtorIsOk() =
let c = Class(1)
equal 1 c.Length
equal 1 (returnLength 1)
equal 1 (Class.StaticLength 1)
3 changes: 2 additions & 1 deletion tests/Python/TestPyInterop.fs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ type Props =
[<Global("Array")>]
type PyArray =
abstract push: item: obj -> unit
[<Emit("len($0)")>]
abstract length: int

[<Fable.Core.AttachMembers>]
Expand Down Expand Up @@ -162,4 +163,4 @@ let factorial (count : int) : int =
let ``test emitPyStatement works with parameters`` () =
factorial 5 |> equal 120

#endif
#endif
Loading