Nymbo/self-hosted-python
1
1/**2 * Runs a string of Python code from JavaScript.3 *4 * The last part of the string may be an expression, in which case, its value5 * is returned.6 *7 * @param {string} code Python code to evaluate8 * @param {PyProxy=} globals An optional Python dictionary to use as the globals.9 * Defaults to :any:`pyodide.globals`. Uses the Python API10 * :any:`pyodide.eval_code` to evaluate the code.11 * @returns {Py2JsResult} The result of the Python code translated to JavaScript. See the12 * documentation for :any:`pyodide.eval_code` for more info.13 */14export function runPython(code: string, globals?: PyProxy | undefined): Py2JsResult;15/**16 * @callback LogFn17 * @param {string} msg18 * @returns {void}19 * @private20 */21/**22 * Inspect a Python code chunk and use :js:func:`pyodide.loadPackage` to install23 * any known packages that the code chunk imports. Uses the Python API24 * :func:`pyodide.find\_imports` to inspect the code.25 *26 * For example, given the following code as input27 *28 * .. code-block:: python29 *30 * import numpy as np x = np.array([1, 2, 3])31 *32 * :js:func:`loadPackagesFromImports` will call33 * ``pyodide.loadPackage(['numpy'])``.34 *35 * @param {string} code The code to inspect.36 * @param {LogFn=} messageCallback The ``messageCallback`` argument of37 * :any:`pyodide.loadPackage` (optional).38 * @param {LogFn=} errorCallback The ``errorCallback`` argument of39 * :any:`pyodide.loadPackage` (optional).40 * @async41 */42export function loadPackagesFromImports(code: string, messageCallback?: LogFn | undefined, errorCallback?: LogFn | undefined): Promise<void>;43/**44 * Runs Python code using `PyCF_ALLOW_TOP_LEVEL_AWAIT45 * <https://docs.python.org/3/library/ast.html?highlight=pycf_allow_top_level_await#ast.PyCF_ALLOW_TOP_LEVEL_AWAIT>`_.46 *47 * .. admonition:: Python imports48 * :class: warning49 *50 * Since pyodide 0.18.0, you must call :js:func:`loadPackagesFromImports` to51 * import any python packages referenced via `import` statements in your code.52 * This function will no longer do it for you.53 *54 * For example:55 *56 * .. code-block:: pyodide57 *58 * let result = await pyodide.runPythonAsync(`59 * from js import fetch60 * response = await fetch("./packages.json")61 * packages = await response.json()62 * # If final statement is an expression, its value is returned to JavaScript63 * len(packages.packages.object_keys())64 * `);65 * console.log(result); // 7966 *67 * @param {string} code Python code to evaluate68 * @param {PyProxy=} globals An optional Python dictionary to use as the globals.69 * Defaults to :any:`pyodide.globals`. Uses the Python API70 * :any:`pyodide.eval_code_async` to evaluate the code.71 * @returns {Py2JsResult} The result of the Python code translated to JavaScript.72 * @async73 */74export function runPythonAsync(code: string, globals?: PyProxy | undefined): Py2JsResult;75/**76 * Registers the JavaScript object ``module`` as a JavaScript module named77 * ``name``. This module can then be imported from Python using the standard78 * Python import system. If another module by the same name has already been79 * imported, this won't have much effect unless you also delete the imported80 * module from ``sys.modules``. This calls the ``pyodide_py`` API81 * :func:`pyodide.register_js_module`.82 *83 * @param {string} name Name of the JavaScript module to add84 * @param {object} module JavaScript object backing the module85 */86export function registerJsModule(name: string, module: object): void;87/**88 * Tell Pyodide about Comlink.89 * Necessary to enable importing Comlink proxies into Python.90 */91export function registerComlink(Comlink: any): void;92/**93 * Unregisters a JavaScript module with given name that has been previously94 * registered with :js:func:`pyodide.registerJsModule` or95 * :func:`pyodide.register_js_module`. If a JavaScript module with that name96 * does not already exist, will throw an error. Note that if the module has97 * already been imported, this won't have much effect unless you also delete98 * the imported module from ``sys.modules``. This calls the ``pyodide_py`` API99 * :func:`pyodide.unregister_js_module`.100 *101 * @param {string} name Name of the JavaScript module to remove102 */103export function unregisterJsModule(name: string): void;104/**105 * Convert the JavaScript object to a Python object as best as possible.106 *107 * This is similar to :any:`JsProxy.to_py` but for use from JavaScript. If the108 * object is immutable or a :any:`PyProxy`, it will be returned unchanged. If109 * the object cannot be converted into Python, it will be returned unchanged.110 *111 * See :ref:`type-translations-jsproxy-to-py` for more information.112 *113 * @param {*} obj114 * @param {object} options115 * @param {number=} options.depth Optional argument to limit the depth of the116 * conversion.117 * @returns {PyProxy} The object converted to Python.118 */119export function toPy(obj: any, { depth }?: {120 depth?: number | undefined;121}): PyProxy;122/**123 * Imports a module and returns it.124 *125 * .. admonition:: Warning126 * :class: warning127 *128 * This function has a completely different behavior than the old removed pyimport function!129 *130 * ``pyimport`` is roughly equivalent to:131 *132 * .. code-block:: js133 *134 * pyodide.runPython(`import ${pkgname}; ${pkgname}`);135 *136 * except that the global namespace will not change.137 *138 * Example:139 *140 * .. code-block:: js141 *142 * let sysmodule = pyodide.pyimport("sys");143 * let recursionLimit = sys.getrecursionlimit();144 *145 * @param {string} mod_name The name of the module to import146 * @returns A PyProxy for the imported module147 */148export function pyimport(mod_name: string): any;149/**150 * Unpack an archive into a target directory.151 *152 * @param {ArrayBuffer} buffer The archive as an ArrayBuffer (it's also fine to pass a TypedArray).153 * @param {string} format The format of the archive. Should be one of the formats recognized by `shutil.unpack_archive`.154 * By default the options are 'bztar', 'gztar', 'tar', 'zip', and 'wheel'. Several synonyms are accepted for each format, e.g.,155 * for 'gztar' any of '.gztar', '.tar.gz', '.tgz', 'tar.gz' or 'tgz' are considered to be synonyms.156 *157 * @param {string=} extract_dir The directory to unpack the archive into. Defaults to the working directory.158 */159export function unpackArchive(buffer: ArrayBuffer, format: string, extract_dir?: string | undefined): void;160/**161 * Sets the interrupt buffer to be `interrupt_buffer`. This is only useful when162 * Pyodide is used in a webworker. The buffer should be a `SharedArrayBuffer`163 * shared with the main browser thread (or another worker). To request an164 * interrupt, a `2` should be written into `interrupt_buffer` (2 is the posix165 * constant for SIGINT).166 *167 * @param {TypedArray} interrupt_buffer168 */169export function setInterruptBuffer(interrupt_buffer: TypedArray): void;170/**171 * Throws a KeyboardInterrupt error if a KeyboardInterrupt has been requested172 * via the interrupt buffer.173 *174 * This can be used to enable keyboard interrupts during execution of JavaScript175 * code, just as `PyErr_CheckSignals` is used to enable keyboard interrupts176 * during execution of C code.177 */178export function checkInterrupt(): void;179export function makePublicAPI(): {180 globals: import("./pyproxy.gen.js").PyProxy;181 FS: any;182 pyodide_py: import("./pyproxy.gen.js").PyProxy;183 version: string;184 loadPackage: typeof loadPackage;185 loadPackagesFromImports: typeof loadPackagesFromImports;186 loadedPackages: any;187 isPyProxy: typeof isPyProxy;188 runPython: typeof runPython;189 runPythonAsync: typeof runPythonAsync;190 registerJsModule: typeof registerJsModule;191 unregisterJsModule: typeof unregisterJsModule;192 setInterruptBuffer: typeof setInterruptBuffer;193 checkInterrupt: typeof checkInterrupt;194 toPy: typeof toPy;195 pyimport: typeof pyimport;196 unpackArchive: typeof unpackArchive;197 registerComlink: typeof registerComlink;198 PythonError: typeof PythonError;199 PyBuffer: typeof PyBuffer;200};201/**202 * A JavaScript error caused by a Python exception.203 *204 * In order to reduce the risk of large memory leaks, the ``PythonError``205 * contains no reference to the Python exception that caused it. You can find206 * the actual Python exception that caused this error as `sys.last_value207 * <https://docs.python.org/3/library/sys.html#sys.last_value>`_.208 *209 * See :ref:`type-translations-errors` for more information.210 *211 * .. admonition:: Avoid Stack Frames212 * :class: warning213 *214 * If you make a :any:`PyProxy` of ``sys.last_value``, you should be215 * especially careful to :any:`destroy() <PyProxy.destroy>` it when you are216 * done. You may leak a large amount of memory including the local217 * variables of all the stack frames in the traceback if you don't. The218 * easiest way is to only handle the exception in Python.219 *220 * @class221 */222export class PythonError {223 /**224 * The Python traceback.225 * @type {string}226 */227 message: string;228}229/**230 *231 * The Pyodide version.232 *233 * It can be either the exact release version (e.g. ``0.1.0``), or234 * the latest release version followed by the number of commits since, and235 * the git hash of the current commit (e.g. ``0.1.0-1-bd84646``).236 *237 * @type {string}238 */239export let version: string;240export type LogFn = (msg: string) => void;241export type Py2JsResult = import('./pyproxy.gen').Py2JsResult;242export type PyProxy = import('./pyproxy.gen').PyProxy;243export type TypedArray = import('./pyproxy.gen').TypedArray;244export type Emscripten = any;245export type FS = any;246import { loadPackage } from "./load-pyodide.js";247import { isPyProxy } from "./pyproxy.gen.js";248import { PyBuffer } from "./pyproxy.gen.js";249import { loadedPackages } from "./load-pyodide.js";250export { loadPackage, loadedPackages, isPyProxy };251 