Skip to content

Commit

Permalink
Replace self._val with separate properties (#1397)
Browse files Browse the repository at this point in the history
  • Loading branch information
bdraco authored Oct 30, 2024
1 parent 41af4a3 commit 3f2c26f
Show file tree
Hide file tree
Showing 5 changed files with 227 additions and 136 deletions.
1 change: 1 addition & 0 deletions CHANGES/1397.misc.rst
46 changes: 41 additions & 5 deletions tests/test_pickle.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@


def test_pickle():
u1 = URL("test")
u1 = URL("picklepickle")
hash(u1)
v = pickle.dumps(u1)
u2 = pickle.loads(v)
Expand All @@ -16,9 +16,45 @@ def test_pickle():


def test_default_style_state():
u = URL("test")
hash(u)
val = ("test", "test", "test", "test", "test")
u = object.__new__(URL)
val = ("set_state", "set_state", "set_state", "set_state", "set_state")
u.__setstate__((None, {"_val": val, "_strict": False, "_cache": {"hash": 1}}))
assert not u._cache
assert u._val == val
assert hash(u) != 1


def test_empty_url_is_not_cached():
u = URL.__new__(URL)
val = ("set_state", "set_state", "set_state", "set_state", "set_state")
u.__setstate__((None, {"_val": val, "_strict": False, "_cache": {"hash": 1}}))
assert u._val == val
assert hash(u) != 1


def test_pickle_does_not_pollute_cache():
"""Verify the unpickling does not pollute the cache.
Since unpickle will call URL.__new__ with default
args, we need to make sure that default args never
end up in the pre_encoded_url or encode_url cache.
"""
u1 = URL.__new__(URL)
u1._scheme = "this"
u1._netloc = "never.appears.any.where.else.in.tests"
u1._path = ""
u1._query = ""
u1._fragment = ""
hash(u1)
v = pickle.dumps(u1)
u2: URL = pickle.loads(v)
assert u1._cache
assert hash(u1) == hash(u2)
assert u2._scheme == "this"
assert u2._netloc == "never.appears.any.where.else.in.tests"
assert u2._path == ""
assert u2._query == ""
assert u2._fragment == ""
# Verify unpickling did not the cache wrong scheme
# for empty args.
assert URL().scheme == ""
assert URL("").scheme == ""
2 changes: 1 addition & 1 deletion tests/test_url_benchmarks.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ def test_raw_host_empty_cache(benchmark: BenchmarkFixture) -> None:
@benchmark
def _run() -> None:
for _ in range(100):
url._cache = {}
url._cache.pop("raw_host", None)
url.raw_host


Expand Down
7 changes: 1 addition & 6 deletions yarl/_parse.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import re
import unicodedata
from functools import lru_cache
from typing import Final, Union
from typing import Union
from urllib.parse import scheme_chars, uses_netloc

from ._quoters import QUOTER
Expand All @@ -20,11 +20,6 @@
USES_AUTHORITY = frozenset(uses_netloc)

SplitURL = tuple[str, str, str, str, str]
SCHEME: Final[int] = 0
NETLOC: Final[int] = 1
PATH: Final[int] = 2
QUERY: Final[int] = 3
FRAGMENT: Final[int] = 4


def split_url(url: str) -> SplitURL:
Expand Down
Loading

0 comments on commit 3f2c26f

Please sign in to comment.