[1.5.x] Fixed #20036 -- Improved GEOS version string parsing

Thanks chikiro.spam at gmail.com for the report.
Backport of 747f7d254 from master.
This commit is contained in:
Claude Paroz 2013-03-13 09:50:45 +01:00
parent e5c57fe1b4
commit 101ec4bf8a
2 changed files with 19 additions and 12 deletions

View File

@ -108,8 +108,11 @@ geos_version.argtypes = None
geos_version.restype = c_char_p geos_version.restype = c_char_p
# Regular expression should be able to parse version strings such as # 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' # '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<version>(?P<major>\d+)\.(?P<minor>\d+)\.(?P<subminor>\d+))((rc(?P<release_candidate>\d+))|dev)?-CAPI-(?P<capi_version>\d+\.\d+\.\d+)$') version_regex = re.compile(
r'^(?P<version>(?P<major>\d+)\.(?P<minor>\d+)\.(?P<subminor>\d+))'
r'((rc(?P<release_candidate>\d+))|dev)?-CAPI-(?P<capi_version>\d+\.\d+\.\d+)( r\d+)?$'
)
def geos_version_info(): def geos_version_info():
""" """
Returns a dictionary containing the various version metadata parsed from Returns a dictionary containing the various version metadata parsed from
@ -119,8 +122,10 @@ def geos_version_info():
""" """
ver = geos_version().decode() ver = geos_version().decode()
m = version_regex.match(ver) m = version_regex.match(ver)
if not m: raise GEOSException('Could not parse version info string "%s"' % ver) if not m:
return dict((key, m.group(key)) for key in ('version', 'release_candidate', 'capi_version', 'major', 'minor', 'subminor')) 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. # Version numbers and whether or not prepared geometry support is available.
_verinfo = geos_version_info() _verinfo = geos_version_info()

View File

@ -1063,15 +1063,17 @@ class GEOSTest(unittest.TestCase, TestDataMixin):
self.assertEqual(mls.interpolate(17), Point(10, 7)) self.assertEqual(mls.interpolate(17), Point(10, 7))
def test_geos_version(self): 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 from django.contrib.gis.geos.libgeos import version_regex
versions = [ ('3.0.0rc4-CAPI-1.3.3', '3.0.0'), versions = [('3.0.0rc4-CAPI-1.3.3', '3.0.0', '1.3.3'),
('3.0.0-CAPI-1.4.1', '3.0.0'), ('3.0.0-CAPI-1.4.1', '3.0.0', '1.4.1'),
('3.4.0dev-CAPI-1.8.0', '3.4.0') ] ('3.4.0dev-CAPI-1.8.0', '3.4.0', '1.8.0'),
for v, expected in versions: ('3.4.0dev-CAPI-1.8.0 r0', '3.4.0', '1.8.0')]
m = version_regex.match(v) for v_init, v_geos, v_capi in versions:
self.assertTrue(m) m = version_regex.match(v_init)
self.assertEqual(m.group('version'), expected) 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(): def suite():