Skip to content
Permalink
Browse files

Implement Python 3 C API compatibility using conditional and macros.

The updated sources are still compatible with Python 2.
  • Loading branch information...
monnerat authored and vkareh committed Jan 19, 2019
1 parent 1eeb215 commit 047c35bf0aaec2601d6e26e5ab479eeccc58f640
Showing with 31 additions and 7 deletions.
  1. +20 −6 src/caja-python-object.c
  2. +11 −1 src/caja-python.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,
@@ -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;

0 comments on commit 047c35b

Please sign in to comment.
You can’t perform that action at this time.