diff --git a/emacspy.pyx b/emacspy.pyx index 90a5397..dd1e214 100644 --- a/emacspy.pyx +++ b/emacspy.pyx @@ -291,7 +291,7 @@ cdef extern from "subinterpreter.c": void make_interpreter(char*) object run_string(char*, char*, object) object call_method(char*, object, object, object, object, object) - object call_function(object, object, char*) + object call_function(char*, object, object, object) object import_module(object, object, char*) object get_global_variable(object, char*) object get_object_attr(char*, object, object, object) @@ -344,10 +344,12 @@ def init(): return ret @defun('py-call-function') - def call_function_python(interpreter_name, function_name, *args): + def call_function_python(interpreter_name, function_name, target_name='', *args): + if target_name: + target_name = target_name.to_python_type() args_py = tuple((arg.to_python_type() for arg in args)) - ret = call_function(function_name.to_python_type(), args_py, \ - str_elisp2c(interpreter_name)) + ret = call_function(str_elisp2c(interpreter_name), function_name.to_python_type(), \ + target_name, args_py) if isinstance(ret, BaseException): raise ret return ret diff --git a/subinterpreter.c b/subinterpreter.c index c7d14fd..f7ce272 100644 --- a/subinterpreter.c +++ b/subinterpreter.c @@ -200,7 +200,8 @@ PyObject *call_method(char *interpreter_name, PyObject *obj_name, PyObject *meth } } -PyObject* call_function (PyObject *callable_name, PyObject *args_pylist, char *interpreter_name) { +PyObject* call_function (char *interpreter_name, PyObject *callable_name, \ + PyObject *target_name, PyObject *args_pylist) { struct interpr *sub_interpreter = get_interpreter(interpreter_name); PyGILState_STATE gil = PyGILState_Ensure(); PyThreadState *orig_tstate = PyThreadState_Get(); @@ -208,8 +209,9 @@ PyObject* call_function (PyObject *callable_name, PyObject *args_pylist, char *i PyObject *global_dict = PyModule_GetDict(sub_interpreter->main_module); PyObject *callable = PyObject_GetItem(global_dict, callable_name); // New reference - PyObject *exception = NULL; PyObject *ret = NULL; + PyObject *obj = NULL; + PyObject *exception = NULL; if (NULL == callable) { PyObject *builtins_name = PyUnicode_FromString("__builtins__"); @@ -223,8 +225,18 @@ PyObject* call_function (PyObject *callable_name, PyObject *args_pylist, char *i assert(callable); - ret = PyObject_Call(callable, args_pylist, NULL); + obj = PyObject_Call(callable, args_pylist, NULL); exception = PyErr_GetRaisedException(); + if (exception) + goto finish; + + if (PyUnicode_GetLength(target_name) > 0) { + ret = Py_True; + PyObject_SetItem(global_dict, target_name, obj); + exception = PyErr_GetRaisedException(); + } else { + ret = obj; + } finish: Py_XDECREF(callable); diff --git a/tests/test.el b/tests/test.el index 173c53a..f932200 100644 --- a/tests/test.el +++ b/tests/test.el @@ -33,10 +33,11 @@ ) (ert-deftest ert-test-emacspy-py-call-function () - (should (eq 3 (py-call-function "test" "len" "123"))) - (should-error (py-call-function "test" "NON-EXISTING-FUNCTION" "123") - :type 'python-exception) - ) + (should (eq 3 (py-call-function "test" "len" nil "123"))) + (should (py-call-function "test" "len" "call_function_test_var" "123")) + (should (eq 3 (py-get-global-variable "test" "call_function_test_var"))) + (should-error (py-call-function "test" "NON-EXISTING-FUNCTION" nil "123") + :type 'python-exception) ) (ert-deftest ert-test-emacspy-py-get-object-attr () (should (string= "0123456789" (py-get-object-attr "test" "string" "digits")))