Skip to content

Commit 72f09b2

Browse files
authored
feat: Add exception types (#173)
1 parent a91b046 commit 72f09b2

File tree

2 files changed

+75
-0
lines changed

2 files changed

+75
-0
lines changed

src/stdlib/Builtins.fs

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,22 @@ type IExports =
230230

231231
abstract print: obj: obj -> unit
232232

233+
/// Print with custom end string (default is newline)
234+
[<NamedParams(fromIndex = 1)>]
235+
abstract print: obj: obj * ``end``: string -> unit
236+
237+
/// Print with custom separator and end string
238+
[<NamedParams(fromIndex = 1)>]
239+
abstract print: obj: obj * sep: string * ``end``: string -> unit
240+
241+
/// Print to a file-like object (e.g., sys.stderr)
242+
[<NamedParams(fromIndex = 1)>]
243+
abstract print: obj: obj * file: TextIOBase -> unit
244+
245+
/// Print to a file-like object with custom end string
246+
[<NamedParams(fromIndex = 1)>]
247+
abstract print: obj: obj * ``end``: string * file: TextIOBase -> unit
248+
233249
[<NamedParams(fromIndex = 1)>]
234250
abstract ``open``:
235251
file: int *
@@ -257,6 +273,55 @@ type IExports =
257273
[<ImportAll("builtins")>]
258274
let builtins: IExports = nativeOnly
259275

276+
// ============================================================================
277+
// Python Built-in Exceptions
278+
// https://docs.python.org/3/library/exceptions.html
279+
// ============================================================================
280+
281+
// BaseException subclasses (not Exception subclasses)
282+
// These require special handling in try/catch as they don't inherit from Exception
283+
284+
/// Raised when the user hits the interrupt key (normally Control-C or Delete).
285+
/// Note: Inherits from BaseException, not Exception.
286+
/// https://docs.python.org/3/library/exceptions.html#KeyboardInterrupt
287+
[<Import("KeyboardInterrupt", "builtins")>]
288+
type KeyboardInterrupt() =
289+
inherit exn()
290+
291+
/// Raised by the sys.exit() function.
292+
/// Note: Inherits from BaseException, not Exception.
293+
/// https://docs.python.org/3/library/exceptions.html#SystemExit
294+
[<Import("SystemExit", "builtins")>]
295+
type SystemExit() =
296+
inherit exn()
297+
298+
/// Raised when a generator or coroutine is closed.
299+
/// Note: Inherits from BaseException, not Exception.
300+
/// https://docs.python.org/3/library/exceptions.html#GeneratorExit
301+
[<Import("GeneratorExit", "builtins")>]
302+
type GeneratorExit() =
303+
inherit exn()
304+
305+
// Exception subclasses
306+
307+
/// Raised when a system function returns a system-related error.
308+
/// https://docs.python.org/3/library/exceptions.html#OSError
309+
[<Import("OSError", "builtins")>]
310+
type OSError() =
311+
inherit exn()
312+
313+
/// Raised when an operation runs out of memory.
314+
/// https://docs.python.org/3/library/exceptions.html#MemoryError
315+
[<Import("MemoryError", "builtins")>]
316+
type MemoryError() =
317+
inherit exn()
318+
319+
/// Raised when the interpreter finds an internal error.
320+
/// https://docs.python.org/3/library/exceptions.html#SystemError
321+
[<Import("SystemError", "builtins")>]
322+
type SystemError() =
323+
inherit exn()
324+
260325
// NOTE: Below we can add builtins that don't require overloads, and do not
261326
// conflict with common F# or .NET functions. If they do we keep them in
262327
// `IExports` above.

src/stdlib/Sys.fs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
module Fable.Python.Sys
33

44
open Fable.Core
5+
open Fable.Python.Builtins
56

67
// fsharplint:disable MemberNames
78

@@ -47,6 +48,15 @@ type IExports =
4748
abstract version: string
4849
/// Python version information as a tuple
4950
abstract version_info: VersionInfo
51+
/// Standard input stream
52+
/// See https://docs.python.org/3/library/sys.html#sys.stdin
53+
abstract stdin: TextIOBase
54+
/// Standard output stream
55+
/// See https://docs.python.org/3/library/sys.html#sys.stdout
56+
abstract stdout: TextIOBase
57+
/// Standard error stream
58+
/// See https://docs.python.org/3/library/sys.html#sys.stderr
59+
abstract stderr: TextIOBase
5060
/// Exits with code 0, indicating success
5161
/// See https://docs.python.org/3/library/sys.html#sys.exit
5262
abstract exit: unit -> 'a

0 commit comments

Comments
 (0)