summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorPatrick Monnerat <[email protected]>2019-02-17 17:36:11 +0100
committerraveit65 <[email protected]>2019-03-31 12:30:00 +0200
commit1d5a8b7aa497c6d3d3257887f95832eda397347c (patch)
tree65471eef27198f9eeb9cf5c3c7f79fb05c13a729 /src
parent75906bebc8b7ce8adef7040d5bebde8ff7f59090 (diff)
downloadpython-caja-1d5a8b7aa497c6d3d3257887f95832eda397347c.tar.bz2
python-caja-1d5a8b7aa497c6d3d3257887f95832eda397347c.tar.xz
Make Caja.OperationHandle usable.
The Caja.OperationHandle.handle property is added: this gives access to the effective handle taking the form of a non-zero signed integer that can be stored in a void pointer. Trying to set this property with an invalid value raises an exception. Upon update_file_info_full call, the new Caja.OperationHandle is preset with a "unique" non-null value generated from an atomic counter. When the python method returns, the handle value is transmitted to Caja whatever it is: this fixes the problem of Caja expecting a non-null handle when CAJA_OPERATION_IN_PROGRESS is returned. Ref: https://github.com/mate-desktop/python-caja/issues/36
Diffstat (limited to 'src')
-rw-r--r--src/caja-python-object.c19
-rw-r--r--src/caja-python.c53
2 files changed, 70 insertions, 2 deletions
diff --git a/src/caja-python-object.c b/src/caja-python-object.c
index e2bdf85..fbc5f79 100644
--- a/src/caja-python-object.c
+++ b/src/caja-python-object.c
@@ -411,20 +411,34 @@ caja_python_object_update_file_info (CajaInfoProvider *provider,
CajaOperationResult ret = CAJA_OPERATION_COMPLETE;
PyObject *py_ret = NULL;
PyGILState_STATE state = pyg_gil_state_ensure();
- PyObject *py_handle = caja_python_boxed_new (_PyCajaOperationHandle_Type, *handle, FALSE);
+ static volatile gssize handle_generator = 1;
debug_enter();
CHECK_OBJECT(object);
+ *handle = NULL;
+
if (PyObject_HasAttrString(object->instance, "update_file_info_full"))
{
+ PyObject *py_handle;
+ void *h;
+
+ /* Generate a new handle with a default value. */
+ do {
+ h = (CajaOperationHandle *) g_atomic_pointer_add (&handle_generator, 1);
+ } while (!h);
+ py_handle = caja_python_boxed_new (_PyCajaOperationHandle_Type,
+ h, FALSE);
+
+
py_ret = PyObject_CallMethod(object->instance,
METHOD_PREFIX "update_file_info_full", "(NNNN)",
pygobject_new((GObject*)provider),
py_handle,
pyg_boxed_new(G_TYPE_CLOSURE, update_complete, TRUE, TRUE),
pygobject_new((GObject*)file));
+ *handle = (void *) ((PyGBoxed *) py_handle)->boxed;
}
else if (PyObject_HasAttrString(object->instance, "update_file_info"))
{
@@ -447,6 +461,9 @@ caja_python_object_update_file_info (CajaInfoProvider *provider,
}
ret = INT_ASLONG(py_ret);
+
+ if (!*handle && ret == CAJA_OPERATION_IN_PROGRESS)
+ ret = CAJA_OPERATION_FAILED;
beach:
free_pygobject_data(file, NULL);
diff --git a/src/caja-python.c b/src/caja-python.c
index e9940ab..f609b1e 100644
--- a/src/caja-python.c
+++ b/src/caja-python.c
@@ -22,6 +22,7 @@
#endif
#include <Python.h>
+#include <structmember.h>
#include <pygobject.h>
#include <gmodule.h>
#include <gtk/gtk.h>
@@ -33,8 +34,12 @@
#if PY_MAJOR_VERSION >= 3
#define STRING_FROMSTRING(str) PyUnicode_FromString(str)
+#define INT_FROMSSIZE_T(val) PyLong_FromSsize_t(val)
+#define INT_ASSSIZE_T(obj) PyLong_AsSsize_t(obj)
#else
#define STRING_FROMSTRING(str) PyString_FromString(str)
+#define INT_FROMSSIZE_T(val) PyInt_FromSsize_t(val)
+#define INT_ASSSIZE_T(obj) PyInt_AsSsize_t(obj)
#endif
static const GDebugKey caja_python_debug_keys[] = {
@@ -49,6 +54,37 @@ static GArray *all_types = NULL;
static GList *all_pyfiles = NULL;
+/* Caja.OperationHandle value access. */
+static PyObject *
+caja_operationhandle_get_handle(PyGBoxed *self, void *closure)
+{
+ return INT_FROMSSIZE_T((Py_ssize_t) (size_t) self->boxed);
+}
+
+static int
+caja_operationhandle_set_handle(PyGBoxed *self, PyObject *value, void *closure)
+{
+ Py_ssize_t val = INT_ASSSIZE_T(value);
+
+ if (!PyErr_Occurred()) {
+ if (val) {
+ self->boxed = (void *) val;
+ return 0;
+ }
+ PyErr_SetString(PyExc_ValueError, "invalid operation handle value");
+ }
+ return -1;
+}
+
+static PyGetSetDef caja_operationhandle_handle = {
+ "handle",
+ (getter) caja_operationhandle_get_handle,
+ (setter) caja_operationhandle_set_handle,
+ "Operation handle value",
+ NULL
+};
+
+
static inline gboolean
np_init_pygobject(void)
{
@@ -163,7 +199,7 @@ caja_python_load_dir (GTypeModule *module,
static gboolean
caja_python_init_python (void)
{
- PyObject *gi, *require_version, *args, *caja;
+ PyObject *gi, *require_version, *args, *caja, *descr;
GModule *libpython;
#if PY_MAJOR_VERSION >= 3
wchar_t *argv[] = { L"caja", NULL };
@@ -256,6 +292,21 @@ caja_python_init_python (void)
IMPORT(OperationHandle, "OperationHandle");
#undef IMPORT
+
+ /* Add the "handle" member to the OperationHandle type. */
+ descr = PyDescr_NewGetSet(_PyCajaOperationHandle_Type,
+ &caja_operationhandle_handle);
+ if (!descr) {
+ PyErr_Print();
+ return FALSE;
+ }
+ if (PyDict_SetItemString(_PyCajaOperationHandle_Type->tp_dict,
+ caja_operationhandle_handle.name, descr)) {
+ Py_DECREF(descr);
+ PyErr_Print();
+ return FALSE;
+ }
+ Py_DECREF(descr);
return TRUE;
}