summaryrefslogtreecommitdiff
path: root/blinker/base.py
diff options
context:
space:
mode:
authorjason kirtland <jek@discorporate.us>2013-07-03 11:01:53 +0200
committerjason kirtland <jek@discorporate.us>2013-07-03 11:01:53 +0200
commitb66e4e9acf6017b3af20fc56666461f01fd4e003 (patch)
treeb2cf14bb58d98f806097e3b48c5feaf0bfe9798d /blinker/base.py
parent60fc2947f0b208299c8dd7125431ba70d3b57e04 (diff)
downloadblinker-b66e4e9acf6017b3af20fc56666461f01fd4e003.tar.gz
blinker.signal() and blinker.Namespace no longer use weak references.
In the original implementation, I was uneasy about releasing a library that held an unbounded, module-level cache, so it was built using a weak value mapping. In practice, users making tons of signals are using Signal() directly, and the weak referencing violates the principle of least surprise in code like `signal('foo').connect(...)`. Previous code is available as WeakNamespace for the time being. The implementation is trivial and will likely be dropped in the future unless use cases are voiced.
Diffstat (limited to 'blinker/base.py')
-rw-r--r--blinker/base.py23
1 files changed, 22 insertions, 1 deletions
diff --git a/blinker/base.py b/blinker/base.py
index 615ef77..d8fe9f7 100644
--- a/blinker/base.py
+++ b/blinker/base.py
@@ -387,7 +387,7 @@ class NamedSignal(Signal):
return "%s; %r>" % (base[:-1], self.name)
-class Namespace(WeakValueDictionary):
+class Namespace(dict):
"""A mapping of signal names to signals."""
def signal(self, name, doc=None):
@@ -402,4 +402,25 @@ class Namespace(WeakValueDictionary):
return self.setdefault(name, NamedSignal(name, doc))
+class WeakNamespace(WeakValueDictionary):
+ """A weak mapping of signal names to signals.
+
+ Automatically cleans up unused Signals when the last reference goes out
+ of scope. This namespace implementation exists for a measure of legacy
+ compatibility with Blinker <= 1.2, and may be dropped in the future.
+
+ """
+
+ def signal(self, name, doc=None):
+ """Return the :class:`NamedSignal` *name*, creating it if required.
+
+ Repeated calls to this function will return the same signal object.
+
+ """
+ try:
+ return self[name]
+ except KeyError:
+ return self.setdefault(name, NamedSignal(name, doc))
+
+
signal = Namespace().signal