Skip to content

Commit cb70ba8

Browse files
committed
[PATCH] utils: remove custom memoize decorator (streamlink#3486)
1 parent 6959fd4 commit cb70ba8

File tree

5 files changed

+16
-24
lines changed

5 files changed

+16
-24
lines changed

src/streamlink/compat.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,15 @@ def bytes(b, enc="ascii"):
5353
from HTMLParser import HTMLParser
5454
html_unescape = unescape = HTMLParser().unescape
5555

56+
try:
57+
from functools import lru_cache
58+
except ImportError:
59+
from backports.functools_lru_cache import lru_cache
5660

5761
getargspec = getattr(inspect, "getfullargspec", inspect.getargspec)
5862

5963

6064
__all__ = ["is_py2", "is_py3", "is_py33", "is_win32", "str", "bytes",
6165
"urlparse", "urlunparse", "urljoin", "parse_qsl", "quote",
6266
"unquote", "unquote_plus", "queue", "range", "urlencode", "devnull", "which",
63-
"izip", "urlsplit", "urlunsplit", "getargspec", "html_unescape"]
67+
"izip", "urlsplit", "urlunsplit", "getargspec", "html_unescape", "lru_cache"]

src/streamlink/plugins/wwenetwork.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,10 @@
55
import re
66

77
from streamlink import PluginError
8-
from streamlink.compat import parse_qsl, urlparse
8+
from streamlink.compat import lru_cache, parse_qsl, urlparse
99
from streamlink.plugin import Plugin, PluginArgument, PluginArguments
1010
from streamlink.plugin.api import useragents
1111
from streamlink.stream import HLSStream
12-
from streamlink.utils import memoize
1312
from streamlink.utils.times import seconds_to_hhmmss
1413

1514
log = logging.getLogger(__name__)
@@ -93,7 +92,7 @@ def login(self, email, password):
9392
return self.auth_token
9493

9594
@property
96-
@memoize
95+
@lru_cache(maxsize=128)
9796
def item_config(self):
9897
log.debug("Loading page config")
9998
p = urlparse(self.url)

src/streamlink/session.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@
1111
from requests.packages.urllib3.util.connection import allowed_gai_family
1212

1313
from streamlink import __version__, plugins
14-
from streamlink.compat import is_win32
14+
from streamlink.compat import is_win32, lru_cache
1515
from streamlink.exceptions import NoPluginError, PluginError
1616
from streamlink.logger import Logger, StreamlinkLogger
1717
from streamlink.options import Options
1818
from streamlink.plugin import api
19-
from streamlink.utils import memoize, update_scheme
19+
from streamlink.utils import update_scheme
2020
from streamlink.utils.l10n import Localization
2121

2222
# Ensure that the Logger class returned is Streamslink's for using the API (for backwards compatibility)
@@ -419,7 +419,7 @@ def set_logoutput(self, output):
419419
"""
420420
self.logger.set_output(output)
421421

422-
@memoize
422+
@lru_cache(maxsize=128)
423423
def resolve_url(self, url, follow_redirect=True):
424424
"""Attempts to find a plugin that can use this URL.
425425

src/streamlink/utils/__init__.py

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import functools
21
import json
32
import re
43
import xml.etree.ElementTree as ET
@@ -139,18 +138,6 @@ def rtmpparse(url):
139138
return tcurl, playpath
140139

141140

142-
def memoize(obj):
143-
cache = obj.cache = {}
144-
145-
@functools.wraps(obj)
146-
def memoizer(*args, **kwargs):
147-
key = str(args) + str(kwargs)
148-
if key not in cache:
149-
cache[key] = obj(*args, **kwargs)
150-
return cache[key]
151-
return memoizer
152-
153-
154141
def search_dict(data, key):
155142
"""
156143
Search for a key in a nested dict, or list of nested dicts, and return the values.

tests/test_session.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,12 @@ def test_override_plugins(self):
4848
self.assertEqual(plugins["testplugin"].__module__, "streamlink.plugin.testplugin_override")
4949

5050
def test_resolve_url(self):
51-
plugins = self.session.get_plugins()
52-
channel = self.session.resolve_url("http://test.se/channel")
53-
self.assertTrue(isinstance(channel, Plugin))
54-
self.assertTrue(isinstance(channel, plugins["testplugin"]))
51+
session = self.subject()
52+
plugins = session.get_plugins()
53+
plugin = session.resolve_url("http://test.se/channel")
54+
self.assertTrue(isinstance(plugin, Plugin))
55+
self.assertTrue(isinstance(plugin, plugins["testplugin"]))
56+
self.assertTrue(hasattr(session.resolve_url, "cache_info"), "resolve_url has a lookup cache")
5557

5658
def test_resolve_url_priority(self):
5759
from tests.plugins.testplugin import TestPlugin

0 commit comments

Comments
 (0)