Skip to content

Commit e2c5873

Browse files
committed
[PYTHON-1174] Allow server versions as long as we recognize the major version
1 parent d3c6d81 commit e2c5873

File tree

3 files changed

+30
-21
lines changed

3 files changed

+30
-21
lines changed

CHANGELOG.rst

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ Features
88
* ssl context and cloud support for Eventlet (PYTHON-1162)
99
* Cloud Twisted support (PYTHON-1163)
1010
* Add additional_write_policy and read_repair to system schema parsing (PYTHON-1048)
11-
* Remove *read_repair_chance* table options (PYTHON-1140)
11+
* Remove *read_repair_chance table options (PYTHON-1140)
12+
* Flexible version parsing (PYTHON-1174)
1213
* [GRAPH] Ability to execute Fluent Graph queries asynchronously (PYTHON-1129)
1314
1415
Bug Fixes

cassandra/util.py

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1929,19 +1929,31 @@ def __init__(self, version):
19291929
version_without_prerelease = version
19301930
parts = list(reversed(version_without_prerelease.split('.')))
19311931
if len(parts) > 4:
1932-
raise ValueError("Invalid version: {}. Only 4 "
1933-
"components plus prerelease are supported".format(version))
1932+
prerelease_string = "-{}".format(self.prerelease) if self.prerelease else ""
1933+
log.warning("Unrecognized version: {}. Only 4 components plus prerelease are supported. "
1934+
"Assuming version as {}{}".format(version, '.'.join(parts[:-5:-1]), prerelease_string))
19341935

1935-
self.major = int(parts.pop())
1936-
self.minor = int(parts.pop()) if parts else 0
1937-
self.patch = int(parts.pop()) if parts else 0
1936+
try:
1937+
self.major = int(parts.pop())
1938+
except ValueError:
1939+
six.reraise(
1940+
ValueError,
1941+
ValueError("Couldn't parse version {}. Version should start with a number".format(version)),
1942+
sys.exc_info()[2]
1943+
)
1944+
try:
1945+
self.minor = int(parts.pop()) if parts else 0
1946+
self.patch = int(parts.pop()) if parts else 0
19381947

1939-
if parts: # we have a build version
1940-
build = parts.pop()
1941-
try:
1942-
self.build = int(build)
1943-
except ValueError:
1944-
self.build = build
1948+
if parts: # we have a build version
1949+
build = parts.pop()
1950+
try:
1951+
self.build = int(build)
1952+
except ValueError:
1953+
self.build = build
1954+
except ValueError:
1955+
assumed_version = "{}.{}.{}.{}-{}".format(self.major, self.minor, self.patch, self.build, self.prerelease)
1956+
log.warning("Unrecognized version {}. Assuming version as {}".format(version, assumed_version))
19451957

19461958
def __hash__(self):
19471959
return self._version

tests/unit/test_util_types.py

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,11 @@ def test_version_parsing(self):
217217
('3.55.1.build12', (3, 55, 1, 'build12', 0)),
218218
('3.55.1.20190429-TEST', (3, 55, 1, 20190429, 'TEST')),
219219
('4.0-SNAPSHOT', (4, 0, 0, 0, 'SNAPSHOT')),
220+
('1.0.5.4.3', (1, 0, 5, 4, 0)),
221+
('1-SNAPSHOT', (1, 0, 0, 0, 'SNAPSHOT')),
222+
('4.0.1.2.3.4.5-ABC-123-SNAP-TEST.blah', (4, 0, 1, 2, 'ABC-123-SNAP-TEST.blah')),
223+
('2.1.hello', (2, 1, 0, 0, 0)),
224+
('2.test.1', (2, 0, 0, 0, 0)),
220225
]
221226

222227
for str_version, expected_result in versions:
@@ -229,18 +234,9 @@ def test_version_parsing(self):
229234
self.assertEqual(v.prerelease, expected_result[4])
230235

231236
# not supported version formats
232-
with self.assertRaises(ValueError):
233-
Version('2.1.hello')
234-
235-
with self.assertRaises(ValueError):
236-
Version('2.test.1')
237-
238237
with self.assertRaises(ValueError):
239238
Version('test.1.0')
240239

241-
with self.assertRaises(ValueError):
242-
Version('1.0.0.0.1')
243-
244240
def test_version_compare(self):
245241
# just tests a bunch of versions
246242

0 commit comments

Comments
 (0)