Code cleanup scrapy.utils.python.WeakKeyCache #4684

This commit is contained in:
Kshitij Sharma 2020-07-29 10:16:18 +05:30
parent 5265853937
commit e7a58fe157
2 changed files with 1 additions and 29 deletions

View File

@ -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"""

View File

@ -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):