From 15c3b04f4257fef54ca5f7ab60012b4936d672a3 Mon Sep 17 00:00:00 2001 From: David Malcolm Date: Fri, 16 Apr 2010 12:57:15 -0400 Subject: Fix various selftests when building against python 3.1 --- gio/gio.override | 4 ++-- glib/pygiochannel.c | 2 +- glib/pygoptiongroup.c | 25 +++++++++++++++++++-- gobject/pygobject.h | 7 ++++++ gobject/pygtype.c | 9 -------- tests/common.py | 9 ++++++++ tests/test_gio.py | 61 ++++++++++++++++++++++++--------------------------- 7 files changed, 71 insertions(+), 46 deletions(-) diff --git a/gio/gio.override b/gio/gio.override index 86c7f33..c9a40fd 100644 --- a/gio/gio.override +++ b/gio/gio.override @@ -330,12 +330,12 @@ _wrap_g_content_type_guess(PyGObject *self, PyObject *args, PyObject *kwargs) { char *kwlist[] = {"filename", "data", "want_uncertain", NULL}; char *filename = NULL, *data = NULL, *type; - int data_size = 0; + Py_ssize_t data_size = 0; gboolean result_uncertain, want_uncertain = FALSE; PyObject *ret; if (!PyArg_ParseTupleAndKeywords (args, kwargs, - "|zz#i:g_content_type_guess", + "|zz#i:content_type_guess", kwlist, &filename, &data, &data_size, &want_uncertain)) diff --git a/glib/pygiochannel.c b/glib/pygiochannel.c index 544d3dd..45ccf52 100644 --- a/glib/pygiochannel.c +++ b/glib/pygiochannel.c @@ -279,7 +279,7 @@ static PyObject* py_io_channel_write_lines(PyGIOChannel* self, PyObject *args, PyObject *kwargs) { static char *kwlist[] = { "lines", NULL }; - char *buf; + char *buf = NULL; Py_ssize_t buf_len; gsize count; GError* error = NULL; diff --git a/glib/pygoptiongroup.c b/glib/pygoptiongroup.c index ea3830b..58b9dad 100644 --- a/glib/pygoptiongroup.c +++ b/glib/pygoptiongroup.c @@ -165,6 +165,11 @@ pyg_option_group_add_entries(PyGOptionGroup *self, PyObject *args, for (pos = 0; pos < entry_count; pos++) { gchar *long_name, *description, *arg_description; +#if PY_VERSION_HEX >= 0x03000000 + int short_name; +#else + char short_name; +#endif entry_tuple = PyList_GetItem(list, pos); if (!PyTuple_Check(entry_tuple)) { @@ -173,9 +178,14 @@ pyg_option_group_add_entries(PyGOptionGroup *self, PyObject *args, g_free(entries); return NULL; } - if (!PyArg_ParseTuple(entry_tuple, "scisz", + if (!PyArg_ParseTuple(entry_tuple, +#if PY_VERSION_HEX >= 0x03000000 + "sCisz", +#else + "scisz", +#endif &long_name, - &(entries[pos].short_name), + &short_name, &(entries[pos].flags), &description, &arg_description)) @@ -185,6 +195,17 @@ pyg_option_group_add_entries(PyGOptionGroup *self, PyObject *args, g_free(entries); return NULL; } +#if PY_VERSION_HEX >= 0x03000000 + if (short_name > 0xff) { + PyErr_SetString(PyExc_TypeError, + "GOptionGroup.add_entries expected a shortname in the range 0-255"); + return NULL; + } + entries[pos].short_name = short_name; +#else + entries[pos].short_name = short_name; +#endif + long_name = g_strdup(long_name); self->strings = g_slist_prepend(self->strings, long_name); entries[pos].long_name = long_name; diff --git a/gobject/pygobject.h b/gobject/pygobject.h index e3c3df1..8772aa4 100644 --- a/gobject/pygobject.h +++ b/gobject/pygobject.h @@ -377,10 +377,17 @@ pygobject_init(int req_major, int req_minor, int req_micro) } /* deprecated macro, use pygobject_init() instead. */ +#if PY_VERSION_HEX >= 0x03000000 +#define init_pygobject() G_STMT_START { \ + if (!pygobject_init(-1, -1, -1)) \ + return -1; \ +} G_STMT_END +#else #define init_pygobject() G_STMT_START { \ if (!pygobject_init(-1, -1, -1)) \ return; \ } G_STMT_END +#endif /* deprecated macro, use pygobject_init() instead. */ #define init_pygobject_check(req_major, req_minor, req_micro) G_STMT_START { \ diff --git a/gobject/pygtype.c b/gobject/pygtype.c index 3915f8f..71769de 100644 --- a/gobject/pygtype.c +++ b/gobject/pygtype.c @@ -39,15 +39,6 @@ typedef struct { PYGLIB_DEFINE_TYPE("gobject.GType", PyGTypeWrapper_Type, PyGTypeWrapper); -static int -pyg_type_wrapper_compare(PyGTypeWrapper *self, PyGTypeWrapper *v) -{ - if (self->type == v->type) return 0; - if (self->type > v->type) return -1; - return 1; -} - - static PyObject* pyg_type_wrapper_richcompare(PyObject *self, PyObject *other, int op) { diff --git a/tests/common.py b/tests/common.py index b5f5bfc..3c248e1 100644 --- a/tests/common.py +++ b/tests/common.py @@ -59,6 +59,15 @@ else: _bytes = str try: + # Python 2: _unicode = unicode except NameError: + # Python 3: "unicode" became the "str" type _unicode = str + +try: + # Python 2: + _long = long +except NameError: + # Python 3: "long" became the "int" type + _long = int diff --git a/tests/test_gio.py b/tests/test_gio.py index d40cb14..b1c4c4e 100644 --- a/tests/test_gio.py +++ b/tests/test_gio.py @@ -4,12 +4,9 @@ import os import unittest import sys -from common import gio, glib +from common import gio, glib, _bytes, _unicode, _long -try: - _unicode = unicode -except NameError: - _unicode = str +#is_py3k = (sys.version_info >= (3, 0)) class TestFile(unittest.TestCase): def setUp(self): @@ -29,7 +26,7 @@ class TestFile(unittest.TestCase): try: stream = file.read_finish(result) self.failUnless(isinstance(stream, gio.InputStream)) - self.assertEquals(stream.read(), "testing") + self.assertEquals(stream.read(), _bytes("testing")) finally: loop.quit() @@ -340,8 +337,8 @@ class TestFile(unittest.TestCase): loop.quit() def progress(current, total): - self.assert_(isinstance(current, long)) - self.assert_(isinstance(total, long)) + self.assert_(isinstance(current, _long)) + self.assert_(isinstance(total, _long)) self.assert_(0 <= current <= total) try: @@ -361,8 +358,8 @@ class TestFile(unittest.TestCase): destination = gio.File('copy.txt') def progress(current, total): - self.assert_(isinstance(current, long)) - self.assert_(isinstance(total, long)) + self.assert_(isinstance(current, _long)) + self.assert_(isinstance(total, _long)) self.assert_(0 <= current <= total) try: @@ -594,10 +591,10 @@ class TestInputStream(unittest.TestCase): os.unlink("inputstream.txt") def testRead(self): - self.assertEquals(self.stream.read(), "testing") + self.assertEquals(self.stream.read(), _bytes("testing")) self.stream = gio.MemoryInputStream() - self.assertEquals(self.stream.read(), '') + self.assertEquals(self.stream.read(), _bytes('')) self.stream = gio.MemoryInputStream() some_data = open("test_gio.py", "rb").read() @@ -614,7 +611,7 @@ class TestInputStream(unittest.TestCase): def testSkip(self): self.stream.skip(2) res = self.stream.read() - self.assertEqual(res, "sting") + self.assertEqual(res, _bytes("sting")) def testSkipAsync(self): def callback(stream, result): @@ -622,7 +619,7 @@ class TestInputStream(unittest.TestCase): size = stream.skip_finish(result) self.assertEqual(size, 2) res = stream.read() - self.assertEqual(res, "sting") + self.assertEqual(res, _bytes("sting")) finally: loop.quit() @@ -634,7 +631,7 @@ class TestInputStream(unittest.TestCase): def test_read_part(self): self.assertEquals(self._read_in_loop(self.stream, lambda: self.stream.read_part()), - 'testing') + _bytes('testing')) stream = gio.MemoryInputStream() some_data = open('test_gio.py', 'rb').read() @@ -645,7 +642,7 @@ class TestInputStream(unittest.TestCase): some_data) def _read_in_loop(self, stream, reader, size_limit=0): - read_data = '' + read_data = _bytes('') while True: read_part = reader() if read_part: @@ -661,7 +658,7 @@ class TestInputStream(unittest.TestCase): self.assertEquals(result.get_op_res_gssize(), 7) try: data = stream.read_finish(result) - self.assertEquals(data, "testing") + self.assertEquals(data, _bytes("testing")) stream.close() finally: loop.quit() @@ -722,7 +719,7 @@ class TestDataInputStream(unittest.TestCase): self.data_stream = gio.DataInputStream(self.base_stream) def test_read_line(self): - self.base_stream.add_data('foo\nbar\n\nbaz') + self.base_stream.add_data(_bytes('foo\nbar\n\nbaz')) self.assertEquals('foo', self.data_stream.read_line()) self.assertEquals('bar', self.data_stream.read_line()) self.assertEquals('', self.data_stream.read_line()) @@ -743,14 +740,14 @@ class TestDataInputStream(unittest.TestCase): loop.run() return line[0] - self.base_stream.add_data('foo\nbar\n\nbaz') + self.base_stream.add_data(_bytes('foo\nbar\n\nbaz')) self.assertEquals('foo', do_read_line_async()) self.assertEquals('bar', do_read_line_async()) self.assertEquals('', do_read_line_async()) self.assertEquals('baz', do_read_line_async()) def test_read_until(self): - self.base_stream.add_data('sentence.end of line\nthe rest') + self.base_stream.add_data(_bytes('sentence.end of line\nthe rest')) self.assertEquals('sentence', self.data_stream.read_until('.!?')) self.assertEquals('end of line', self.data_stream.read_until('\n\r')) self.assertEquals('the rest', self.data_stream.read_until('#$%^&')) @@ -772,7 +769,7 @@ class TestDataInputStream(unittest.TestCase): # Note the weird difference between synchronous and # asynchronous version. See bug #584284. - self.base_stream.add_data('sentence.end of line\nthe rest') + self.base_stream.add_data(_bytes('sentence.end of line\nthe rest')) self.assertEquals('sentence', do_read_until_async('.!?')) self.assertEquals('.end of line', do_read_until_async('\n\r')) self.assertEquals('\nthe rest', do_read_until_async('#$%^&')) @@ -783,17 +780,17 @@ class TestMemoryInputStream(unittest.TestCase): self.stream = gio.MemoryInputStream() def test_add_data(self): - self.stream.add_data('foobar') - self.assertEquals('foobar', self.stream.read()) + self.stream.add_data(_bytes('foobar')) + self.assertEquals(_bytes('foobar'), self.stream.read()) - self.stream.add_data('ham ') + self.stream.add_data(_bytes('ham ')) self.stream.add_data(None) - self.stream.add_data('spam') - self.assertEquals('ham spam', self.stream.read()) + self.stream.add_data(_bytes('spam')) + self.assertEquals(_bytes('ham spam'), self.stream.read()) def test_new_from_data(self): - stream = gio.memory_input_stream_new_from_data('spam') - self.assertEquals('spam', stream.read()) + stream = gio.memory_input_stream_new_from_data(_bytes('spam')) + self.assertEquals(_bytes('spam'), stream.read()) class TestOutputStream(unittest.TestCase): @@ -919,10 +916,10 @@ class TestMemoryOutputStream(unittest.TestCase): def test_get_contents(self): self.stream.write('foobar') - self.assertEquals('foobar', self.stream.get_contents()) + self.assertEquals(_bytes('foobar'), self.stream.get_contents()) self.stream.write('baz') - self.assertEquals('foobarbaz', self.stream.get_contents()) + self.assertEquals(_bytes('foobarbaz'), self.stream.get_contents()) class TestVolumeMonitor(unittest.TestCase): @@ -957,12 +954,12 @@ class TestContentTypeGuess(unittest.TestCase): self.assertEquals('image/svg+xml', mime_type) def testFromContents(self): - mime_type = gio.content_type_guess(data='') + mime_type = gio.content_type_guess(data=_bytes('')) self.assertEquals('text/html', mime_type) def testFromContentsUncertain(self): mime_type, result_uncertain = gio.content_type_guess( - data='', want_uncertain=True) + data=_bytes(''), want_uncertain=True) self.assertEquals('text/html', mime_type) self.assertEquals(bool, type(result_uncertain)) -- cgit