models: Make sure we really only add __weakref__ once

This commit is contained in:
Thomas Adamcik 2015-04-11 00:47:53 +02:00
parent 512e51fba2
commit 6327a67874
2 changed files with 16 additions and 3 deletions

View File

@ -1,6 +1,7 @@
from __future__ import absolute_import, unicode_literals
import copy
import inspect
import json
import weakref
@ -145,11 +146,12 @@ class ImmutableObjectMeta(type):
value._name = key
attrs['_fields'] = fields
attrs['__slots__'] = fields.values()
attrs['_instances'] = weakref.WeakValueDictionary()
attrs['__slots__'] = fields.values()
for base in bases:
if '__weakref__' in getattr(base, '__slots__', []):
anncestors = [b for base in bases for b in inspect.getmro(base)]
for anncestor in anncestors:
if '__weakref__' in getattr(anncestor, '__slots__', []):
break
else:
attrs['__slots__'].append('__weakref__')

View File

@ -8,6 +8,17 @@ from mopidy.models import (
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):
def test_same_instance(self):