MCPcopy Create free account
hub / github.com/BasisResearch/lean.py / lean_py_initialize

Function lean_py_initialize

LeanPy/native/python_bridge.c:393–426  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

391}
392
393LEAN_EXPORT lean_obj_res lean_py_initialize(lean_obj_arg unit, lean_obj_arg world) {
394 (void)world;
395 if (py_initialized) {
396 return lean_io_result_mk_ok(lean_box(0));
397 }
398 if (!try_load_python()) {
399 return raise_io_error("LeanPy: could not load libpython (set LEANPY_LIBPYTHON to override)");
400 }
401#define X(ret, name, args) \
402 p_##name = (ret (*) args) dlsym(py_handle, #name); \
403 if (!p_##name) { \
404 return raise_io_error("LeanPy: dlsym failed for " #name); \
405 }
406 PYSYMS
407#undef X
408 /* Singletons. */
409 p_Py_None = (PyObject *) dlsym(py_handle, "_Py_NoneStruct");
410 p_Py_True = (PyObject *) dlsym(py_handle, "_Py_TrueStruct");
411 p_Py_False = (PyObject *) dlsym(py_handle, "_Py_FalseStruct");
412 if (!p_Py_None || !p_Py_True || !p_Py_False) {
413 return raise_io_error("LeanPy: failed to resolve Py_None/True/False");
414 }
415 /* PyExc_RuntimeError is exported as a `PyObject *` global; we want the
416 * value of the symbol, so dereference once. */
417 {
418 PyObject **slot = (PyObject **) dlsym(py_handle, "PyExc_RuntimeError");
419 if (slot) p_PyExc_RuntimeError = *slot;
420 }
421 if (!p_Py_IsInitialized()) {
422 p_Py_Initialize();
423 }
424 py_initialized = 1;
425 return lean_io_result_mk_ok(lean_box(0));
426}
427
428LEAN_EXPORT lean_obj_res lean_py_is_initialized(lean_obj_arg unit, lean_obj_arg world) {
429 (void)world;

Callers

nothing calls this directly

Calls 3

lean_boxFunction · 0.85
try_load_pythonFunction · 0.85
raise_io_errorFunction · 0.85

Tested by

no test coverage detected