Handle IndexError during version string parsing

Some odd version strings can cause SemanticVersion.from_pip_string()
to raise IndexError.  This change converts IndexError to ValueError.

Change-Id: Ic3046817b6c5808c61c4a3bc3d912501e6a52274
Closes-Bug: #1570145
This commit is contained in:
Randall Nortman 2016-04-13 21:05:07 -04:00
parent d4e29cb5d2
commit a27f51242b
2 changed files with 11 additions and 1 deletions

View File

@ -99,7 +99,8 @@ class SkipFileWrites(base.BaseTestCase):
(self.option_value.lower() in options.TRUE_VALUES
or self.env_value is not None))
_changelog_content = """04316fe\x00Make python\x00 (review/monty_taylor/27519)
_changelog_content = """7780758\x00Break parser\x00 (tag: 1_foo.1)
04316fe\x00Make python\x00 (review/monty_taylor/27519)
378261a\x00Add an integration test script.\x00
3c373ac\x00Merge "Lib\x00 (HEAD, tag: 2013.2.rc2, tag: 2013.2, mile-proposed)
182feb3\x00Fix pip invocation for old versions of pip.\x00 (tag: 0.5.17)
@ -159,6 +160,7 @@ class GitLogsTest(base.BaseTestCase):
self.assertNotIn("ev)il", changelog_contents)
self.assertNotIn("e(vi)l", changelog_contents)
self.assertNotIn('Merge "', changelog_contents)
self.assertNotIn('1_foo.1', changelog_contents)
def test_generate_authors(self):
author_old = u"Foo Foo <email@foo.com>"

View File

@ -142,6 +142,14 @@ class SemanticVersion(object):
ever released - we're treating that as a critical bug that we ever
made them and have stopped doing that.
"""
try:
return klass._from_pip_string_unsafe(version_string)
except IndexError:
raise ValueError("Invalid version %r" % version_string)
@classmethod
def _from_pip_string_unsafe(klass, version_string):
# Versions need to start numerically, ignore if not
if not version_string[:1].isdigit():
raise ValueError("Invalid version %r" % version_string)