pyodide 0.22.1 → 0.23.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/ffi.d.ts ADDED
@@ -0,0 +1,807 @@
1
+ // Generated by dts-bundle-generator v6.7.0
2
+
3
+ declare type PyProxyCache = {
4
+ cacheId: number;
5
+ refcnt: number;
6
+ leaked?: boolean;
7
+ };
8
+ declare type PyProxyProps = {
9
+ /**
10
+ * captureThis tracks whether this should be passed as the first argument to
11
+ * the Python function or not. We keep it false by default. To make a PyProxy
12
+ * where the ``this`` argument is included, call the :js:meth:`captureThis` method.
13
+ */
14
+ captureThis: boolean;
15
+ /**
16
+ * isBound tracks whether bind has been called
17
+ */
18
+ isBound: boolean;
19
+ /**
20
+ * the ``this`` value that has been bound to the PyProxy
21
+ */
22
+ boundThis?: any;
23
+ /**
24
+ * Any extra arguments passed to bind are used for partial function
25
+ * application. These are stored here.
26
+ */
27
+ boundArgs: any[];
28
+ roundtrip: boolean;
29
+ };
30
+ interface PyProxy {
31
+ [x: string]: any;
32
+ }
33
+ /**
34
+ * A :js:class:`~pyodide.ffi.PyProxy` is an object that allows idiomatic use of a Python object from
35
+ * JavaScript. See :ref:`type-translations-pyproxy`.
36
+ */
37
+ declare class PyProxy {
38
+ /** @private */
39
+ $$: {
40
+ ptr: number;
41
+ cache: PyProxyCache;
42
+ destroyed_msg?: string;
43
+ };
44
+ /** @private */
45
+ $$props: PyProxyProps;
46
+ /** @private */
47
+ $$flags: number;
48
+ /**
49
+ * @private
50
+ * @hideconstructor
51
+ */
52
+ constructor();
53
+ /** @private */
54
+ get [Symbol.toStringTag](): string;
55
+ /**
56
+ * The name of the type of the object.
57
+ *
58
+ * Usually the value is ``"module.name"`` but for builtins or
59
+ * interpreter-defined types it is just ``"name"``. As pseudocode this is:
60
+ *
61
+ * .. code-block:: python
62
+ *
63
+ * ty = type(x)
64
+ * if ty.__module__ == 'builtins' or ty.__module__ == "__main__":
65
+ * return ty.__name__
66
+ * else:
67
+ * ty.__module__ + "." + ty.__name__
68
+ *
69
+ */
70
+ get type(): string;
71
+ toString(): string;
72
+ /**
73
+ * Destroy the :js:class:`~pyodide.ffi.PyProxy`. This will release the memory. Any further attempt
74
+ * to use the object will raise an error.
75
+ *
76
+ * In a browser supporting :js:data:`FinalizationRegistry`, Pyodide will
77
+ * automatically destroy the :js:class:`~pyodide.ffi.PyProxy` when it is garbage collected, however
78
+ * there is no guarantee that the finalizer will be run in a timely manner so
79
+ * it is better to destroy the proxy explicitly.
80
+ *
81
+ * @param options
82
+ * @param options.message The error message to print if use is attempted after
83
+ * destroying. Defaults to "Object has already been destroyed".
84
+ *
85
+ */
86
+ destroy(options?: {
87
+ message?: string;
88
+ destroyRoundtrip?: boolean;
89
+ }): void;
90
+ /**
91
+ * Make a new :js:class:`~pyodide.ffi.PyProxy` pointing to the same Python object.
92
+ * Useful if the :js:class:`~pyodide.ffi.PyProxy` is destroyed somewhere else.
93
+ */
94
+ copy(): PyProxy;
95
+ /**
96
+ * Converts the :js:class:`~pyodide.ffi.PyProxy` into a JavaScript object as best as possible. By
97
+ * default does a deep conversion, if a shallow conversion is desired, you can
98
+ * use ``proxy.toJs({depth : 1})``. See :ref:`Explicit Conversion of PyProxy
99
+ * <type-translations-pyproxy-to-js>` for more info.
100
+ * @param options
101
+ * @return The JavaScript object resulting from the conversion.
102
+ */
103
+ toJs({ depth, pyproxies, create_pyproxies, dict_converter, default_converter, }?: {
104
+ /** How many layers deep to perform the conversion. Defaults to infinite */
105
+ depth?: number;
106
+ /**
107
+ * If provided, :js:meth:`toJs` will store all PyProxies created in this
108
+ * list. This allows you to easily destroy all the PyProxies by iterating
109
+ * the list without having to recurse over the generated structure. The most
110
+ * common use case is to create a new empty list, pass the list as
111
+ * ``pyproxies``, and then later iterate over ``pyproxies`` to destroy all of
112
+ * created proxies.
113
+ */
114
+ pyproxies?: PyProxy[];
115
+ /**
116
+ * If false, :js:meth:`toJs` will throw a
117
+ * :py:exc:`~pyodide.ffi.ConversionError` rather than producing a
118
+ * :js:class:`~pyodide.ffi.PyProxy`.
119
+ */
120
+ create_pyproxies?: boolean;
121
+ /**
122
+ * A function to be called on an iterable of pairs ``[key, value]``. Convert
123
+ * this iterable of pairs to the desired output. For instance,
124
+ * :js:func:`Object.fromEntries` would convert the dict to an object,
125
+ * :js:func:`Array.from` converts it to an :js:class:`Array` of pairs, and
126
+ * ``(it) => new Map(it)`` converts it to a :js:class:`Map` (which is the
127
+ * default behavior).
128
+ */
129
+ dict_converter?: (array: Iterable<[
130
+ key: string,
131
+ value: any
132
+ ]>) => any;
133
+ /**
134
+ * Optional argument to convert objects with no default conversion. See the
135
+ * documentation of :meth:`~pyodide.ffi.to_js`.
136
+ */
137
+ default_converter?: (obj: PyProxy, convert: (obj: PyProxy) => any, cacheConversion: (obj: PyProxy, result: any) => void) => any;
138
+ }): any;
139
+ /**
140
+ * Check whether the :js:class:`~pyodide.ffi.PyProxy` is a :js:class:`~pyodide.ffi.PyProxyWithLength`.
141
+ * @deprecated Use ``obj instanceof pyodide.ffi.PyProxyWithLength`` instead.
142
+ */
143
+ supportsLength(): this is PyProxyWithLength;
144
+ /**
145
+ * Check whether the :js:class:`~pyodide.ffi.PyProxy` is a :js:class:`~pyodide.ffi.PyProxyWithGet`.
146
+ * @deprecated Use ``obj instanceof pyodide.ffi.PyProxyWithGet`` instead.
147
+ */
148
+ supportsGet(): this is PyProxyWithGet;
149
+ /**
150
+ * Check whether the :js:class:`~pyodide.ffi.PyProxy` is a :js:class:`~pyodide.ffi.PyProxyWithSet`.
151
+ * @deprecated Use ``obj instanceof pyodide.ffi.PyProxyWithSet`` instead.
152
+ */
153
+ supportsSet(): this is PyProxyWithSet;
154
+ /**
155
+ * Check whether the :js:class:`~pyodide.ffi.PyProxy` is a :js:class:`~pyodide.ffi.PyProxyWithHas`.
156
+ * @deprecated Use ``obj instanceof pyodide.ffi.PyProxyWithHas`` instead.
157
+ */
158
+ supportsHas(): this is PyProxyWithHas;
159
+ /**
160
+ * Check whether the :js:class:`~pyodide.ffi.PyProxy` is a
161
+ * :js:class:`~pyodide.ffi.PyIterable`.
162
+ * @deprecated Use ``obj instanceof pyodide.ffi.PyIterable`` instead.
163
+ */
164
+ isIterable(): this is PyIterable;
165
+ /**
166
+ * Check whether the :js:class:`~pyodide.ffi.PyProxy` is a
167
+ * :js:class:`~pyodide.ffi.PyIterator`
168
+ * @deprecated Use ``obj instanceof pyodide.ffi.PyIterator`` instead.
169
+ */
170
+ isIterator(): this is PyIterator;
171
+ /**
172
+ * Check whether the :js:class:`~pyodide.ffi.PyProxy` is a :js:class:`~pyodide.ffi.PyAwaitable`
173
+ * @deprecated Use :js:class:`obj instanceof pyodide.ffi.PyAwaitable <pyodide.ffi.PyAwaitable>` instead.
174
+ */
175
+ isAwaitable(): this is PyAwaitable;
176
+ /**
177
+ * Check whether the :js:class:`~pyodide.ffi.PyProxy` is a :js:class:`~pyodide.ffi.PyBuffer`.
178
+ * @deprecated Use ``obj instanceof pyodide.ffi.PyBuffer`` instead.
179
+ */
180
+ isBuffer(): this is PyBuffer;
181
+ /**
182
+ * Check whether the :js:class:`~pyodide.ffi.PyProxy` is a :js:class:`~pyodide.ffi.PyCallable`.
183
+ * @deprecated ``obj instanceof pyodide.ffi.PyCallable`` instead.
184
+ */
185
+ isCallable(): this is PyCallable;
186
+ }
187
+ /**
188
+ * A :js:class:`~pyodide.ffi.PyProxy` whose proxied Python object has a :meth:`~object.__len__`
189
+ * method.
190
+ */
191
+ declare class PyProxyWithLength extends PyProxy {
192
+ /** @private */
193
+ static [Symbol.hasInstance](obj: any): obj is PyProxy;
194
+ }
195
+ interface PyProxyWithLength extends PyLengthMethods {
196
+ }
197
+ declare class PyLengthMethods {
198
+ /**
199
+ * The length of the object.
200
+ */
201
+ get length(): number;
202
+ }
203
+ /**
204
+ * A :js:class:`~pyodide.ffi.PyProxy` whose proxied Python object has a
205
+ * :meth:`~object.__getitem__` method.
206
+ */
207
+ declare class PyProxyWithGet extends PyProxy {
208
+ /** @private */
209
+ static [Symbol.hasInstance](obj: any): obj is PyProxy;
210
+ }
211
+ interface PyProxyWithGet extends PyGetItemMethods {
212
+ }
213
+ declare class PyGetItemMethods {
214
+ /**
215
+ * This translates to the Python code ``obj[key]``.
216
+ *
217
+ * @param key The key to look up.
218
+ * @returns The corresponding value.
219
+ */
220
+ get(key: any): any;
221
+ }
222
+ /**
223
+ * A :js:class:`~pyodide.ffi.PyProxy` whose proxied Python object has a
224
+ * :meth:`~object.__setitem__` or :meth:`~object.__delitem__` method.
225
+ */
226
+ declare class PyProxyWithSet extends PyProxy {
227
+ /** @private */
228
+ static [Symbol.hasInstance](obj: any): obj is PyProxy;
229
+ }
230
+ interface PyProxyWithSet extends PySetItemMethods {
231
+ }
232
+ declare class PySetItemMethods {
233
+ /**
234
+ * This translates to the Python code ``obj[key] = value``.
235
+ *
236
+ * @param key The key to set.
237
+ * @param value The value to set it to.
238
+ */
239
+ set(key: any, value: any): void;
240
+ /**
241
+ * This translates to the Python code ``del obj[key]``.
242
+ *
243
+ * @param key The key to delete.
244
+ */
245
+ delete(key: any): void;
246
+ }
247
+ /**
248
+ * A :js:class:`~pyodide.ffi.PyProxy` whose proxied Python object has a
249
+ * :meth:`~object.__contains__` method.
250
+ */
251
+ declare class PyProxyWithHas extends PyProxy {
252
+ /** @private */
253
+ static [Symbol.hasInstance](obj: any): obj is PyProxy;
254
+ }
255
+ interface PyProxyWithHas extends PyContainsMethods {
256
+ }
257
+ declare class PyContainsMethods {
258
+ /**
259
+ * This translates to the Python code ``key in obj``.
260
+ *
261
+ * @param key The key to check for.
262
+ * @returns Is ``key`` present?
263
+ */
264
+ has(key: any): boolean;
265
+ }
266
+ /**
267
+ * A :js:class:`~pyodide.ffi.PyProxy` whose proxied Python object is :std:term:`iterable`
268
+ * (i.e., it has an :meth:`~object.__iter__` method).
269
+ */
270
+ declare class PyIterable extends PyProxy {
271
+ /** @private */
272
+ static [Symbol.hasInstance](obj: any): obj is PyProxy;
273
+ }
274
+ interface PyIterable extends PyIterableMethods {
275
+ }
276
+ declare class PyIterableMethods {
277
+ /**
278
+ * This translates to the Python code ``iter(obj)``. Return an iterator
279
+ * associated to the proxy. See the documentation for
280
+ * :js:data:`Symbol.iterator`.
281
+ *
282
+ * This will be used implicitly by ``for(let x of proxy){}``.
283
+ */
284
+ [Symbol.iterator](): Iterator<any, any, any>;
285
+ }
286
+ /**
287
+ * A :js:class:`~pyodide.ffi.PyProxy` whose proxied Python object is :std:term:`asynchronous
288
+ * iterable` (i.e., has an :meth:`~object.__aiter__` method).
289
+ */
290
+ declare class PyAsyncIterable extends PyProxy {
291
+ /** @private */
292
+ static [Symbol.hasInstance](obj: any): obj is PyProxy;
293
+ }
294
+ interface PyAsyncIterable extends PyAsyncIterableMethods {
295
+ }
296
+ declare class PyAsyncIterableMethods {
297
+ /**
298
+ * This translates to the Python code ``aiter(obj)``. Return an async iterator
299
+ * associated to the proxy. See the documentation for :js:data:`Symbol.asyncIterator`.
300
+ *
301
+ * This will be used implicitly by ``for(await let x of proxy){}``.
302
+ */
303
+ [Symbol.asyncIterator](): AsyncIterator<any, any, any>;
304
+ }
305
+ /**
306
+ * A :js:class:`~pyodide.ffi.PyProxy` whose proxied Python object is an :term:`iterator`
307
+ * (i.e., has a :meth:`~generator.send` or :meth:`~iterator.__next__` method).
308
+ */
309
+ declare class PyIterator extends PyProxy {
310
+ /** @private */
311
+ static [Symbol.hasInstance](obj: any): obj is PyProxy;
312
+ }
313
+ interface PyIterator extends PyIteratorMethods {
314
+ }
315
+ declare class PyIteratorMethods {
316
+ /** @private */
317
+ [Symbol.iterator](): this;
318
+ /**
319
+ * This translates to the Python code ``next(obj)``. Returns the next value of
320
+ * the generator. See the documentation for :js:meth:`Generator.next` The
321
+ * argument will be sent to the Python generator.
322
+ *
323
+ * This will be used implicitly by ``for(let x of proxy){}``.
324
+ *
325
+ * @param any The value to send to the generator. The value will be assigned
326
+ * as a result of a yield expression.
327
+ * @returns An Object with two properties: ``done`` and ``value``. When the
328
+ * generator yields ``some_value``, ``next`` returns ``{done : false, value :
329
+ * some_value}``. When the generator raises a :py:exc:`StopIteration`
330
+ * exception, ``next`` returns ``{done : true, value : result_value}``.
331
+ */
332
+ next(arg?: any): IteratorResult<any, any>;
333
+ }
334
+ /**
335
+ * A :js:class:`~pyodide.ffi.PyProxy` whose proxied Python object is a :std:term:`generator`
336
+ * (i.e., it is an instance of :py:class:`~collections.abc.Generator`).
337
+ */
338
+ declare class PyGenerator extends PyProxy {
339
+ /** @private */
340
+ static [Symbol.hasInstance](obj: any): obj is PyProxy;
341
+ }
342
+ interface PyGenerator extends PyGeneratorMethods {
343
+ }
344
+ declare class PyGeneratorMethods {
345
+ /**
346
+ * Throws an exception into the Generator.
347
+ *
348
+ * See the documentation for :js:meth:`Generator.throw`.
349
+ *
350
+ * @param exception Error The error to throw into the generator. Must be an
351
+ * instanceof ``Error``.
352
+ * @returns An Object with two properties: ``done`` and ``value``. When the
353
+ * generator yields ``some_value``, ``return`` returns ``{done : false, value
354
+ * : some_value}``. When the generator raises a
355
+ * ``StopIteration(result_value)`` exception, ``return`` returns ``{done :
356
+ * true, value : result_value}``.
357
+ */
358
+ throw(exc: any): IteratorResult<any, any>;
359
+ /**
360
+ * Throws a :py:exc:`GeneratorExit` into the generator and if the
361
+ * :py:exc:`GeneratorExit` is not caught returns the argument value ``{done:
362
+ * true, value: v}``. If the generator catches the :py:exc:`GeneratorExit` and
363
+ * returns or yields another value the next value of the generator this is
364
+ * returned in the normal way. If it throws some error other than
365
+ * :py:exc:`GeneratorExit` or :py:exc:`StopIteration`, that error is propagated. See
366
+ * the documentation for :js:meth:`Generator.return`.
367
+ *
368
+ * @param any The value to return from the generator.
369
+ * @returns An Object with two properties: ``done`` and ``value``. When the
370
+ * generator yields ``some_value``, ``return`` returns ``{done : false, value
371
+ * : some_value}``. When the generator raises a
372
+ * ``StopIteration(result_value)`` exception, ``return`` returns ``{done :
373
+ * true, value : result_value}``.
374
+ */
375
+ return(v: any): IteratorResult<any, any>;
376
+ }
377
+ /**
378
+ * A :js:class:`~pyodide.ffi.PyProxy` whose proxied Python object is an
379
+ * :std:term:`asynchronous iterator`
380
+ */
381
+ declare class PyAsyncIterator extends PyProxy {
382
+ /** @private */
383
+ static [Symbol.hasInstance](obj: any): obj is PyProxy;
384
+ }
385
+ interface PyAsyncIterator extends PyAsyncIteratorMethods {
386
+ }
387
+ declare class PyAsyncIteratorMethods {
388
+ /** @private */
389
+ [Symbol.asyncIterator](): this;
390
+ /**
391
+ * This translates to the Python code ``anext(obj)``. Returns the next value
392
+ * of the asynchronous iterator. The argument will be sent to the Python
393
+ * iterator (if it's a generator for instance).
394
+ *
395
+ * This will be used implicitly by ``for(let x of proxy){}``.
396
+ *
397
+ * @param any The value to send to a generator. The value will be assigned as
398
+ * a result of a yield expression.
399
+ * @returns An Object with two properties: ``done`` and ``value``. When the
400
+ * iterator yields ``some_value``, ``next`` returns ``{done : false, value :
401
+ * some_value}``. When the giterator is done, ``next`` returns
402
+ * ``{done : true }``.
403
+ */
404
+ next(arg?: any): Promise<IteratorResult<any, any>>;
405
+ }
406
+ /**
407
+ * A :js:class:`~pyodide.ffi.PyProxy` whose proxied Python object is an
408
+ * :std:term:`asynchronous generator` (i.e., it is an instance of
409
+ * :py:class:`~collections.abc.AsyncGenerator`)
410
+ */
411
+ declare class PyAsyncGenerator extends PyProxy {
412
+ /** @private */
413
+ static [Symbol.hasInstance](obj: any): obj is PyProxy;
414
+ }
415
+ interface PyAsyncGenerator extends PyAsyncGeneratorMethods {
416
+ }
417
+ declare class PyAsyncGeneratorMethods {
418
+ /**
419
+ * Throws an exception into the Generator.
420
+ *
421
+ * See the documentation for :js:meth:`AsyncGenerator.throw`.
422
+ *
423
+ * @param exception Error The error to throw into the generator. Must be an
424
+ * instanceof ``Error``.
425
+ * @returns An Object with two properties: ``done`` and ``value``. When the
426
+ * generator yields ``some_value``, ``return`` returns ``{done : false, value
427
+ * : some_value}``. When the generator raises a
428
+ * ``StopIteration(result_value)`` exception, ``return`` returns ``{done :
429
+ * true, value : result_value}``.
430
+ */
431
+ throw(exc: any): Promise<IteratorResult<any, any>>;
432
+ /**
433
+ * Throws a :py:exc:`GeneratorExit` into the generator and if the
434
+ * :py:exc:`GeneratorExit` is not caught returns the argument value ``{done:
435
+ * true, value: v}``. If the generator catches the :py:exc:`GeneratorExit` and
436
+ * returns or yields another value the next value of the generator this is
437
+ * returned in the normal way. If it throws some error other than
438
+ * :py:exc:`GeneratorExit` or :py:exc:`StopAsyncIteration`, that error is
439
+ * propagated. See the documentation for :js:meth:`AsyncGenerator.throw`
440
+ *
441
+ * @param any The value to return from the generator.
442
+ * @returns An Object with two properties: ``done`` and ``value``. When the
443
+ * generator yields ``some_value``, ``return`` returns ``{done : false, value
444
+ * : some_value}``. When the generator raises a :py:exc:`StopAsyncIteration`
445
+ * exception, ``return`` returns ``{done : true, value : result_value}``.
446
+ */
447
+ return(v: any): Promise<IteratorResult<any, any>>;
448
+ }
449
+ /**
450
+ * A :js:class:`~pyodide.ffi.PyProxy` whose proxied Python object is :ref:`awaitable
451
+ * <asyncio-awaitables>` (i.e., has an :meth:`~object.__await__` method).
452
+ */
453
+ declare class PyAwaitable extends PyProxy {
454
+ /** @private */
455
+ static [Symbol.hasInstance](obj: any): obj is PyProxy;
456
+ }
457
+ interface PyAwaitable extends Promise<any> {
458
+ }
459
+ /**
460
+ * A :js:class:`~pyodide.ffi.PyProxy` whose proxied Python object is
461
+ * :std:term:`callable` (i.e., has an :py:meth:`~operator.__call__` method).
462
+ */
463
+ declare class PyCallable extends PyProxy {
464
+ /** @private */
465
+ static [Symbol.hasInstance](obj: any): obj is PyCallable;
466
+ }
467
+ interface PyCallable extends PyCallableMethods {
468
+ (...args: any[]): any;
469
+ }
470
+ declare class PyCallableMethods {
471
+ /**
472
+ * The ``apply()`` method calls the specified function with a given this
473
+ * value, and arguments provided as an array (or an array-like object). Like
474
+ * :js:meth:`Function.apply`.
475
+ *
476
+ * @param thisArg The ``this`` argument. Has no effect unless the
477
+ * :js:class:`~pyodide.ffi.PyCallable` has :js:meth:`captureThis` set. If
478
+ * :js:meth:`captureThis` is set, it will be passed as the first argument to
479
+ * the Python function.
480
+ * @param jsargs The array of arguments
481
+ * @returns The result from the function call.
482
+ */
483
+ apply(thisArg: any, jsargs: any): any;
484
+ /**
485
+ * Calls the function with a given this value and arguments provided
486
+ * individually. See :js:meth:`Function.call`.
487
+ *
488
+ * @param thisArg The ``this`` argument. Has no effect unless the
489
+ * :js:class:`~pyodide.ffi.PyCallable` has :js:meth:`captureThis` set. If
490
+ * :js:meth:`captureThis` is set, it will be passed as the first argument to
491
+ * the Python function.
492
+ * @param jsargs The arguments
493
+ * @returns The result from the function call.
494
+ */
495
+ call(thisArg: any, ...jsargs: any): any;
496
+ /**
497
+ * Call the function with key word arguments. The last argument must be an
498
+ * object with the keyword arguments.
499
+ */
500
+ callKwargs(...jsargs: any): any;
501
+ /**
502
+ * The ``bind()`` method creates a new function that, when called, has its
503
+ * ``this`` keyword set to the provided value, with a given sequence of
504
+ * arguments preceding any provided when the new function is called. See
505
+ * :js:meth:`Function.bind`.
506
+ *
507
+ * If the :js:class:`~pyodide.ffi.PyCallable` does not have
508
+ * :js:meth:`captureThis` set, the ``this`` parameter will be discarded. If it
509
+ * does have :js:meth:`captureThis` set, ``thisArg`` will be set to the first
510
+ * argument of the Python function. The returned proxy and the original proxy
511
+ * have the same lifetime so destroying either destroys both.
512
+ *
513
+ * @param thisArg The value to be passed as the ``this`` parameter to the
514
+ * target function ``func`` when the bound function is called.
515
+ * @param jsargs Extra arguments to prepend to arguments provided to the bound
516
+ * function when invoking ``func``.
517
+ * @returns
518
+ */
519
+ bind(thisArg: any, ...jsargs: any): PyProxy;
520
+ /**
521
+ * Returns a :js:class:`~pyodide.ffi.PyProxy` that passes ``this`` as the first argument to the
522
+ * Python function. The returned :js:class:`~pyodide.ffi.PyProxy` has the internal ``captureThis``
523
+ * property set.
524
+ *
525
+ * It can then be used as a method on a JavaScript object. The returned proxy
526
+ * and the original proxy have the same lifetime so destroying either destroys
527
+ * both.
528
+ *
529
+ * For example:
530
+ *
531
+ * .. code-block:: pyodide
532
+ *
533
+ * let obj = { a : 7 };
534
+ * pyodide.runPython(`
535
+ * def f(self):
536
+ * return self.a
537
+ * `);
538
+ * // Without captureThis, it doesn't work to use f as a method for obj:
539
+ * obj.f = pyodide.globals.get("f");
540
+ * obj.f(); // raises "TypeError: f() missing 1 required positional argument: 'self'"
541
+ * // With captureThis, it works fine:
542
+ * obj.f = pyodide.globals.get("f").captureThis();
543
+ * obj.f(); // returns 7
544
+ *
545
+ * @returns The resulting :js:class:`~pyodide.ffi.PyProxy`. It has the same lifetime as the
546
+ * original :js:class:`~pyodide.ffi.PyProxy` but passes ``this`` to the wrapped function.
547
+ *
548
+ */
549
+ captureThis(): PyProxy;
550
+ }
551
+ /**
552
+ * A :js:class:`~pyodide.ffi.PyProxy` whose proxied Python object supports the
553
+ * Python :external:doc:`c-api/buffer`.
554
+ *
555
+ * Examples of buffers include {py:class}`bytes` objects and numpy
556
+ * {external+numpy:ref}`arrays`.
557
+ */
558
+ declare class PyBuffer extends PyProxy {
559
+ /** @private */
560
+ static [Symbol.hasInstance](obj: any): obj is PyBuffer;
561
+ }
562
+ interface PyBuffer extends PyBufferMethods {
563
+ }
564
+ declare class PyBufferMethods {
565
+ /**
566
+ * Get a view of the buffer data which is usable from JavaScript. No copy is
567
+ * ever performed.
568
+ *
569
+ * We do not support suboffsets, if the buffer requires suboffsets we will
570
+ * throw an error. JavaScript nd array libraries can't handle suboffsets
571
+ * anyways. In this case, you should use the :js:meth:`~PyProxy.toJs` api or
572
+ * copy the buffer to one that doesn't use suboffsets (using e.g.,
573
+ * :py:func:`numpy.ascontiguousarray`).
574
+ *
575
+ * If the buffer stores big endian data or half floats, this function will
576
+ * fail without an explicit type argument. For big endian data you can use
577
+ * :js:meth:`~PyProxy.toJs`. :js:class:`DataView` has support for big endian
578
+ * data, so you might want to pass ``'dataview'`` as the type argument in that
579
+ * case.
580
+ *
581
+ * @param type The type of the :js:attr:`~pyodide.ffi.PyBufferView.data` field
582
+ * in the output. Should be one of: ``"i8"``, ``"u8"``, ``"u8clamped"``,
583
+ * ``"i16"``, ``"u16"``, ``"i32"``, ``"u32"``, ``"i32"``, ``"u32"``,
584
+ * ``"i64"``, ``"u64"``, ``"f32"``, ``"f64``, or ``"dataview"``. This argument
585
+ * is optional, if absent :js:meth:`~pyodide.ffi.PyBuffer.getBuffer` will try
586
+ * to determine the appropriate output type based on the buffer format string
587
+ * (see :std:ref:`struct-format-strings`).
588
+ */
589
+ getBuffer(type?: string): PyBufferView;
590
+ }
591
+ export declare type TypedArray = Int8Array | Uint8Array | Int16Array | Uint16Array | Int32Array | Uint32Array | Uint8ClampedArray | Float32Array | Float64Array;
592
+ /**
593
+ * A :js:class:`~pyodide.ffi.PyProxy` whose proxied Python object is a :py:class:`dict`.
594
+ */
595
+ declare class PyDict extends PyProxy {
596
+ /** @private */
597
+ static [Symbol.hasInstance](obj: any): obj is PyProxy;
598
+ }
599
+ interface PyDict extends PyProxyWithGet, PyProxyWithSet, PyProxyWithHas, PyProxyWithLength, PyIterable {
600
+ }
601
+ /**
602
+ * A class to allow access to Python data buffers from JavaScript. These are
603
+ * produced by :js:meth:`~pyodide.ffi.PyBuffer.getBuffer` and cannot be constructed directly.
604
+ * When you are done, release it with the :js:func:`~PyBufferView.release` method.
605
+ * See the Python :external:doc:`c-api/buffer` documentation for more
606
+ * information.
607
+ *
608
+ * To find the element ``x[a_1, ..., a_n]``, you could use the following code:
609
+ *
610
+ * .. code-block:: js
611
+ *
612
+ * function multiIndexToIndex(pybuff, multiIndex){
613
+ * if(multindex.length !==pybuff.ndim){
614
+ * throw new Error("Wrong length index");
615
+ * }
616
+ * let idx = pybuff.offset;
617
+ * for(let i = 0; i < pybuff.ndim; i++){
618
+ * if(multiIndex[i] < 0){
619
+ * multiIndex[i] = pybuff.shape[i] - multiIndex[i];
620
+ * }
621
+ * if(multiIndex[i] < 0 || multiIndex[i] >= pybuff.shape[i]){
622
+ * throw new Error("Index out of range");
623
+ * }
624
+ * idx += multiIndex[i] * pybuff.stride[i];
625
+ * }
626
+ * return idx;
627
+ * }
628
+ * console.log("entry is", pybuff.data[multiIndexToIndex(pybuff, [2, 0, -1])]);
629
+ *
630
+ * .. admonition:: Converting between TypedArray types
631
+ * :class: warning
632
+ *
633
+ * The following naive code to change the type of a typed array does not
634
+ * work:
635
+ *
636
+ * .. code-block:: js
637
+ *
638
+ * // Incorrectly convert a TypedArray.
639
+ * // Produces a Uint16Array that points to the entire WASM memory!
640
+ * let myarray = new Uint16Array(buffer.data.buffer);
641
+ *
642
+ * Instead, if you want to convert the output TypedArray, you need to say:
643
+ *
644
+ * .. code-block:: js
645
+ *
646
+ * // Correctly convert a TypedArray.
647
+ * let myarray = new Uint16Array(
648
+ * buffer.data.buffer,
649
+ * buffer.data.byteOffset,
650
+ * buffer.data.byteLength
651
+ * );
652
+ */
653
+ declare class PyBufferView {
654
+ /**
655
+ * The offset of the first entry of the array. For instance if our array
656
+ * is 3d, then you will find ``array[0,0,0]`` at
657
+ * ``pybuf.data[pybuf.offset]``
658
+ */
659
+ offset: number;
660
+ /**
661
+ * If the data is read only, you should not modify it. There is no way for us
662
+ * to enforce this, but it may cause very weird behavior. See
663
+ * :py:attr:`memoryview.readonly`.
664
+ */
665
+ readonly: boolean;
666
+ /**
667
+ * The format string for the buffer. See :ref:`struct-format-strings`
668
+ * and :py:attr:`memoryview.format`.
669
+ */
670
+ format: string;
671
+ /**
672
+ * How large is each entry in bytes? See :py:attr:`memoryview.itemsize`.
673
+ */
674
+ itemsize: number;
675
+ /**
676
+ * The number of dimensions of the buffer. If ``ndim`` is 0, the buffer
677
+ * represents a single scalar or struct. Otherwise, it represents an
678
+ * array. See :py:attr:`memoryview.ndim`.
679
+ */
680
+ ndim: number;
681
+ /**
682
+ * The total number of bytes the buffer takes up. This is equal to
683
+ * :js:attr:`buff.data.byteLength <TypedArray.byteLength>`. See :py:attr:`memoryview.nbytes`.
684
+ */
685
+ nbytes: number;
686
+ /**
687
+ * The shape of the buffer, that is how long it is in each dimension.
688
+ * The length will be equal to ``ndim``. For instance, a 2x3x4 array
689
+ * would have shape ``[2, 3, 4]``. See :py:attr:`memoryview.shape`.
690
+ */
691
+ shape: number[];
692
+ /**
693
+ * An array of of length ``ndim`` giving the number of elements to skip
694
+ * to get to a new element in each dimension. See the example definition
695
+ * of a ``multiIndexToIndex`` function above. See :py:attr:`memoryview.strides`.
696
+ */
697
+ strides: number[];
698
+ /**
699
+ * The actual data. A typed array of an appropriate size backed by a segment
700
+ * of the WASM memory.
701
+ *
702
+ * The ``type`` argument of :js:meth:`~pyodide.ffi.PyBuffer.getBuffer` determines
703
+ * which sort of :js:class:`TypedArray` or :js:class:`DataView` to return. By
704
+ * default :js:meth:`~pyodide.ffi.PyBuffer.getBuffer` will look at the format string
705
+ * to determine the most appropriate option. Most often the result is a
706
+ * :js:class:`Uint8Array`.
707
+ *
708
+ * .. admonition:: Contiguity
709
+ * :class: warning
710
+ *
711
+ * If the buffer is not contiguous, the :js:attr:`~PyBufferView.readonly`
712
+ * TypedArray will contain data that is not part of the buffer. Modifying
713
+ * this data leads to undefined behavior.
714
+ *
715
+ * .. admonition:: Read only buffers
716
+ * :class: warning
717
+ *
718
+ * If :js:attr:`buffer.readonly <PyBufferView.readonly>` is ``true``, you
719
+ * should not modify the buffer. Modifying a read only buffer leads to
720
+ * undefined behavior.
721
+ *
722
+ */
723
+ data: TypedArray;
724
+ /**
725
+ * Is it C contiguous? See :py:attr:`memoryview.c_contiguous`.
726
+ */
727
+ c_contiguous: boolean;
728
+ /**
729
+ * Is it Fortran contiguous? See :py:attr:`memoryview.f_contiguous`.
730
+ */
731
+ f_contiguous: boolean;
732
+ /** @private */
733
+ _released: boolean;
734
+ /** @private */
735
+ _view_ptr: number;
736
+ /** @private */
737
+ constructor();
738
+ /**
739
+ * Release the buffer. This allows the memory to be reclaimed.
740
+ */
741
+ release(): void;
742
+ }
743
+ /**
744
+ * A JavaScript error caused by a Python exception.
745
+ *
746
+ * In order to reduce the risk of large memory leaks, the :py:exc:`PythonError`
747
+ * contains no reference to the Python exception that caused it. You can find
748
+ * the actual Python exception that caused this error as
749
+ * :py:data:`sys.last_value`.
750
+ *
751
+ * See :ref:`type translations of errors <type-translations-errors>` for more
752
+ * information.
753
+ *
754
+ * .. admonition:: Avoid leaking stack Frames
755
+ * :class: warning
756
+ *
757
+ * If you make a :js:class:`~pyodide.ffi.PyProxy` of
758
+ * :py:data:`sys.last_value`, you should be especially careful to
759
+ * :js:meth:`~pyodide.ffi.PyProxy.destroy` it when you are done. You may leak a large
760
+ * amount of memory including the local variables of all the stack frames in
761
+ * the traceback if you don't. The easiest way is to only handle the
762
+ * exception in Python.
763
+ *
764
+ * @hideconstructor
765
+ */
766
+ declare class PythonError extends Error {
767
+ /**
768
+ * The address of the error we are wrapping. We may later compare this
769
+ * against sys.last_value.
770
+ * WARNING: we don't own a reference to this pointer, dereferencing it
771
+ * may be a use-after-free error!
772
+ * @private
773
+ */
774
+ __error_address: number;
775
+ /**
776
+ * The name of the Python error class, e.g, :py:exc:`RuntimeError` or
777
+ * :py:exc:`KeyError`.
778
+ */
779
+ type: string;
780
+ constructor(type: string, message: string, error_address: number);
781
+ }
782
+ /**
783
+ * See :ref:`js-api-pyodide-ffi`
784
+ * @hidetype
785
+ */
786
+ declare const ffi: {
787
+ PyProxy: typeof PyProxy;
788
+ PyProxyWithLength: typeof PyProxyWithLength;
789
+ PyProxyWithGet: typeof PyProxyWithGet;
790
+ PyProxyWithSet: typeof PyProxyWithSet;
791
+ PyProxyWithHas: typeof PyProxyWithHas;
792
+ PyDict: typeof PyDict;
793
+ PyIterable: typeof PyIterable;
794
+ PyAsyncIterable: typeof PyAsyncIterable;
795
+ PyIterator: typeof PyIterator;
796
+ PyAsyncIterator: typeof PyAsyncIterator;
797
+ PyGenerator: typeof PyGenerator;
798
+ PyAsyncGenerator: typeof PyAsyncGenerator;
799
+ PyAwaitable: typeof PyAwaitable;
800
+ PyCallable: typeof PyCallable;
801
+ PyBuffer: typeof PyBuffer;
802
+ PyBufferView: typeof PyBufferView;
803
+ PythonError: typeof PythonError;
804
+ };
805
+
806
+ export type {};
807
+ export type {PyAsyncGenerator, PyAsyncIterable, PyAsyncIterator, PyAwaitable, PyBuffer, PyBufferView, PyCallable, PyDict, PyGenerator, PyIterable, PyIterator, PyProxy, PyProxyWithGet, PyProxyWithHas, PyProxyWithLength, PyProxyWithSet, PythonError};