summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPatrick Monnerat <[email protected]>2019-01-19 01:20:47 +0100
committerVictor Kareh <[email protected]>2019-01-30 15:59:28 -0500
commit047c35bf0aaec2601d6e26e5ab479eeccc58f640 (patch)
tree7879f33a5eee365488b3de8ee530562d0d8f1be0
parent1eeb21507d8ab6f063d0f3a6c42c067404c03ac3 (diff)
downloadpython-caja-047c35bf0aaec2601d6e26e5ab479eeccc58f640.tar.bz2
python-caja-047c35bf0aaec2601d6e26e5ab479eeccc58f640.tar.xz
Implement Python 3 C API compatibility using conditional and macros.
The updated sources are still compatible with Python 2.
-rw-r--r--src/caja-python-object.c26
-rw-r--r--src/caja-python.c12
2 files changed, 31 insertions, 7 deletions
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,
diff --git a/src/caja-python.c b/src/caja-python.c
index 7f13e36..4a310a4 100644
--- a/src/caja-python.c
+++ b/src/caja-python.c
@@ -31,6 +31,12 @@
#include <libcaja-extension/caja-extension-types.h>
+#if PY_MAJOR_VERSION >= 3
+#define STRING_FROMSTRING(str) PyUnicode_FromString(str)
+#else
+#define STRING_FROMSTRING(str) PyString_FromString(str)
+#endif
+
static const GDebugKey caja_python_debug_keys[] = {
{"misc", CAJA_PYTHON_DEBUG_MISC},
};
@@ -145,7 +151,7 @@ caja_python_load_dir (GTypeModule *module,
/* sys.path.insert(0, dirname) */
sys_path = PySys_GetObject("path");
- py_path = PyString_FromString(dirname);
+ py_path = STRING_FROMSTRING(dirname);
PyList_Insert(sys_path, 0, py_path);
Py_DECREF(py_path);
}
@@ -159,7 +165,11 @@ caja_python_init_python (void)
{
PyObject *gi, *require_version, *args, *caja;
GModule *libpython;
+#if PY_MAJOR_VERSION >= 3
+ wchar_t *argv[] = { L"caja", NULL };
+#else
char *argv[] = { "caja", NULL };
+#endif
if (Py_IsInitialized())
return TRUE;