[pygobject] tests: remove python 2.5/3.2 compat code



commit 52693463749f045355a7ddeace033d369fe2e786
Author: Christoph Reiter <creiter src gnome org>
Date:   Sun Apr 2 14:13:08 2017 +0200

    tests: remove python 2.5/3.2 compat code
    
    2.7/3.3 support the u/b prefixes and both have callable()

 gi/overrides/Gtk.py         |    4 +---
 tests/compathelper.py       |   40 ----------------------------------------
 tests/helper.py             |    2 +-
 tests/test_fields.py        |    6 ++----
 tests/test_gi.py            |   26 +++++++++-----------------
 tests/test_glib.py          |   12 +++++-------
 tests/test_iochannel.py     |   21 +++++++++------------
 tests/test_mainloop.py      |    4 +---
 tests/test_overrides_gtk.py |   13 ++++++-------
 tests/test_properties.py    |   29 ++++++++++++-----------------
 10 files changed, 46 insertions(+), 111 deletions(-)
---
diff --git a/gi/overrides/Gtk.py b/gi/overrides/Gtk.py
index 63990ed..08d2612 100644
--- a/gi/overrides/Gtk.py
+++ b/gi/overrides/Gtk.py
@@ -30,10 +30,8 @@ from gi import PyGIDeprecationWarning
 
 if sys.version_info >= (3, 0):
     _basestring = str
-    _callable = lambda c: hasattr(c, '__call__')
 else:
     _basestring = basestring
-    _callable = callable
 
 Gtk = get_introspection_module('Gtk')
 
@@ -91,7 +89,7 @@ def _extract_handler_and_args(obj_or_map, handler_name):
         args = handler[1:]
         handler = handler[0]
 
-    elif not _callable(handler):
+    elif not callable(handler):
         raise TypeError('Handler %s is not a method, function or tuple' % handler)
 
     return handler, args
diff --git a/tests/compathelper.py b/tests/compathelper.py
index d7ad650..d4f4d27 100644
--- a/tests/compathelper.py
+++ b/tests/compathelper.py
@@ -1,5 +1,4 @@
 import sys
-import collections
 
 PY2 = PY3 = False
 
@@ -31,51 +30,12 @@ if sys.version_info >= (3, 0):
     '''
     _basestring = str
 
-    '''
-    for tests that need to write to intefaces that take bytes in
-    python 3
-
-    python 3 has a seperate bytes type for low level functions like os.write
-
-    python 2 treats these as strings
-
-    any tests that need to write a string of bytes should do something like
-    this:
-
-    from compathelper import _bytes
-    os.write(_bytes("hello"))
-    '''
-
-    _bytes = lambda s: s.encode()
-
-    '''
-    for tests that need to write to intefaces that take unicode in
-    python 2
-
-    python 3 strings are unicode encoded as UTF-8 so the unicode object
-    doesn't exist
-
-    python 2 differs between a string an unicode string and you must specify
-    an encoding.  This macro will specify UTF-8 in python 2
-
-    any tests that need to use unicode should do this
-
-    from compathelper import _unicode
-    unicode_string = _unicode('this is a unicode string')
-    '''
-
-    _unicode = lambda s: str(s)
-
-    callable = lambda x: isinstance(x, collections.Callable)
     from io import StringIO
     StringIO
     PY3 = True
 else:
     _long = long
     _basestring = basestring
-    _bytes = str
-    _unicode = lambda s: unicode(s, 'UTF-8')
-    callable = callable
     from StringIO import StringIO
     StringIO
     PY2 = True
diff --git a/tests/helper.py b/tests/helper.py
index 0068945..0d2f204 100644
--- a/tests/helper.py
+++ b/tests/helper.py
@@ -10,7 +10,7 @@ import gi
 from gi import PyGIDeprecationWarning
 from gi.repository import GLib
 
-from compathelper import callable, StringIO
+from compathelper import StringIO
 
 
 ExceptionInfo = namedtuple("ExceptionInfo", ["type", "value", "traceback"])
diff --git a/tests/test_fields.py b/tests/test_fields.py
index def3511..ac09949 100644
--- a/tests/test_fields.py
+++ b/tests/test_fields.py
@@ -8,8 +8,6 @@ from gi.repository import GLib
 from gi.repository import Regress
 from gi.repository import GIMarshallingTests
 
-from compathelper import _unicode
-
 
 class Number(object):
 
@@ -108,8 +106,8 @@ class TestFields(unittest.TestCase):
         s.string_ = "hello"
         self.assertEqual(s.string_, "hello")
 
-        s.string_ = _unicode("hello")
-        self.assertEqual(s.string_, _unicode("hello"))
+        s.string_ = u"hello"
+        self.assertEqual(s.string_, u"hello")
 
         s.string_ = None
         self.assertEqual(s.string_, None)
diff --git a/tests/test_gi.py b/tests/test_gi.py
index 57442a4..173744a 100644
--- a/tests/test_gi.py
+++ b/tests/test_gi.py
@@ -23,18 +23,11 @@ from gi.repository import GObject, GLib, Gio
 
 from gi.repository import GIMarshallingTests
 
-from compathelper import _bytes, _unicode, PY2, PY3
+from compathelper import PY2, PY3
 from helper import capture_exceptions
 
-if PY2:
-    CONSTANT_UTF8 = "const \xe2\x99\xa5 utf8"
-    PY2_UNICODE_UTF8 = unicode(CONSTANT_UTF8, 'UTF-8')
-    CHAR_255 = '\xff'
-else:
-    CONSTANT_UTF8 = "const ♥ utf8"
-    CHAR_255 = bytes([255])
 
-CONSTANT_NUMBER = 42
+CONSTANT_UTF8 = "const ♥ utf8"
 
 
 class Number(object):
@@ -67,7 +60,7 @@ class TestConstant(unittest.TestCase):
         self.assertEqual(CONSTANT_UTF8, GIMarshallingTests.CONSTANT_UTF8)
 
     def test_constant_number(self):
-        self.assertEqual(CONSTANT_NUMBER, GIMarshallingTests.CONSTANT_NUMBER)
+        self.assertEqual(42, GIMarshallingTests.CONSTANT_NUMBER)
 
     def test_min_max_int(self):
         self.assertEqual(GLib.MAXINT32, 2 ** 31 - 1)
@@ -145,7 +138,7 @@ class TestUInt8(unittest.TestCase):
         number = Number(self.MAX)
 
         GIMarshallingTests.uint8_in(number)
-        GIMarshallingTests.uint8_in(CHAR_255)
+        GIMarshallingTests.uint8_in(b'\xff')
 
         number.value += 1
         self.assertRaises(OverflowError, GIMarshallingTests.uint8_in, number)
@@ -427,7 +420,7 @@ class TestInt(unittest.TestCase):
     def test_int_inout(self):
         self.assertEqual(self.MIN, GIMarshallingTests.int_inout_max_min(Number(self.MAX)))
         self.assertEqual(self.MAX, GIMarshallingTests.int_inout_min_max(Number(self.MIN)))
-        self.assertRaises(TypeError, GIMarshallingTests.int_inout_min_max, Number(self.MIN), CONSTANT_NUMBER)
+        self.assertRaises(TypeError, GIMarshallingTests.int_inout_min_max, Number(self.MIN), 42)
 
 
 class TestUInt(unittest.TestCase):
@@ -671,9 +664,9 @@ class TestUtf8(unittest.TestCase):
     def test_utf8_none_in(self):
         GIMarshallingTests.utf8_none_in(CONSTANT_UTF8)
         if sys.version_info < (3, 0):
-            GIMarshallingTests.utf8_none_in(PY2_UNICODE_UTF8)
+            GIMarshallingTests.utf8_none_in(CONSTANT_UTF8.decode("utf-8"))
 
-        self.assertRaises(TypeError, GIMarshallingTests.utf8_none_in, CONSTANT_NUMBER)
+        self.assertRaises(TypeError, GIMarshallingTests.utf8_none_in, 42)
         self.assertRaises(TypeError, GIMarshallingTests.utf8_none_in, None)
 
     def test_utf8_none_out(self):
@@ -700,7 +693,7 @@ class TestFilename(unittest.TestCase):
         shutil.rmtree(self.workdir)
 
     def test_filename_in(self):
-        fname = os.path.join(self.workdir, _unicode('testäø.txt'))
+        fname = os.path.join(self.workdir, u'testäø.txt')
         self.assertRaises(GLib.GError, GLib.file_get_contents, fname)
 
         with open(fname.encode('UTF-8'), 'wb') as f:
@@ -720,7 +713,6 @@ class TestFilename(unittest.TestCase):
 
         dirname = GLib.Dir.make_tmp('testäø.XXXXXX')
         self.assertTrue(os.path.sep + 'testäø.' in dirname, dirname)
-        dirname = _bytes(dirname)
         self.assertTrue(os.path.isdir(dirname))
         os.rmdir(dirname)
 
@@ -916,7 +908,7 @@ class TestArray(unittest.TestCase):
 
     def test_array_uint8_in(self):
         GIMarshallingTests.array_uint8_in(Sequence([97, 98, 99, 100]))
-        GIMarshallingTests.array_uint8_in(_bytes("abcd"))
+        GIMarshallingTests.array_uint8_in(b"abcd")
 
     def test_array_string_in(self):
         GIMarshallingTests.array_string_in(['foo', 'bar'])
diff --git a/tests/test_glib.py b/tests/test_glib.py
index 75d9e6a..78366a9 100644
--- a/tests/test_glib.py
+++ b/tests/test_glib.py
@@ -11,8 +11,6 @@ import subprocess
 from gi.repository import GLib
 from gi import PyGIDeprecationWarning
 
-from compathelper import _unicode, _bytes
-
 
 class TestGLib(unittest.TestCase):
 
@@ -25,11 +23,11 @@ class TestGLib(unittest.TestCase):
         self.assertEqual(GLib.find_program_in_path('non existing'), None)
 
     def test_markup_escape_text(self):
-        self.assertEqual(GLib.markup_escape_text(_unicode('a&bä')), 'a&amp;bä')
-        self.assertEqual(GLib.markup_escape_text(_bytes('a&b\x05')), 'a&amp;b&#x5;')
+        self.assertEqual(GLib.markup_escape_text(u'a&bä'), 'a&amp;bä')
+        self.assertEqual(GLib.markup_escape_text(b'a&b\x05'), 'a&amp;b&#x5;')
 
         # with explicit length argument
-        self.assertEqual(GLib.markup_escape_text(_bytes('a\x05\x01\x02'), 2), 'a&#x5;')
+        self.assertEqual(GLib.markup_escape_text(b'a\x05\x01\x02', 2), 'a&#x5;')
 
     def test_progname(self):
         GLib.set_prgname('moo')
@@ -64,12 +62,12 @@ class TestGLib(unittest.TestCase):
         self.assertEqual(GLib.filename_display_basename('bar/foo'), 'foo')
 
         # this is locale dependent, so we cannot completely verify the result
-        res = GLib.filename_from_utf8(_unicode('aäb'))
+        res = GLib.filename_from_utf8(u'aäb')
         self.assertTrue(isinstance(res, bytes))
         self.assertGreaterEqual(len(res), 3)
 
         # with explicit length argument
-        self.assertEqual(GLib.filename_from_utf8(_unicode('aäb'), 1), b'a')
+        self.assertEqual(GLib.filename_from_utf8(u'aäb', 1), b'a')
 
     def test_uri_extract(self):
         res = GLib.uri_list_extract_uris('''# some comment
diff --git a/tests/test_iochannel.py b/tests/test_iochannel.py
index 241b9e2..95b8d1f 100644
--- a/tests/test_iochannel.py
+++ b/tests/test_iochannel.py
@@ -1,6 +1,5 @@
 # -*- Mode: Python -*-
 # encoding: UTF-8
-from __future__ import unicode_literals
 
 import os
 import unittest
@@ -17,8 +16,6 @@ except ImportError:
 from gi.repository import GLib
 from gi import PyGIDeprecationWarning
 
-from compathelper import _unicode
-
 
 class IOChannel(unittest.TestCase):
     def setUp(self):
@@ -26,7 +23,7 @@ class IOChannel(unittest.TestCase):
 
         self.testutf8 = os.path.join(self.workdir, 'testutf8.txt')
         with open(self.testutf8, 'wb') as f:
-            f.write('''hello ♥ world
+            f.write(u'''hello ♥ world
 second line
 
 À demain!'''.encode('UTF-8'))
@@ -47,11 +44,11 @@ second line
         ch = GLib.IOChannel(filename=self.testutf8)
         self.assertEqual(ch.get_encoding(), 'UTF-8')
         self.assertTrue(ch.get_close_on_unref())
-        self.assertEqual(_unicode(ch.readline()), 'hello ♥ world\n')
+        self.assertEqual(ch.readline(), 'hello ♥ world\n')
         self.assertEqual(ch.get_buffer_condition(), GLib.IOCondition.IN)
         self.assertEqual(ch.readline(), 'second line\n')
         self.assertEqual(ch.readline(), '\n')
-        self.assertEqual(_unicode(ch.readline()), 'À demain!')
+        self.assertEqual(ch.readline(), 'À demain!')
         self.assertEqual(ch.get_buffer_condition(), 0)
         self.assertEqual(ch.readline(), '')
         ch.shutdown(True)
@@ -60,10 +57,10 @@ second line
         ch = GLib.IOChannel(filename=self.testlatin1, mode='r')
         ch.set_encoding('latin1')
         self.assertEqual(ch.get_encoding(), 'latin1')
-        self.assertEqual(_unicode(ch.readline()), 'hellø world\n')
+        self.assertEqual(ch.readline(), 'hellø world\n')
         self.assertEqual(ch.readline(), 'second line\n')
         self.assertEqual(ch.readline(), '\n')
-        self.assertEqual(_unicode(ch.readline()), 'À demain!')
+        self.assertEqual(ch.readline(), 'À demain!')
         ch.shutdown(True)
 
     def test_file_iter(self):
@@ -72,7 +69,7 @@ second line
         for item in ch:
             items.append(item)
         self.assertEqual(len(items), 4)
-        self.assertEqual(_unicode(items[0]), 'hello ♥ world\n')
+        self.assertEqual(items[0], 'hello ♥ world\n')
         ch.shutdown(True)
 
     def test_file_readlines(self):
@@ -82,8 +79,8 @@ second line
         # empty one
         self.assertGreaterEqual(len(lines), 4)
         self.assertLessEqual(len(lines), 5)
-        self.assertEqual(_unicode(lines[0]), 'hello ♥ world\n')
-        self.assertEqual(_unicode(lines[3]), 'À demain!')
+        self.assertEqual(lines[0], 'hello ♥ world\n')
+        self.assertEqual(lines[3], 'À demain!')
         if len(lines) == 4:
             self.assertEqual(lines[4], '')
 
@@ -130,7 +127,7 @@ second line
         ch.shutdown(True)
 
         with open(self.testout, 'rb') as f:
-            self.assertEqual(f.read().decode('latin1'), 'hellø world\nÀ demain!')
+            self.assertEqual(f.read().decode('latin1'), u'hellø world\nÀ demain!')
 
     def test_file_writelines(self):
         ch = GLib.IOChannel(filename=self.testout, mode='w')
diff --git a/tests/test_mainloop.py b/tests/test_mainloop.py
index ae3919e..fda6787 100644
--- a/tests/test_mainloop.py
+++ b/tests/test_mainloop.py
@@ -8,8 +8,6 @@ import unittest
 
 from gi.repository import GLib
 
-from compathelper import _bytes
-
 
 class TestMainLoop(unittest.TestCase):
 
@@ -32,7 +30,7 @@ class TestMainLoop(unittest.TestCase):
         GLib.child_watch_add(GLib.PRIORITY_DEFAULT, pid, child_died, loop)
 
         os.close(pipe_r)
-        os.write(pipe_w, _bytes("Y"))
+        os.write(pipe_w, b"Y")
         os.close(pipe_w)
 
         def excepthook(type, value, traceback):
diff --git a/tests/test_overrides_gtk.py b/tests/test_overrides_gtk.py
index e7397eb..61b7dc0 100644
--- a/tests/test_overrides_gtk.py
+++ b/tests/test_overrides_gtk.py
@@ -8,7 +8,6 @@ import time
 import sys
 import warnings
 
-from compathelper import _unicode, _bytes
 from helper import ignore_gi_deprecation_warnings, capture_glib_warnings
 
 import gi.overrides
@@ -930,7 +929,7 @@ class TestTreeModel(unittest.TestCase):
                                                 GLib.MININT64,
                                                 0xffffffffffffffff,
                                                 254,
-                                                _bytes('a')
+                                                b'a'
                                                 ))
         # test set
         parent = tree_store.append(parent)
@@ -952,7 +951,7 @@ class TestTreeModel(unittest.TestCase):
                        11, GLib.MININT64,
                        12, 0xffffffffffffffff,
                        13, 254,
-                       14, _bytes('a'))
+                       14, b'a')
 
         parent = tree_store.append(parent)
         i = 98
@@ -972,7 +971,7 @@ class TestTreeModel(unittest.TestCase):
                                 11: GLib.MININT64,
                                 12: 0xffffffffffffffff,
                                 13: 254,
-                                14: _bytes('a')})
+                                14: b'a'})
 
         parent = tree_store.append(parent)
         i = 99
@@ -993,7 +992,7 @@ class TestTreeModel(unittest.TestCase):
                                 GLib.MININT64,
                                 0xffffffffffffffff,
                                 254,
-                                _bytes('a')))
+                                b'a'))
 
         # len gets the number of children in the root node
         # since we kept appending to the previous node
@@ -1103,7 +1102,7 @@ class TestTreeModel(unittest.TestCase):
                                bool(i % 2)))
 
         i = 93
-        label = _unicode('this is row #93')
+        label = u'this is row #93'
         treeiter = list_store.append()
         list_store.set_value(treeiter, 0, i)
         list_store.set_value(treeiter, 1, label)
@@ -1127,7 +1126,7 @@ class TestTreeModel(unittest.TestCase):
 
         # test automatic unicode->str conversion
         i = 94
-        label = _unicode('this is row #94')
+        label = u'this is row #94'
         treeiter = list_store.append((i,
                                       label,
                                       TestGtk.TestClass(self, i, label),
diff --git a/tests/test_properties.py b/tests/test_properties.py
index 3fb5749..ce035cd 100644
--- a/tests/test_properties.py
+++ b/tests/test_properties.py
@@ -27,14 +27,7 @@ from gi.repository import GIMarshallingTests
 from gi.repository import Regress
 from gi import _propertyhelper as propertyhelper
 
-if sys.version_info < (3, 0):
-    TEST_UTF8 = "\xe2\x99\xa5"
-    UNICODE_UTF8 = unicode(TEST_UTF8, 'UTF-8')
-else:
-    TEST_UTF8 = "♥"
-    UNICODE_UTF8 = TEST_UTF8
-
-from compathelper import _long, _unicode
+from compathelper import _long
 from helper import capture_glib_warnings, capture_output
 
 
@@ -174,12 +167,14 @@ class TestPropertyObject(unittest.TestCase):
         self.assertEqual(obj.props.construct, "789")
 
     def test_utf8(self):
-        obj = new(PropertyObject, construct_only=UNICODE_UTF8)
-        self.assertEqual(obj.props.construct_only, TEST_UTF8)
-        obj.set_property('construct', UNICODE_UTF8)
-        self.assertEqual(obj.props.construct, TEST_UTF8)
-        obj.props.normal = UNICODE_UTF8
-        self.assertEqual(obj.props.normal, TEST_UTF8)
+        test_utf8 = "♥"
+        unicode_utf8 = u"♥"
+        obj = new(PropertyObject, construct_only=unicode_utf8)
+        self.assertEqual(obj.props.construct_only, test_utf8)
+        obj.set_property('construct', unicode_utf8)
+        self.assertEqual(obj.props.construct, test_utf8)
+        obj.props.normal = unicode_utf8
+        self.assertEqual(obj.props.normal, test_utf8)
 
     def test_int_to_str(self):
         obj = new(PropertyObject, construct_only=1)
@@ -1137,10 +1132,10 @@ class CPropertiesTestBase(object):
         self.assertEqual(self.get_prop(obj, 'some-strv'), ['hello', 'world'])
 
         # unicode on py2
-        obj = GIMarshallingTests.PropertiesObject(some_strv=[_unicode('foo')])
-        self.assertEqual(self.get_prop(obj, 'some-strv'), [_unicode('foo')])
+        obj = GIMarshallingTests.PropertiesObject(some_strv=[u'foo'])
+        self.assertEqual(self.get_prop(obj, 'some-strv'), [u'foo'])
         self.assertRaises(TypeError, self.set_prop, self.obj, 'some-strv',
-                          [_unicode('foo'), 1])
+                          [u'foo', 1])
 
     def test_boxed_struct(self):
         self.assertEqual(self.get_prop(self.obj, 'some-boxed-struct'), None)


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]