summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/blinker/_saferef.py9
-rw-r--r--src/blinker/base.py19
-rw-r--r--tests/test_context.py12
-rw-r--r--tests/test_saferef.py4
-rw-r--r--tests/test_signals.py26
-rw-r--r--tests/test_utilities.py2
6 files changed, 40 insertions, 32 deletions
diff --git a/src/blinker/_saferef.py b/src/blinker/_saferef.py
index 8848dea..31864f0 100644
--- a/src/blinker/_saferef.py
+++ b/src/blinker/_saferef.py
@@ -180,6 +180,7 @@ class BoundMethodWeakref:
self.self_name = str(im_self)
self.func_name = str(im_func.__name__)
+ @classmethod
def calculate_key(cls, target):
"""Calculate the reference key for this reference.
@@ -188,8 +189,6 @@ class BoundMethodWeakref:
"""
return (id(get_self(target)), id(get_func(target)))
- calculate_key = classmethod(calculate_key)
-
def __str__(self):
"""Give a friendly representation of the object."""
return "{}({}.{})".format(
@@ -204,11 +203,11 @@ class BoundMethodWeakref:
"""Whether we are still a valid reference."""
return self() is not None
- def __cmp__(self, other):
+ def __eq__(self, other):
"""Compare with another reference."""
if not isinstance(other, self.__class__):
- return cmp(self.__class__, type(other))
- return cmp(self.key, other.key)
+ return operator.eq(self.__class__, type(other))
+ return operator.eq(self.key, other.key)
def __call__(self):
"""Return a strong reference to the bound method.
diff --git a/src/blinker/base.py b/src/blinker/base.py
index 467e0ac..247b5cb 100644
--- a/src/blinker/base.py
+++ b/src/blinker/base.py
@@ -138,17 +138,17 @@ class Signal:
self.receiver_connected.send(
self, receiver=receiver, sender=sender, weak=weak
)
- except:
+ except TypeError as e:
self.disconnect(receiver, sender)
- raise
+ raise e
if receiver_connected.receivers and self is not receiver_connected:
try:
receiver_connected.send(
self, receiver_arg=receiver, sender_arg=sender, weak_arg=weak
)
- except:
+ except TypeError as e:
self.disconnect(receiver, sender)
- raise
+ raise e
return receiver
def connect_via(self, sender, weak=False):
@@ -203,9 +203,9 @@ class Signal:
self.connect(receiver, sender=sender, weak=False)
try:
yield None
- except:
+ except Exception as e:
self.disconnect(receiver)
- raise
+ raise e
else:
self.disconnect(receiver)
@@ -223,7 +223,7 @@ class Signal:
"""
warn(
- "temporarily_connected_to is deprecated; " "use connected_to instead.",
+ "temporarily_connected_to is deprecated; use connected_to instead.",
DeprecationWarning,
)
return self.connected_to(receiver, sender)
@@ -244,8 +244,7 @@ class Signal:
# for lowest possible cost.
if __debug__ and sender and len(sender) > 1:
raise TypeError(
- "send() accepts only one positional "
- "argument, %s given" % len(sender)
+ f"send() accepts only one positional argument, {len(sender)} given"
)
return []
@@ -256,7 +255,7 @@ class Signal:
sender = None
elif len(sender) > 1:
raise TypeError(
- "send() accepts only one positional argument, " "%s given" % len(sender)
+ f"send() accepts only one positional argument, {len(sender)} given"
)
else:
sender = sender[0]
diff --git a/tests/test_context.py b/tests/test_context.py
index 020014c..f45cba7 100644
--- a/tests/test_context.py
+++ b/tests/test_context.py
@@ -5,7 +5,9 @@ def test_temp_connection():
sig = Signal()
canary = []
- receiver = lambda sender: canary.append(sender)
+
+ def receiver(sender):
+ canary.append(sender)
sig.send(1)
with sig.connected_to(receiver):
@@ -20,7 +22,9 @@ def test_temp_connection_for_sender():
sig = Signal()
canary = []
- receiver = lambda sender: canary.append(sender)
+
+ def receiver(sender):
+ canary.append(sender)
with sig.connected_to(receiver, sender=2):
sig.send(1)
@@ -34,7 +38,9 @@ def test_temp_connection_failure():
sig = Signal()
canary = []
- receiver = lambda sender: canary.append(sender)
+
+ def receiver(sender):
+ canary.append(sender)
class Failure(Exception):
pass
diff --git a/tests/test_saferef.py b/tests/test_saferef.py
index f510ff3..123183b 100644
--- a/tests/test_saferef.py
+++ b/tests/test_saferef.py
@@ -60,14 +60,14 @@ class TestSaferef(unittest.TestCase):
def setUp(self):
ts = []
ss = []
- for x in range(100):
+ for _ in range(100):
t = _Sample1()
ts.append(t)
s = safe_ref(t.x, self._closure)
ss.append(s)
ts.append(_sample2)
ss.append(safe_ref(_sample2, self._closure))
- for x in range(30):
+ for _ in range(30):
t = _Sample3()
ts.append(t)
s = safe_ref(t, self._closure)
diff --git a/tests/test_signals.py b/tests/test_signals.py
index 864afc8..0879b5c 100644
--- a/tests/test_signals.py
+++ b/tests/test_signals.py
@@ -191,17 +191,20 @@ def test_signal_signals_weak_sender():
def test_empty_bucket_growth():
+ def senders():
+ return (
+ len(sig._by_sender),
+ sum(len(i) for i in sig._by_sender.values()),
+ )
+
+ def receivers():
+ return (
+ len(sig._by_receiver),
+ sum(len(i) for i in sig._by_receiver.values()),
+ )
+
sentinel = Sentinel()
sig = blinker.Signal()
- senders = lambda: (
- len(sig._by_sender),
- sum(len(i) for i in sig._by_sender.values()),
- )
- receivers = lambda: (
- len(sig._by_receiver),
- sum(len(i) for i in sig._by_receiver.values()),
- )
-
receiver1 = sentinel.make_receiver("receiver1")
receiver2 = sentinel.make_receiver("receiver2")
@@ -245,7 +248,7 @@ def test_meta_connect_failure():
pytest.raises(TypeError, sig.connect, receiver)
assert not sig.receivers
assert not sig._by_receiver
- assert sig._by_sender == {src.blinker.base.ANY_ID: set()}
+ assert sig._by_sender == {blinker.base.ANY_ID: set()}
blinker.receiver_connected._clear_state()
@@ -461,7 +464,8 @@ def test_no_double_send():
def test_has_receivers():
- received = lambda sender: None
+ def received(_):
+ return None
sig = blinker.Signal()
assert not sig.has_receivers_for(None)
diff --git a/tests/test_utilities.py b/tests/test_utilities.py
index 6df9b42..a07a53a 100644
--- a/tests/test_utilities.py
+++ b/tests/test_utilities.py
@@ -19,6 +19,6 @@ def test_symbols():
def test_pickled_symbols():
foo = symbol("foo")
- for protocol in 0, 1, 2:
+ for _ in 0, 1, 2:
roundtrip = pickle.loads(pickle.dumps(foo))
assert roundtrip is foo