This repository has been archived by the owner on Jan 21, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Backport changes from trunk to pass tests on Mac.
- Loading branch information
Showing
3 changed files
with
90 additions
and
113 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
from weakref import ref | ||
|
||
from persistent.interfaces import IPersistent | ||
from persistent.wref import (WeakRef, WeakRefMarker, | ||
PersistentWeakKeyDictionary) | ||
|
||
class wref(ref): | ||
def __reduce_ex__(self, proto): | ||
return _wref_reconstructor, () | ||
|
||
class Dummy(object): pass | ||
|
||
def _wref_reconstructor(): | ||
"""Return a dead reference on reconstruction""" | ||
return wref(Dummy()) | ||
|
||
def getWeakRef(ob): | ||
"""Get either a persistent or non-presistent weakref""" | ||
if IPersistent.providedBy(ob): | ||
return WeakRef(ob) | ||
else: | ||
return wref(ob) | ||
|
||
class FlexibleWeakKeyDictionary(PersistentWeakKeyDictionary): | ||
|
||
def __setitem__(self, key, value): | ||
self.data[getWeakRef(key)] = value | ||
|
||
def __getitem__(self, key): | ||
return self.data[getWeakRef(key)] | ||
|
||
def __delitem__(self, key): | ||
del self.data[getWeakRef(key)] | ||
|
||
def get(self, key, default=None): | ||
return self.data.get(getWeakRef(key), default) | ||
|
||
def __contains__(self, key): | ||
return getWeakRef(key) in self.data | ||
|
||
def update(self, adict): | ||
if isinstance(adict, PersistentWeakKeyDictionary): | ||
self.data.update(adict.update) | ||
else: | ||
for k, v in adict.items(): | ||
self.data[getWeakRef(k)] = v | ||
|
||
def keys(self): | ||
return [k() for k in self.data.keys()] | ||
|
||
def __len__(self): | ||
return len(self.data) |