static SCM call_callable(SCM scm_args) { SCM stack = scm_make_stack(SCM_BOOL_T, SCM_EOL); SCM frame = scm_stack_ref(stack, scm_from_int(0)); SCM proc = scm_frame_procedure(frame); PyObject *callable = scm_to_pointer(scm_assq_ref(gsubr_alist, proc)); scm_dynwind_begin(0); PyObject *py_args = scm2py(scm_args); if (py_args == NULL) py2scm_exception(); /* does not return */ scm_dynwind_py_decref(py_args); struct call_callable_data data = { callable, py_args }; PyObject *py_result = (PyObject *)scm_without_guile( (void *(*)(void *))call_callable1, &data); if (py_result == NULL) py2scm_exception(); /* does not return */ scm_dynwind_py_decref(py_result); SCM scm_result = py2scm(py_result); scm_dynwind_end(); return scm_result; }
static PyObject *load_wrapper(PyObject *name_arg) { return scm2py( scm_eval( scm_list_2(scm_from_utf8_symbol("load"), py2scm(name_arg)), scm_current_module())); }
static PyObject *lookup_wrapper(const char *name) { return scm2py(scm_variable_ref(scm_c_lookup(name))); }
static PyObject *eval_string_wrapper(PyObject *string_arg) { const char *s = PyString_AsString(string_arg); SCM string = scm_from_utf8_stringn(s, PyString_Size(string_arg)); return scm2py(scm_eval_string(string)); }
PyObject *scm2py(SCM value) { if (value == NULL) return NULL; if (value == SCM_UNSPECIFIED) { Py_INCREF(Py_None); return Py_None; } if (scm_is_exact_integer(value)) return PyInt_FromLong(scm_to_long(value)); if (scm_is_real(value)) return PyFloat_FromDouble(scm_to_double(value)); if (scm_is_bool(value)) { PyObject *result = scm_to_bool(value) ? Py_True : Py_False; Py_INCREF(result); return result; } if (value == SCM_EOL) return PyTuple_New(0); if (scm_is_string(value)) { size_t len = 0; char *s = scm_to_utf8_stringn(value, &len); PyObject *result = PyUnicode_FromStringAndSize(s, len); free(s); return result; } if (scm_is_pair(value)) { unsigned int len = scm_to_uint(scm_length(value)); PyObject *result = PyTuple_New(len); scm_dynwind_begin(0); scm_dynwind_unwind_handler( (void (*)(void *))Py_DecRef, result, 0); unsigned int i; for (i = 0; i < len; i++) { PyObject *item = scm2py(scm_car(value)); if (item == NULL) { scm_dynwind_end(); Py_DECREF(result); return NULL; } PyTuple_SET_ITEM(result, i, item); value = scm_cdr(value); } scm_dynwind_end(); return result; } if (scm_to_bool(scm_procedure_p(value))) { SCM ptr = scm_assq_ref(gsubr_alist, value); if (!scm_is_false(ptr)) { PyObject *result = scm_to_pointer(ptr); Py_INCREF(result); return result; } Procedure *result = (Procedure *)ProcedureType.tp_alloc(&ProcedureType, 0); if (result == NULL) return NULL; result->proc = value; return (PyObject *)result; } char *msg = scm_to_utf8_stringn( scm_simple_format( SCM_BOOL_F, scm_from_utf8_string( "Guile expression ~S doesn't have a " "corresponding Python value"), scm_list_1(value)), NULL); PyErr_SetString(PyExc_TypeError, msg); free(msg); return NULL; }