diff options
| author | Claude Paroz <claude@2xlibre.net> | 2016-12-01 11:38:01 +0100 |
|---|---|---|
| committer | Claude Paroz <claude@2xlibre.net> | 2017-01-18 16:21:28 +0100 |
| commit | c716fe87821df00f9f03ecc761c914d1682591a2 (patch) | |
| tree | 0436706cdb190acbc76fb5fcf6d66f16e09fafa3 /django/dispatch | |
| parent | e63d98b7beb16d1410168a2315cbe04c43c9c80d (diff) | |
Refs #23919 -- Removed six.PY2/PY3 usage
Thanks Tim Graham for the review.
Diffstat (limited to 'django/dispatch')
| -rw-r--r-- | django/dispatch/dispatcher.py | 15 | ||||
| -rw-r--r-- | django/dispatch/weakref_backports.py | 67 |
2 files changed, 3 insertions, 79 deletions
diff --git a/django/dispatch/dispatcher.py b/django/dispatch/dispatcher.py index db2f6839f8..8cf1680b6e 100644 --- a/django/dispatch/dispatcher.py +++ b/django/dispatch/dispatcher.py @@ -2,15 +2,9 @@ import sys import threading import weakref -from django.utils import six from django.utils.inspect import func_accepts_kwargs from django.utils.six.moves import range -if six.PY2: - from .weakref_backports import WeakMethod -else: - from weakref import WeakMethod - def _make_id(target): if hasattr(target, '__func__'): @@ -107,13 +101,10 @@ class Signal(object): receiver_object = receiver # Check for bound methods if hasattr(receiver, '__self__') and hasattr(receiver, '__func__'): - ref = WeakMethod + ref = weakref.WeakMethod receiver_object = receiver.__self__ - if six.PY3: - receiver = ref(receiver) - weakref.finalize(receiver_object, self._remove_receiver) - else: - receiver = ref(receiver, self._remove_receiver) + receiver = ref(receiver) + weakref.finalize(receiver_object, self._remove_receiver) with self.lock: self._clear_dead_receivers() diff --git a/django/dispatch/weakref_backports.py b/django/dispatch/weakref_backports.py deleted file mode 100644 index 736f7e1ab8..0000000000 --- a/django/dispatch/weakref_backports.py +++ /dev/null @@ -1,67 +0,0 @@ -""" -weakref_backports is a partial backport of the weakref module for python -versions below 3.4. - -Copyright (C) 2013 Python Software Foundation, see LICENSE.python for details. - -The following changes were made to the original sources during backporting: - - * Added `self` to `super` calls. - * Removed `from None` when raising exceptions. - -""" -from weakref import ref - - -class WeakMethod(ref): - """ - A custom `weakref.ref` subclass which simulates a weak reference to - a bound method, working around the lifetime problem of bound methods. - """ - - __slots__ = "_func_ref", "_meth_type", "_alive", "__weakref__" - - def __new__(cls, meth, callback=None): - try: - obj = meth.__self__ - func = meth.__func__ - except AttributeError: - raise TypeError("argument should be a bound method, not {}" - .format(type(meth))) - def _cb(arg): - # The self-weakref trick is needed to avoid creating a reference - # cycle. - self = self_wr() - if self._alive: - self._alive = False - if callback is not None: - callback(self) - self = ref.__new__(cls, obj, _cb) - self._func_ref = ref(func, _cb) - self._meth_type = type(meth) - self._alive = True - self_wr = ref(self) - return self - - def __call__(self): - obj = super(WeakMethod, self).__call__() - func = self._func_ref() - if obj is None or func is None: - return None - return self._meth_type(func, obj) - - def __eq__(self, other): - if isinstance(other, WeakMethod): - if not self._alive or not other._alive: - return self is other - return ref.__eq__(self, other) and self._func_ref == other._func_ref - return False - - def __ne__(self, other): - if isinstance(other, WeakMethod): - if not self._alive or not other._alive: - return self is not other - return ref.__ne__(self, other) or self._func_ref != other._func_ref - return True - - __hash__ = ref.__hash__ |
