Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix setting the idna_encode_size #1357

Merged
merged 3 commits into from
Oct 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGES/1357.breaking.rst
3 changes: 3 additions & 0 deletions tests/test_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ def test_cache_configure_explicit() -> None:
idna_encode_size=128,
encode_host_size=128,
)
assert yarl.cache_info()["idna_decode"].maxsize == 128
assert yarl.cache_info()["idna_encode"].maxsize == 128
assert yarl.cache_info()["encode_host"].maxsize == 128


def test_cache_configure_waring() -> None:
Expand Down
13 changes: 8 additions & 5 deletions yarl/_url.py
Original file line number Diff line number Diff line change
Expand Up @@ -1763,17 +1763,18 @@ def cache_info() -> CacheInfo:
@rewrite_module
def cache_configure(
*,
idna_encode_size: Union[int, None, object] = _DEFAULT_IDNA_SIZE,
idna_encode_size: Union[int, None] = _DEFAULT_IDNA_SIZE,
idna_decode_size: Union[int, None] = _DEFAULT_IDNA_SIZE,
ip_address_size: Union[int, None, object] = _SENTINEL,
host_validate_size: Union[int, None, object] = _SENTINEL,
encode_host_size: Union[int, None] = _DEFAULT_ENCODE_SIZE,
encode_host_size: Union[int, None, object] = _SENTINEL,
) -> None:
"""Configure LRU cache sizes."""
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 not None:
if encode_host_size is _SENTINEL:
encode_host_size = _DEFAULT_ENCODE_SIZE
for size in (ip_address_size, host_validate_size):
if size is not _SENTINEL:
warnings.warn(
Expand All @@ -1790,10 +1791,12 @@ def cache_configure(
elif size is _SENTINEL:
size = _DEFAULT_ENCODE_SIZE
if TYPE_CHECKING:
assert isinstance(size, int)
assert not isinstance(size, object)
if size > encode_host_size:
encode_host_size = size

if TYPE_CHECKING:
assert not isinstance(encode_host_size, object)
_encode_host = lru_cache(encode_host_size)(_encode_host.__wrapped__)
_idna_decode = lru_cache(idna_decode_size)(_idna_decode.__wrapped__)
_idna_encode = lru_cache(idna_decode_size)(_idna_encode.__wrapped__)
_idna_encode = lru_cache(idna_encode_size)(_idna_encode.__wrapped__)