[pygobject] minor fixes in tests for py3k compat



commit dec9001d26c97949e7b3578086cb35e98075c047
Author: John (J5) Palmieri <johnp redhat com>
Date:   Thu Sep 9 07:36:04 2010 -0400

    minor fixes in tests for py3k compat
    
    * add a _bytes wrapper for API that expects bytes in py3k but str in py2
    * fix some more exception handling using sys.exc_info()[:2]
    * use range instead of xrange, items instead of iteritems since py3k
      dropped support for the different ways of accessing iterators
      - this is less efficient in py2 but we plan to target py3k as the
        primary platform
    * use list(dict.items()) since py3k only returns iterables which are not
      indexable
    * missed some _long wrapping
    
    https://bugzilla.gnome.org/show_bug.cgi?id=615872

 tests/compathelper.py    |   18 ++++++++++++++++++
 tests/test_everything.py |    5 +++--
 tests/test_mainloop.py   |    3 ++-
 tests/test_option.py     |    6 ++++--
 tests/test_overrides.py  |    4 ++--
 tests/test_properties.py |    6 +++---
 6 files changed, 32 insertions(+), 10 deletions(-)
---
diff --git a/tests/compathelper.py b/tests/compathelper.py
index 8d2d092..754285c 100644
--- a/tests/compathelper.py
+++ b/tests/compathelper.py
@@ -27,6 +27,24 @@ if sys.version_info >= (3, 0):
     isinstance(_basestring, "hello")
     '''
     _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()
 else:
     _long = long
     _basestring = basestring
+    _bytes = str
diff --git a/tests/test_everything.py b/tests/test_everything.py
index 3ef5eb3..171ef8d 100644
--- a/tests/test_everything.py
+++ b/tests/test_everything.py
@@ -99,7 +99,8 @@ class TestEverything(unittest.TestCase):
     def test_wrong_type_of_arguments(self):
         try:
             Everything.test_int8()
-        except TypeError, e:
+        except TypeError:
+            (e_type, e) = sys.exc_info()[:2]
             self.assertEquals(e.args, ("test_int8() takes exactly 1 argument(s) (0 given)",))
 
     def test_gtypes(self):
@@ -349,7 +350,7 @@ class TestProperties(unittest.TestCase):
 
         object_.props.hash_table = {'mec': 56}
         self.assertTrue(isinstance(object_.props.hash_table, dict))
-        self.assertEquals(object_.props.hash_table.items()[0], ('mec', 56))
+        self.assertEquals(list(object_.props.hash_table.items())[0], ('mec', 56))
 
     def test_list(self):
         object_ = Everything.TestObj()
diff --git a/tests/test_mainloop.py b/tests/test_mainloop.py
index 4286d13..80e2aec 100644
--- a/tests/test_mainloop.py
+++ b/tests/test_mainloop.py
@@ -7,6 +7,7 @@ import unittest
 
 import glib
 
+from compathelper import _bytes
 
 class TestMainLoop(unittest.TestCase):
     def testExceptionHandling(self):
@@ -27,7 +28,7 @@ class TestMainLoop(unittest.TestCase):
         glib.child_watch_add(pid, child_died, loop)
 
         os.close(pipe_r)
-        os.write(pipe_w, "Y")
+        os.write(pipe_w, _bytes("Y"))
         os.close(pipe_w)
 
         def excepthook(type, value, traceback):
diff --git a/tests/test_option.py b/tests/test_option.py
index c65a325..a6ecc98 100644
--- a/tests/test_option.py
+++ b/tests/test_option.py
@@ -12,6 +12,7 @@ except ImportError:
 from glib.option import OptionParser, OptionGroup, OptionValueError, \
      make_option, BadOptionError
 
+from compathelper import _bytes
 
 class TestOption(unittest.TestCase):
     EXCEPTION_MESSAGE = "This callback fails"
@@ -28,7 +29,7 @@ class TestOption(unittest.TestCase):
 
     def _create_group(self):
         def option_callback(option, opt, value, parser):
-            raise StandardError(self.EXCEPTION_MESSAGE)
+            raise Exception(self.EXCEPTION_MESSAGE)
 
         group = OptionGroup(
             "unittest", "Unit test options", "Show all unittest options",
@@ -113,6 +114,7 @@ class TestOption(unittest.TestCase):
                 ["test_option.py", "--callback-failure-test"])
         finally:
             sys.stderr = old_stderr
+
         assert (sio.getvalue().split('\n')[-2] ==
-                "StandardError: " + self.EXCEPTION_MESSAGE)
+                "Exception: " + self.EXCEPTION_MESSAGE)
 
diff --git a/tests/test_overrides.py b/tests/test_overrides.py
index deb6cd8..8791f94 100644
--- a/tests/test_overrides.py
+++ b/tests/test_overrides.py
@@ -187,7 +187,7 @@ class TestGtk(unittest.TestCase):
 
         tree_store = Gtk.TreeStore(int, 'gchararray', TestGtk.TestClass)
         parent = None
-        for i in xrange(100):
+        for i in range(100):
             label = 'this is child #%d' % i
             testobj = TestGtk.TestClass(self, i, label)
             parent = tree_store.append(parent, (i, label, testobj))
@@ -214,7 +214,7 @@ class TestGtk(unittest.TestCase):
 
     def test_list_store(self):
         list_store = Gtk.ListStore(int, str, 'GIOverrideTreeAPITest')
-        for i in xrange(100):
+        for i in range(100):
             label = 'this is row #%d' % i
             testobj = TestGtk.TestClass(self, i, label)
             parent = list_store.append((i, label, testobj))
diff --git a/tests/test_properties.py b/tests/test_properties.py
index 4892ba1..19e1136 100644
--- a/tests/test_properties.py
+++ b/tests/test_properties.py
@@ -206,9 +206,9 @@ class TestProperty(unittest.TestCase):
         o.float = 3.14
         self.assertEqual(o.float, 3.14)
 
-        self.assertEqual(o.long, long(0))
-        o.long = long(100)
-        self.assertEqual(o.long, long(100))
+        self.assertEqual(o.long, _long(0))
+        o.long = _long(100)
+        self.assertEqual(o.long, _long(100))
 
     def testCustomGetter(self):
         class C(gobject.GObject):



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