pyodide 0.23.0-alpha.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/pyodide.d.ts CHANGED
@@ -4,11 +4,17 @@
4
4
  *
5
5
  * The Pyodide version.
6
6
  *
7
- * The version here follows PEP440 which is different from the one in package.json,
8
- * as we want to compare this with the version of Pyodide Python package without conversion.
7
+ * The version here is a Python version, following :pep:`440`. This is different
8
+ * from the version in ``package.json`` which follows the node package manager
9
+ * version convention.
9
10
  */
10
11
  export declare const version: string;
11
- declare function isPyProxy(jsobj: any): jsobj is PyProxy;
12
+ interface CanvasInterface {
13
+ setCanvas2D(canvas: HTMLCanvasElement): void;
14
+ getCanvas2D(): HTMLCanvasElement | undefined;
15
+ setCanvas3D(canvas: HTMLCanvasElement): void;
16
+ getCanvas3D(): HTMLCanvasElement | undefined;
17
+ }
12
18
  declare type PyProxyCache = {
13
19
  cacheId: number;
14
20
  refcnt: number;
@@ -18,7 +24,7 @@ declare type PyProxyProps = {
18
24
  /**
19
25
  * captureThis tracks whether this should be passed as the first argument to
20
26
  * the Python function or not. We keep it false by default. To make a PyProxy
21
- * where the `this` argument is included, call the `captureThis` method.
27
+ * where the ``this`` argument is included, call the :js:meth:`captureThis` method.
22
28
  */
23
29
  captureThis: boolean;
24
30
  /**
@@ -26,7 +32,7 @@ declare type PyProxyProps = {
26
32
  */
27
33
  isBound: boolean;
28
34
  /**
29
- * the `this` value that has been bound to the PyProxy
35
+ * the ``this`` value that has been bound to the PyProxy
30
36
  */
31
37
  boundThis?: any;
32
38
  /**
@@ -36,25 +42,31 @@ declare type PyProxyProps = {
36
42
  boundArgs: any[];
37
43
  roundtrip: boolean;
38
44
  };
39
- export declare type PyProxy = PyProxyClass & {
45
+ /** @deprecated Use `import type { PyProxy } from "pyodide/ffi"` instead */
46
+ interface PyProxy {
40
47
  [x: string]: any;
41
- };
42
- declare class PyProxyClass {
48
+ }
49
+ /**
50
+ * A :js:class:`~pyodide.ffi.PyProxy` is an object that allows idiomatic use of a Python object from
51
+ * JavaScript. See :ref:`type-translations-pyproxy`.
52
+ */
53
+ declare class PyProxy {
54
+ /** @private */
43
55
  $$: {
44
56
  ptr: number;
45
57
  cache: PyProxyCache;
46
58
  destroyed_msg?: string;
47
59
  };
60
+ /** @private */
48
61
  $$props: PyProxyProps;
62
+ /** @private */
49
63
  $$flags: number;
50
64
  /**
51
65
  * @private
52
66
  * @hideconstructor
53
67
  */
54
68
  constructor();
55
- /**
56
- * @private
57
- */
69
+ /** @private */
58
70
  get [Symbol.toStringTag](): string;
59
71
  /**
60
72
  * The name of the type of the object.
@@ -74,11 +86,11 @@ declare class PyProxyClass {
74
86
  get type(): string;
75
87
  toString(): string;
76
88
  /**
77
- * Destroy the ``PyProxy``. This will release the memory. Any further attempt
89
+ * Destroy the :js:class:`~pyodide.ffi.PyProxy`. This will release the memory. Any further attempt
78
90
  * to use the object will raise an error.
79
91
  *
80
92
  * In a browser supporting :js:data:`FinalizationRegistry`, Pyodide will
81
- * automatically destroy the ``PyProxy`` when it is garbage collected, however
93
+ * automatically destroy the :js:class:`~pyodide.ffi.PyProxy` when it is garbage collected, however
82
94
  * there is no guarantee that the finalizer will be run in a timely manner so
83
95
  * it is better to destroy the proxy explicitly.
84
96
  *
@@ -92,12 +104,12 @@ declare class PyProxyClass {
92
104
  destroyRoundtrip?: boolean;
93
105
  }): void;
94
106
  /**
95
- * Make a new PyProxy pointing to the same Python object.
96
- * Useful if the PyProxy is destroyed somewhere else.
107
+ * Make a new :js:class:`~pyodide.ffi.PyProxy` pointing to the same Python object.
108
+ * Useful if the :js:class:`~pyodide.ffi.PyProxy` is destroyed somewhere else.
97
109
  */
98
110
  copy(): PyProxy;
99
111
  /**
100
- * Converts the ``PyProxy`` into a JavaScript object as best as possible. By
112
+ * Converts the :js:class:`~pyodide.ffi.PyProxy` into a JavaScript object as best as possible. By
101
113
  * default does a deep conversion, if a shallow conversion is desired, you can
102
114
  * use ``proxy.toJs({depth : 1})``. See :ref:`Explicit Conversion of PyProxy
103
115
  * <type-translations-pyproxy-to-js>` for more info.
@@ -108,16 +120,18 @@ declare class PyProxyClass {
108
120
  /** How many layers deep to perform the conversion. Defaults to infinite */
109
121
  depth?: number;
110
122
  /**
111
- * If provided, ``toJs`` will store all PyProxies created in this list. This
112
- * allows you to easily destroy all the PyProxies by iterating the list
113
- * without having to recurse over the generated structure. The most common
114
- * use case is to create a new empty list, pass the list as `pyproxies`, and
115
- * then later iterate over `pyproxies` to destroy all of created proxies.
123
+ * If provided, :js:meth:`toJs` will store all PyProxies created in this
124
+ * list. This allows you to easily destroy all the PyProxies by iterating
125
+ * the list without having to recurse over the generated structure. The most
126
+ * common use case is to create a new empty list, pass the list as
127
+ * ``pyproxies``, and then later iterate over ``pyproxies`` to destroy all of
128
+ * created proxies.
116
129
  */
117
130
  pyproxies?: PyProxy[];
118
131
  /**
119
- * If false, ``toJs`` will throw a :py:exc:`~pyodide.ffi.ConversionError` rather than
120
- * producing a ``PyProxy``.
132
+ * If false, :js:meth:`toJs` will throw a
133
+ * :py:exc:`~pyodide.ffi.ConversionError` rather than producing a
134
+ * :js:class:`~pyodide.ffi.PyProxy`.
121
135
  */
122
136
  create_pyproxies?: boolean;
123
137
  /**
@@ -139,81 +153,105 @@ declare class PyProxyClass {
139
153
  default_converter?: (obj: PyProxy, convert: (obj: PyProxy) => any, cacheConversion: (obj: PyProxy, result: any) => void) => any;
140
154
  }): any;
141
155
  /**
142
- * Check whether the :any:`PyProxy.length` getter is available on this PyProxy. A
143
- * Typescript type guard.
156
+ * Check whether the :js:class:`~pyodide.ffi.PyProxy` is a :js:class:`~pyodide.ffi.PyProxyWithLength`.
157
+ * @deprecated Use ``obj instanceof pyodide.ffi.PyProxyWithLength`` instead.
144
158
  */
145
159
  supportsLength(): this is PyProxyWithLength;
146
160
  /**
147
- * Check whether the :any:`PyProxy.get` method is available on this PyProxy. A
148
- * Typescript type guard.
161
+ * Check whether the :js:class:`~pyodide.ffi.PyProxy` is a :js:class:`~pyodide.ffi.PyProxyWithGet`.
162
+ * @deprecated Use ``obj instanceof pyodide.ffi.PyProxyWithGet`` instead.
149
163
  */
150
164
  supportsGet(): this is PyProxyWithGet;
151
165
  /**
152
- * Check whether the :any:`PyProxy.set` method is available on this PyProxy. A
153
- * Typescript type guard.
166
+ * Check whether the :js:class:`~pyodide.ffi.PyProxy` is a :js:class:`~pyodide.ffi.PyProxyWithSet`.
167
+ * @deprecated Use ``obj instanceof pyodide.ffi.PyProxyWithSet`` instead.
154
168
  */
155
169
  supportsSet(): this is PyProxyWithSet;
156
170
  /**
157
- * Check whether the :any:`PyProxy.has` method is available on this PyProxy. A
158
- * Typescript type guard.
171
+ * Check whether the :js:class:`~pyodide.ffi.PyProxy` is a :js:class:`~pyodide.ffi.PyProxyWithHas`.
172
+ * @deprecated Use ``obj instanceof pyodide.ffi.PyProxyWithHas`` instead.
159
173
  */
160
174
  supportsHas(): this is PyProxyWithHas;
161
175
  /**
162
- * Check whether the PyProxy is :term:`iterable`. A Typescript type guard for
163
- * :any:`PyProxy.[iterator]`.
176
+ * Check whether the :js:class:`~pyodide.ffi.PyProxy` is a
177
+ * :js:class:`~pyodide.ffi.PyIterable`.
178
+ * @deprecated Use ``obj instanceof pyodide.ffi.PyIterable`` instead.
164
179
  */
165
- isIterable(): this is PyProxyIterable;
180
+ isIterable(): this is PyIterable;
166
181
  /**
167
- * Check whether the PyProxy is :term:`iterable`. A Typescript type guard for
168
- * :any:`PyProxy.next`.
182
+ * Check whether the :js:class:`~pyodide.ffi.PyProxy` is a
183
+ * :js:class:`~pyodide.ffi.PyIterator`
184
+ * @deprecated Use ``obj instanceof pyodide.ffi.PyIterator`` instead.
169
185
  */
170
- isIterator(): this is PyProxyIterator;
186
+ isIterator(): this is PyIterator;
171
187
  /**
172
- * Check whether the PyProxy is :ref:`awaitable <asyncio-awaitables>`. A Typescript type guard, if this
173
- * function returns true Typescript considers the PyProxy to be a ``Promise``.
188
+ * Check whether the :js:class:`~pyodide.ffi.PyProxy` is a :js:class:`~pyodide.ffi.PyAwaitable`
189
+ * @deprecated Use :js:class:`obj instanceof pyodide.ffi.PyAwaitable <pyodide.ffi.PyAwaitable>` instead.
174
190
  */
175
- isAwaitable(): this is PyProxyAwaitable;
191
+ isAwaitable(): this is PyAwaitable;
176
192
  /**
177
- * Check whether the PyProxy implements the :external:doc:`c-api/buffer`. A Typescript type guard for
178
- * :any:`PyProxy.getBuffer`.
193
+ * Check whether the :js:class:`~pyodide.ffi.PyProxy` is a :js:class:`~pyodide.ffi.PyBuffer`.
194
+ * @deprecated Use ``obj instanceof pyodide.ffi.PyBuffer`` instead.
179
195
  */
180
- isBuffer(): this is PyProxyBuffer;
196
+ isBuffer(): this is PyBuffer;
181
197
  /**
182
- * Check whether the PyProxy is :std:term:`callable`. A Typescript type guard,
183
- * if this returns true then Typescript considers the Proxy to be callable of
184
- * signature ``(args... : any[]) => PyProxy | number | bigint | string |
185
- * boolean | undefined``.
198
+ * Check whether the :js:class:`~pyodide.ffi.PyProxy` is a :js:class:`~pyodide.ffi.PyCallable`.
199
+ * @deprecated ``obj instanceof pyodide.ffi.PyCallable`` instead.
186
200
  */
187
- isCallable(): this is PyProxyCallable;
201
+ isCallable(): this is PyCallable;
202
+ }
203
+ /**
204
+ * A :js:class:`~pyodide.ffi.PyProxy` whose proxied Python object has a :meth:`~object.__len__`
205
+ * method.
206
+ */
207
+ declare class PyProxyWithLength extends PyProxy {
208
+ /** @private */
209
+ static [Symbol.hasInstance](obj: any): obj is PyProxy;
188
210
  }
189
- export declare type PyProxyWithLength = PyProxy & PyProxyLengthMethods;
190
- declare class PyProxyLengthMethods {
211
+ /** @deprecated Use `import type { PyProxyWithLength } from "pyodide/ffi"` instead */
212
+ interface PyProxyWithLength extends PyLengthMethods {
213
+ }
214
+ declare class PyLengthMethods {
191
215
  /**
192
216
  * The length of the object.
193
- *
194
- * Present only if the proxied Python object has a :meth:`~object.__len__` method.
195
217
  */
196
218
  get length(): number;
197
219
  }
198
- export declare type PyProxyWithGet = PyProxy & PyProxyGetItemMethods;
199
- declare class PyProxyGetItemMethods {
220
+ /**
221
+ * A :js:class:`~pyodide.ffi.PyProxy` whose proxied Python object has a
222
+ * :meth:`~object.__getitem__` method.
223
+ */
224
+ declare class PyProxyWithGet extends PyProxy {
225
+ /** @private */
226
+ static [Symbol.hasInstance](obj: any): obj is PyProxy;
227
+ }
228
+ /** @deprecated Use `import type { PyProxyWithGet } from "pyodide/ffi"` instead */
229
+ interface PyProxyWithGet extends PyGetItemMethods {
230
+ }
231
+ declare class PyGetItemMethods {
200
232
  /**
201
233
  * This translates to the Python code ``obj[key]``.
202
234
  *
203
- * Present only if the proxied Python object has a :meth:`~object.__getitem__` method.
204
- *
205
235
  * @param key The key to look up.
206
236
  * @returns The corresponding value.
207
237
  */
208
238
  get(key: any): any;
209
239
  }
210
- export declare type PyProxyWithSet = PyProxy & PyProxySetItemMethods;
211
- declare class PyProxySetItemMethods {
240
+ /**
241
+ * A :js:class:`~pyodide.ffi.PyProxy` whose proxied Python object has a
242
+ * :meth:`~object.__setitem__` or :meth:`~object.__delitem__` method.
243
+ */
244
+ declare class PyProxyWithSet extends PyProxy {
245
+ /** @private */
246
+ static [Symbol.hasInstance](obj: any): obj is PyProxy;
247
+ }
248
+ /** @deprecated Use `import type { PyProxyWithSet } from "pyodide/ffi"` instead */
249
+ interface PyProxyWithSet extends PySetItemMethods {
250
+ }
251
+ declare class PySetItemMethods {
212
252
  /**
213
253
  * This translates to the Python code ``obj[key] = value``.
214
254
  *
215
- * Present only if the proxied Python object has a :meth:`~object.__setitem__` method.
216
- *
217
255
  * @param key The key to set.
218
256
  * @param value The value to set it to.
219
257
  */
@@ -221,40 +259,75 @@ declare class PyProxySetItemMethods {
221
259
  /**
222
260
  * This translates to the Python code ``del obj[key]``.
223
261
  *
224
- * Present only if the proxied Python object has a :meth:`~object.__delitem__` method.
225
- *
226
262
  * @param key The key to delete.
227
263
  */
228
264
  delete(key: any): void;
229
265
  }
230
- export declare type PyProxyWithHas = PyProxy & PyProxyContainsMethods;
231
- declare class PyProxyContainsMethods {
266
+ /**
267
+ * A :js:class:`~pyodide.ffi.PyProxy` whose proxied Python object has a
268
+ * :meth:`~object.__contains__` method.
269
+ */
270
+ declare class PyProxyWithHas extends PyProxy {
271
+ /** @private */
272
+ static [Symbol.hasInstance](obj: any): obj is PyProxy;
273
+ }
274
+ /** @deprecated Use `import type { PyProxyWithHas } from "pyodide/ffi"` instead */
275
+ interface PyProxyWithHas extends PyContainsMethods {
276
+ }
277
+ declare class PyContainsMethods {
232
278
  /**
233
279
  * This translates to the Python code ``key in obj``.
234
280
  *
235
- * Present only if the proxied Python object has a :meth:`~object.__contains__` method.
236
- *
237
281
  * @param key The key to check for.
238
282
  * @returns Is ``key`` present?
239
283
  */
240
284
  has(key: any): boolean;
241
285
  }
242
- export declare type PyProxyIterable = PyProxy & PyProxyIterableMethods;
243
- declare class PyProxyIterableMethods {
286
+ declare class PyIterable extends PyProxy {
287
+ /** @private */
288
+ static [Symbol.hasInstance](obj: any): obj is PyProxy;
289
+ }
290
+ /** @deprecated Use `import type { PyIterable } from "pyodide/ffi"` instead */
291
+ interface PyIterable extends PyIterableMethods {
292
+ }
293
+ /** @deprecated Use :js:class:`pyodide.ffi.PyIterable` instead. */
294
+ export declare type PyProxyIterable = PyIterable;
295
+ declare class PyIterableMethods {
244
296
  /**
245
297
  * This translates to the Python code ``iter(obj)``. Return an iterator
246
298
  * associated to the proxy. See the documentation for
247
- * :js:data:`Symbol.iterator`
248
- *
249
- * Present only if the proxied Python object is :std:term:`iterable` (i.e.,
250
- * has an :meth:`~object.__iter__` method).
299
+ * :js:data:`Symbol.iterator`.
251
300
  *
252
301
  * This will be used implicitly by ``for(let x of proxy){}``.
253
302
  */
254
303
  [Symbol.iterator](): Iterator<any, any, any>;
255
304
  }
256
- export declare type PyProxyIterator = PyProxy & PyProxyIteratorMethods;
257
- declare class PyProxyIteratorMethods {
305
+ declare class PyAsyncIterable extends PyProxy {
306
+ /** @private */
307
+ static [Symbol.hasInstance](obj: any): obj is PyProxy;
308
+ }
309
+ /** @deprecated Use `import type { PyAsyncIterable } from "pyodide/ffi"` instead */
310
+ interface PyAsyncIterable extends PyAsyncIterableMethods {
311
+ }
312
+ declare class PyAsyncIterableMethods {
313
+ /**
314
+ * This translates to the Python code ``aiter(obj)``. Return an async iterator
315
+ * associated to the proxy. See the documentation for :js:data:`Symbol.asyncIterator`.
316
+ *
317
+ * This will be used implicitly by ``for(await let x of proxy){}``.
318
+ */
319
+ [Symbol.asyncIterator](): AsyncIterator<any, any, any>;
320
+ }
321
+ declare class PyIterator extends PyProxy {
322
+ /** @private */
323
+ static [Symbol.hasInstance](obj: any): obj is PyProxy;
324
+ }
325
+ /** @deprecated Use `import type { PyIterator } from "pyodide/ffi"` instead */
326
+ interface PyIterator extends PyIteratorMethods {
327
+ }
328
+ /** @deprecated Use :js:class:`pyodide.ffi.PyIterator` instead. */
329
+ export declare type PyProxyIterator = PyIterator;
330
+ declare class PyIteratorMethods {
258
331
  /** @private */
259
332
  [Symbol.iterator](): this;
260
333
  /**
@@ -264,32 +337,151 @@ declare class PyProxyIteratorMethods {
264
337
  *
265
338
  * This will be used implicitly by ``for(let x of proxy){}``.
266
339
  *
267
- * Present only if the proxied Python object is an :term:`iterator` (i.e.,
268
- * has a :meth:`~generator.send` or :meth:`~iterator.__next__` method).
269
- *
270
340
  * @param any The value to send to the generator. The value will be assigned
271
341
  * as a result of a yield expression.
272
342
  * @returns An Object with two properties: ``done`` and ``value``. When the
273
343
  * generator yields ``some_value``, ``next`` returns ``{done : false, value :
274
- * some_value}``. When the generator raises a :any:`StopIteration`
344
+ * some_value}``. When the generator raises a :py:exc:`StopIteration`
275
345
  * exception, ``next`` returns ``{done : true, value : result_value}``.
276
346
  */
277
347
  next(arg?: any): IteratorResult<any, any>;
278
348
  }
279
- export declare type PyProxyAwaitable = PyProxy & Promise<any>;
280
- export declare type PyProxyCallable = PyProxy & PyProxyCallableMethods & ((...args: any[]) => any);
281
- declare class PyProxyCallableMethods {
349
+ declare class PyGenerator extends PyProxy {
350
+ /** @private */
351
+ static [Symbol.hasInstance](obj: any): obj is PyProxy;
352
+ }
353
+ /** @deprecated Use `import type { PyGenerator } from "pyodide/ffi"` instead */
354
+ interface PyGenerator extends PyGeneratorMethods {
355
+ }
356
+ declare class PyGeneratorMethods {
357
+ /**
358
+ * Throws an exception into the Generator.
359
+ *
360
+ * See the documentation for :js:meth:`Generator.throw`.
361
+ *
362
+ * @param exception Error The error to throw into the generator. Must be an
363
+ * instanceof ``Error``.
364
+ * @returns An Object with two properties: ``done`` and ``value``. When the
365
+ * generator yields ``some_value``, ``return`` returns ``{done : false, value
366
+ * : some_value}``. When the generator raises a
367
+ * ``StopIteration(result_value)`` exception, ``return`` returns ``{done :
368
+ * true, value : result_value}``.
369
+ */
370
+ throw(exc: any): IteratorResult<any, any>;
371
+ /**
372
+ * Throws a :py:exc:`GeneratorExit` into the generator and if the
373
+ * :py:exc:`GeneratorExit` is not caught returns the argument value ``{done:
374
+ * true, value: v}``. If the generator catches the :py:exc:`GeneratorExit` and
375
+ * returns or yields another value the next value of the generator this is
376
+ * returned in the normal way. If it throws some error other than
377
+ * :py:exc:`GeneratorExit` or :py:exc:`StopIteration`, that error is propagated. See
378
+ * the documentation for :js:meth:`Generator.return`.
379
+ *
380
+ * @param any The value to return from the generator.
381
+ * @returns An Object with two properties: ``done`` and ``value``. When the
382
+ * generator yields ``some_value``, ``return`` returns ``{done : false, value
383
+ * : some_value}``. When the generator raises a
384
+ * ``StopIteration(result_value)`` exception, ``return`` returns ``{done :
385
+ * true, value : result_value}``.
386
+ */
387
+ return(v: any): IteratorResult<any, any>;
388
+ }
389
+ declare class PyAsyncIterator extends PyProxy {
390
+ /** @private */
391
+ static [Symbol.hasInstance](obj: any): obj is PyProxy;
392
+ }
393
+ /** @deprecated Use `import type { PyAsyncIterator } from "pyodide/ffi"` instead */
394
+ interface PyAsyncIterator extends PyAsyncIteratorMethods {
395
+ }
396
+ declare class PyAsyncIteratorMethods {
397
+ /** @private */
398
+ [Symbol.asyncIterator](): this;
399
+ /**
400
+ * This translates to the Python code ``anext(obj)``. Returns the next value
401
+ * of the asynchronous iterator. The argument will be sent to the Python
402
+ * iterator (if it's a generator for instance).
403
+ *
404
+ * This will be used implicitly by ``for(let x of proxy){}``.
405
+ *
406
+ * @param any The value to send to a generator. The value will be assigned as
407
+ * a result of a yield expression.
408
+ * @returns An Object with two properties: ``done`` and ``value``. When the
409
+ * iterator yields ``some_value``, ``next`` returns ``{done : false, value :
410
+ * some_value}``. When the giterator is done, ``next`` returns
411
+ * ``{done : true }``.
412
+ */
413
+ next(arg?: any): Promise<IteratorResult<any, any>>;
414
+ }
415
+ declare class PyAsyncGenerator extends PyProxy {
416
+ /** @private */
417
+ static [Symbol.hasInstance](obj: any): obj is PyProxy;
418
+ }
419
+ /** @deprecated Use `import type { PyAsyncGenerator } from "pyodide/ffi"` instead */
420
+ interface PyAsyncGenerator extends PyAsyncGeneratorMethods {
421
+ }
422
+ declare class PyAsyncGeneratorMethods {
423
+ /**
424
+ * Throws an exception into the Generator.
425
+ *
426
+ * See the documentation for :js:meth:`AsyncGenerator.throw`.
427
+ *
428
+ * @param exception Error The error to throw into the generator. Must be an
429
+ * instanceof ``Error``.
430
+ * @returns An Object with two properties: ``done`` and ``value``. When the
431
+ * generator yields ``some_value``, ``return`` returns ``{done : false, value
432
+ * : some_value}``. When the generator raises a
433
+ * ``StopIteration(result_value)`` exception, ``return`` returns ``{done :
434
+ * true, value : result_value}``.
435
+ */
436
+ throw(exc: any): Promise<IteratorResult<any, any>>;
437
+ /**
438
+ * Throws a :py:exc:`GeneratorExit` into the generator and if the
439
+ * :py:exc:`GeneratorExit` is not caught returns the argument value ``{done:
440
+ * true, value: v}``. If the generator catches the :py:exc:`GeneratorExit` and
441
+ * returns or yields another value the next value of the generator this is
442
+ * returned in the normal way. If it throws some error other than
443
+ * :py:exc:`GeneratorExit` or :py:exc:`StopAsyncIteration`, that error is
444
+ * propagated. See the documentation for :js:meth:`AsyncGenerator.throw`
445
+ *
446
+ * @param any The value to return from the generator.
447
+ * @returns An Object with two properties: ``done`` and ``value``. When the
448
+ * generator yields ``some_value``, ``return`` returns ``{done : false, value
449
+ * : some_value}``. When the generator raises a :py:exc:`StopAsyncIteration`
450
+ * exception, ``return`` returns ``{done : true, value : result_value}``.
451
+ */
452
+ return(v: any): Promise<IteratorResult<any, any>>;
453
+ }
454
+ declare class PyAwaitable extends PyProxy {
455
+ /** @private */
456
+ static [Symbol.hasInstance](obj: any): obj is PyProxy;
457
+ }
458
+ /** @deprecated Use `import type { PyAwaitable } from "pyodide/ffi"` instead */
459
+ interface PyAwaitable extends Promise<any> {
460
+ }
461
+ /** @deprecated Use :js:class:`pyodide.ffi.PyAwaitable` instead. */
462
+ export declare type PyProxyAwaitable = PyAwaitable;
463
+ declare class PyCallable extends PyProxy {
464
+ /** @private */
465
+ static [Symbol.hasInstance](obj: any): obj is PyCallable;
466
+ }
467
+ /**
468
+ * @deprecated Use :js:class:`pyodide.ffi.PyCallable` instead.
469
+ */
470
+ export declare type PyProxyCallable = PyCallable;
471
+ /** @deprecated Use `import type { PyCallable } from "pyodide/ffi"` instead */
472
+ interface PyCallable extends PyCallableMethods {
473
+ (...args: any[]): any;
474
+ }
475
+ declare class PyCallableMethods {
282
476
  /**
283
477
  * The ``apply()`` method calls the specified function with a given this
284
478
  * value, and arguments provided as an array (or an array-like object). Like
285
479
  * :js:meth:`Function.apply`.
286
480
  *
287
- * Present only if the proxied Python object is :std:term:`callable` (i.e., has a
288
- * :meth:`~object.__call__` method).
289
- *
290
481
  * @param thisArg The ``this`` argument. Has no effect unless the
291
- * :any:`PyProxy` has :any:`captureThis` set. If :any:`captureThis` is set, it
292
- * will be passed as the first argument to the Python function.
482
+ * :js:class:`~pyodide.ffi.PyCallable` has :js:meth:`captureThis` set. If
483
+ * :js:meth:`captureThis` is set, it will be passed as the first argument to
484
+ * the Python function.
293
485
  * @param jsargs The array of arguments
294
486
  * @returns The result from the function call.
295
487
  */
@@ -298,20 +490,17 @@ declare class PyProxyCallableMethods {
298
490
  * Calls the function with a given this value and arguments provided
299
491
  * individually. See :js:meth:`Function.call`.
300
492
  *
301
- * Present only if the proxied Python object is :term:`callable` (i.e., has a
302
- * :meth:`~object.__call__` method).
303
- *
304
493
  * @param thisArg The ``this`` argument. Has no effect unless the
305
- * :any:`PyProxy` has :any:`captureThis` set. If :any:`captureThis` is set, it
306
- * will be passed as the first argument to the Python function.
494
+ * :js:class:`~pyodide.ffi.PyCallable` has :js:meth:`captureThis` set. If
495
+ * :js:meth:`captureThis` is set, it will be passed as the first argument to
496
+ * the Python function.
307
497
  * @param jsargs The arguments
308
498
  * @returns The result from the function call.
309
499
  */
310
500
  call(thisArg: any, ...jsargs: any): any;
311
501
  /**
312
502
  * Call the function with key word arguments. The last argument must be an
313
- * object with the keyword arguments. Present only if the proxied Python
314
- * object is :term:`callable` (i.e., has a :meth:`~object.__call__` method).
503
+ * object with the keyword arguments.
315
504
  */
316
505
  callKwargs(...jsargs: any): any;
317
506
  /**
@@ -320,14 +509,11 @@ declare class PyProxyCallableMethods {
320
509
  * arguments preceding any provided when the new function is called. See
321
510
  * :js:meth:`Function.bind`.
322
511
  *
323
- * If the `PyProxy` does not have :any:`captureThis` set, the ``this``
324
- * parameter will be discarded. If it does have :any:`captureThis` set,
325
- * ``thisArg`` will be set to the first argument of the Python function. The
326
- * returned proxy and the original proxy have the same lifetime so destroying
327
- * either destroys both.
328
- *
329
- * Present only if the proxied Python object is :term:`callable` (i.e., has a
330
- * :meth:`~object.__call__` method)
512
+ * If the :js:class:`~pyodide.ffi.PyCallable` does not have
513
+ * :js:meth:`captureThis` set, the ``this`` parameter will be discarded. If it
514
+ * does have :js:meth:`captureThis` set, ``thisArg`` will be set to the first
515
+ * argument of the Python function. The returned proxy and the original proxy
516
+ * have the same lifetime so destroying either destroys both.
331
517
  *
332
518
  * @param thisArg The value to be passed as the ``this`` parameter to the
333
519
  * target function ``func`` when the bound function is called.
@@ -337,8 +523,8 @@ declare class PyProxyCallableMethods {
337
523
  */
338
524
  bind(thisArg: any, ...jsargs: any): PyProxy;
339
525
  /**
340
- * Returns a ``PyProxy`` that passes ``this`` as the first argument to the
341
- * Python function. The returned ``PyProxy`` has the internal ``captureThis``
526
+ * Returns a :js:class:`~pyodide.ffi.PyProxy` that passes ``this`` as the first argument to the
527
+ * Python function. The returned :js:class:`~pyodide.ffi.PyProxy` has the internal ``captureThis``
342
528
  * property set.
343
529
  *
344
530
  * It can then be used as a method on a JavaScript object. The returned proxy
@@ -347,112 +533,72 @@ declare class PyProxyCallableMethods {
347
533
  *
348
534
  * For example:
349
535
  *
350
- * .. code-block:: js
536
+ * .. code-block:: pyodide
351
537
  *
352
538
  * let obj = { a : 7 };
353
539
  * pyodide.runPython(`
354
540
  * def f(self):
355
541
  * return self.a
356
542
  * `);
357
- * // Without captureThis, it doesn't work to use ``f`` as a method for `obj`:
543
+ * // Without captureThis, it doesn't work to use f as a method for obj:
358
544
  * obj.f = pyodide.globals.get("f");
359
545
  * obj.f(); // raises "TypeError: f() missing 1 required positional argument: 'self'"
360
546
  * // With captureThis, it works fine:
361
547
  * obj.f = pyodide.globals.get("f").captureThis();
362
548
  * obj.f(); // returns 7
363
549
  *
364
- * @returns The resulting ``PyProxy``. It has the same lifetime as the
365
- * original ``PyProxy`` but passes ``this`` to the wrapped function.
550
+ * @returns The resulting :js:class:`~pyodide.ffi.PyProxy`. It has the same lifetime as the
551
+ * original :js:class:`~pyodide.ffi.PyProxy` but passes ``this`` to the wrapped function.
366
552
  *
367
553
  */
368
554
  captureThis(): PyProxy;
369
555
  }
370
- export declare type PyProxyBuffer = PyProxy & PyProxyBufferMethods;
371
- declare class PyProxyBufferMethods {
556
+ declare class PyBuffer extends PyProxy {
557
+ /** @private */
558
+ static [Symbol.hasInstance](obj: any): obj is PyBuffer;
559
+ }
560
+ /** @deprecated Use `import type { PyBuffer } from "pyodide/ffi"` instead */
561
+ interface PyBuffer extends PyBufferMethods {
562
+ }
563
+ declare class PyBufferMethods {
372
564
  /**
373
565
  * Get a view of the buffer data which is usable from JavaScript. No copy is
374
566
  * ever performed.
375
567
  *
376
- * Present only if the proxied Python object supports the Python
377
- * :external:doc:`c-api/buffer`.
378
- *
379
568
  * We do not support suboffsets, if the buffer requires suboffsets we will
380
569
  * throw an error. JavaScript nd array libraries can't handle suboffsets
381
- * anyways. In this case, you should use the :any:`toJs` api or copy the
382
- * buffer to one that doesn't use suboffets (using e.g.,
570
+ * anyways. In this case, you should use the :js:meth:`~PyProxy.toJs` api or
571
+ * copy the buffer to one that doesn't use suboffsets (using e.g.,
383
572
  * :py:func:`numpy.ascontiguousarray`).
384
573
  *
385
574
  * If the buffer stores big endian data or half floats, this function will
386
575
  * fail without an explicit type argument. For big endian data you can use
387
- * ``toJs``. :js:class:`DataView` has support for big endian data, so you
388
- * might want to pass ``'dataview'`` as the type argument in that case.
576
+ * :js:meth:`~PyProxy.toJs`. :js:class:`DataView` has support for big endian
577
+ * data, so you might want to pass ``'dataview'`` as the type argument in that
578
+ * case.
389
579
  *
390
- * @param type The type of the :js:attr:`~pyodide.PyBuffer.data` field in the
391
- * output. Should be one of: ``"i8"``, ``"u8"``, ``"u8clamped"``, ``"i16"``,
392
- * ``"u16"``, ``"i32"``, ``"u32"``, ``"i32"``, ``"u32"``, ``"i64"``,
393
- * ``"u64"``, ``"f32"``, ``"f64``, or ``"dataview"``. This argument is
394
- * optional, if absent ``getBuffer`` will try to determine the appropriate
395
- * output type based on the buffer format string (see
396
- * :std:ref:`struct-format-strings`).
397
- * @returns :js:class:`~pyodide.PyBuffer`
580
+ * @param type The type of the :js:attr:`~pyodide.ffi.PyBufferView.data` field
581
+ * in the output. Should be one of: ``"i8"``, ``"u8"``, ``"u8clamped"``,
582
+ * ``"i16"``, ``"u16"``, ``"i32"``, ``"u32"``, ``"i32"``, ``"u32"``,
583
+ * ``"i64"``, ``"u64"``, ``"f32"``, ``"f64``, or ``"dataview"``. This argument
584
+ * is optional, if absent :js:meth:`~pyodide.ffi.PyBuffer.getBuffer` will try
585
+ * to determine the appropriate output type based on the buffer format string
586
+ * (see :std:ref:`struct-format-strings`).
398
587
  */
399
- getBuffer(type?: string): PyBuffer;
588
+ getBuffer(type?: string): PyBufferView;
400
589
  }
401
590
  export declare type TypedArray = Int8Array | Uint8Array | Int16Array | Uint16Array | Int32Array | Uint32Array | Uint8ClampedArray | Float32Array | Float64Array;
402
- export declare type PyProxyDict = PyProxyWithGet & PyProxyWithSet & PyProxyWithHas;
403
- /**
404
- * A class to allow access to a Python data buffers from JavaScript. These are
405
- * produced by :js:func:`PyProxy.getBuffer` and cannot be constructed directly.
406
- * When you are done, release it with the :js:func:`~PyBuffer.release` method.
407
- * See the Python :external:doc:`c-api/buffer` documentation for more
408
- * information.
409
- *
410
- * To find the element ``x[a_1, ..., a_n]``, you could use the following code:
411
- *
412
- * .. code-block:: js
413
- *
414
- * function multiIndexToIndex(pybuff, multiIndex){
415
- * if(multindex.length !==pybuff.ndim){
416
- * throw new Error("Wrong length index");
417
- * }
418
- * let idx = pybuff.offset;
419
- * for(let i = 0; i < pybuff.ndim; i++){
420
- * if(multiIndex[i] < 0){
421
- * multiIndex[i] = pybuff.shape[i] - multiIndex[i];
422
- * }
423
- * if(multiIndex[i] < 0 || multiIndex[i] >= pybuff.shape[i]){
424
- * throw new Error("Index out of range");
425
- * }
426
- * idx += multiIndex[i] * pybuff.stride[i];
427
- * }
428
- * return idx;
429
- * }
430
- * console.log("entry is", pybuff.data[multiIndexToIndex(pybuff, [2, 0, -1])]);
431
- *
432
- * .. admonition:: Converting between TypedArray types
433
- * :class: warning
434
- *
435
- * The following naive code to change the type of a typed array does not
436
- * work:
437
- *
438
- * .. code-block:: js
439
- *
440
- * // Incorrectly convert a TypedArray.
441
- * // Produces a Uint16Array that points to the entire WASM memory!
442
- * let myarray = new Uint16Array(buffer.data.buffer);
443
- *
444
- * Instead, if you want to convert the output TypedArray, you need to say:
445
- *
446
- * .. code-block:: js
447
- *
448
- * // Correctly convert a TypedArray.
449
- * let myarray = new Uint16Array(
450
- * buffer.data.buffer,
451
- * buffer.data.byteOffset,
452
- * buffer.data.byteLength
453
- * );
454
- */
455
- export declare class PyBuffer {
591
+ declare class PyDict extends PyProxy {
592
+ /** @private */
593
+ static [Symbol.hasInstance](obj: any): obj is PyProxy;
594
+ }
595
+ /** @deprecated Use `import type { PyDict } from "pyodide/ffi"` instead */
596
+ interface PyDict extends PyProxyWithGet, PyProxyWithSet, PyProxyWithHas, PyProxyWithLength, PyIterable {
597
+ }
598
+ /** @deprecated Use :js:class:`pyodide.ffi.PyDict` instead. */
599
+ export declare type PyProxyDict = PyDict;
600
+ /** @deprecated Use `import type { PyBufferView } from "pyodide/ffi"` instead */
601
+ declare class PyBufferView {
456
602
  /**
457
603
  * The offset of the first entry of the array. For instance if our array
458
604
  * is 3d, then you will find ``array[0,0,0]`` at
@@ -460,7 +606,7 @@ export declare class PyBuffer {
460
606
  */
461
607
  offset: number;
462
608
  /**
463
- * If the data is readonly, you should not modify it. There is no way for us
609
+ * If the data is read only, you should not modify it. There is no way for us
464
610
  * to enforce this, but it may cause very weird behavior. See
465
611
  * :py:attr:`memoryview.readonly`.
466
612
  */
@@ -471,7 +617,7 @@ export declare class PyBuffer {
471
617
  */
472
618
  format: string;
473
619
  /**
474
- * How large is each entry (in bytes)? See :any:`memoryview.itemsize`.
620
+ * How large is each entry in bytes? See :py:attr:`memoryview.itemsize`.
475
621
  */
476
622
  itemsize: number;
477
623
  /**
@@ -482,7 +628,7 @@ export declare class PyBuffer {
482
628
  ndim: number;
483
629
  /**
484
630
  * The total number of bytes the buffer takes up. This is equal to
485
- * ``buff.data.byteLength``. See :py:attr:`memoryview.nbytes`.
631
+ * :js:attr:`buff.data.byteLength <TypedArray.byteLength>`. See :py:attr:`memoryview.nbytes`.
486
632
  */
487
633
  nbytes: number;
488
634
  /**
@@ -501,45 +647,41 @@ export declare class PyBuffer {
501
647
  * The actual data. A typed array of an appropriate size backed by a segment
502
648
  * of the WASM memory.
503
649
  *
504
- * The ``type`` argument of :any:`PyProxy.getBuffer` determines which sort of
505
- * :js:class:`TypedArray` or :js:class:`DataView` to return. By default
506
- * :any:`PyProxy.getBuffer` will look at the format string to determine the
507
- * most appropriate option. Most often the result is a :js:class:`Uint8Array`.
650
+ * The ``type`` argument of :js:meth:`~pyodide.ffi.PyBuffer.getBuffer` determines
651
+ * which sort of :js:class:`TypedArray` or :js:class:`DataView` to return. By
652
+ * default :js:meth:`~pyodide.ffi.PyBuffer.getBuffer` will look at the format string
653
+ * to determine the most appropriate option. Most often the result is a
654
+ * :js:class:`Uint8Array`.
508
655
  *
509
656
  * .. admonition:: Contiguity
510
657
  * :class: warning
511
658
  *
512
- * If the buffer is not contiguous, the ``data`` TypedArray will contain
513
- * data that is not part of the buffer. Modifying this data leads to
514
- * undefined behavior.
659
+ * If the buffer is not contiguous, the :js:attr:`~PyBufferView.readonly`
660
+ * TypedArray will contain data that is not part of the buffer. Modifying
661
+ * this data leads to undefined behavior.
515
662
  *
516
- * .. admonition:: Readonly buffers
663
+ * .. admonition:: Read only buffers
517
664
  * :class: warning
518
665
  *
519
- * If ``buffer.readonly`` is ``true``, you should not modify the buffer.
520
- * Modifying a readonly buffer leads to undefined behavior.
666
+ * If :js:attr:`buffer.readonly <PyBufferView.readonly>` is ``true``, you
667
+ * should not modify the buffer. Modifying a read only buffer leads to
668
+ * undefined behavior.
521
669
  *
522
670
  */
523
671
  data: TypedArray;
524
672
  /**
525
- * Is it C contiguous? See :any:`memoryview.c_contiguous`.
673
+ * Is it C contiguous? See :py:attr:`memoryview.c_contiguous`.
526
674
  */
527
675
  c_contiguous: boolean;
528
676
  /**
529
- * Is it Fortran contiguous? See :any:`memoryview.f_contiguous`.
677
+ * Is it Fortran contiguous? See :py:attr:`memoryview.f_contiguous`.
530
678
  */
531
679
  f_contiguous: boolean;
532
- /**
533
- * @private
534
- */
680
+ /** @private */
535
681
  _released: boolean;
536
- /**
537
- * @private
538
- */
682
+ /** @private */
539
683
  _view_ptr: number;
540
- /**
541
- * @private
542
- */
684
+ /** @private */
543
685
  constructor();
544
686
  /**
545
687
  * Release the buffer. This allows the memory to be reclaimed.
@@ -551,9 +693,6 @@ declare function loadPackage(names: string | PyProxy | Array<string>, options?:
551
693
  errorCallback?: (message: string) => void;
552
694
  checkIntegrity?: boolean;
553
695
  }, errorCallbackDeprecated?: (message: string) => void): Promise<void>;
554
- declare let loadedPackages: {
555
- [key: string]: string;
556
- };
557
696
  declare class PythonError extends Error {
558
697
  /**
559
698
  * The address of the error we are wrapping. We may later compare this
@@ -564,13 +703,13 @@ declare class PythonError extends Error {
564
703
  */
565
704
  __error_address: number;
566
705
  /**
567
- * The name of the Python error class, e.g, :any:`RuntimeError` or
568
- * :any:`KeyError`.
706
+ * The name of the Python error class, e.g, :py:exc:`RuntimeError` or
707
+ * :py:exc:`KeyError`.
569
708
  */
570
709
  type: string;
571
710
  constructor(type: string, message: string, error_address: number);
572
711
  }
573
- declare type InFuncType = () => null | undefined | string | ArrayBuffer | ArrayBufferView | number;
712
+ declare type InFuncType = () => null | undefined | string | ArrayBuffer | Uint8Array | number;
574
713
  declare function setStdin(options?: {
575
714
  stdin?: InFuncType;
576
715
  error?: boolean;
@@ -587,76 +726,361 @@ declare function setStderr(options?: {
587
726
  raw?: (a: number) => void;
588
727
  isatty?: boolean;
589
728
  }): void;
590
- declare let pyodide_py: PyProxy;
591
- declare let globals: PyProxy;
592
- declare function runPython(code: string, options?: {
593
- globals?: PyProxy;
594
- }): any;
595
- declare function loadPackagesFromImports(code: string, options?: {
596
- messageCallback?: (message: string) => void;
597
- errorCallback?: (message: string) => void;
598
- checkIntegrity?: boolean;
599
- }, errorCallbackDeprecated?: (message: string) => void): Promise<void>;
600
- declare function runPythonAsync(code: string, options?: {
601
- globals?: PyProxy;
602
- }): Promise<any>;
603
- declare function registerJsModule(name: string, module: object): void;
604
- declare function registerComlink(Comlink: any): void;
605
- declare function unregisterJsModule(name: string): void;
606
- declare function toPy(obj: any, { depth, defaultConverter, }?: {
607
- /**
608
- * Optional argument to limit the depth of the conversion.
609
- */
610
- depth: number;
611
- /**
612
- * Optional argument to convert objects with no default conversion. See the
613
- * documentation of :any:`JsProxy.to_py`.
614
- */
615
- defaultConverter?: (value: any, converter: (value: any) => any, cacheConversion: (input: any, output: any) => void) => any;
616
- }): any;
617
- declare function pyimport(mod_name: string): PyProxy;
618
- declare function unpackArchive(buffer: TypedArray | ArrayBuffer, format: string, options?: {
619
- extractDir?: string;
620
- }): void;
621
729
  declare type NativeFS = {
622
- syncfs: Function;
623
- };
624
- declare function mountNativeFS(path: string, fileSystemHandle: FileSystemDirectoryHandle): Promise<NativeFS>;
625
- declare function setInterruptBuffer(interrupt_buffer: TypedArray): void;
626
- declare function checkInterrupt(): void;
627
- export declare type PyodideInterface = {
628
- globals: typeof globals;
629
- FS: typeof FS;
630
- PATH: typeof PATH;
631
- ERRNO_CODES: typeof ERRNO_CODES;
632
- pyodide_py: typeof pyodide_py;
633
- version: typeof version;
634
- loadPackage: typeof loadPackage;
635
- loadPackagesFromImports: typeof loadPackagesFromImports;
636
- loadedPackages: typeof loadedPackages;
637
- isPyProxy: typeof isPyProxy;
638
- runPython: typeof runPython;
639
- runPythonAsync: typeof runPythonAsync;
640
- registerJsModule: typeof registerJsModule;
641
- unregisterJsModule: typeof unregisterJsModule;
642
- setInterruptBuffer: typeof setInterruptBuffer;
643
- checkInterrupt: typeof checkInterrupt;
644
- toPy: typeof toPy;
645
- pyimport: typeof pyimport;
646
- unpackArchive: typeof unpackArchive;
647
- mountNativeFS: typeof mountNativeFS;
648
- registerComlink: typeof registerComlink;
649
- PythonError: typeof PythonError;
650
- PyBuffer: typeof PyBuffer;
651
- setStdin: typeof setStdin;
652
- setStdout: typeof setStdout;
653
- setStderr: typeof setStderr;
654
- };
655
- declare let FS: any;
656
- declare let PATH: any;
657
- declare let ERRNO_CODES: {
658
- [code: string]: number;
730
+ syncfs: () => Promise<void>;
659
731
  };
732
+ declare class PyodideAPI {
733
+ /** @hidden */
734
+ static version: string;
735
+ /** @hidden */
736
+ static loadPackage: typeof loadPackage;
737
+ /** @hidden */
738
+ static loadedPackages: {
739
+ [key: string]: string;
740
+ };
741
+ /** @hidden */
742
+ static ffi: {
743
+ PyProxy: typeof PyProxy;
744
+ PyProxyWithLength: typeof PyProxyWithLength;
745
+ PyProxyWithGet: typeof PyProxyWithGet;
746
+ PyProxyWithSet: typeof PyProxyWithSet;
747
+ PyProxyWithHas: typeof PyProxyWithHas;
748
+ PyDict: typeof PyDict;
749
+ PyIterable: typeof PyIterable;
750
+ PyAsyncIterable: typeof PyAsyncIterable;
751
+ PyIterator: typeof PyIterator;
752
+ PyAsyncIterator: typeof PyAsyncIterator;
753
+ PyGenerator: typeof PyGenerator;
754
+ PyAsyncGenerator: typeof PyAsyncGenerator;
755
+ PyAwaitable: typeof PyAwaitable;
756
+ PyCallable: typeof PyCallable;
757
+ PyBuffer: typeof PyBuffer;
758
+ PyBufferView: typeof PyBufferView;
759
+ PythonError: typeof PythonError;
760
+ };
761
+ /** @hidden */
762
+ static setStdin: typeof setStdin;
763
+ /** @hidden */
764
+ static setStdout: typeof setStdout;
765
+ /** @hidden */
766
+ static setStderr: typeof setStderr;
767
+ /**
768
+ *
769
+ * An alias to the global Python namespace.
770
+ *
771
+ * For example, to access a variable called ``foo`` in the Python global
772
+ * scope, use ``pyodide.globals.get("foo")``
773
+ */
774
+ static globals: PyProxy;
775
+ /**
776
+ * An alias to the `Emscripten File System API
777
+ * <https://emscripten.org/docs/api_reference/Filesystem-API.html>`_.
778
+ *
779
+ * This provides a wide range of POSIX-`like` file/device operations, including
780
+ * `mount
781
+ * <https://emscripten.org/docs/api_reference/Filesystem-API.html#FS.mount>`_
782
+ * which can be used to extend the in-memory filesystem with features like `persistence
783
+ * <https://emscripten.org/docs/api_reference/Filesystem-API.html#persistent-data>`_.
784
+ *
785
+ * While all the file systems implementations are enabled, only the default
786
+ * ``MEMFS`` is guaranteed to work in all runtime settings. The implementations
787
+ * are available as members of ``FS.filesystems``:
788
+ * ``IDBFS``, ``NODEFS``, ``PROXYFS``, ``WORKERFS``.
789
+ */
790
+ static FS: any;
791
+ /**
792
+ * An alias to the `Emscripten Path API
793
+ * <https://github.com/emscripten-core/emscripten/blob/main/src/library_path.js>`_.
794
+ *
795
+ * This provides a variety of operations for working with file system paths, such as
796
+ * ``dirname``, ``normalize``, and ``splitPath``.
797
+ */
798
+ static PATH: any;
799
+ /**
800
+ * This provides APIs to set a canvas for rendering graphics.
801
+ *
802
+ * For example, you need to set a canvas if you want to use the
803
+ * SDL library. See :ref:`using-sdl` for more information.
804
+ */
805
+ static canvas: CanvasInterface;
806
+ /**
807
+ * A map from posix error names to error codes.
808
+ */
809
+ static ERRNO_CODES: {
810
+ [code: string]: number;
811
+ };
812
+ /**
813
+ * An alias to the Python :ref:`pyodide <python-api>` package.
814
+ *
815
+ * You can use this to call functions defined in the Pyodide Python package
816
+ * from JavaScript.
817
+ */
818
+ static pyodide_py: PyProxy;
819
+ /**
820
+ * Inspect a Python code chunk and use :js:func:`pyodide.loadPackage` to install
821
+ * any known packages that the code chunk imports. Uses the Python API
822
+ * :func:`pyodide.code.find\_imports` to inspect the code.
823
+ *
824
+ * For example, given the following code as input
825
+ *
826
+ * .. code-block:: python
827
+ *
828
+ * import numpy as np
829
+ * x = np.array([1, 2, 3])
830
+ *
831
+ * :js:func:`loadPackagesFromImports` will call
832
+ * ``pyodide.loadPackage(['numpy'])``.
833
+ *
834
+ * @param code The code to inspect.
835
+ * @param options Options passed to :js:func:`pyodide.loadPackage`.
836
+ * @param options.messageCallback A callback, called with progress messages
837
+ * (optional)
838
+ * @param options.errorCallback A callback, called with error/warning messages
839
+ * (optional)
840
+ * @param options.checkIntegrity If true, check the integrity of the downloaded
841
+ * packages (default: true)
842
+ * @param errorCallbackDeprecated @ignore
843
+ * @async
844
+ */
845
+ static loadPackagesFromImports(code: string, options?: {
846
+ messageCallback?: (message: string) => void;
847
+ errorCallback?: (message: string) => void;
848
+ checkIntegrity?: boolean;
849
+ }, errorCallbackDeprecated?: (message: string) => void): Promise<void>;
850
+ /**
851
+ * Runs a string of Python code from JavaScript, using :py:func:`~pyodide.code.eval_code`
852
+ * to evaluate the code. If the last statement in the Python code is an
853
+ * expression (and the code doesn't end with a semicolon), the value of the
854
+ * expression is returned.
855
+ *
856
+ * @param code Python code to evaluate
857
+ * @param options
858
+ * @param options.globals An optional Python dictionary to use as the globals.
859
+ * Defaults to :js:attr:`pyodide.globals`.
860
+ * @param options.locals An optional Python dictionary to use as the locals.
861
+ * Defaults to the same as ``globals``.
862
+ * @returns The result of the Python code translated to JavaScript. See the
863
+ * documentation for :py:func:`~pyodide.code.eval_code` for more info.
864
+ */
865
+ static runPython(code: string, options?: {
866
+ globals?: PyProxy;
867
+ locals?: PyProxy;
868
+ }): any;
869
+ /**
870
+ * Run a Python code string with top level await using
871
+ * :py:func:`~pyodide.code.eval_code_async` to evaluate the code. Returns a promise which
872
+ * resolves when execution completes. If the last statement in the Python code
873
+ * is an expression (and the code doesn't end with a semicolon), the returned
874
+ * promise will resolve to the value of this expression.
875
+ *
876
+ * For example:
877
+ *
878
+ * .. code-block:: pyodide
879
+ *
880
+ * let result = await pyodide.runPythonAsync(`
881
+ * from js import fetch
882
+ * response = await fetch("./repodata.json")
883
+ * packages = await response.json()
884
+ * # If final statement is an expression, its value is returned to JavaScript
885
+ * len(packages.packages.object_keys())
886
+ * `);
887
+ * console.log(result); // 79
888
+ *
889
+ * .. admonition:: Python imports
890
+ * :class: warning
891
+ *
892
+ * Since pyodide 0.18.0, you must call :js:func:`loadPackagesFromImports` to
893
+ * import any python packages referenced via ``import`` statements in your
894
+ * code. This function will no longer do it for you.
895
+ *
896
+ * @param code Python code to evaluate
897
+ * @param options
898
+ * @param options.globals An optional Python dictionary to use as the globals.
899
+ * Defaults to :js:attr:`pyodide.globals`.
900
+ * @param options.locals An optional Python dictionary to use as the locals.
901
+ * Defaults to the same as ``globals``.
902
+ * @returns The result of the Python code translated to JavaScript.
903
+ * @async
904
+ */
905
+ static runPythonAsync(code: string, options?: {
906
+ globals?: PyProxy;
907
+ locals?: PyProxy;
908
+ }): Promise<any>;
909
+ /**
910
+ * Registers the JavaScript object ``module`` as a JavaScript module named
911
+ * ``name``. This module can then be imported from Python using the standard
912
+ * Python import system. If another module by the same name has already been
913
+ * imported, this won't have much effect unless you also delete the imported
914
+ * module from :py:data:`sys.modules`. This calls
915
+ * :func:`~pyodide.ffi.register_js_module`.
916
+ *
917
+ * @param name Name of the JavaScript module to add
918
+ * @param module JavaScript object backing the module
919
+ */
920
+ static registerJsModule(name: string, module: object): void;
921
+ /**
922
+ * Unregisters a JavaScript module with given name that has been previously
923
+ * registered with :js:func:`pyodide.registerJsModule` or
924
+ * :func:`~pyodide.ffi.register_js_module`. If a JavaScript module with that
925
+ * name does not already exist, will throw an error. Note that if the module has
926
+ * already been imported, this won't have much effect unless you also delete the
927
+ * imported module from :py:data:`sys.modules`. This calls
928
+ * :func:`~pyodide.ffi.unregister_js_module`.
929
+ *
930
+ * @param name Name of the JavaScript module to remove
931
+ */
932
+ static unregisterJsModule(name: string): void;
933
+ /**
934
+ * Convert a JavaScript object to a Python object as best as possible.
935
+ *
936
+ * This is similar to :py:meth:`~pyodide.ffi.JsProxy.to_py` but for use from
937
+ * JavaScript. If the object is immutable or a :js:class:`~pyodide.ffi.PyProxy`,
938
+ * it will be returned unchanged. If the object cannot be converted into Python,
939
+ * it will be returned unchanged.
940
+ *
941
+ * See :ref:`type-translations-jsproxy-to-py` for more information.
942
+ *
943
+ * @param obj The object to convert.
944
+ * @param options
945
+ * @returns The object converted to Python.
946
+ */
947
+ static toPy(obj: any, { depth, defaultConverter, }?: {
948
+ /**
949
+ * Optional argument to limit the depth of the conversion.
950
+ */
951
+ depth: number;
952
+ /**
953
+ * Optional argument to convert objects with no default conversion. See the
954
+ * documentation of :py:meth:`~pyodide.ffi.JsProxy.to_py`.
955
+ */
956
+ defaultConverter?: (value: any, converter: (value: any) => any, cacheConversion: (input: any, output: any) => void) => any;
957
+ }): any;
958
+ /**
959
+ * Imports a module and returns it.
960
+ *
961
+ * .. admonition:: Warning
962
+ * :class: warning
963
+ *
964
+ * This function has a completely different behavior than the old removed pyimport function!
965
+ *
966
+ * ``pyimport`` is roughly equivalent to:
967
+ *
968
+ * .. code-block:: js
969
+ *
970
+ * pyodide.runPython(`import ${pkgname}; ${pkgname}`);
971
+ *
972
+ * except that the global namespace will not change.
973
+ *
974
+ * Example:
975
+ *
976
+ * .. code-block:: js
977
+ *
978
+ * let sysmodule = pyodide.pyimport("sys");
979
+ * let recursionLimit = sysmodule.getrecursionlimit();
980
+ *
981
+ * @param mod_name The name of the module to import
982
+ * @returns A PyProxy for the imported module
983
+ */
984
+ static pyimport(mod_name: string): PyProxy;
985
+ /**
986
+ * Unpack an archive into a target directory.
987
+ *
988
+ * @param buffer The archive as an :js:class:`ArrayBuffer` or :js:class:`TypedArray`.
989
+ * @param format The format of the archive. Should be one of the formats
990
+ * recognized by :py:func:`shutil.unpack_archive`. By default the options are
991
+ * ``'bztar'``, ``'gztar'``, ``'tar'``, ``'zip'``, and ``'wheel'``. Several
992
+ * synonyms are accepted for each format, e.g., for ``'gztar'`` any of
993
+ * ``'.gztar'``, ``'.tar.gz'``, ``'.tgz'``, ``'tar.gz'`` or ``'tgz'`` are
994
+ * considered to be
995
+ * synonyms.
996
+ *
997
+ * @param options
998
+ * @param options.extractDir The directory to unpack the archive into. Defaults
999
+ * to the working directory.
1000
+ */
1001
+ static unpackArchive(buffer: TypedArray | ArrayBuffer, format: string, options?: {
1002
+ extractDir?: string;
1003
+ }): void;
1004
+ /**
1005
+ * Mounts a :js:class:`FileSystemDirectoryHandle` into the target directory.
1006
+ *
1007
+ * @param path The absolute path in the Emscripten file system to mount the
1008
+ * native directory. If the directory does not exist, it will be created. If it
1009
+ * does exist, it must be empty.
1010
+ * @param fileSystemHandle A handle returned by ``navigator.storage.getDirectory()``
1011
+ * or ``window.showDirectoryPicker()``.
1012
+ */
1013
+ static mountNativeFS(path: string, fileSystemHandle: FileSystemDirectoryHandle): Promise<NativeFS>;
1014
+ /**
1015
+ * Tell Pyodide about Comlink.
1016
+ * Necessary to enable importing Comlink proxies into Python.
1017
+ */
1018
+ static registerComlink(Comlink: any): void;
1019
+ /**
1020
+ * Sets the interrupt buffer to be ``interrupt_buffer``. This is only useful
1021
+ * when Pyodide is used in a webworker. The buffer should be a
1022
+ * :js:class:`SharedArrayBuffer` shared with the main browser thread (or another
1023
+ * worker). In that case, signal ``signum`` may be sent by writing ``signum``
1024
+ * into the interrupt buffer. If ``signum`` does not satisfy 0 < ``signum`` < 65
1025
+ * it will be silently ignored.
1026
+ *
1027
+ * You can disable interrupts by calling ``setInterruptBuffer(undefined)``.
1028
+ *
1029
+ * If you wish to trigger a :py:exc:`KeyboardInterrupt`, write ``SIGINT`` (a 2)
1030
+ * into the interrupt buffer.
1031
+ *
1032
+ * By default ``SIGINT`` raises a :py:exc:`KeyboardInterrupt` and all other signals
1033
+ * are ignored. You can install custom signal handlers with the signal module.
1034
+ * Even signals that normally have special meaning and can't be overridden like
1035
+ * ``SIGKILL`` and ``SIGSEGV`` are ignored by default and can be used for any
1036
+ * purpose you like.
1037
+ */
1038
+ static setInterruptBuffer(interrupt_buffer: TypedArray): void;
1039
+ /**
1040
+ * Throws a :py:exc:`KeyboardInterrupt` error if a :py:exc:`KeyboardInterrupt` has
1041
+ * been requested via the interrupt buffer.
1042
+ *
1043
+ * This can be used to enable keyboard interrupts during execution of JavaScript
1044
+ * code, just as :c:func:`PyErr_CheckSignals` is used to enable keyboard interrupts
1045
+ * during execution of C code.
1046
+ */
1047
+ static checkInterrupt(): void;
1048
+ /**
1049
+ * Is ``jsobj`` a :js:class:`~pyodide.ffi.PyProxy`?
1050
+ * @deprecated Use :js:class:`obj instanceof pyodide.ffi.PyProxy <pyodide.ffi.PyProxy>` instead.
1051
+ * @param jsobj Object to test.
1052
+ */
1053
+ static isPyProxy(jsobj: any): jsobj is PyProxy;
1054
+ /**
1055
+ * An alias for :js:class:`pyodide.ffi.PyBufferView`.
1056
+ *
1057
+ * @hidetype
1058
+ * @alias
1059
+ * @doc_kind class
1060
+ * @deprecated
1061
+ */
1062
+ static get PyBuffer(): typeof PyBufferView;
1063
+ /**
1064
+ * An alias for :js:class:`pyodide.ffi.PyBuffer`.
1065
+ *
1066
+ * @hidetype
1067
+ * @alias
1068
+ * @doc_kind class
1069
+ * @deprecated
1070
+ */
1071
+ static get PyProxyBuffer(): typeof PyBuffer;
1072
+ /**
1073
+ * An alias for :js:class:`pyodide.ffi.PyBuffer`.
1074
+ *
1075
+ * @hidetype
1076
+ * @alias
1077
+ * @doc_kind class
1078
+ * @deprecated
1079
+ */
1080
+ static get PythonError(): typeof PythonError;
1081
+ }
1082
+ /** @hidetype */
1083
+ export declare type PyodideInterface = typeof PyodideAPI;
660
1084
  export declare type Py2JsResult = any;
661
1085
  /**
662
1086
  * See documentation for loadPyodide.
@@ -667,6 +1091,7 @@ export declare type ConfigType = {
667
1091
  lockFileURL: string;
668
1092
  homedir: string;
669
1093
  fullStdLib?: boolean;
1094
+ stdLibURL?: string;
670
1095
  stdin?: () => string;
671
1096
  stdout?: (msg: string) => void;
672
1097
  stderr?: (msg: string) => void;
@@ -693,7 +1118,7 @@ export declare function loadPyodide(options?: {
693
1118
  indexURL?: string;
694
1119
  /**
695
1120
  * The URL from which Pyodide will load the Pyodide ``repodata.json`` lock
696
- * file. You can produce custom lock files with :any:`micropip.freeze`.
1121
+ * file. You can produce custom lock files with :py:func:`micropip.freeze`.
697
1122
  * Default: ```${indexURL}/repodata.json```
698
1123
  */
699
1124
  lockFileURL?: string;
@@ -708,6 +1133,14 @@ export declare function loadPyodide(options?: {
708
1133
  * Default: ``false``
709
1134
  */
710
1135
  fullStdLib?: boolean;
1136
+ /**
1137
+ * The URL from which to load the standard library ``python_stdlib.zip``
1138
+ * file. This URL includes the most of the Python stadard library. Some
1139
+ * stdlib modules were unvendored, and can be loaded separately
1140
+ * with ``fullStdLib=true`` option or by their package name.
1141
+ * Default: ```${indexURL}/python_stdlib.zip```
1142
+ */
1143
+ stdLibURL?: string;
711
1144
  /**
712
1145
  * Override the standard input callback. Should ask the user for one line of
713
1146
  * input.
@@ -739,4 +1172,10 @@ export declare function loadPyodide(options?: {
739
1172
  _node_mounts?: string[];
740
1173
  }): Promise<PyodideInterface>;
741
1174
 
742
- export {};
1175
+ export type {
1176
+ PyBuffer as PyProxyBuffer,
1177
+ PyBufferView as PyBuffer,
1178
+ };
1179
+
1180
+ export type {};
1181
+ export type {PyProxy, PyProxyWithGet, PyProxyWithHas, PyProxyWithLength, PyProxyWithSet};