-
-
Notifications
You must be signed in to change notification settings - Fork 787
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
NFC Use initialization function to load _pyodide_core (#3333)
This adds `_pyodide_core` to the init tab and initializes it using that. This is a small step toward unvendoring the Pyodide ffi.
- Loading branch information
Showing
5 changed files
with
111 additions
and
89 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
#define PY_SSIZE_T_CLEAN | ||
#include "Python.h" | ||
#include "hiwire.h" | ||
#include "python2js.h" | ||
#include <emscripten.h> | ||
#include <stdbool.h> | ||
|
||
#define FATAL_ERROR(args...) \ | ||
do { \ | ||
PyErr_Format(PyExc_ImportError, args); \ | ||
FAIL(); \ | ||
} while (0) | ||
|
||
#define FAIL_IF_STATUS_EXCEPTION(status) \ | ||
if (PyStatus_Exception(status)) { \ | ||
goto finally; \ | ||
} | ||
|
||
#define TRY_INIT(mod) \ | ||
do { \ | ||
int mod##_init(); \ | ||
if (mod##_init()) { \ | ||
FATAL_ERROR("Failed to initialize module %s.", #mod); \ | ||
} \ | ||
} while (0) | ||
|
||
#define TRY_INIT_WITH_CORE_MODULE(mod) \ | ||
do { \ | ||
int mod##_init(PyObject* mod); \ | ||
if (mod##_init(core_module)) { \ | ||
FATAL_ERROR("Failed to initialize module %s.", #mod); \ | ||
} \ | ||
} while (0) | ||
|
||
static struct PyModuleDef core_module_def = { | ||
PyModuleDef_HEAD_INIT, | ||
.m_name = "_pyodide_core", | ||
.m_doc = "Pyodide C builtins", | ||
.m_size = -1, | ||
}; | ||
|
||
PyObject* | ||
PyInit__pyodide_core(void) | ||
{ | ||
bool success = false; | ||
PyObject* _pyodide = NULL; | ||
PyObject* core_module = NULL; | ||
JsRef _pyodide_proxy = NULL; | ||
|
||
_pyodide = PyImport_ImportModule("_pyodide"); | ||
if (_pyodide == NULL) { | ||
FATAL_ERROR("Failed to import _pyodide module."); | ||
} | ||
|
||
core_module = PyModule_Create(&core_module_def); | ||
if (core_module == NULL) { | ||
FATAL_ERROR("Failed to create core module."); | ||
} | ||
|
||
TRY_INIT_WITH_CORE_MODULE(error_handling); | ||
TRY_INIT(hiwire); | ||
TRY_INIT(docstring); | ||
TRY_INIT(js2python); | ||
TRY_INIT_WITH_CORE_MODULE(python2js); | ||
TRY_INIT(python2js_buffer); | ||
TRY_INIT_WITH_CORE_MODULE(JsProxy); | ||
TRY_INIT_WITH_CORE_MODULE(pyproxy); | ||
|
||
PyObject* module_dict = PyImport_GetModuleDict(); /* borrowed */ | ||
if (PyDict_SetItemString(module_dict, "_pyodide_core", core_module)) { | ||
FATAL_ERROR("Failed to add '_pyodide_core' module to modules dict."); | ||
FAIL(); | ||
} | ||
|
||
// Enable JavaScript access to the _pyodide module. | ||
_pyodide_proxy = python2js(_pyodide); | ||
if (_pyodide_proxy == NULL) { | ||
FATAL_ERROR("Failed to create _pyodide proxy."); | ||
} | ||
EM_ASM({ API._pyodide = Hiwire.pop_value($0); }, _pyodide_proxy); | ||
|
||
success = true; | ||
finally: | ||
Py_CLEAR(_pyodide); | ||
if (!success) { | ||
Py_CLEAR(core_module); | ||
} | ||
return core_module; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters