[GRASS-SVN] r69617 - in grass/branches/releasebranch_7_2/lib/python/script: . testsuite

svn_grass at osgeo.org svn_grass at osgeo.org
Sat Oct 1 10:50:07 PDT 2016


Author: wenzeslaus
Date: 2016-10-01 10:50:06 -0700 (Sat, 01 Oct 2016)
New Revision: 69617

Modified:
   grass/branches/releasebranch_7_2/lib/python/script/testsuite/test_start_command_functions.py
   grass/branches/releasebranch_7_2/lib/python/script/testsuite/test_utils.py
   grass/branches/releasebranch_7_2/lib/python/script/utils.py
Log:
pythonlib: use isinstance to distinguish string types (meant to fix r65787, needed for r65804 according to #3171)

Modified: grass/branches/releasebranch_7_2/lib/python/script/testsuite/test_start_command_functions.py
===================================================================
--- grass/branches/releasebranch_7_2/lib/python/script/testsuite/test_start_command_functions.py	2016-10-01 17:15:30 UTC (rev 69616)
+++ grass/branches/releasebranch_7_2/lib/python/script/testsuite/test_start_command_functions.py	2016-10-01 17:50:06 UTC (rev 69617)
@@ -1,3 +1,5 @@
+# -*- coding: utf-8 -*-
+# the utf-8 is important because we do use the characters
 """Tests of start_command function family (location independent)"""
 
 from grass.gunittest.case import TestCase
@@ -3,5 +5,5 @@
 from grass.gunittest.main import test
 
-from grass.script.core import start_command, PIPE
+from grass.script.core import start_command, PIPE, run_command
 
 
@@ -38,5 +40,28 @@
         self.assertEquals(returncode, 1)
         self.assertIn(b'raster', stderr)
 
+
+class TestPythonModuleWithUnicodeParameters(TestCase):
+    """Tests if unicode works in parameters of Python modules
+
+    This in fact tests also the `parser()` function (original motivation
+    for this tests).
+
+    Using g.search.module because it takes any option values.
+    """
+
+    def test_python_module_ascii(self):
+        """This tests if Python module works"""
+        run_command('g.search.modules', keyword=b'Priserny kun')
+
+    def test_python_module_czech_nonascii(self):
+        """This likely fails on non-UTF-8 systems (i.e. MS Win)"""
+        run_command('g.search.modules', keyword=b'Příšerný kůň')
+
+    def test_python_module_czech_unicode(self):
+        """This likely fails on non-UTF-8 systems (i.e. MS Win)"""
+        run_command('g.search.modules', keyword=u'Příšerný kůň')
+
+
 if __name__ == '__main__':
     test()

Modified: grass/branches/releasebranch_7_2/lib/python/script/testsuite/test_utils.py
===================================================================
--- grass/branches/releasebranch_7_2/lib/python/script/testsuite/test_utils.py	2016-10-01 17:15:30 UTC (rev 69616)
+++ grass/branches/releasebranch_7_2/lib/python/script/testsuite/test_utils.py	2016-10-01 17:50:06 UTC (rev 69617)
@@ -39,7 +39,11 @@
     def test_unicode(self):
         self.assertEqual(b'text', utils.encode(u'text'))
 
+    def test_bytes_grabage_in_out(self):
+        """If the input is bytes we should not touch it for encoding"""
+        self.assertEqual(b'Příšerný kůň', utils.encode(b'Příšerný kůň'))
 
+
 class TestDecode(TestCase):
     """Tests function `encode` that convert value to unicode."""
 

Modified: grass/branches/releasebranch_7_2/lib/python/script/utils.py
===================================================================
--- grass/branches/releasebranch_7_2/lib/python/script/utils.py	2016-10-01 17:15:30 UTC (rev 69616)
+++ grass/branches/releasebranch_7_2/lib/python/script/utils.py	2016-10-01 17:50:06 UTC (rev 69617)
@@ -150,26 +150,31 @@
         self[key] = value
 
 
-def decode(bytes):
-    """Decode bytes with default locale
+def decode(bytes_):
+    """Decode bytes with default locale and return (unicode) string
 
-    :param bytes bytes: the bytes to decode
+    No-op if parameter is not bytes (assumed unicode string).
+
+    :param bytes bytes_: the bytes to decode
     """
-    enc = locale.getdefaultlocale()[1]
-    if hasattr(bytes, 'decode'):
-        return bytes.decode(enc) if enc else bytes.decode()
-    return bytes
+    if isinstance(bytes_, bytes):
+        enc = locale.getdefaultlocale()[1]
+        return bytes_.decode(enc) if enc else bytes_.decode()
+    return bytes_
 
 
 def encode(string):
-    """Encode string with default locale -> bytes
+    """Encode string with default locale and return bytes with that encoding
 
+    No-op if parameter is bytes (assumed already encoded).
+    This ensures garbage in, garbage out.
+
     :param str string: the string to encode
     """
+    if isinstance(string, bytes):
+        return string
     enc = locale.getdefaultlocale()[1]
-    if hasattr(string, 'encode'):
-        return string.encode(enc) if enc else string.encode()
-    return string
+    return string.encode(enc) if enc else string.encode()
 
 
 def parse_key_val(s, sep='=', dflt=None, val_type=None, vsep=None):



More information about the grass-commit mailing list