Skip to content
Closed
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
5 changes: 5 additions & 0 deletions examples/files/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
*.py
fable_modules/
obj/
bin/
src/text.txt
5 changes: 5 additions & 0 deletions examples/files/build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/usr/bin/env sh
set -eux
dotnet fable-py src/Files.fsproj --outDir ./src
chmod +x src/program.py
python3 src/program.py
16 changes: 16 additions & 0 deletions examples/files/src/Files.fsproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<Compile Include="Program.fs" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="../../../src/Fable.Python.fsproj" />
</ItemGroup>

</Project>
6 changes: 6 additions & 0 deletions examples/files/src/Program.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
open Fable.Python.Builtins

builtins.``open``(StringPath "test.txt", OpenTextMode.Write).write("Hello World, from Fable.Python! :)")

let openFile = builtins.``open``(StringPath "test.txt", OpenTextMode.Read)
builtins.print(openFile.read())
24 changes: 6 additions & 18 deletions src/stdlib/Builtins.fs
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,14 @@ open Fable.Core
type TextIOBase =
abstract read : unit -> string
abstract read : __size: int -> string
abstract write : string -> unit

type TextIOWrapper =
inherit IDisposable
inherit TextIOBase

[<StringEnum>]
type OpenTextModeUpdating =
type OpenTextMode =
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So @dbrattli here I did something ugly. I tried to get the OpenTextMode union working with its different open modes but I could only get it working by adding the read, write and update to the same union context. What would you recommend to do here?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@MMagueta Yes this looks like the right thing to do 👍

| [<CompiledName("r+")>] ReadUpdate
| [<CompiledName("+r")>] UpdateRead
| [<CompiledName("rt+")>] ReadTextUpdate
Expand Down Expand Up @@ -49,15 +50,6 @@ type OpenTextModeUpdating =
| [<CompiledName("tx+")>] TextCreateUpdate
| [<CompiledName("t+x")>] TextUpdateCreate
| [<CompiledName("+tx")>] UpdateTextCreate

[<StringEnum>]
type OpenTextModeReading =
| [<CompiledName("rt")>] Read
| [<CompiledName("rt")>] ReadText
| [<CompiledName("tr")>] TextRead

[<StringEnum>]
type OpenTextModeWriting =
| [<CompiledName("w")>] Write
| [<CompiledName("wt")>] WriteText
| [<CompiledName("tw")>] TextWrite
Expand All @@ -67,13 +59,9 @@ type OpenTextModeWriting =
| [<CompiledName("x")>] Create
| [<CompiledName("xt")>] CreateText
| [<CompiledName("tx")>] TextCreate

[<Erase>]
type OpenTextMode =
| OpenTextModeUpdating
| OpenTextModeWriting
| OpenTextModeReading

| [<CompiledName("rt")>] Read
| [<CompiledName("rt")>] ReadText
| [<CompiledName("tr")>] TextRead

[<Erase>]
type _OpenFile =
Expand Down Expand Up @@ -133,4 +121,4 @@ let builtins: IExports = nativeOnly
let __name__: string = nativeOnly

/// Python print function. Takes a single argument, so can be used with e.g string interpolation.
let print obj = builtins.print obj
let print obj = builtins.print obj
1 change: 1 addition & 0 deletions test.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ABC
5 changes: 5 additions & 0 deletions test/TestBuiltins.fs
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,9 @@ let ``test print works`` () =
let result = builtins.print "Hello, world!"
result |> equal ()

[<Fact>]
let ``test write works`` () =
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I still need to fix this test, don't take it for granted

let result = builtins.``open``(StringPath "test.txt", OpenTextMode.OpenTextModeWriting.Read)
result.write "ABC" |> equal ()

let ``test __name__ works`` () = __name__ |> equal "test_builtins"