models: Make sure we really only add __weakref__ once
This commit is contained in:
parent
512e51fba2
commit
6327a67874
@ -1,6 +1,7 @@
|
|||||||
from __future__ import absolute_import, unicode_literals
|
from __future__ import absolute_import, unicode_literals
|
||||||
|
|
||||||
import copy
|
import copy
|
||||||
|
import inspect
|
||||||
import json
|
import json
|
||||||
import weakref
|
import weakref
|
||||||
|
|
||||||
@ -145,11 +146,12 @@ class ImmutableObjectMeta(type):
|
|||||||
value._name = key
|
value._name = key
|
||||||
|
|
||||||
attrs['_fields'] = fields
|
attrs['_fields'] = fields
|
||||||
attrs['__slots__'] = fields.values()
|
|
||||||
attrs['_instances'] = weakref.WeakValueDictionary()
|
attrs['_instances'] = weakref.WeakValueDictionary()
|
||||||
|
attrs['__slots__'] = fields.values()
|
||||||
|
|
||||||
for base in bases:
|
anncestors = [b for base in bases for b in inspect.getmro(base)]
|
||||||
if '__weakref__' in getattr(base, '__slots__', []):
|
for anncestor in anncestors:
|
||||||
|
if '__weakref__' in getattr(anncestor, '__slots__', []):
|
||||||
break
|
break
|
||||||
else:
|
else:
|
||||||
attrs['__slots__'].append('__weakref__')
|
attrs['__slots__'].append('__weakref__')
|
||||||
|
|||||||
@ -8,6 +8,17 @@ from mopidy.models import (
|
|||||||
TlTrack, Track, model_json_decoder)
|
TlTrack, Track, model_json_decoder)
|
||||||
|
|
||||||
|
|
||||||
|
class InheritanecTest(unittest.TestCase):
|
||||||
|
|
||||||
|
def test_weakref_and_slots_play_nice_in_subclass(self):
|
||||||
|
# Check that the following does not happen:
|
||||||
|
# TypeError: Error when calling the metaclass bases
|
||||||
|
# __weakref__ slot disallowed: either we already got one...
|
||||||
|
|
||||||
|
class Foo(Track):
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
class CachingTest(unittest.TestCase):
|
class CachingTest(unittest.TestCase):
|
||||||
|
|
||||||
def test_same_instance(self):
|
def test_same_instance(self):
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user