Skip to content

Scrapy, a fast high-level web crawling & scraping framework for Python.

Merge branch 'master' into typing-utils

Andrey Rakhmatullin712ee9884871
diff --cc scrapy/utils/datatypes.py
index 0f6bdc5ab,14b6e79ff..d5b9544cc
--- a/scrapy/utils/datatypes.py
+++ b/scrapy/utils/datatypes.py
@@@ -5,13 -5,14 +5,17 @@@ Python Standard Library
This module must not depend on any module outside the Standard Library.
"""
+ import collections
+ import warnings
import weakref
from collections.abc import Mapping
- from typing import Any, Optional, OrderedDict, Sequence, TypeVar
-from typing import Any, AnyStr
++from typing import Any, AnyStr, Optional, OrderedDict, Sequence, TypeVar
+
+ from scrapy.exceptions import ScrapyDeprecationWarning
+_KT = TypeVar("_KT")
+_VT = TypeVar("_VT")
+
class CaselessDict(dict):
__slots__ = ()
@@@ -67,7 -80,49 +83,49 @@@
return dict.pop(self, self.normkey(key), *args)
+ class CaseInsensitiveDict(collections.UserDict):
+ """A dict-like structure that accepts strings or bytes
+ as keys and allows case-insensitive lookups.
+ """
+
+ def __init__(self, *args, **kwargs) -> None:
+ self._keys: dict = {}
+ super().__init__(*args, **kwargs)
+
+ def __getitem__(self, key: AnyStr) -> Any:
+ normalized_key = self._normkey(key)
+ return super().__getitem__(self._keys[normalized_key.lower()])
+
+ def __setitem__(self, key: AnyStr, value: Any) -> None:
+ normalized_key = self._normkey(key)
+ try:
+ lower_key = self._keys[normalized_key.lower()]
+ del self[lower_key]
+ except KeyError:
+ pass
+ super().__setitem__(normalized_key, self._normvalue(value))
+ self._keys[normalized_key.lower()] = normalized_key
+
+ def __delitem__(self, key: AnyStr) -> None:
+ normalized_key = self._normkey(key)
+ stored_key = self._keys.pop(normalized_key.lower())
+ super().__delitem__(stored_key)
+
+ def __contains__(self, key: AnyStr) -> bool: # type: ignore[override]
+ normalized_key = self._normkey(key)
+ return normalized_key.lower() in self._keys
+
+ def __repr__(self) -> str:
+ return f"<{self.__class__.__name__}: {super().__repr__()}>"
+
+ def _normkey(self, key: AnyStr) -> AnyStr:
+ return key
+
+ def _normvalue(self, value: Any) -> Any:
+ return value
+
+
-class LocalCache(collections.OrderedDict):
+class LocalCache(OrderedDict[_KT, _VT]):
"""Dictionary with a finite number of keys.
Older items expires first.
diff --cc scrapy/utils/gz.py
index c0eb77e07,77e0197d8..c7f74030e
--- a/scrapy/utils/gz.py
+++ b/scrapy/utils/gz.py
@@@ -18,13 -15,18 +18,13 @@@ def gunzip(data: bytes) -> bytes
try:
chunk = f.read1(8196)
output_list.append(chunk)
- except (IOError, EOFError, struct.error):
+ except (OSError, EOFError, struct.error):
# complete only if there is some data, otherwise re-raise
# see issue 87 about catching struct.error
- # some pages are quite small so output_list is empty and f.extrabuf
- # contains the whole page content
- if output_list or getattr(f, "extrabuf", None):
- try:
- output_list.append(f.extrabuf[-f.extrasize :])
- finally:
- break
- else:
- raise
+ # some pages are quite small so output_list is empty
+ if output_list:
+ break
+ raise
return b"".join(output_list)