[pygobject] Make sure version information passed to require_version is a string.



commit 9cbf370d0034bffa60be67f6d47eee94e4045c18
Author: Benjamin Berg <bberg redhat com>
Date:   Fri Apr 21 13:35:05 2017 +0200

    Make sure version information passed to require_version is a string.
    
    This simply makes it easier for someone to find an error in cases where
    a floating point is passed by accident.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=781582

 gi/__init__.py                 |    7 +++++++
 tests/test_import_machinery.py |   12 ++++++++++++
 2 files changed, 19 insertions(+), 0 deletions(-)
---
diff --git a/gi/__init__.py b/gi/__init__.py
index 9fefc79..185c4d4 100644
--- a/gi/__init__.py
+++ b/gi/__init__.py
@@ -108,6 +108,13 @@ def require_version(namespace, version):
     """
     repository = Repository.get_default()
 
+    if sys.version_info[0] <= 2:
+        if not isinstance(version, basestring):
+            raise ValueError('Namespace version needs to be a string.')
+    else:
+        if not isinstance(version, str):
+            raise ValueError('Namespace version needs to be a string.')
+
     if namespace in repository.get_loaded_namespaces():
         loaded_version = repository.get_version(namespace)
         if loaded_version != version:
diff --git a/tests/test_import_machinery.py b/tests/test_import_machinery.py
index 044117d..bdc1a72 100644
--- a/tests/test_import_machinery.py
+++ b/tests/test_import_machinery.py
@@ -140,6 +140,18 @@ class TestImporter(unittest.TestCase):
                 from gi.repository import InvalidGObjectRepositoryModuleName
                 InvalidGObjectRepositoryModuleName
 
+    def test_require_version_versiontype(self):
+        import gi
+        with self.assertRaises(ValueError) as context:
+            gi.require_version('GLib', 2.0)
+
+        # Test that unicode strings work in python 2
+        if sys.version_info[0] <= 2:
+            gi.require_version('GLib', unicode('2.0'))
+        else:
+            with self.assertRaises(ValueError) as context:
+                gi.require_version('GLib', b'2.0')
+
     def test_require_versions(self):
         import gi
         gi.require_versions({'GLib': '2.0', 'Gio': '2.0', 'GObject': '2.0'})


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