OrbitzWorldwide/droned — pull request #29 by JustinVenus (2012-03-29 15:59:46+00:00)
Title: fixing a subtle bug introduced by weak reference changes

problem:

In romeo.foundation.RomeoKeyValue.__init__ objects are recursively created before the instanceMap dictionary is fully populated b/c the constructor had not relinquished control back to romeo.entity.ParameterizedSingleton.__call__. This causes a RuntimeError b/c we look up objects expecting them to be in the instanceMap. With the introduction of weak references the dictionary was not yet populated.

solution:
Use original code strategy for populating the instanceMap in romeo.entity.ParameterizedSingleton.__call__ and use a decorator to update the dictionary with a weakreference if it is not currently.

def _weak_decorator(func):
"""Provides a work around for recursive creation of entities in
an entities constructor. That would otherwise lead to runtime
exceptions.
"""
def decorator(cls, *args, **kwargs):
result = func(cls, *args, **kwargs)
if isinstance(result, weakref.ref):
return result()
elif result:
cls._instanceMap[result._instanceID] = weakref.ref(
result, #making sure we clean up the map dict
lambda x: cls._weak_callback(result._instanceID,x)
)
return result
return decorator