[pygobject/py3k: 18/20] Changes for tests to run in python 3



commit dc91f48833e5f3b94f02a0be16d0d47c10d364f5
Author: John Ehresman <jpe wingware com>
Date:   Thu Apr 15 12:55:34 2010 -0400

    Changes for tests to run in python 3

 tests/runtests.py        |    3 +--
 tests/test_gio.py        |   15 +++++++++++----
 tests/test_mainloop.py   |    3 +--
 tests/test_option.py     |    7 +++++--
 tests/test_properties.py |   31 ++++++++++++++++++++++---------
 tests/test_signal.py     |   17 +++++++++++------
 tests/test_source.py     |    6 +++---
 7 files changed, 54 insertions(+), 28 deletions(-)
---
diff --git a/tests/runtests.py b/tests/runtests.py
index fc0558d..76b404c 100644
--- a/tests/runtests.py
+++ b/tests/runtests.py
@@ -29,8 +29,7 @@ os.chdir(dir)
 
 def gettestnames():
     files = glob.glob('*.py')
-    names = map(lambda x: x[:-3], files)
-    map(names.remove, SKIP_FILES)
+    names = [x[:-3] for x in files if x[:-3] not in SKIP_FILES]
     return names
 
 suite = unittest.TestSuite()
diff --git a/tests/test_gio.py b/tests/test_gio.py
index feafc70..d40cb14 100644
--- a/tests/test_gio.py
+++ b/tests/test_gio.py
@@ -2,9 +2,14 @@
 
 import os
 import unittest
+import sys
 
 from common import gio, glib
 
+try:
+    _unicode = unicode
+except NameError:
+    _unicode = str
 
 class TestFile(unittest.TestCase):
     def setUp(self):
@@ -194,8 +199,8 @@ class TestFile(unittest.TestCase):
                       gio.File("file:///"),
                       gio.File(uri="file:///"),
                       gio.File(path="/"),
-                      gio.File(u"/"),
-                      gio.File(path=u"/")]:
+                      gio.File(_unicode("/")),
+                      gio.File(path=_unicode("/"))]:
             self.failUnless(isinstance(gfile, gio.File))
             self.assertEquals(gfile.get_path(), "/")
             self.assertEquals(gfile.get_uri(), "file:///")
@@ -263,7 +268,8 @@ class TestFile(unittest.TestCase):
             try:
                 try:
                     retval = gfile.mount_enclosing_volume_finish(result)
-                except gio.Error, e:
+                except gio.Error:
+                    e = sys.exc_info()[1]
                     # If we run the tests too fast
                     if e.code == gio.ERROR_ALREADY_MOUNTED:
                         print ('WARNING: testfile is already mounted, '
@@ -454,7 +460,8 @@ class TestFile(unittest.TestCase):
         caught = False
         try:
             self.file.replace_contents("this won't work", etag="wrong")
-        except gio.Error, e:
+        except gio.Error:
+            e = sys.exc_info()[1]
             self.assertEqual(e.code, gio.ERROR_WRONG_ETAG)
             caught = True
         self.failUnless(caught)
diff --git a/tests/test_mainloop.py b/tests/test_mainloop.py
index 95e5f78..afb550a 100644
--- a/tests/test_mainloop.py
+++ b/tests/test_mainloop.py
@@ -1,6 +1,5 @@
 #!/usr/bin/env python
 
-import exceptions
 import os
 import sys
 import select
@@ -31,7 +30,7 @@ class TestMainLoop(unittest.TestCase):
         os.close(pipe_w)
 
         def excepthook(type, value, traceback):
-            assert type is exceptions.Exception
+            assert type is Exception
             assert value.args[0] == "deadbabe"
         sys.excepthook = excepthook
 
diff --git a/tests/test_option.py b/tests/test_option.py
index 6e0d449..ad5f1b3 100644
--- a/tests/test_option.py
+++ b/tests/test_option.py
@@ -2,8 +2,11 @@
 
 import unittest
 import sys
-from StringIO import StringIO
-
+try:
+    from StringIO import StringIO
+except ImportError:
+    from io import StringIO
+    
 from glib.option import OptionParser, OptionGroup, OptionValueError, \
      make_option, BadOptionError
 
diff --git a/tests/test_properties.py b/tests/test_properties.py
index ccfcb34..b05234c 100644
--- a/tests/test_properties.py
+++ b/tests/test_properties.py
@@ -12,6 +12,11 @@ from gobject.constants import \
      G_MININT, G_MAXINT, G_MAXUINT, G_MINLONG, G_MAXLONG, \
      G_MAXULONG
 
+try:
+    _long = long
+except NameError:
+    _long = int
+
 class PropertyObject(GObject):
     normal = gobject.property(type=str)
     construct = gobject.property(
@@ -80,12 +85,18 @@ class TestProperties(unittest.TestCase):
     def testUint64(self):
         obj = new(PropertyObject)
         self.assertEqual(obj.props.uint64, 0)
-        obj.props.uint64 = 1L
-        self.assertEqual(obj.props.uint64, 1L)
+
+        obj.props.uint64 = _long(1)
+        self.assertEqual(obj.props.uint64, _long(1))
+        obj.props.uint64 = 1
+        self.assertEqual(obj.props.uint64, _long(1))
+
+        obj.props.uint64 = _long(1)
+        self.assertEqual(obj.props.uint64, _long(1))
         obj.props.uint64 = 1
-        self.assertEqual(obj.props.uint64, 1L)
+        self.assertEqual(obj.props.uint64, _long(1))
 
-        self.assertRaises((TypeError, OverflowError), obj.set_property, "uint64", -1L)
+        self.assertRaises((TypeError, OverflowError), obj.set_property, "uint64", _long(-1))
         self.assertRaises((TypeError, OverflowError), obj.set_property, "uint64", -1)
 
     def testUInt64DefaultValue(self):
@@ -93,10 +104,12 @@ class TestProperties(unittest.TestCase):
             class TimeControl(GObject):
                 __gproperties__ = {
                     'time': (TYPE_UINT64, 'Time', 'Time',
-                             0L, (1<<64) - 1, 0L,
+                             _long(0), (1<<64) - 1, _long(0),
                              PARAM_READABLE)
                     }
-        except OverflowError, ex:
+        except OverflowError:
+            import sys
+            ex = sys.exc_info()[1]
             self.fail(str(ex))
 
     def testRange(self):
@@ -202,9 +215,9 @@ class TestProperty(unittest.TestCase):
         o.float = 3.14
         self.assertEqual(o.float, 3.14)
 
-        self.assertEqual(o.long, 0L)
-        o.long = 100L
-        self.assertEqual(o.long, 100L)
+        self.assertEqual(o.long, _long(0))
+        o.long = _long(100)
+        self.assertEqual(o.long, _long(100))
 
     def testCustomGetter(self):
         class C(gobject.GObject):
diff --git a/tests/test_signal.py b/tests/test_signal.py
index 96e0731..9724aff 100644
--- a/tests/test_signal.py
+++ b/tests/test_signal.py
@@ -6,6 +6,11 @@ import sys
 
 from common import gobject, testhelper
 
+try:
+    _long = long
+except NameError:
+    _long = int
+
 class C(gobject.GObject):
     __gsignals__ = { 'my_signal': (gobject.SIGNAL_RUN_FIRST, gobject.TYPE_NONE,
                                    (gobject.TYPE_INT,)) }
@@ -302,7 +307,7 @@ class SigPropClass(gobject.GObject):
         if pspec.name == 'foo':
             self._foo = value
         else:
-            raise AttributeError, 'unknown property %s' % pspec.name
+            raise AttributeError('unknown property %s' % pspec.name)
         try:
             self.emit("my-signal", 1)
         except TypeError:
@@ -326,7 +331,7 @@ class CM(gobject.GObject):
         test1=(f, None, ()),
         test2=(l, None, (str,)),
         test3=(l, int, (double,)),
-        test4=(f, None, (bool, long, float, double, int, uint, ulong)),
+        test4=(f, None, (bool, _long, float, double, int, uint, ulong)),
         test_float=(l, float, (float,)),
         test_double=(l, double, (double, )),
         test_string=(l, str, (str, )),
@@ -349,7 +354,7 @@ class _TestCMarshaller:
         self.assertEqual(rv, 20)
 
     def testTest4(self):
-        self.obj.emit("test4", True, 10L, 3.14, 1.78, 20, 30L, 31L)
+        self.obj.emit("test4", True, _long(10), 3.14, 1.78, 20, _long(30), _long(31))
 
     def testTestReturnFloat(self):
         rv = self.obj.emit("test-float", 1.234)
@@ -371,9 +376,9 @@ if 'generic-c-marshaller' in gobject.features:
     class TestCMarshaller(_TestCMarshaller, unittest.TestCase):
         pass
 else:
-    print
-    print '** WARNING: LIBFFI disabled, not testing'
-    print
+    print ('')
+    print ('** WARNING: LIBFFI disabled, not testing')
+    print ('')
 
 # Test for 374653
 class TestPyGValue(unittest.TestCase):
diff --git a/tests/test_source.py b/tests/test_source.py
index abe2f69..e32ce1e 100644
--- a/tests/test_source.py
+++ b/tests/test_source.py
@@ -91,9 +91,9 @@ class TestSource(unittest.TestCase):
 
 
 class TestTimeout(unittest.TestCase):
-     def test504337(self):
-	timeout_source = glib.Timeout(20)
-	idle_source = glib.Idle()
+    def test504337(self):
+        timeout_source = glib.Timeout(20)
+        idle_source = glib.Idle()
 
 
 if __name__ == '__main__':



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