Skip to content

Commit

Permalink
Fix loop variable being redefined in cache_configure (#1363)
Browse files Browse the repository at this point in the history
  • Loading branch information
bdraco authored Oct 21, 2024
1 parent f42f34e commit ed2d648
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 18 deletions.
1 change: 1 addition & 0 deletions CHANGES/1363.breaking.rst
33 changes: 33 additions & 0 deletions tests/test_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,39 @@ def test_cache_configure_None() -> None:
)


def test_cache_configure_None_including_deprecated() -> None:
msg = (
r"cache_configure\(\) no longer accepts the ip_address_size "
r"or host_validate_size arguments, they are used to set the "
r"encode_host_size instead and will be removed in the future"
)
with pytest.warns(DeprecationWarning, match=msg):
yarl.cache_configure(
idna_decode_size=None,
idna_encode_size=None,
encode_host_size=None,
ip_address_size=None,
host_validate_size=None,
)
assert yarl.cache_info()["idna_decode"].maxsize is None
assert yarl.cache_info()["idna_encode"].maxsize is None
assert yarl.cache_info()["encode_host"].maxsize is None


def test_cache_configure_None_only_deprecated() -> None:
msg = (
r"cache_configure\(\) no longer accepts the ip_address_size "
r"or host_validate_size arguments, they are used to set the "
r"encode_host_size instead and will be removed in the future"
)
with pytest.warns(DeprecationWarning, match=msg):
yarl.cache_configure(
ip_address_size=None,
host_validate_size=None,
)
assert yarl.cache_info()["encode_host"].maxsize is None


def test_cache_configure_explicit() -> None:
yarl.cache_configure(
idna_decode_size=128,
Expand Down
39 changes: 21 additions & 18 deletions yarl/_url.py
Original file line number Diff line number Diff line change
Expand Up @@ -1430,27 +1430,30 @@ def cache_configure(
global _idna_decode, _idna_encode, _encode_host
# ip_address_size, host_validate_size are no longer
# used, but are kept for backwards compatibility.
if encode_host_size is _SENTINEL:
encode_host_size = _DEFAULT_ENCODE_SIZE
if ip_address_size is not _SENTINEL or host_validate_size is not _SENTINEL:
warnings.warn(
"cache_configure() no longer accepts the "
"ip_address_size or host_validate_size arguments, "
"they are used to set the encode_host_size instead "
"and will be removed in the future",
DeprecationWarning,
stacklevel=2,
)

if encode_host_size is not None:
for size in (ip_address_size, host_validate_size):
if size is not _SENTINEL:
warnings.warn(
"cache_configure() no longer accepts the "
"ip_address_size or host_validate_size arguments, "
"they are used to set the encode_host_size instead "
"and will be removed in the future",
DeprecationWarning,
stacklevel=2,
)
if size is None:
encode_host_size = None
break
elif size is _SENTINEL:
size = _DEFAULT_ENCODE_SIZE
if TYPE_CHECKING:
assert not isinstance(size, object)
if size > encode_host_size:
encode_host_size = size
elif encode_host_size is _SENTINEL:
if size is not _SENTINEL:
encode_host_size = size
elif size is not _SENTINEL:
if TYPE_CHECKING:
assert isinstance(size, int)
assert isinstance(encode_host_size, int)
encode_host_size = max(size, encode_host_size)
if encode_host_size is _SENTINEL:
encode_host_size = _DEFAULT_ENCODE_SIZE

if TYPE_CHECKING:
assert not isinstance(encode_host_size, object)
Expand Down

0 comments on commit ed2d648

Please sign in to comment.