diff options
author | Johan Dahlin <johan@gnome.org> | 2008-07-26 13:23:28 +0000 |
---|---|---|
committer | Johan Dahlin <johan@src.gnome.org> | 2008-07-26 13:23:28 +0000 |
commit | e889e0030877bb0b3a7e83f945b8b07230973c01 (patch) | |
tree | d8dd4045f066a3e83dac29435102eeaeb4ad7986 | |
parent | 83627fd48cdd1a39eec874627ef97ed560e231b8 (diff) | |
download | pygobject-e889e0030877bb0b3a7e83f945b8b07230973c01.tar.gz pygobject-e889e0030877bb0b3a7e83f945b8b07230973c01.tar.xz pygobject-e889e0030877bb0b3a7e83f945b8b07230973c01.zip |
Add macros for supporting additional python versions. Start using them for
2008-07-26 Johan Dahlin <johan@gnome.org>
* glib/glibmodule.c (get_handler_priority), (pyglib_idle_add),
(pyglib_timeout_add), (pyglib_timeout_add_seconds),
(pyglib_io_add_watch), (pyglib_child_watch_add),
(pyglib_markup_escape_text), (pyglib_main_depth),
(pyglib_filename_from_utf8), (pyglib_get_application_name),
(pyglib_get_prgname), (PYGLIB_MODULE_START):
* glib/pygiochannel.c (py_io_channel_next),
(py_io_channel_shutdown), (py_io_channel_get_buffer_size),
(py_io_channel_get_buffered), (py_io_channel_get_encoding),
(py_io_channel_read_chars), (py_io_channel_write_chars),
(py_io_channel_write_lines), (py_io_channel_flush),
(py_io_channel_set_flags), (py_io_channel_get_flags),
(py_io_channel_get_buffer_condition), (py_io_channel_win32_poll),
(py_io_channel_read_line), (py_io_channel_read_lines),
(py_io_channel_seek), (pyglib_iochannel_register_types):
* glib/pyglib-private.h:
* glib/pyglib-python-compat.h:
* glib/pyglib.c (pyglib_init), (pyglib_error_check),
(pyglib_gerror_exception_check),
(pyglib_register_exception_for_domain):
Add macros for supporting additional python versions.
Start using them for the glib module. Tested on python 2.5 and 3.0.
svn path=/trunk/; revision=870
-rw-r--r-- | ChangeLog | 25 | ||||
-rw-r--r-- | glib/Makefile.am | 9 | ||||
-rw-r--r-- | glib/glibmodule.c | 39 | ||||
-rw-r--r-- | glib/pygiochannel.c | 106 | ||||
-rw-r--r-- | glib/pyglib-private.h | 17 | ||||
-rw-r--r-- | glib/pyglib-python-compat.h | 110 | ||||
-rw-r--r-- | glib/pyglib.c | 22 |
7 files changed, 206 insertions, 122 deletions
@@ -1,5 +1,30 @@ 2008-07-26 Johan Dahlin <johan@gnome.org> + * glib/glibmodule.c (get_handler_priority), (pyglib_idle_add), + (pyglib_timeout_add), (pyglib_timeout_add_seconds), + (pyglib_io_add_watch), (pyglib_child_watch_add), + (pyglib_markup_escape_text), (pyglib_main_depth), + (pyglib_filename_from_utf8), (pyglib_get_application_name), + (pyglib_get_prgname), (PYGLIB_MODULE_START): + * glib/pygiochannel.c (py_io_channel_next), + (py_io_channel_shutdown), (py_io_channel_get_buffer_size), + (py_io_channel_get_buffered), (py_io_channel_get_encoding), + (py_io_channel_read_chars), (py_io_channel_write_chars), + (py_io_channel_write_lines), (py_io_channel_flush), + (py_io_channel_set_flags), (py_io_channel_get_flags), + (py_io_channel_get_buffer_condition), (py_io_channel_win32_poll), + (py_io_channel_read_line), (py_io_channel_read_lines), + (py_io_channel_seek), (pyglib_iochannel_register_types): + * glib/pyglib-private.h: + * glib/pyglib-python-compat.h: + * glib/pyglib.c (pyglib_init), (pyglib_error_check), + (pyglib_gerror_exception_check), + (pyglib_register_exception_for_domain): + Add macros for supporting additional python versions. + Start using them for the glib module. Tested on python 2.5 and 3.0. + +2008-07-26 Johan Dahlin <johan@gnome.org> + * configure.ac: * m4/python.m4: Add macros which are compatible with python > 2.6. diff --git a/glib/Makefile.am b/glib/Makefile.am index e5be61f..8a8c5f3 100644 --- a/glib/Makefile.am +++ b/glib/Makefile.am @@ -21,10 +21,11 @@ endif libpyglib_2_0_la_CFLAGS = $(GLIB_CFLAGS) libpyglib_2_0_la_LIBADD = $(GLIB_LIBS) $(FFI_LIBS) -libpyglib_2_0_la_SOURCES = \ - pyglib.c \ - pyglib.h \ - pyglib-private.h +libpyglib_2_0_la_SOURCES = \ + pyglib.c \ + pyglib.h \ + pyglib-private.h \ + pyglib-python-compat.h _glib_la_CFLAGS = $(GLIB_CFLAGS) _glib_la_LDFLAGS = $(common_ldflags) -export-symbols-regex init_glib diff --git a/glib/glibmodule.c b/glib/glibmodule.c index dc7afca..9228057 100644 --- a/glib/glibmodule.c +++ b/glib/glibmodule.c @@ -68,19 +68,19 @@ get_handler_priority(gint *priority, PyObject *kwargs) } pos = 0; PyDict_Next(kwargs, &pos, &key, &val); - if (!PyString_Check(key)) { + if (!_PyUnicode_Check(key)) { PyErr_SetString(PyExc_TypeError, "keyword argument name is not a string"); return -1; } - if (strcmp(PyString_AsString(key), "priority") != 0) { + if (strcmp(_PyUnicode_AsString(key), "priority") != 0) { PyErr_SetString(PyExc_TypeError, "only 'priority' keyword argument accepted"); return -1; } - *priority = PyInt_AsLong(val); + *priority = _PyLong_AsLong(val); if (PyErr_Occurred()) { PyErr_Clear(); PyErr_SetString(PyExc_ValueError, "could not get priority value"); @@ -125,7 +125,7 @@ pyglib_idle_add(PyObject *self, PyObject *args, PyObject *kwargs) handler_id = g_idle_add_full(priority, _pyglib_handler_marshal, data, _pyglib_destroy_notify); - return PyInt_FromLong(handler_id); + return _PyLong_FromLong(handler_id); } @@ -165,7 +165,7 @@ pyglib_timeout_add(PyObject *self, PyObject *args, PyObject *kwargs) handler_id = g_timeout_add_full(priority, interval, _pyglib_handler_marshal, data, _pyglib_destroy_notify); - return PyInt_FromLong(handler_id); + return _PyLong_FromLong(handler_id); } static PyObject * @@ -204,7 +204,7 @@ pyglib_timeout_add_seconds(PyObject *self, PyObject *args, PyObject *kwargs) handler_id = g_timeout_add_seconds_full(priority, interval, _pyglib_handler_marshal, data, _pyglib_destroy_notify); - return PyInt_FromLong(handler_id); + return _PyLong_FromLong(handler_id); } static gboolean @@ -294,7 +294,7 @@ pyglib_io_add_watch(PyObject *self, PyObject *args, PyObject *kwargs) (GDestroyNotify)_pyglib_destroy_notify); g_io_channel_unref(iochannel); - return PyInt_FromLong(handler_id); + return _PyLong_FromLong(handler_id); } static PyObject * @@ -374,7 +374,7 @@ pyglib_child_watch_add(PyObject *unused, PyObject *args, PyObject *kwargs) Py_INCREF(child_data->data); id = g_child_watch_add_full(priority, pid, child_watch_func, child_data, child_watch_dnotify); - return PyInt_FromLong(id); + return _PyLong_FromLong(id); } static PyObject * @@ -391,7 +391,7 @@ pyglib_markup_escape_text(PyObject *unused, PyObject *args, PyObject *kwargs) return NULL; text_out = g_markup_escape_text(text_in, text_size); - retval = PyString_FromString(text_out); + retval = _PyUnicode_FromString(text_out); g_free(text_out); return retval; } @@ -410,7 +410,7 @@ pyglib_get_current_time(PyObject *unused) static PyObject * pyglib_main_depth(PyObject *unused) { - return PyInt_FromLong(g_main_depth()); + return _PyLong_FromLong(g_main_depth()); } static PyObject * @@ -463,7 +463,7 @@ pyglib_filename_from_utf8(PyObject *self, PyObject *args) g_free(filename); return NULL; } - py_filename = PyString_FromStringAndSize(filename, bytes_written); + py_filename = _PyUnicode_FromStringAndSize(filename, bytes_written); g_free(filename); return py_filename; } @@ -479,7 +479,7 @@ pyglib_get_application_name(PyObject *self) Py_INCREF(Py_None); return Py_None; } - return PyString_FromString(name); + return _PyUnicode_FromString(name); } static PyObject* @@ -504,7 +504,7 @@ pyglib_get_prgname(PyObject *self) Py_INCREF(Py_None); return Py_None; } - return PyString_FromString(name); + return _PyUnicode_FromString(name); } static PyObject* @@ -520,7 +520,7 @@ pyglib_set_prgname(PyObject *self, PyObject *args) } -static PyMethodDef pyglib_functions[] = { +static PyMethodDef _glib_functions[] = { { "spawn_async", (PyCFunction)pyglib_spawn_async, METH_VARARGS|METH_KEYWORDS }, { "main_context_default", @@ -710,15 +710,11 @@ pyglib_register_constants(PyObject *m) (char*) g_quark_to_string(G_OPTION_ERROR)); } -DL_EXPORT(void) -init_glib(void) +PYGLIB_MODULE_START(_glib, "glib._glib") { - PyObject *m, *d; + PyObject *d = PyModule_GetDict(module); - m = Py_InitModule("glib._glib", pyglib_functions); - pyglib_register_constants(m); - - d = PyModule_GetDict(m); + pyglib_register_constants(module); pyglib_register_api(d); pyglib_register_error(d); pyglib_register_version_tuples(d); @@ -730,3 +726,4 @@ init_glib(void) pyglib_option_context_register_types(d); pyglib_option_group_register_types(d); } +PYGLIB_MODULE_END diff --git a/glib/pygiochannel.c b/glib/pygiochannel.c index 697f91e..118bb7a 100644 --- a/glib/pygiochannel.c +++ b/glib/pygiochannel.c @@ -17,6 +17,8 @@ typedef struct { int softspace; /* to make print >> chan, "foo" ... work */ } PyGIOChannel; +PYGLIB_DEFINE_TYPE("gobject.IOChannel", PyGIOChannel_Type, PyGIOChannel) + static PyObject* py_io_channel_next(PyGIOChannel *self) { @@ -36,10 +38,9 @@ py_io_channel_next(PyGIOChannel *self) return NULL; } - ret_obj = PyString_FromStringAndSize(str_return, length); + ret_obj = _PyUnicode_FromStringAndSize(str_return, length); g_free(str_return); return ret_obj; - } static int @@ -86,7 +87,7 @@ py_io_channel_shutdown(PyGIOChannel* self, PyObject *args, PyObject *kwargs) if (pyglib_error_check(&error)) return NULL; - return PyInt_FromLong(ret); + return _PyLong_FromLong(ret); } /* character encoding conversion involved functions. @@ -118,7 +119,7 @@ py_io_channel_get_buffer_size(PyGIOChannel* self, PyObject *args, PyObject *kwar size = g_io_channel_get_buffer_size(self->channel); - return PyInt_FromLong(size); + return _PyLong_FromLong(size); } static PyObject* @@ -147,7 +148,7 @@ py_io_channel_get_buffered(PyGIOChannel* self, PyObject *args, PyObject *kwargs) buffered = g_io_channel_get_buffered(self->channel); - return PyInt_FromLong(buffered); + return _PyLong_FromLong(buffered); } static PyObject* @@ -184,7 +185,7 @@ py_io_channel_get_encoding(PyGIOChannel* self, PyObject *args, PyObject *kwargs) return Py_None; } - return PyString_FromString(encoding); + return _PyUnicode_FromString(encoding); } #define CHUNK_SIZE (8 * 1024) @@ -203,7 +204,7 @@ py_io_channel_read_chars(PyGIOChannel* self, PyObject *args, PyObject *kwargs) return NULL; if (max_count == 0) - return PyString_FromString(""); + return _PyUnicode_FromString(""); while (status == G_IO_STATUS_NORMAL && (max_count == -1 || total_read < max_count)) { @@ -220,16 +221,16 @@ py_io_channel_read_chars(PyGIOChannel* self, PyObject *args, PyObject *kwargs) } if ( ret_obj == NULL ) { - ret_obj = PyString_FromStringAndSize((char *)NULL, buf_size); + ret_obj = _PyUnicode_FromStringAndSize((char *)NULL, buf_size); if (ret_obj == NULL) goto failure; } - else if (buf_size + total_read > PyString_GET_SIZE(ret_obj)) { - if (_PyString_Resize(&ret_obj, buf_size + total_read) == -1) + else if (buf_size + total_read > _PyUnicode_GET_SIZE(ret_obj)) { + if (_PyUnicode_Resize(&ret_obj, buf_size + total_read) == -1) goto failure; } - buf = PyString_AS_STRING(ret_obj) + total_read; + buf = _PyUnicode_AS_STRING(ret_obj) + total_read; pyglib_unblock_threads(); status = g_io_channel_read_chars(self->channel, buf, buf_size, @@ -241,8 +242,8 @@ py_io_channel_read_chars(PyGIOChannel* self, PyObject *args, PyObject *kwargs) total_read += single_read; } - if ( total_read != PyString_GET_SIZE(ret_obj) ) { - if (_PyString_Resize(&ret_obj, total_read) == -1) + if ( total_read != _PyUnicode_GET_SIZE(ret_obj) ) { + if (_PyUnicode_Resize(&ret_obj, total_read) == -1) goto failure; } return ret_obj; @@ -272,7 +273,7 @@ py_io_channel_write_chars(PyGIOChannel* self, PyObject *args, PyObject *kwargs) if (pyglib_error_check(&error)) return NULL; - return PyInt_FromLong(count); + return _PyLong_FromLong(count); } static PyObject* @@ -298,13 +299,13 @@ py_io_channel_write_lines(PyGIOChannel* self, PyObject *args, PyObject *kwargs) PyErr_Clear(); goto normal_exit; } - if (!PyString_Check(value)) { + if (!_PyUnicode_Check(value)) { PyErr_SetString(PyExc_TypeError, "gobject.IOChannel.writelines must" " be sequence/iterator of strings"); Py_DECREF(iter); return NULL; } - PyString_AsStringAndSize(value, &buf, &buf_len); + _PyUnicode_AsStringAndSize(value, &buf, &buf_len); pyglib_unblock_threads(); status = g_io_channel_write_chars(self->channel, buf, buf_len, &count, &error); pyglib_unblock_threads(); @@ -337,7 +338,7 @@ py_io_channel_flush(PyGIOChannel* self, PyObject *args, PyObject *kwargs) if (pyglib_error_check(&error)) return NULL; - return PyInt_FromLong(status); + return _PyLong_FromLong(status); } static PyObject* @@ -356,7 +357,7 @@ py_io_channel_set_flags(PyGIOChannel* self, PyObject *args, PyObject *kwargs) if (pyglib_error_check(&error)) return NULL; - return PyInt_FromLong(status); + return _PyLong_FromLong(status); } static PyObject* @@ -370,7 +371,7 @@ py_io_channel_get_flags(PyGIOChannel* self, PyObject *args, PyObject *kwargs) return NULL; flags = g_io_channel_get_flags(self->channel); - return PyInt_FromLong(flags); + return _PyLong_FromLong(flags); } static PyObject* @@ -384,7 +385,7 @@ py_io_channel_get_buffer_condition(PyGIOChannel* self, PyObject *args, PyObject return NULL; cond = g_io_channel_get_buffer_condition(self->channel); - return PyInt_FromLong(cond); + return _PyLong_FromLong(cond); } static PyObject* @@ -535,7 +536,7 @@ py_io_channel_win32_poll(PyObject *self, PyObject *args, PyObject *kwargs) pyfd = PyList_GET_ITEM(pyfds, i); ((PyGPollFD *) pyfd)->pollfd = pollfd[i]; } - return PyInt_FromLong(result); + return _PyLong_FromLong(result); } static PyObject * @@ -579,7 +580,7 @@ py_io_channel_read_line(PyGIOChannel* self, PyObject *args, PyObject *kwargs) &terminator_pos, &error); if (pyglib_error_check(&error)) return NULL; - ret_obj = PyString_FromStringAndSize(str_return, length); + ret_obj = _PyUnicode_FromStringAndSize(str_return, length); g_free(str_return); return ret_obj; } @@ -608,7 +609,7 @@ py_io_channel_read_lines(PyGIOChannel* self, PyObject *args, PyObject *kwargs) Py_DECREF(line); return NULL; } - line = PyString_FromStringAndSize(str_return, length); + line = _PyUnicode_FromStringAndSize(str_return, length); g_free(str_return); if (PyList_Append(list, line)) { Py_DECREF(line); @@ -649,7 +650,7 @@ py_io_channel_seek(PyGIOChannel* self, PyObject *args, PyObject *kwargs) if (pyglib_error_check(&error)) return NULL; - return PyInt_FromLong(status); + return _PyLong_FromLong(status); } #if 0 // Not wrapped @@ -764,55 +765,18 @@ py_io_channel_init(PyGIOChannel *self, PyObject *args, PyObject *kwargs) return 0; } - -PyTypeObject PyGIOChannel_Type = { - PyObject_HEAD_INIT(NULL) - 0, /* ob_size */ - "gobject.IOChannel", /* tp_name */ - sizeof(PyGIOChannel), /* tp_basicsize */ - 0, /* tp_itemsize */ - /* methods */ - (destructor)py_io_channel_dealloc, /* tp_dealloc */ - (printfunc)0, /* tp_print */ - (getattrfunc)NULL, /* tp_getattr */ - (setattrfunc)0, /* tp_setattr */ - (cmpfunc)py_io_channel_compare, /* tp_compare */ - (reprfunc)0, /* tp_repr */ - 0, /* tp_as_number */ - 0, /* tp_as_sequence */ - 0, /* tp_as_mapping */ - (hashfunc)py_io_channel_hash, /* tp_hash */ - (ternaryfunc)0, /* tp_call */ - (reprfunc)0, /* tp_str */ - (getattrofunc)0, /* tp_getattro */ - (setattrofunc)0, /* tp_setattro */ - 0, /* tp_as_buffer */ - Py_TPFLAGS_DEFAULT, /* tp_flags */ - NULL, /* Documentation string */ - (traverseproc)0, /* tp_traverse */ - (inquiry)0, /* tp_clear */ - (richcmpfunc)0, /* tp_richcompare */ - 0, /* tp_weaklistoffset */ - (getiterfunc)py_io_channel_get_iter,/* tp_iter */ - (iternextfunc)py_io_channel_next, /* tp_iternext */ - py_io_channel_methods, /* tp_methods */ - py_io_channel_members, /* tp_members */ - NULL, /* tp_getset */ - (PyTypeObject *)0, /* tp_base */ - (PyObject *)0, /* tp_dict */ - 0, /* tp_descr_get */ - 0, /* tp_descr_set */ - 0, /* tp_dictoffset */ - (initproc)py_io_channel_init, /* tp_init */ - (allocfunc)0, /* tp_alloc */ - (newfunc)0, /* tp_new */ - 0, /* tp_free */ - (inquiry)0, /* tp_is_gc */ - (PyObject *)0, /* tp_bases */ -}; - void pyglib_iochannel_register_types(PyObject *d) { + PyGIOChannel_Type.tp_init = (initproc)py_io_channel_init; + PyGIOChannel_Type.tp_dealloc = (destructor)py_io_channel_dealloc; + PyGIOChannel_Type.tp_flags = Py_TPFLAGS_DEFAULT; + PyGIOChannel_Type.tp_members = py_io_channel_members; + PyGIOChannel_Type.tp_methods = py_io_channel_methods; + PyGIOChannel_Type.tp_hash = (hashfunc)py_io_channel_hash; + PyGIOChannel_Type.tp_compare = (cmpfunc)py_io_channel_compare; + PyGIOChannel_Type.tp_iter = (getiterfunc)py_io_channel_get_iter; + PyGIOChannel_Type.tp_iternext = (iternextfunc)py_io_channel_next; + PYGLIB_REGISTER_TYPE(d, PyGIOChannel_Type, "IOChannel"); } diff --git a/glib/pyglib-private.h b/glib/pyglib-private.h index dd0b2ce..21488b2 100644 --- a/glib/pyglib-private.h +++ b/glib/pyglib-private.h @@ -25,12 +25,9 @@ #include <Python.h> #include <glib.h> -G_BEGIN_DECLS +#include <pyglib-python-compat.h> -/* Compilation on Python 2.4 */ -#if PY_VERSION_HEX < 0x02050000 -typedef int Py_ssize_t; -#endif +G_BEGIN_DECLS struct _PyGLib_Functions { gboolean threads_enabled; @@ -39,16 +36,6 @@ struct _PyGLib_Functions { PyGLibThreadBlockFunc unblock_threads; }; -#define PYGLIB_REGISTER_TYPE(d, type, name) \ - type.ob_type = &PyType_Type; \ - if (!type.tp_alloc) \ - type.tp_alloc = PyType_GenericAlloc; \ - if (!type.tp_new) \ - type.tp_new = PyType_GenericNew; \ - if (PyType_Ready(&type)) \ - return; \ - PyDict_SetItemString(d, name, (PyObject *)&type); - gboolean _pyglib_handler_marshal(gpointer user_data); void _pyglib_destroy_notify(gpointer user_data); diff --git a/glib/pyglib-python-compat.h b/glib/pyglib-python-compat.h new file mode 100644 index 0000000..ccf2b85 --- /dev/null +++ b/glib/pyglib-python-compat.h @@ -0,0 +1,110 @@ +/* -*- Mode: C; c-basic-offset: 4 -*- + * pyglib - Python bindings for GLib toolkit. + * Copyright (C) 2008 Johan Dahlin + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 + * USA + */ + +#ifndef __PYGLIB_PYTHON_COMPAT_H__ +#define __PYGLIB_PYTHON_COMPAT_H__ + +/* Compilation on Python 2.4 */ +#if PY_VERSION_HEX < 0x02050000 +typedef int Py_ssize_t; +#endif + +/* Compilation on Python 2.x */ +#if PY_VERSION_HEX < 0x03000000 +#define PYGLIB_MODULE_START(symbol, modname) \ +DL_EXPORT(void) init##symbol(void) \ +{ \ + PyObject *module; \ + module = Py_InitModule(modname, symbol##_functions); +#define PYGLIB_MODULE_END } +#define PYGLIB_DEFINE_TYPE(typename, symbol, csymbol) \ +PyTypeObject symbol = { \ + PyObject_HEAD_INIT(NULL) \ + 0, \ + typename, \ + sizeof(csymbol) \ +}; +#define PYGLIB_REGISTER_TYPE(d, type, name) \ + if (!type.tp_alloc) \ + type.tp_alloc = PyType_GenericAlloc; \ + if (!type.tp_new) \ + type.tp_new = PyType_GenericNew; \ + if (PyType_Ready(&type)) \ + return; \ + PyDict_SetItemString(d, name, (PyObject *)&type); + +#define _PyUnicode_Check PyString_Check +#define _PyUnicode_AsString PyString_AsString +#define _PyUnicode_AsStringAndSize PyString_AsStringAndSize +#define _PyUnicode_FromString PyString_FromString +#define _PyUnicode_FromStringAndSize PyString_FromStringAndSize +#define _PyUnicode_AS_STRING PyString_AS_STRING +#define _PyUnicode_GET_SIZE PyString_GET_SIZE +#define _PyUnicode_Resize _PyString_Resize +#define _PyLong_Check PyInt_Check +#define _PyLong_FromLong PyInt_FromLong +#define _PyLong_AsLong PyInt_AsLong +#else +#define PYGLIB_MODULE_START(symbol, modname) \ + static struct PyModuleDef _##symbol##module = { \ + PyModuleDef_HEAD_INIT, \ + modname, \ + NULL, \ + -1, \ + symbol##_functions, \ + NULL, \ + NULL, \ + NULL, \ + NULL \ +}; \ +PyMODINIT_FUNC PyInit_##symbol(void) \ +{ \ + PyObject *module; \ + module = PyModule_Create(&_##symbol##module); +#define PYGLIB_MODULE_END return module; } +#define PYGLIB_DEFINE_TYPE(typename, symbol, csymbol) \ +PyTypeObject symbol = { \ + PyVarObject_HEAD_INIT(NULL, 0) \ + typename, \ + sizeof(csymbol) \ +}; +#define PYGLIB_REGISTER_TYPE(d, type, name) \ + if (!type.tp_alloc) \ + type.tp_alloc = PyType_GenericAlloc; \ + if (!type.tp_new) \ + type.tp_new = PyType_GenericNew; \ + if (PyType_Ready(&type)) \ + return; \ + PyDict_SetItemString(d, name, (PyObject *)&type); + +#define _PyUnicode_Check PyUnicode_Check +#define _PyUnicode_AsString PyUnicode_AsString +#define _PyUnicode_AsStringAndSize(obj, buf, size) PyUnicode_AsStringAndSize(obj, size) +#define _PyUnicode_FromString PyUnicode_FromString +#define _PyUnicode_FromStringAndSize PyUnicode_FromStringAndSize +#define _PyUnicode_AS_STRING _PyUnicode_AsString +#define _PyUnicode_GET_SIZE PyUnicode_GET_SIZE +#define _PyUnicode_Resize PyUnicode_Resize +#define _PyLong_Check PyLong_Check +#define _PyLong_FromLong PyLong_FromLong +#define _PyLong_AsLong PyLong_AsLong +#endif + +#endif /* __PYGLIB_PYTHON_COMPAT_H__ */ diff --git a/glib/pyglib.c b/glib/pyglib.c index ac9a45b..ac6ec42 100644 --- a/glib/pyglib.c +++ b/glib/pyglib.c @@ -61,7 +61,7 @@ pyglib_init(void) Py_XDECREF(traceback); PyErr_Format(PyExc_ImportError, "could not import glib (error was: %s)", - PyString_AsString(py_orig_exc)); + _PyUnicode_AsString(py_orig_exc)); Py_DECREF(py_orig_exc); } else PyErr_SetString(PyExc_ImportError, @@ -232,23 +232,23 @@ pyglib_error_check(GError **error) if (exception_table != NULL) { PyObject *item; - item = PyDict_GetItem(exception_table, PyInt_FromLong((*error)->domain)); + item = PyDict_GetItem(exception_table, _PyLong_FromLong((*error)->domain)); if (item != NULL) exc_type = item; } exc_instance = PyObject_CallFunction(exc_type, "z", (*error)->message); PyObject_SetAttrString(exc_instance, "domain", - d=PyString_FromString(g_quark_to_string((*error)->domain))); + d=_PyUnicode_FromString(g_quark_to_string((*error)->domain))); Py_DECREF(d); PyObject_SetAttrString(exc_instance, "code", - d=PyInt_FromLong((*error)->code)); + d=_PyLong_FromLong((*error)->code)); Py_DECREF(d); if ((*error)->message) { PyObject_SetAttrString(exc_instance, "message", - d=PyString_FromString((*error)->message)); + d=_PyUnicode_FromString((*error)->message)); Py_DECREF(d); } else { PyObject_SetAttrString(exc_instance, "message", Py_None); @@ -301,28 +301,28 @@ pyglib_gerror_exception_check(GError **error) Py_XDECREF(traceback); py_message = PyObject_GetAttrString(value, "message"); - if (!py_message || !PyString_Check(py_message)) { + if (!py_message || !_PyUnicode_Check(py_message)) { bad_gerror_message = "gobject.GError instances must have a 'message' string attribute"; goto bad_gerror; } py_domain = PyObject_GetAttrString(value, "domain"); - if (!py_domain || !PyString_Check(py_domain)) { + if (!py_domain || !_PyUnicode_Check(py_domain)) { bad_gerror_message = "gobject.GError instances must have a 'domain' string attribute"; Py_DECREF(py_message); goto bad_gerror; } py_code = PyObject_GetAttrString(value, "code"); - if (!py_code || !PyInt_Check(py_code)) { + if (!py_code || !_PyLong_Check(py_code)) { bad_gerror_message = "gobject.GError instances must have a 'code' int attribute"; Py_DECREF(py_message); Py_DECREF(py_domain); goto bad_gerror; } - g_set_error(error, g_quark_from_string(PyString_AsString(py_domain)), - PyInt_AsLong(py_code), PyString_AsString(py_message)); + g_set_error(error, g_quark_from_string(_PyUnicode_AsString(py_domain)), + _PyLong_AsLong(py_code), _PyUnicode_AsString(py_message)); Py_DECREF(py_message); Py_DECREF(py_code); @@ -360,7 +360,7 @@ pyglib_register_exception_for_domain(gchar *name, exception_table = PyDict_New(); PyDict_SetItem(exception_table, - PyInt_FromLong(error_domain), + _PyLong_FromLong(error_domain), exception); return exception; |