Skip to content
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
65 changes: 65 additions & 0 deletions src/stdlib/Builtins.fs
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,22 @@ type IExports =

abstract print: obj: obj -> unit

/// Print with custom end string (default is newline)
[<NamedParams(fromIndex = 1)>]
abstract print: obj: obj * ``end``: string -> unit

/// Print with custom separator and end string
[<NamedParams(fromIndex = 1)>]
abstract print: obj: obj * sep: string * ``end``: string -> unit

/// Print to a file-like object (e.g., sys.stderr)
[<NamedParams(fromIndex = 1)>]
abstract print: obj: obj * file: TextIOBase -> unit

/// Print to a file-like object with custom end string
[<NamedParams(fromIndex = 1)>]
abstract print: obj: obj * ``end``: string * file: TextIOBase -> unit

[<NamedParams(fromIndex = 1)>]
abstract ``open``:
file: int *
Expand Down Expand Up @@ -257,6 +273,55 @@ type IExports =
[<ImportAll("builtins")>]
let builtins: IExports = nativeOnly

// ============================================================================
// Python Built-in Exceptions
// https://docs.python.org/3/library/exceptions.html
// ============================================================================

// BaseException subclasses (not Exception subclasses)
// These require special handling in try/catch as they don't inherit from Exception

/// Raised when the user hits the interrupt key (normally Control-C or Delete).
/// Note: Inherits from BaseException, not Exception.
/// https://docs.python.org/3/library/exceptions.html#KeyboardInterrupt
[<Import("KeyboardInterrupt", "builtins")>]
type KeyboardInterrupt() =
inherit exn()

/// Raised by the sys.exit() function.
/// Note: Inherits from BaseException, not Exception.
/// https://docs.python.org/3/library/exceptions.html#SystemExit
[<Import("SystemExit", "builtins")>]
type SystemExit() =
inherit exn()

/// Raised when a generator or coroutine is closed.
/// Note: Inherits from BaseException, not Exception.
/// https://docs.python.org/3/library/exceptions.html#GeneratorExit
[<Import("GeneratorExit", "builtins")>]
type GeneratorExit() =
inherit exn()

// Exception subclasses

/// Raised when a system function returns a system-related error.
/// https://docs.python.org/3/library/exceptions.html#OSError
[<Import("OSError", "builtins")>]
type OSError() =
inherit exn()

/// Raised when an operation runs out of memory.
/// https://docs.python.org/3/library/exceptions.html#MemoryError
[<Import("MemoryError", "builtins")>]
type MemoryError() =
inherit exn()

/// Raised when the interpreter finds an internal error.
/// https://docs.python.org/3/library/exceptions.html#SystemError
[<Import("SystemError", "builtins")>]
type SystemError() =
inherit exn()

// NOTE: Below we can add builtins that don't require overloads, and do not
// conflict with common F# or .NET functions. If they do we keep them in
// `IExports` above.
Expand Down
10 changes: 10 additions & 0 deletions src/stdlib/Sys.fs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
module Fable.Python.Sys

open Fable.Core
open Fable.Python.Builtins

// fsharplint:disable MemberNames

Expand Down Expand Up @@ -47,6 +48,15 @@ type IExports =
abstract version: string
/// Python version information as a tuple
abstract version_info: VersionInfo
/// Standard input stream
/// See https://docs.python.org/3/library/sys.html#sys.stdin
abstract stdin: TextIOBase
/// Standard output stream
/// See https://docs.python.org/3/library/sys.html#sys.stdout
abstract stdout: TextIOBase
/// Standard error stream
/// See https://docs.python.org/3/library/sys.html#sys.stderr
abstract stderr: TextIOBase
/// Exits with code 0, indicating success
/// See https://docs.python.org/3/library/sys.html#sys.exit
abstract exit: unit -> 'a
Expand Down