-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathBuiltins.fs
More file actions
422 lines (323 loc) · 12.4 KB
/
Builtins.fs
File metadata and controls
422 lines (323 loc) · 12.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
/// Type bindings for Python builtins: https://docs.python.org/3/library/functions.html#built-in-funcs
module Fable.Python.Builtins
open System
open System.Collections.Generic
open Fable.Core
// fsharplint:disable MemberNames,InterfaceNames
type TextIOBase =
abstract read: unit -> string
abstract read: __size: int -> string
abstract write: __s: string -> int
abstract writelines: __lines: string seq -> unit
abstract readline: __size: int -> string
abstract readlines: __hint: int -> string list
abstract tell: unit -> int
type TextIOWrapper =
inherit IDisposable
inherit TextIOBase
module OpenTextMode =
[<Literal>]
let ReadUpdate = "r+"
[<Literal>]
let UpdateRead = "+r"
[<Literal>]
let ReadTextUpdate = "rt+"
[<Literal>]
let ReadUpdateText = "r+t"
[<Literal>]
let UpdateReadText = "+rt"
[<Literal>]
let TextReadUpdate = "tr+"
[<Literal>]
let TextUpdateRead = "t+r"
[<Literal>]
let UpdateTextRead = "+tr"
[<Literal>]
let WriteUpdate = "w+"
[<Literal>]
let UpdateWrite = "+w"
[<Literal>]
let WriteTextUpdate = "wt+"
[<Literal>]
let WriteUpdateText = "w+t"
[<Literal>]
let UpdateWriteText = "+wt"
[<Literal>]
let TextWriteUpdate = "tw+"
[<Literal>]
let TextUpdateWrite = "t+w"
[<Literal>]
let UpdateTextWrite = "+tw"
[<Literal>]
let AppendUpdate = "a+"
[<Literal>]
let UpdateAppend = "+a"
[<Literal>]
let AppendTextUpdate = "at+"
[<Literal>]
let AppendUpdateText = "a+t"
[<Literal>]
let UpdateAppendText = "+at"
[<Literal>]
let TextAppendUpdate = "ta+"
[<Literal>]
let TextUpdateAppend = "t+a"
[<Literal>]
let UpdateTextAppend = "+ta"
[<Literal>]
let CreateUpdate = "x+"
[<Literal>]
let UpdateCreate = "+x"
[<Literal>]
let CreateTextUpdate = "xt+"
[<Literal>]
let CreateUpdateText = "x+t"
[<Literal>]
let UpdateCreateText = "+xt"
[<Literal>]
let TextCreateUpdate = "tx+"
[<Literal>]
let TextUpdateCreate = "t+x"
[<Literal>]
let UpdateTextCreate = "+tx"
[<Literal>]
let Read = "rt"
[<Literal>]
let ReadText = "rt"
[<Literal>]
let TextRead = "tr"
[<Literal>]
let Write = "w"
[<Literal>]
let WriteText = "wt"
[<Literal>]
let TextWrite = "tw"
[<Literal>]
let Append = "a"
[<Literal>]
let AppendText = "at"
[<Literal>]
let TextAppend = "ta"
[<Literal>]
let Create = "x"
[<Literal>]
let CreateText = "xt"
[<Literal>]
let TextCreate = "tx"
type _Opener = Tuple<string, int> -> int
[<Erase>]
type IExports =
/// Return the absolute value of the argument.
abstract abs: int -> int
/// Return the absolute value of the argument.
abstract abs: float -> float
/// Return a Unicode string of one character with ordinal i; 0 <= i <= 0x10ffff.
abstract chr: int -> char
/// Return the names in the current scope.
abstract dir: unit -> string list
/// Return an alphabetized list of names comprising (some of) the
/// attributes of the given object, and of attributes reachable from
/// it
abstract dir: obj -> string list
/// Return the identity of an object.
abstract id: obj -> int
///Return the length (the number of items) of an object. The argument may
///be a sequence (such as a string, bytes, tuple, list, or range) or a
///collection (such as a dictionary, set, or frozen set).
abstract len: obj -> int
/// Make an iterator that computes the function using arguments from
/// the iterable. Stops when iterable is exhausted.
abstract map: ('T1 -> 'T2) * IEnumerable<'T1> -> IEnumerable<'T2>
/// Make an iterator that computes the function using arguments from each
/// of the iterables. Stops when the shortest iterable is exhausted.
abstract map: System.Func<'T1, 'T2, 'T3> * IEnumerable<'T1> * IEnumerable<'T2> -> IEnumerable<'T3>
/// Make an iterator that computes the function using arguments from each
/// of the iterables. Stops when the shortest iterable is exhausted.
abstract map:
System.Func<'T1, 'T2, 'T3, 'T4> * IEnumerable<'T1> * IEnumerable<'T2> * IEnumerable<'T3> -> IEnumerable<'T4>
/// Return the Unicode code point for a one-character string.
abstract ord: char -> int
/// Object to string
abstract str: obj -> string
/// Object to int
abstract int: obj -> int
/// Object to float
abstract float: obj -> float
/// Object to bool
abstract bool: obj -> bool
/// Convert to bytes
abstract bytes: byte[] -> byte[]
/// Convert string to bytes with encoding
abstract bytes: string * encoding: string -> byte[]
/// Create a new empty dictionary.
abstract dict: unit -> Dictionary<string, obj>
/// Create a dictionary from an iterable of key/value pairs.
abstract dict: IEnumerable<string * 'V> -> Dictionary<string, 'V>
/// Create a list from an iterable.
abstract list: IEnumerable<'T> -> ResizeArray<'T>
/// Return the largest item in an iterable or the largest of two or more arguments.
abstract max: 'T * 'T -> 'T
/// Return the largest item in an iterable or the largest of two or more arguments.
abstract max: 'T * 'T * 'T -> 'T
/// Return the largest item in an iterable or the largest of two or more arguments.
abstract max: IEnumerable<'T> -> 'T
/// Return the smallest item in an iterable or the smallest of two or more arguments.
abstract min: 'T * 'T -> 'T
/// Return the smallest item in an iterable or the smallest of two or more arguments.
abstract min: 'T * 'T * 'T -> 'T
/// Return the smallest item in an iterable or the smallest of two or more arguments.
abstract min: IEnumerable<'T> -> 'T
/// Return the sum of a 'start' value (default: 0) plus an iterable of numbers.
abstract sum: IEnumerable<'T> -> 'T
/// Return True if bool(x) is True for all values x in the iterable.
abstract all: IEnumerable<bool> -> bool
/// Return True if bool(x) is True for any x in the iterable.
abstract any: IEnumerable<bool> -> bool
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 *
?mode: string *
?buffering: int *
?encoding: string *
?errors: string *
?newline: string *
?closefd: bool *
?opener: _Opener ->
TextIOWrapper
[<NamedParams(fromIndex = 1)>]
abstract ``open``:
file: string *
?mode: string *
?buffering: int *
?encoding: string *
?errors: string *
?newline: string *
?closefd: bool *
?opener: _Opener ->
TextIOWrapper
/// Return the value of the named attribute of object. name must be a string.
/// See https://docs.python.org/3/library/functions.html#getattr
abstract getattr: obj: obj * name: string -> 'T
/// Return the value of the named attribute of object. If the attribute does not exist,
/// default is returned.
/// See https://docs.python.org/3/library/functions.html#getattr
abstract getattr: obj: obj * name: string * ``default``: 'T -> 'T
/// Sets the named attribute on the given object to the specified value.
/// See https://docs.python.org/3/library/functions.html#setattr
abstract setattr: obj: obj * name: string * value: obj -> unit
/// Return True if the string is the name of one of the object's attributes.
/// See https://docs.python.org/3/library/functions.html#hasattr
abstract hasattr: obj: obj * name: string -> bool
/// Return True if the object argument is an instance of the classinfo argument.
/// See https://docs.python.org/3/library/functions.html#isinstance
abstract isinstance: obj: obj * classinfo: obj -> bool
/// Return the type of an object.
/// See https://docs.python.org/3/library/functions.html#type
abstract ``type``: obj -> obj
[<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.
[<Emit("__name__")>]
let __name__: string = nativeOnly
// ============================================================================
// Python type references
//
// These expose Python's built-in types as values, primarily for use with
// `Fable.Core.PyInterop.pyInstanceof`. F# names are prefixed with `py` to
// avoid colliding with F#/.NET built-in types.
// ============================================================================
/// Reference to Python's `int` type.
[<Emit("int")>]
let pyInt: obj = nativeOnly
/// Reference to Python's `float` type.
[<Emit("float")>]
let pyFloat: obj = nativeOnly
/// Reference to Python's `bool` type.
[<Emit("bool")>]
let pyBool: obj = nativeOnly
/// Reference to Python's `str` type.
[<Emit("str")>]
let pyStr: obj = nativeOnly
/// Reference to Python's `bytes` type.
[<Emit("bytes")>]
let pyBytes: obj = nativeOnly
/// Reference to Python's `list` type.
[<Emit("list")>]
let pyList: obj = nativeOnly
/// Reference to Python's `dict` type.
[<Emit("dict")>]
let pyDict: obj = nativeOnly
/// Reference to Python's `tuple` type.
[<Emit("tuple")>]
let pyTuple: obj = nativeOnly
/// Reference to Python's `set` type.
[<Emit("set")>]
let pySet: obj = nativeOnly
/// Python's `None` singleton, typed as `obj` for use as a sentinel
/// (e.g. JSON null) and in interop sites that need an explicit None value.
[<Emit("None")>]
let pyNone: obj = nativeOnly
/// Python print function. Takes a single argument, so can be used with e.g string interpolation.
let print obj = builtins.print obj
/// Return the value of the named attribute of object with a default.
let getattr obj name defaultValue =
builtins.getattr (obj, name, defaultValue)
/// Sets the named attribute on the given object to the specified value.
let setattr obj name value = builtins.setattr (obj, name, value)
/// Return True if the string is the name of one of the object's attributes.
let hasattr obj name = builtins.hasattr (obj, name)