Skip to content

Commit

Permalink
Merge pull request #148 from pallets-eco/ident
Browse files Browse the repository at this point in the history
restore identity handling for str and int senders
  • Loading branch information
davidism authored Apr 28, 2024
2 parents 3f9f1c9 + e27fc05 commit 0068cb4
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 7 deletions.
2 changes: 2 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ Version 1.8.1

Unreleased

- Restore identity handling for ``str`` and ``int`` senders. :pr:`148`


Version 1.8.0
-------------
Expand Down
11 changes: 11 additions & 0 deletions src/blinker/_utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,20 @@ def __getnewargs__(self) -> tuple[t.Any]:


def make_id(obj: object) -> t.Hashable:
"""Get a stable identifier for a receiver or sender, to be used as a dict
key or in a set.
"""
if inspect.ismethod(obj):
# The id of a bound method is not stable, but the id of the unbound
# function and instance are.
return id(obj.__func__), id(obj.__self__)

if isinstance(obj, (str, int)):
# Instances with the same value always compare equal and have the same
# hash, even if the id may change.
return obj

# Assume other types are not hashable but will always be the same instance.
return id(obj)


Expand Down
14 changes: 7 additions & 7 deletions tests/test_signals.py
Original file line number Diff line number Diff line change
Expand Up @@ -485,15 +485,15 @@ def values_are_empty_sets_(dictionary: dict[t.Any, t.Any]) -> None:


def test_int_sender() -> None:
sentinel = []
count = 0

def received(sender: t.Any) -> None:
sentinel.append(sender)
nonlocal count
count += 1

sig = blinker.Signal()

sig.connect(received, sender=123456789)
sig.send(123456789)
assert len(sentinel) == 1
sig.send(123456789 + 0) # Forces a new id with CPython
assert len(sentinel) == 2
sig.send(123456789) # Python compiler uses same instance for same literal value.
assert count == 1
sig.send(int("123456789")) # Force different instance with same value.
assert count == 2

0 comments on commit 0068cb4

Please sign in to comment.