[GRASS-SVN] r65595 - grass/trunk/lib/python/pygrass/modules/interface/testsuite

svn_grass at osgeo.org svn_grass at osgeo.org
Wed Jul 15 10:27:38 PDT 2015


Author: zarch
Date: 2015-07-15 10:27:37 -0700 (Wed, 15 Jul 2015)
New Revision: 65595

Modified:
   grass/trunk/lib/python/pygrass/modules/interface/testsuite/test_modules.py
Log:
pygrass: change test to work on both python2 and python3

Modified: grass/trunk/lib/python/pygrass/modules/interface/testsuite/test_modules.py
===================================================================
--- grass/trunk/lib/python/pygrass/modules/interface/testsuite/test_modules.py	2015-07-15 17:26:24 UTC (rev 65594)
+++ grass/trunk/lib/python/pygrass/modules/interface/testsuite/test_modules.py	2015-07-15 17:27:37 UTC (rev 65595)
@@ -4,6 +4,7 @@
 
 @author: pietro
 """
+import sys
 from fnmatch import fnmatch
 from grass.gunittest.case import TestCase
 from grass.gunittest.main import test
@@ -11,11 +12,30 @@
 from grass.script.core import get_commands
 from grass.pygrass.modules.interface import Module
 
+PY2 = sys.version_info[0] == 2
+if PY2:
+    from StringIO import StringIO
+else:
+    from io import BytesIO as StringIO
 
+
 SKIP = ["g.parser", ]
 
 
-class TestModulesMeta(type):
+# taken from six
+def with_metaclass(meta, *bases):
+    """Create a base class with a metaclass."""
+    # This requires a bit of explanation: the basic idea is to make a dummy
+    # metaclass for one level of class instantiation that replaces itself with
+    # the actual metaclass.
+    class metaclass(meta):
+
+        def __new__(cls, name, this_bases, d):
+            return meta(name, bases, d)
+    return type.__new__(metaclass, 'temporary_class', (), {})
+
+
+class ModulesMeta(type):
     def __new__(mcs, name, bases, dict):
 
         def gen_test(cmd):
@@ -31,15 +51,15 @@
         return type.__new__(mcs, name, bases, dict)
 
 
-class TestModules(TestCase):
-    __metaclass__ = TestModulesMeta
+class TestModules(with_metaclass(ModulesMeta, TestCase)):
+    pass
 
 
 class TestModulesPickability(TestCase):
     def test_rsun(self):
         """Test if a Module instance is pickable"""
         import pickle
-        from StringIO import StringIO
+
         out = StringIO()
         pickle.dump(Module('r.sun'), out)
         out.close()



More information about the grass-commit mailing list