mirror of https://github.com/scrapy/scrapy.git
Code cleanup scrapy.utils.python.WeakKeyCache #4684
This commit is contained in:
parent
5265853937
commit
e7a58fe157
|
|
@ -273,18 +273,6 @@ def equal_attributes(obj1, obj2, attributes):
|
|||
return True
|
||||
|
||||
|
||||
class WeakKeyCache:
|
||||
|
||||
def __init__(self, default_factory):
|
||||
self.default_factory = default_factory
|
||||
self._weakdict = weakref.WeakKeyDictionary()
|
||||
|
||||
def __getitem__(self, key):
|
||||
if key not in self._weakdict:
|
||||
self._weakdict[key] = self.default_factory(key)
|
||||
return self._weakdict[key]
|
||||
|
||||
|
||||
@deprecated
|
||||
def retry_on_eintr(function, *args, **kw):
|
||||
"""Run a function and retry it while getting EINTR errors"""
|
||||
|
|
|
|||
|
|
@ -9,7 +9,7 @@ from warnings import catch_warnings
|
|||
|
||||
from scrapy.utils.python import (
|
||||
memoizemethod_noargs, binary_is_text, equal_attributes,
|
||||
WeakKeyCache, get_func_args, to_bytes, to_unicode,
|
||||
get_func_args, to_bytes, to_unicode,
|
||||
without_none_values, MutableChain)
|
||||
|
||||
|
||||
|
|
@ -155,22 +155,6 @@ class UtilsPythonTestCase(unittest.TestCase):
|
|||
a.meta['z'] = 2
|
||||
self.assertFalse(equal_attributes(a, b, [compare_z, 'x']))
|
||||
|
||||
def test_weakkeycache(self):
|
||||
class _Weakme:
|
||||
pass
|
||||
|
||||
_values = count()
|
||||
wk = WeakKeyCache(lambda k: next(_values))
|
||||
k = _Weakme()
|
||||
v = wk[k]
|
||||
self.assertEqual(v, wk[k])
|
||||
self.assertNotEqual(v, wk[_Weakme()])
|
||||
self.assertEqual(v, wk[k])
|
||||
del k
|
||||
for _ in range(100):
|
||||
if wk._weakdict:
|
||||
gc.collect()
|
||||
self.assertFalse(len(wk._weakdict))
|
||||
|
||||
def test_get_func_args(self):
|
||||
def f1(a, b, c):
|
||||
|
|
|
|||
Loading…
Reference in New Issue