[pygobject] fix up tests so they run in py3k



commit 7197f85c9be2b03636639ac909ca2c3170653509
Author: John (J5) Palmieri <johnp redhat com>
Date:   Wed Aug 18 10:29:19 2010 -0400

    fix up tests so they run in py3k
    
    * add a compat helper that should only be used by tests
    * fix long notation to use the compat helper instead
    * add parens to print statements
    * use compatable try/except pattern
    
    https://bugzilla.gnome.org/show_bug.cgi?id=615872

 gobject/propertyhelper.py |   16 +++++++++++-----
 tests/compathelper.py     |   32 ++++++++++++++++++++++++++++++++
 tests/test_option.py      |    7 ++++++-
 tests/test_properties.py  |   24 ++++++++++++++----------
 tests/test_signal.py      |   14 +++++++-------
 tests/test_source.py      |    4 ++--
 6 files changed, 72 insertions(+), 25 deletions(-)
---
diff --git a/gobject/propertyhelper.py b/gobject/propertyhelper.py
index e299273..2f77a4d 100644
--- a/gobject/propertyhelper.py
+++ b/gobject/propertyhelper.py
@@ -36,6 +36,12 @@ from gobject.constants import \
      G_MININT, G_MAXINT, G_MAXUINT, G_MINLONG, G_MAXLONG, \
      G_MAXULONG
 
+if sys.version_info >= (3, 0):
+    _basestring = str
+    _long = int
+else:
+    _basestring = basestring
+    _long = long
 
 class property(object):
     """
@@ -107,11 +113,11 @@ class property(object):
         self.default = self._get_default(default)
         self._check_default()
 
-        if not isinstance(nick, basestring):
+        if not isinstance(nick, _basestring):
             raise TypeError("nick must be a string")
         self.nick = nick
 
-        if not isinstance(blurb, basestring):
+        if not isinstance(blurb, _basestring):
             raise TypeError("blurb must be a string")
         self.blurb = blurb
 
@@ -171,12 +177,12 @@ class property(object):
             raise exc
 
     def _type_from_python(self, type):
-        if type == int:
+        if type == _long:
+            return TYPE_LONG
+        elif type == int:
             return TYPE_INT
         elif type == bool:
             return TYPE_BOOLEAN
-        elif type == long:
-            return TYPE_LONG
         elif type == float:
             return TYPE_DOUBLE
         elif type == str:
diff --git a/tests/compathelper.py b/tests/compathelper.py
new file mode 100644
index 0000000..8d2d092
--- /dev/null
+++ b/tests/compathelper.py
@@ -0,0 +1,32 @@
+import sys
+
+if sys.version_info >= (3, 0):
+    '''
+    for tests that need to test long values in python 2
+
+    python 3 does not differentiate between long and int
+    and does not supply a long keyword
+
+    instead of testing longs by using values such as 10L
+    test writters should do this:
+
+    from compathelper import _long
+    _long(10)
+    '''
+    _long = int
+
+    '''
+    for tests that need to test string values in python 2
+
+    python 3 does differentiate between str and bytes
+    and does not supply a basestring keyword
+
+    any tests that use basestring should do this:
+
+    from compathelper import _basestring
+    isinstance(_basestring, "hello")
+    '''
+    _basestring = str
+else:
+    _long = long
+    _basestring = basestring
diff --git a/tests/test_option.py b/tests/test_option.py
index 6e0d449..c65a325 100644
--- a/tests/test_option.py
+++ b/tests/test_option.py
@@ -2,7 +2,12 @@
 
 import unittest
 import sys
-from StringIO import StringIO
+
+# py3k has StringIO in a different module
+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..4892ba1 100644
--- a/tests/test_properties.py
+++ b/tests/test_properties.py
@@ -1,4 +1,5 @@
 
+import sys
 import struct
 import unittest
 
@@ -12,6 +13,8 @@ from gobject.constants import \
      G_MININT, G_MAXINT, G_MAXUINT, G_MINLONG, G_MAXLONG, \
      G_MAXULONG
 
+from compathelper import _long
+
 class PropertyObject(GObject):
     normal = gobject.property(type=str)
     construct = gobject.property(
@@ -80,12 +83,12 @@ 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, 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 +96,11 @@ 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:
+            (etype, ex) = sys.exc_info()[2:]
             self.fail(str(ex))
 
     def testRange(self):
@@ -182,7 +186,7 @@ class TestProperty(unittest.TestCase):
             str = gobject.property(type=str)
             int = gobject.property(type=int)
             float = gobject.property(type=float)
-            long = gobject.property(type=long)
+            long = gobject.property(type=_long)
 
         self.failUnless(hasattr(C.props, 'str'))
         self.failUnless(hasattr(C.props, 'int'))
@@ -202,9 +206,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 075b03e..d68cb5b 100644
--- a/tests/test_signal.py
+++ b/tests/test_signal.py
@@ -6,7 +6,7 @@ import sys
 
 import gobject
 import testhelper
-
+from compathelper import _long
 
 class C(gobject.GObject):
     __gsignals__ = { 'my_signal': (gobject.SIGNAL_RUN_FIRST, gobject.TYPE_NONE,
@@ -304,7 +304,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:
@@ -328,7 +328,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, )),
@@ -351,7 +351,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)
@@ -373,9 +373,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 c4ea62c..339fb32 100644
--- a/tests/test_source.py
+++ b/tests/test_source.py
@@ -92,8 +92,8 @@ class TestSource(unittest.TestCase):
 
 class TestTimeout(unittest.TestCase):
      def test504337(self):
-	timeout_source = glib.Timeout(20)
-	idle_source = glib.Idle()
+        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]