Skip to content

Commit 06c6555

Browse files
authored
Propagate int-max-str-digits ValueError (#1155)
1 parent 905c90c commit 06c6555

3 files changed

Lines changed: 51 additions & 1 deletion

File tree

src/packaging/version.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -405,7 +405,13 @@ def __init__(self, version: str) -> None:
405405
try:
406406
self._release = tuple(map(int, version.split(".")))
407407
except ValueError:
408-
raise InvalidVersion(f"Invalid version: {version!r}") from None
408+
# Empty parts (from "1..2", ".1", etc.) are invalid versions.
409+
# Any other ValueError (e.g. int str-digits limit) should
410+
# propagate to the caller.
411+
if "" in version.split("."):
412+
raise InvalidVersion(f"Invalid version: {version!r}") from None
413+
# TODO: remove "no cover" when Python 3.9 is dropped.
414+
raise # pragma: no cover
409415

410416
self._epoch = 0
411417
self._pre = None

tests/test_specifiers.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import itertools
88
import operator
99
import re
10+
import sys
1011
import typing
1112

1213
import pytest
@@ -2065,6 +2066,27 @@ def test_contains_rejects_invalid_specifier(
20652066
spec = SpecifierSet(specifier, prereleases=True)
20662067
assert not spec.contains(input)
20672068

2069+
@pytest.mark.skipif(
2070+
not hasattr(sys, "get_int_max_str_digits"),
2071+
reason="requires int max str digits limit",
2072+
)
2073+
@pytest.mark.parametrize(
2074+
"specifier",
2075+
[
2076+
">=" + "1" * 5000,
2077+
">=1.0a" + "1" * 5000,
2078+
],
2079+
)
2080+
def test_contains_oversized_version_raises_valueerror(self, specifier: str) -> None:
2081+
old = sys.get_int_max_str_digits()
2082+
sys.set_int_max_str_digits(4300)
2083+
try:
2084+
spec = SpecifierSet(specifier)
2085+
with pytest.raises(ValueError, match="Exceeds the limit"):
2086+
spec.contains("1.0")
2087+
finally:
2088+
sys.set_int_max_str_digits(old)
2089+
20682090
@pytest.mark.parametrize(
20692091
("version", "specifier", "expected"),
20702092
[

tests/test_version.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,28 @@ def test_invalid_versions(self, version: str) -> None:
213213
with pytest.raises(InvalidVersion):
214214
Version(version)
215215

216+
@pytest.mark.skipif(
217+
not hasattr(sys, "get_int_max_str_digits"),
218+
reason="requires int max str digits limit",
219+
)
220+
@pytest.mark.parametrize(
221+
"version",
222+
[
223+
# Simple path (digits only)
224+
"1" * 5000,
225+
# Regex path (has pre-release)
226+
"1.0a" + "1" * 5000,
227+
],
228+
)
229+
def test_oversized_version_raises_valueerror(self, version: str) -> None:
230+
old = sys.get_int_max_str_digits()
231+
sys.set_int_max_str_digits(4300)
232+
try:
233+
with pytest.raises(ValueError, match="Exceeds the limit"):
234+
Version(version)
235+
finally:
236+
sys.set_int_max_str_digits(old)
237+
216238
@pytest.mark.parametrize(
217239
("version", "normalized"),
218240
[

0 commit comments

Comments
 (0)