deprecated weakkeycache by specifying in __init__

This commit is contained in:
Kshitij Sharma 2020-08-05 09:11:59 +05:30
parent 49337bd2ae
commit 9d84289109
3 changed files with 27 additions and 2 deletions

View File

@ -7,10 +7,12 @@ import inspect
import re
import sys
import weakref
import warnings
from functools import partial, wraps
from itertools import chain
from scrapy.utils.decorators import deprecated
from scrapy.exceptions import ScrapyDeprecationWarning
def flatten(x):
@ -275,10 +277,10 @@ def equal_attributes(obj1, obj2, attributes):
return True
@deprecated
class WeakKeyCache:
def __init__(self, default_factory):
warnings.warn("Call to deprecated Class WeakKeyCache", category=ScrapyDeprecationWarning, stacklevel=2)
self.default_factory = default_factory
self._weakdict = weakref.WeakKeyDictionary()

3
scrapy/utils/tester.py Normal file
View File

@ -0,0 +1,3 @@
from scrapy.utils.decorators import deprecated

View File

@ -1,15 +1,18 @@
import functools
import gc
import operator
import platform
import unittest
from itertools import count
from sys import version_info
from warnings import catch_warnings
from scrapy.utils.python import (
memoizemethod_noargs, binary_is_text, equal_attributes,
get_func_args, to_bytes, to_unicode,
WeakKeyCache, get_func_args, to_bytes, to_unicode,
without_none_values, MutableChain)
__doctests__ = ['scrapy.utils.python']
@ -152,6 +155,23 @@ 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):
pass