mirror of https://github.com/pyodide/pyodide.git
Add private APIs to get Python version before bootstrapping Python (#3625)
Use them in Module.preRun to initialize the filesystem.
This commit is contained in:
parent
959ceb9d57
commit
ba5545cbf1
3
Makefile
3
Makefile
|
@ -85,7 +85,8 @@ dist/libpyodide.a: \
|
|||
src/core/pyproxy.o \
|
||||
src/core/python2js_buffer.o \
|
||||
src/core/python2js.o \
|
||||
src/core/pyodide_pre.o
|
||||
src/core/pyodide_pre.o \
|
||||
src/core/pyversion.o
|
||||
emar rcs dist/libpyodide.a $(filter %.o,$^)
|
||||
|
||||
|
||||
|
|
|
@ -0,0 +1,21 @@
|
|||
#define PY_SSIZE_T_CLEAN
|
||||
#include "Python.h"
|
||||
#include <emscripten.h>
|
||||
|
||||
EMSCRIPTEN_KEEPALIVE int
|
||||
py_version_major()
|
||||
{
|
||||
return PY_MAJOR_VERSION;
|
||||
}
|
||||
|
||||
EMSCRIPTEN_KEEPALIVE int
|
||||
py_version_minor()
|
||||
{
|
||||
return PY_MINOR_VERSION;
|
||||
}
|
||||
|
||||
EMSCRIPTEN_KEEPALIVE int
|
||||
py_version_micro()
|
||||
{
|
||||
return PY_MICRO_VERSION;
|
||||
}
|
|
@ -0,0 +1,13 @@
|
|||
#ifndef PYVERSION_H
|
||||
#define PYVERSION_H
|
||||
|
||||
int
|
||||
py_version_major();
|
||||
|
||||
int
|
||||
py_version_minor();
|
||||
|
||||
int
|
||||
py_version_micro();
|
||||
|
||||
#endif /* PYVERSION_H */
|
|
@ -130,19 +130,22 @@ function mountLocalDirectories(Module: Module, mounts: string[]) {
|
|||
* @param stdlibPromise A promise that resolves to the standard library.
|
||||
*/
|
||||
function installStdlib(Module: Module, stdlibURL: string) {
|
||||
// TODO(ryanking13): Get python version from Python.h
|
||||
|
||||
const stdlibPromise: Promise<Uint8Array> = loadBinaryFile(stdlibURL);
|
||||
|
||||
Module.preRun.push(() => {
|
||||
/* @ts-ignore */
|
||||
const pymajor = Module._py_version_major();
|
||||
/* @ts-ignore */
|
||||
const pyminor = Module._py_version_minor();
|
||||
|
||||
Module.FS.mkdirTree("/lib");
|
||||
Module.FS.mkdirTree("/lib/python3.11/site-packages");
|
||||
Module.FS.mkdirTree(`/lib/python${pymajor}.${pyminor}/site-packages`);
|
||||
|
||||
Module.addRunDependency("install-stdlib");
|
||||
|
||||
stdlibPromise
|
||||
.then((stdlib: Uint8Array) => {
|
||||
Module.FS.writeFile("/lib/python311.zip", stdlib);
|
||||
Module.FS.writeFile(`/lib/python${pymajor}${pyminor}.zip`, stdlib);
|
||||
})
|
||||
.catch((e) => {
|
||||
console.error("Error occurred while installing the standard library:");
|
||||
|
|
|
@ -1782,3 +1782,15 @@ def test_python_error(selenium):
|
|||
)
|
||||
assert msg.endswith("TypeError: oops\n")
|
||||
assert ty == "TypeError"
|
||||
|
||||
|
||||
def test_python_version(selenium):
|
||||
selenium.run_js(
|
||||
"""
|
||||
sys = pyodide.pyimport("sys");
|
||||
assert(() => sys.version_info.major === pyodide._module._py_version_major());
|
||||
assert(() => sys.version_info.minor === pyodide._module._py_version_minor());
|
||||
assert(() => sys.version_info.micro === pyodide._module._py_version_micro());
|
||||
sys.destroy();
|
||||
"""
|
||||
)
|
||||
|
|
Loading…
Reference in New Issue