From 047c35bf0aaec2601d6e26e5ab479eeccc58f640 Mon Sep 17 00:00:00 2001 From: Patrick Monnerat Date: Sat, 19 Jan 2019 01:20:47 +0100 Subject: Implement Python 3 C API compatibility using conditional and macros. The updated sources are still compatible with Python 2. --- src/caja-python-object.c | 26 ++++++++++++++++++++------ 1 file changed, 20 insertions(+), 6 deletions(-) (limited to 'src/caja-python-object.c') diff --git a/src/caja-python-object.c b/src/caja-python-object.c index fb9d3d7..d3d46fc 100644 --- a/src/caja-python-object.c +++ b/src/caja-python-object.c @@ -44,6 +44,20 @@ static GObjectClass *parent_class; +#if PY_MAJOR_VERSION >= 3 +#define STRING_CHECK(obj) PyUnicode_Check(obj) +#define STRING_FROMSTRING(str) PyUnicode_FromString(str) +#define STRING_ASSTRING(obj) PyUnicode_AsUTF8(obj) +#define INT_CHECK(obj) PyLong_Check(obj) +#define INT_ASLONG(obj) PyLong_AsLong(obj) +#else +#define STRING_CHECK(obj) PyString_Check(obj) +#define STRING_FROMSTRING(str) PyString_FromString(str) +#define STRING_ASSTRING(obj) PyString_AsString(obj) +#define INT_CHECK(obj) PyInt_Check(obj) +#define INT_ASLONG(obj) PyInt(obj) +#endif + /* These macros assumes the following things: * a METHOD_NAME is defined with is a string * a goto label called beach @@ -85,7 +99,7 @@ static GObjectClass *parent_class; #define HANDLE_LIST(py_ret, type, type_name) \ { \ Py_ssize_t i = 0; \ - if (!PySequence_Check(py_ret) || PyString_Check(py_ret)) \ + if (!PySequence_Check(py_ret) || STRING_CHECK(py_ret)) \ { \ PyErr_SetString(PyExc_TypeError, \ METHOD_NAME " must return a sequence"); \ @@ -194,7 +208,7 @@ caja_python_object_get_widget (CajaLocationWidgetProvider *provider, CHECK_OBJECT(object); CHECK_METHOD_NAME(object->instance); - py_uri = PyString_FromString(uri); + py_uri = STRING_FROMSTRING(uri); py_ret = PyObject_CallMethod(object->instance, METHOD_PREFIX METHOD_NAME, "(NN)", py_uri, @@ -425,14 +439,14 @@ caja_python_object_update_file_info (CajaInfoProvider *provider, HANDLE_RETVAL(py_ret); - if (!PyInt_Check(py_ret)) + if (!INT_CHECK(py_ret)) { PyErr_SetString(PyExc_TypeError, METHOD_NAME " must return None or a int"); goto beach; } - ret = PyInt_AsLong(py_ret); + ret = INT_ASLONG(py_ret); beach: free_pygobject_data(file, NULL); @@ -522,7 +536,7 @@ caja_python_object_get_type (GTypeModule *module, NULL }; - debug_enter_args("type=%s", PyString_AsString(PyObject_GetAttrString(type, "__name__"))); + debug_enter_args("type=%s", STRING_ASSTRING(PyObject_GetAttrString(type, "__name__"))); info = g_new0 (GTypeInfo, 1); info->class_size = sizeof (CajaPythonObjectClass); @@ -534,7 +548,7 @@ caja_python_object_get_type (GTypeModule *module, Py_INCREF(type); type_name = g_strdup_printf("%s+CajaPython", - PyString_AsString(PyObject_GetAttrString(type, "__name__"))); + STRING_ASSTRING(PyObject_GetAttrString(type, "__name__"))); gtype = g_type_module_register_type (module, G_TYPE_OBJECT, -- cgit v1.2.1