prompt: We are currently solving the following issue within our repository. Here is the issue text:
  --- BEGIN ISSUE ---
  KeyError in is_generator_with_return_value
  
  ```Python traceback
  Traceback (most recent call last):
    File "/usr/local/lib/python3.8/site-packages/twisted/internet/defer.py", line 1418, in _inlineCallbacks
      result = g.send(result)
    File "/app/python/lib/python3.8/site-packages/scrapy/core/downloader/middleware.py", line 42, in process_request
      defer.returnValue((yield download_func(request=request, spider=spider)))
    File "/usr/local/lib/python3.8/site-packages/twisted/internet/defer.py", line 1362, in returnValue
      raise _DefGen_Return(val)
  twisted.internet.defer._DefGen_Return: <200 https://www.example.com>
  
  During handling of the above exception, another exception occurred:
  
  Traceback (most recent call last):
    File "/app/python/lib/python3.8/site-packages/scrapy/utils/defer.py", line 55, in mustbe_deferred
      result = f(*args, **kw)
    File "/app/python/lib/python3.8/site-packages/scrapy/core/spidermw.py", line 60, in process_spider_input
      return scrape_func(response, request, spider)
    File "/app/python/lib/python3.8/site-packages/scrapy/core/scraper.py", line 148, in call_spider
      warn_on_generator_with_return_value(spider, callback)
    File "/app/python/lib/python3.8/site-packages/scrapy/utils/misc.py", line 202, in warn_on_generator_with_return_value
      if is_generator_with_return_value(callable):
    File "/app/python/lib/python3.8/site-packages/scrapy/utils/misc.py", line 180, in is_generator_with_return_value
      return _generator_callbacks_cache[callable]
    File "/app/python/lib/python3.8/site-packages/scrapy/utils/datatypes.py", line 281, in __getitem__
      return super(LocalWeakReferencedCache, self).__getitem__(key)
    File "/usr/local/lib/python3.8/weakref.py", line 383, in __getitem__
      return self.data[ref(key)]
  KeyError: <weakref at 0x7f06ff011720; to 'method' at 0x7f07042b5e00 (parse_foo)>
  ```
  
  This is Scrapy 2.0.1. The problem happens only sometimes, but in different spiders in the same project.
  
  --- END ISSUE ---
  
  Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
  --- BEGIN FILES ---
  Path: `scrapy/utils/datatypes.py`
  Content:
  ```
  1 """
  2 This module contains data types used by Scrapy which are not included in the
  3 Python Standard Library.
  4 
  5 This module must not depend on any module outside the Standard Library.
  6 """
  7 
  8 import collections
  9 import weakref
  10 from collections.abc import Mapping
  11 
  12 
  13 class CaselessDict(dict):
  14 
  15     __slots__ = ()
  16 
  17     def __init__(self, seq=None):
  18         super(CaselessDict, self).__init__()
  19         if seq:
  20             self.update(seq)
  21 
  22     def __getitem__(self, key):
  23         return dict.__getitem__(self, self.normkey(key))
  24 
  25     def __setitem__(self, key, value):
  26         dict.__setitem__(self, self.normkey(key), self.normvalue(value))
  27 
  28     def __delitem__(self, key):
  29         dict.__delitem__(self, self.normkey(key))
  30 
  31     def __contains__(self, key):
  32         return dict.__contains__(self, self.normkey(key))
  33     has_key = __contains__
  34 
  35     def __copy__(self):
  36         return self.__class__(self)
  37     copy = __copy__
  38 
  39     def normkey(self, key):
  40         """Method to normalize dictionary key access"""
  41         return key.lower()
  42 
  43     def normvalue(self, value):
  44         """Method to normalize values prior to be setted"""
  45         return value
  46 
  47     def get(self, key, def_val=None):
  48         return dict.get(self, self.normkey(key), self.normvalue(def_val))
  49 
  50     def setdefault(self, key, def_val=None):
  51         return dict.setdefault(self, self.normkey(key), self.normvalue(def_val))
  52 
  53     def update(self, seq):
  54         seq = seq.items() if isinstance(seq, Mapping) else seq
  55         iseq = ((self.normkey(k), self.normvalue(v)) for k, v in seq)
  56         super(CaselessDict, self).update(iseq)
  57 
  58     @classmethod
  59     def fromkeys(cls, keys, value=None):
  60         return cls((k, value) for k in keys)
  61 
  62     def pop(self, key, *args):
  63         return dict.pop(self, self.normkey(key), *args)
  64 
  65 
  66 class LocalCache(collections.OrderedDict):
  67     """Dictionary with a finite number of keys.
  68 
  69     Older items expires first.
  70     """
  71 
  72     def __init__(self, limit=None):
  73         super(LocalCache, self).__init__()
  74         self.limit = limit
  75 
  76     def __setitem__(self, key, value):
  77         if self.limit:
  78             while len(self) >= self.limit:
  79                 self.popitem(last=False)
  80         super(LocalCache, self).__setitem__(key, value)
  81 
  82 
  83 class LocalWeakReferencedCache(weakref.WeakKeyDictionary):
  84     """
  85     A weakref.WeakKeyDictionary implementation that uses LocalCache as its
  86     underlying data structure, making it ordered and capable of being size-limited.
  87 
  88     Useful for memoization, while avoiding keeping received
  89     arguments in memory only because of the cached references.
  90 
  91     Note: like LocalCache and unlike weakref.WeakKeyDictionary,
  92     it cannot be instantiated with an initial dictionary.
  93     """
  94 
  95     def __init__(self, limit=None):
  96         super(LocalWeakReferencedCache, self).__init__()
  97         self.data = LocalCache(limit=limit)
  98 
  99     def __setitem__(self, key, value):
  100         try:
  101             super(LocalWeakReferencedCache, self).__setitem__(key, value)
  102         except TypeError:
  103             pass  # key is not weak-referenceable, skip caching
  104 
  105     def __getitem__(self, key):
  106         try:
  107             return super(LocalWeakReferencedCache, self).__getitem__(key)
  108         except TypeError:
  109             return None  # key is not weak-referenceable, it's not cached
  110 
  111 
  112 class SequenceExclude:
  113     """Object to test if an item is NOT within some sequence."""
  114 
  115     def __init__(self, seq):
  116         self.seq = seq
  117 
  118     def __contains__(self, item):
  119         return item not in self.seq
  120
  ```
  --- END FILES ---
  
  Please first localize the bug based on the issue statement, and then generate a patch according to the `git diff` format fenced by three backticks.
  
  Here is an example:
  
  ```diff
  diff --git a/examples/server_async.py b/examples/server_async.py
  --- a/examples/server_async.py
  +++ b/examples/server_async.py
  @@ -313,4 +313,4 @@
   
   
   if __name__ == "__main__":
  -    asyncio.run(run_async_server("."), debug=True)
  +    asyncio.run(run_async_server(), debug=True)
  diff --git a/examples/server_sync.py b/examples/server_sync.py
  --- a/examples/server_sync.py
  +++ b/examples/server_sync.py
  @@ -313,5 +313,5 @@
   
   
   if __name__ == "__main__":
  -    server = run_sync_server(".")
  +    server = run_sync_server()
       server.shutdown()
  
  ```
  
golden_diff: diff --git a/scrapy/utils/datatypes.py b/scrapy/utils/datatypes.py
  --- a/scrapy/utils/datatypes.py
  +++ b/scrapy/utils/datatypes.py
  @@ -105,8 +105,8 @@
       def __getitem__(self, key):
           try:
               return super(LocalWeakReferencedCache, self).__getitem__(key)
  -        except TypeError:
  -            return None  # key is not weak-referenceable, it's not cached
  +        except (TypeError, KeyError):
  +            return None  # key is either not weak-referenceable or not cached
   
   
   class SequenceExclude:
  
verification_info: {"golden_diff": "diff --git a/scrapy/utils/datatypes.py b/scrapy/utils/datatypes.py\n--- a/scrapy/utils/datatypes.py\n+++ b/scrapy/utils/datatypes.py\n@@ -105,8 +105,8 @@\n     def __getitem__(self, key):\n         try:\n             return super(LocalWeakReferencedCache, self).__getitem__(key)\n-        except TypeError:\n-            return None  # key is not weak-referenceable, it's not cached\n+        except (TypeError, KeyError):\n+            return None  # key is either not weak-referenceable or not cached\n \n \n class SequenceExclude:\n", "issue": "KeyError in is_generator_with_return_value\n\r\n```Python traceback\r\nTraceback (most recent call last):\r\n  File \"/usr/local/lib/python3.8/site-packages/twisted/internet/defer.py\", line 1418, in _inlineCallbacks\r\n    result = g.send(result)\r\n  File \"/app/python/lib/python3.8/site-packages/scrapy/core/downloader/middleware.py\", line 42, in process_request\r\n    defer.returnValue((yield download_func(request=request, spider=spider)))\r\n  File \"/usr/local/lib/python3.8/site-packages/twisted/internet/defer.py\", line 1362, in returnValue\r\n    raise _DefGen_Return(val)\r\ntwisted.internet.defer._DefGen_Return: <200 https://www.example.com>\r\n\r\nDuring handling of the above exception, another exception occurred:\r\n\r\nTraceback (most recent call last):\r\n  File \"/app/python/lib/python3.8/site-packages/scrapy/utils/defer.py\", line 55, in mustbe_deferred\r\n    result = f(*args, **kw)\r\n  File \"/app/python/lib/python3.8/site-packages/scrapy/core/spidermw.py\", line 60, in process_spider_input\r\n    return scrape_func(response, request, spider)\r\n  File \"/app/python/lib/python3.8/site-packages/scrapy/core/scraper.py\", line 148, in call_spider\r\n    warn_on_generator_with_return_value(spider, callback)\r\n  File \"/app/python/lib/python3.8/site-packages/scrapy/utils/misc.py\", line 202, in warn_on_generator_with_return_value\r\n    if is_generator_with_return_value(callable):\r\n  File \"/app/python/lib/python3.8/site-packages/scrapy/utils/misc.py\", line 180, in is_generator_with_return_value\r\n    return _generator_callbacks_cache[callable]\r\n  File \"/app/python/lib/python3.8/site-packages/scrapy/utils/datatypes.py\", line 281, in __getitem__\r\n    return super(LocalWeakReferencedCache, self).__getitem__(key)\r\n  File \"/usr/local/lib/python3.8/weakref.py\", line 383, in __getitem__\r\n    return self.data[ref(key)]\r\nKeyError: <weakref at 0x7f06ff011720; to 'method' at 0x7f07042b5e00 (parse_foo)>\r\n```\r\n\r\nThis is Scrapy 2.0.1. The problem happens only sometimes, but in different spiders in the same project.\n", "before_files": [{"content": "\"\"\"\nThis module contains data types used by Scrapy which are not included in the\nPython Standard Library.\n\nThis module must not depend on any module outside the Standard Library.\n\"\"\"\n\nimport collections\nimport weakref\nfrom collections.abc import Mapping\n\n\nclass CaselessDict(dict):\n\n    __slots__ = ()\n\n    def __init__(self, seq=None):\n        super(CaselessDict, self).__init__()\n        if seq:\n            self.update(seq)\n\n    def __getitem__(self, key):\n        return dict.__getitem__(self, self.normkey(key))\n\n    def __setitem__(self, key, value):\n        dict.__setitem__(self, self.normkey(key), self.normvalue(value))\n\n    def __delitem__(self, key):\n        dict.__delitem__(self, self.normkey(key))\n\n    def __contains__(self, key):\n        return dict.__contains__(self, self.normkey(key))\n    has_key = __contains__\n\n    def __copy__(self):\n        return self.__class__(self)\n    copy = __copy__\n\n    def normkey(self, key):\n        \"\"\"Method to normalize dictionary key access\"\"\"\n        return key.lower()\n\n    def normvalue(self, value):\n        \"\"\"Method to normalize values prior to be setted\"\"\"\n        return value\n\n    def get(self, key, def_val=None):\n        return dict.get(self, self.normkey(key), self.normvalue(def_val))\n\n    def setdefault(self, key, def_val=None):\n        return dict.setdefault(self, self.normkey(key), self.normvalue(def_val))\n\n    def update(self, seq):\n        seq = seq.items() if isinstance(seq, Mapping) else seq\n        iseq = ((self.normkey(k), self.normvalue(v)) for k, v in seq)\n        super(CaselessDict, self).update(iseq)\n\n    @classmethod\n    def fromkeys(cls, keys, value=None):\n        return cls((k, value) for k in keys)\n\n    def pop(self, key, *args):\n        return dict.pop(self, self.normkey(key), *args)\n\n\nclass LocalCache(collections.OrderedDict):\n    \"\"\"Dictionary with a finite number of keys.\n\n    Older items expires first.\n    \"\"\"\n\n    def __init__(self, limit=None):\n        super(LocalCache, self).__init__()\n        self.limit = limit\n\n    def __setitem__(self, key, value):\n        if self.limit:\n            while len(self) >= self.limit:\n                self.popitem(last=False)\n        super(LocalCache, self).__setitem__(key, value)\n\n\nclass LocalWeakReferencedCache(weakref.WeakKeyDictionary):\n    \"\"\"\n    A weakref.WeakKeyDictionary implementation that uses LocalCache as its\n    underlying data structure, making it ordered and capable of being size-limited.\n\n    Useful for memoization, while avoiding keeping received\n    arguments in memory only because of the cached references.\n\n    Note: like LocalCache and unlike weakref.WeakKeyDictionary,\n    it cannot be instantiated with an initial dictionary.\n    \"\"\"\n\n    def __init__(self, limit=None):\n        super(LocalWeakReferencedCache, self).__init__()\n        self.data = LocalCache(limit=limit)\n\n    def __setitem__(self, key, value):\n        try:\n            super(LocalWeakReferencedCache, self).__setitem__(key, value)\n        except TypeError:\n            pass  # key is not weak-referenceable, skip caching\n\n    def __getitem__(self, key):\n        try:\n            return super(LocalWeakReferencedCache, self).__getitem__(key)\n        except TypeError:\n            return None  # key is not weak-referenceable, it's not cached\n\n\nclass SequenceExclude:\n    \"\"\"Object to test if an item is NOT within some sequence.\"\"\"\n\n    def __init__(self, seq):\n        self.seq = seq\n\n    def __contains__(self, item):\n        return item not in self.seq\n", "path": "scrapy/utils/datatypes.py"}], "after_files": [{"content": "\"\"\"\nThis module contains data types used by Scrapy which are not included in the\nPython Standard Library.\n\nThis module must not depend on any module outside the Standard Library.\n\"\"\"\n\nimport collections\nimport weakref\nfrom collections.abc import Mapping\n\n\nclass CaselessDict(dict):\n\n    __slots__ = ()\n\n    def __init__(self, seq=None):\n        super(CaselessDict, self).__init__()\n        if seq:\n            self.update(seq)\n\n    def __getitem__(self, key):\n        return dict.__getitem__(self, self.normkey(key))\n\n    def __setitem__(self, key, value):\n        dict.__setitem__(self, self.normkey(key), self.normvalue(value))\n\n    def __delitem__(self, key):\n        dict.__delitem__(self, self.normkey(key))\n\n    def __contains__(self, key):\n        return dict.__contains__(self, self.normkey(key))\n    has_key = __contains__\n\n    def __copy__(self):\n        return self.__class__(self)\n    copy = __copy__\n\n    def normkey(self, key):\n        \"\"\"Method to normalize dictionary key access\"\"\"\n        return key.lower()\n\n    def normvalue(self, value):\n        \"\"\"Method to normalize values prior to be setted\"\"\"\n        return value\n\n    def get(self, key, def_val=None):\n        return dict.get(self, self.normkey(key), self.normvalue(def_val))\n\n    def setdefault(self, key, def_val=None):\n        return dict.setdefault(self, self.normkey(key), self.normvalue(def_val))\n\n    def update(self, seq):\n        seq = seq.items() if isinstance(seq, Mapping) else seq\n        iseq = ((self.normkey(k), self.normvalue(v)) for k, v in seq)\n        super(CaselessDict, self).update(iseq)\n\n    @classmethod\n    def fromkeys(cls, keys, value=None):\n        return cls((k, value) for k in keys)\n\n    def pop(self, key, *args):\n        return dict.pop(self, self.normkey(key), *args)\n\n\nclass LocalCache(collections.OrderedDict):\n    \"\"\"Dictionary with a finite number of keys.\n\n    Older items expires first.\n    \"\"\"\n\n    def __init__(self, limit=None):\n        super(LocalCache, self).__init__()\n        self.limit = limit\n\n    def __setitem__(self, key, value):\n        if self.limit:\n            while len(self) >= self.limit:\n                self.popitem(last=False)\n        super(LocalCache, self).__setitem__(key, value)\n\n\nclass LocalWeakReferencedCache(weakref.WeakKeyDictionary):\n    \"\"\"\n    A weakref.WeakKeyDictionary implementation that uses LocalCache as its\n    underlying data structure, making it ordered and capable of being size-limited.\n\n    Useful for memoization, while avoiding keeping received\n    arguments in memory only because of the cached references.\n\n    Note: like LocalCache and unlike weakref.WeakKeyDictionary,\n    it cannot be instantiated with an initial dictionary.\n    \"\"\"\n\n    def __init__(self, limit=None):\n        super(LocalWeakReferencedCache, self).__init__()\n        self.data = LocalCache(limit=limit)\n\n    def __setitem__(self, key, value):\n        try:\n            super(LocalWeakReferencedCache, self).__setitem__(key, value)\n        except TypeError:\n            pass  # key is not weak-referenceable, skip caching\n\n    def __getitem__(self, key):\n        try:\n            return super(LocalWeakReferencedCache, self).__getitem__(key)\n        except (TypeError, KeyError):\n            return None  # key is either not weak-referenceable or not cached\n\n\nclass SequenceExclude:\n    \"\"\"Object to test if an item is NOT within some sequence.\"\"\"\n\n    def __init__(self, seq):\n        self.seq = seq\n\n    def __contains__(self, item):\n        return item not in self.seq\n", "path": "scrapy/utils/datatypes.py"}]}