|
19 | 19 | # https://tools.ietf.org/html/rfc3696
|
20 | 20 |
|
21 | 21 | import string
|
| 22 | +from functools import lru_cache |
22 | 23 | from importlib import resources
|
23 | 24 | from typing import Optional
|
24 | 25 |
|
|
31 | 32 | FRAGMENT_SAFE_CHARACTERS = URL_SAFE_CHARACTERS | {"?", "/"}
|
32 | 33 |
|
33 | 34 |
|
34 |
| -# This file is sourced from http://data.iana.org/TLD/tlds-alpha-by-domain.txt |
35 |
| -# The file contains additional information about the date that it was last updated. |
36 |
| -traversable = resources.files("hypothesis.vendor") / "tlds-alpha-by-domain.txt" |
37 |
| -_comment, *_tlds = traversable.read_text(encoding="utf-8").splitlines() |
38 |
| -assert _comment.startswith("#") |
| 35 | +@lru_cache(maxsize=1) |
| 36 | +def get_top_level_domains() -> tuple[str, ...]: |
| 37 | + # This file is sourced from http://data.iana.org/TLD/tlds-alpha-by-domain.txt |
| 38 | + # The file contains additional information about the date that it was last updated. |
| 39 | + traversable = resources.files("hypothesis.vendor") / "tlds-alpha-by-domain.txt" |
| 40 | + _comment, *_tlds = traversable.read_text(encoding="utf-8").splitlines() |
| 41 | + assert _comment.startswith("#") |
39 | 42 |
|
40 |
| -# Remove special-use domain names from the list. For more discussion |
41 |
| -# see https://github.com/HypothesisWorks/hypothesis/pull/3572 |
42 |
| -TOP_LEVEL_DOMAINS = ["COM", *sorted((d for d in _tlds if d != "ARPA"), key=len)] |
| 43 | + # Remove special-use domain names from the list. For more discussion |
| 44 | + # see https://github.com/HypothesisWorks/hypothesis/pull/3572 |
| 45 | + return ("COM", *sorted((d for d in _tlds if d != "ARPA"), key=len)) |
43 | 46 |
|
44 | 47 |
|
45 | 48 | class DomainNameStrategy(st.SearchStrategy):
|
@@ -101,7 +104,7 @@ def do_draw(self, data):
|
101 | 104 | # prevent us from generating at least a 1 character subdomain.
|
102 | 105 | # 3 - Randomize the TLD between upper and lower case characters.
|
103 | 106 | domain = data.draw(
|
104 |
| - st.sampled_from(TOP_LEVEL_DOMAINS) |
| 107 | + st.sampled_from(get_top_level_domains()) |
105 | 108 | .filter(lambda tld: len(tld) + 2 <= self.max_length)
|
106 | 109 | .flatmap(
|
107 | 110 | lambda tld: st.tuples(
|
|
0 commit comments