- struct value *value = NULL;
- PyObject *result, *callable, *args;
- struct cleanup *cleanup;
-
- cleanup = ensure_python_env (gdbarch, language);
-
- args = convert_values_to_python (argc, argv);
-
- callable = PyObject_GetAttrString ((PyObject *) cookie, "invoke");
- if (! callable)
+ /* The gdbpy_enter object needs to be placed first, so that it's the last to
+ be destroyed. */
+ gdbpy_enter enter_py (gdbarch, language);
+ struct value *value;
+ gdbpy_ref<> result;
+ gdbpy_ref<> args (convert_values_to_python (argc, argv));
+
+ /* convert_values_to_python can return NULL on error. If we
+ encounter this, do not call the function, but allow the Python ->
+ error code conversion below to deal with the Python exception.
+ Note, that this is different if the function simply does not
+ have arguments. */
+
+ if (args != NULL)