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

Function lean_py_to_lean_obj

LeanPy/native/python_bridge.c:1201–1235  ·  view source on GitHub ↗

* lean_py_to_lean_obj: extract a lean_object* from a Python LeanObjHandle. * Returns Option α: some(obj) if the Python object is a LeanObjHandle, * none otherwise. * * @[extern "lean_py_to_lean_obj"] * opaque Py.toLeanObj (py : @& Py) : IO (Option α) */

Source from the content-addressed store, hash-verified

1199}
1200
1201/*
1202 * lean_py_to_lean_obj: extract a lean_object* from a Python LeanObjHandle.
1203 * Returns Option α: some(obj) if the Python object is a LeanObjHandle,
1204 * none otherwise.
1205 *
1206 * @[extern "lean_py_to_lean_obj"]
1207 * opaque Py.toLeanObj (py : @& Py) : IO (Option α)
1208 */
1209LEAN_EXPORT lean_obj_res lean_py_to_lean_obj(b_lean_obj_arg py_ext, lean_obj_arg world) {
1210 (void)world;
1211 ENSURE_INIT(); WITH_GIL();
1212
1213 if (!lean_is_external(py_ext)) {
1214 /* Return none */
1215 return lean_io_result_mk_ok(lean_box(0));
1216 }
1217
1218 PyObject *pyobj = unwrap_pyobject(py_ext);
1219 if (!pyobj) {
1220 return lean_io_result_mk_ok(lean_box(0));
1221 }
1222
1223 /* Check if pyobj is an instance of LeanObjHandle.
1224 Cast to LeanObjHandle* to read ob_type (PyObject is opaque). */
1225 PyObject *handle_type = get_lean_obj_handle_type();
1226 LeanObjHandle *candidate = (LeanObjHandle *)pyobj;
1227 if (!handle_type || candidate->ob_type != handle_type) {
1228 return lean_io_result_mk_ok(lean_box(0)); /* none */
1229 }
1230
1231 LeanObjHandle *h = (LeanObjHandle *)pyobj;
1232 lean_object *result = h->ptr;
1233 if (!result) {
1234 return lean_io_result_mk_ok(lean_box(0)); /* none */
1235 }
1236
1237 lean_inc(result); /* caller gets their own ref */
1238

Callers

nothing calls this directly

Calls 7

lean_is_externalFunction · 0.85
lean_boxFunction · 0.85
unwrap_pyobjectFunction · 0.85
get_lean_obj_handle_typeFunction · 0.85
lean_incFunction · 0.85
lean_alloc_ctorFunction · 0.85
lean_ctor_setFunction · 0.85

Tested by

no test coverage detected