Skip to content

Commit 047c35b

Browse files
monneratvkareh
authored andcommitted
Implement Python 3 C API compatibility using conditional and macros.
The updated sources are still compatible with Python 2.
1 parent 1eeb215 commit 047c35b

File tree

2 files changed

+31
-7
lines changed

2 files changed

+31
-7
lines changed

src/caja-python-object.c

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,20 @@
4444

4545
static GObjectClass *parent_class;
4646

47+
#if PY_MAJOR_VERSION >= 3
48+
#define STRING_CHECK(obj) PyUnicode_Check(obj)
49+
#define STRING_FROMSTRING(str) PyUnicode_FromString(str)
50+
#define STRING_ASSTRING(obj) PyUnicode_AsUTF8(obj)
51+
#define INT_CHECK(obj) PyLong_Check(obj)
52+
#define INT_ASLONG(obj) PyLong_AsLong(obj)
53+
#else
54+
#define STRING_CHECK(obj) PyString_Check(obj)
55+
#define STRING_FROMSTRING(str) PyString_FromString(str)
56+
#define STRING_ASSTRING(obj) PyString_AsString(obj)
57+
#define INT_CHECK(obj) PyInt_Check(obj)
58+
#define INT_ASLONG(obj) PyInt(obj)
59+
#endif
60+
4761
/* These macros assumes the following things:
4862
* a METHOD_NAME is defined with is a string
4963
* a goto label called beach
@@ -85,7 +99,7 @@ static GObjectClass *parent_class;
8599
#define HANDLE_LIST(py_ret, type, type_name) \
86100
{ \
87101
Py_ssize_t i = 0; \
88-
if (!PySequence_Check(py_ret) || PyString_Check(py_ret)) \
102+
if (!PySequence_Check(py_ret) || STRING_CHECK(py_ret)) \
89103
{ \
90104
PyErr_SetString(PyExc_TypeError, \
91105
METHOD_NAME " must return a sequence"); \
@@ -194,7 +208,7 @@ caja_python_object_get_widget (CajaLocationWidgetProvider *provider,
194208
CHECK_OBJECT(object);
195209
CHECK_METHOD_NAME(object->instance);
196210

197-
py_uri = PyString_FromString(uri);
211+
py_uri = STRING_FROMSTRING(uri);
198212

199213
py_ret = PyObject_CallMethod(object->instance, METHOD_PREFIX METHOD_NAME,
200214
"(NN)", py_uri,
@@ -425,14 +439,14 @@ caja_python_object_update_file_info (CajaInfoProvider *provider,
425439

426440
HANDLE_RETVAL(py_ret);
427441

428-
if (!PyInt_Check(py_ret))
442+
if (!INT_CHECK(py_ret))
429443
{
430444
PyErr_SetString(PyExc_TypeError,
431445
METHOD_NAME " must return None or a int");
432446
goto beach;
433447
}
434448

435-
ret = PyInt_AsLong(py_ret);
449+
ret = INT_ASLONG(py_ret);
436450

437451
beach:
438452
free_pygobject_data(file, NULL);
@@ -522,7 +536,7 @@ caja_python_object_get_type (GTypeModule *module,
522536
NULL
523537
};
524538

525-
debug_enter_args("type=%s", PyString_AsString(PyObject_GetAttrString(type, "__name__")));
539+
debug_enter_args("type=%s", STRING_ASSTRING(PyObject_GetAttrString(type, "__name__")));
526540
info = g_new0 (GTypeInfo, 1);
527541

528542
info->class_size = sizeof (CajaPythonObjectClass);
@@ -534,7 +548,7 @@ caja_python_object_get_type (GTypeModule *module,
534548
Py_INCREF(type);
535549

536550
type_name = g_strdup_printf("%s+CajaPython",
537-
PyString_AsString(PyObject_GetAttrString(type, "__name__")));
551+
STRING_ASSTRING(PyObject_GetAttrString(type, "__name__")));
538552

539553
gtype = g_type_module_register_type (module,
540554
G_TYPE_OBJECT,

src/caja-python.c

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,12 @@
3131

3232
#include <libcaja-extension/caja-extension-types.h>
3333

34+
#if PY_MAJOR_VERSION >= 3
35+
#define STRING_FROMSTRING(str) PyUnicode_FromString(str)
36+
#else
37+
#define STRING_FROMSTRING(str) PyString_FromString(str)
38+
#endif
39+
3440
static const GDebugKey caja_python_debug_keys[] = {
3541
{"misc", CAJA_PYTHON_DEBUG_MISC},
3642
};
@@ -145,7 +151,7 @@ caja_python_load_dir (GTypeModule *module,
145151

146152
/* sys.path.insert(0, dirname) */
147153
sys_path = PySys_GetObject("path");
148-
py_path = PyString_FromString(dirname);
154+
py_path = STRING_FROMSTRING(dirname);
149155
PyList_Insert(sys_path, 0, py_path);
150156
Py_DECREF(py_path);
151157
}
@@ -159,7 +165,11 @@ caja_python_init_python (void)
159165
{
160166
PyObject *gi, *require_version, *args, *caja;
161167
GModule *libpython;
168+
#if PY_MAJOR_VERSION >= 3
169+
wchar_t *argv[] = { L"caja", NULL };
170+
#else
162171
char *argv[] = { "caja", NULL };
172+
#endif
163173

164174
if (Py_IsInitialized())
165175
return TRUE;

0 commit comments

Comments
 (0)