jenkins-bot has submitted this change. ( 
https://gerrit.wikimedia.org/r/c/pywikibot/core/+/629087 )

Change subject: [IMPR] Use functools.cache for _code_fam_from_url function
......................................................................

[IMPR] Use functools.cache for _code_fam_from_url function

Change-Id: I5b159c6c6397f1bce9c24946eb56c1b8e87596cc
---
M pywikibot/__init__.py
M tests/utils.py
2 files changed, 28 insertions(+), 24 deletions(-)

Approvals:
  Mpaa: Looks good to me, approved
  jenkins-bot: Verified



diff --git a/pywikibot/__init__.py b/pywikibot/__init__.py
index 97dadda..041dcfa 100644
--- a/pywikibot/__init__.py
+++ b/pywikibot/__init__.py
@@ -63,11 +63,18 @@
     issue_deprecation_warning,
     normalize_username,
     MediaWikiVersion as _MediaWikiVersion,
-    redirect_func,
     ModuleDeprecationWrapper as _ModuleDeprecationWrapper,
+    PYTHON_VERSION,
+    redirect_func,
 )
 from pywikibot.tools.formatter import color_format

+if PYTHON_VERSION >= (3, 9, 0):
+    from functools import cache
+else:
+    from functools import lru_cache
+    cache = lru_cache(None)
+

 textlib_methods = (
     'categoryFormat', 'compileLinkR', 'extract_templates_and_params',
@@ -1136,36 +1143,33 @@


 _sites = {}
-_url_cache = {}  # The code/fam pair for each URL


-def _code_fam_from_url(url):
+@cache
+def _code_fam_from_url(url: str):
     """Set url to cache and get code and family from cache.

     Site helper method.
     @param url: The site URL to get code and family
-    @type url: str
     @raises pywikibot.exceptions.SiteDefinitionError: Unknown URL
     """
-    if url not in _url_cache:
-        matched_sites = []
-        # Iterate through all families and look, which does apply to
-        # the given URL
-        for fam in config.family_files:
-            family = Family.load(fam)
-            code = family.from_url(url)
-            if code is not None:
-                matched_sites.append((code, family))
+    matched_sites = []
+    # Iterate through all families and look, which does apply to
+    # the given URL
+    for fam in config.family_files:
+        family = Family.load(fam)
+        code = family.from_url(url)
+        if code is not None:
+            matched_sites.append((code, family))

-        if not matched_sites:
-            # TODO: As soon as AutoFamily is ready, try and use an
-            #       AutoFamily
-            raise SiteDefinitionError("Unknown URL '{0}'.".format(url))
-        if len(matched_sites) > 1:
-            warning('Found multiple matches for URL "{0}": {1} (use first)'
-                    .format(url, ', '.join(str(s) for s in matched_sites)))
-        _url_cache[url] = matched_sites[0]
-    return _url_cache[url]
+    if not matched_sites:
+        # TODO: As soon as AutoFamily is ready, try and use an
+        #       AutoFamily
+        raise SiteDefinitionError("Unknown URL '{}'.".format(url))
+    if len(matched_sites) > 1:
+        warning('Found multiple matches for URL "{}": {} (use first)'
+                .format(url, ', '.join(str(s) for s in matched_sites)))
+    return matched_sites[0]


 @_deprecate_arg('sysop', None)
diff --git a/tests/utils.py b/tests/utils.py
index 0bea97d..2dbab7b 100644
--- a/tests/utils.py
+++ b/tests/utils.py
@@ -641,7 +641,7 @@

 @contextmanager
 def empty_sites():
-    """Empty pywikibot._sites and pywikibot._url_cache cache on entry point."""
+    """Empty pywikibot _sites and _code_fam_from_url cache on entry point."""
     pywikibot._sites = {}
-    pywikibot._url_cache = {}
+    pywikibot._code_fam_from_url.cache_clear()
     yield

--
To view, visit https://gerrit.wikimedia.org/r/c/pywikibot/core/+/629087
To unsubscribe, or for help writing mail filters, visit 
https://gerrit.wikimedia.org/r/settings

Gerrit-Project: pywikibot/core
Gerrit-Branch: master
Gerrit-Change-Id: I5b159c6c6397f1bce9c24946eb56c1b8e87596cc
Gerrit-Change-Number: 629087
Gerrit-PatchSet: 5
Gerrit-Owner: Xqt <[email protected]>
Gerrit-Reviewer: Mpaa <[email protected]>
Gerrit-Reviewer: jenkins-bot
Gerrit-MessageType: merged
_______________________________________________
Pywikibot-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/pywikibot-commits

Reply via email to