From 747f7d25490abc3d7fdb119f0ce3708d450eb4c2 Mon Sep 17 00:00:00 2001 From: Claude Paroz Date: Wed, 13 Mar 2013 09:50:45 +0100 Subject: [PATCH] Fixed #20036 -- Improved GEOS version string parsing Thanks chikiro.spam at gmail.com for the report. --- django/contrib/gis/geos/libgeos.py | 13 +++++++++---- django/contrib/gis/geos/tests/test_geos.py | 18 ++++++++++-------- 2 files changed, 19 insertions(+), 12 deletions(-) diff --git a/django/contrib/gis/geos/libgeos.py b/django/contrib/gis/geos/libgeos.py index f011208ea0..05e5d1209b 100644 --- a/django/contrib/gis/geos/libgeos.py +++ b/django/contrib/gis/geos/libgeos.py @@ -108,8 +108,11 @@ geos_version.argtypes = None geos_version.restype = c_char_p # Regular expression should be able to parse version strings such as -# '3.0.0rc4-CAPI-1.3.3', '3.0.0-CAPI-1.4.1' or '3.4.0dev-CAPI-1.8.0' -version_regex = re.compile(r'^(?P(?P\d+)\.(?P\d+)\.(?P\d+))((rc(?P\d+))|dev)?-CAPI-(?P\d+\.\d+\.\d+)$') +# '3.0.0rc4-CAPI-1.3.3', '3.0.0-CAPI-1.4.1', '3.4.0dev-CAPI-1.8.0' or '3.4.0dev-CAPI-1.8.0 r0' +version_regex = re.compile( + r'^(?P(?P\d+)\.(?P\d+)\.(?P\d+))' + r'((rc(?P\d+))|dev)?-CAPI-(?P\d+\.\d+\.\d+)( r\d+)?$' +) def geos_version_info(): """ Returns a dictionary containing the various version metadata parsed from @@ -119,8 +122,10 @@ def geos_version_info(): """ ver = geos_version().decode() m = version_regex.match(ver) - if not m: raise GEOSException('Could not parse version info string "%s"' % ver) - return dict((key, m.group(key)) for key in ('version', 'release_candidate', 'capi_version', 'major', 'minor', 'subminor')) + if not m: + raise GEOSException('Could not parse version info string "%s"' % ver) + return dict((key, m.group(key)) for key in ( + 'version', 'release_candidate', 'capi_version', 'major', 'minor', 'subminor')) # Version numbers and whether or not prepared geometry support is available. _verinfo = geos_version_info() diff --git a/django/contrib/gis/geos/tests/test_geos.py b/django/contrib/gis/geos/tests/test_geos.py index de9471211e..66d890d8cb 100644 --- a/django/contrib/gis/geos/tests/test_geos.py +++ b/django/contrib/gis/geos/tests/test_geos.py @@ -1080,15 +1080,17 @@ class GEOSTest(unittest.TestCase, TestDataMixin): self.assertEqual(mls.interpolate(17), Point(10, 7)) def test_geos_version(self): - "Testing the GEOS version regular expression." + """Testing the GEOS version regular expression.""" from django.contrib.gis.geos.libgeos import version_regex - versions = [ ('3.0.0rc4-CAPI-1.3.3', '3.0.0'), - ('3.0.0-CAPI-1.4.1', '3.0.0'), - ('3.4.0dev-CAPI-1.8.0', '3.4.0') ] - for v, expected in versions: - m = version_regex.match(v) - self.assertTrue(m) - self.assertEqual(m.group('version'), expected) + versions = [('3.0.0rc4-CAPI-1.3.3', '3.0.0', '1.3.3'), + ('3.0.0-CAPI-1.4.1', '3.0.0', '1.4.1'), + ('3.4.0dev-CAPI-1.8.0', '3.4.0', '1.8.0'), + ('3.4.0dev-CAPI-1.8.0 r0', '3.4.0', '1.8.0')] + for v_init, v_geos, v_capi in versions: + m = version_regex.match(v_init) + self.assertTrue(m, msg="Unable to parse the version string '%s'" % v_init) + self.assertEqual(m.group('version'), v_geos) + self.assertEqual(m.group('capi_version'), v_capi) def suite():