Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save davidfraser/ac8706bb876f4bc30234c4f44c7a4cfb to your computer and use it in GitHub Desktop.
Save davidfraser/ac8706bb876f4bc30234c4f44c7a4cfb to your computer and use it in GitHub Desktop.
Patches CPython's _weakrefset.py to prevent a spurious TypeError when adding an item to a WeakSet
This patches WeakSet.add to prevent it having a spurious error if a weak ref goes away between calling this function and adding it
This is done analogously to the patch in https://github.com/python/cpython/commit/f8de3fea1280d55377d40c6e04b64114f9da2fa6:
"#10360: catch TypeError in WeakSet.__contains__, just like WeakKeyDictionary does."
See https://bugs.python.org/issue10360 for infomration on that
@@ -83,7 +83,11 @@
def add(self, item):
if self._pending_removals:
self._commit_removals()
- self.data.add(ref(item, self._remove))
+ try:
+ self.data.add(ref(item, self._remove))
+ except TypeError:
+ #Can't add to the cache if ref doesn't exist
+ pass
def clear(self):
if self._pending_removals:
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment