Set the reference to be used the current thread of execution.

        After calling this function, the specified proxy will act like it was
        the referenced object.

        proxy:
            proxy object for which the reference shall be set. If the specified
            object is not an instance of `ThreadLocalProxy`, the behavior is
            unspecified. Typically, an ``AttributeError`` is going to be
            raised.

        new_reference:
            reference the proxy should point to for the current thread after
            calling this function.

def set_reference(proxy, new_reference):
        '''
        Set the reference to be used the current thread of execution.

        After calling this function, the specified proxy will act like it was
        the referenced object.

        proxy:
            proxy object for which the reference shall be set. If the specified
            object is not an instance of `ThreadLocalProxy`, the behavior is
            unspecified. Typically, an ``AttributeError`` is going to be
            raised.

        new_reference:
            reference the proxy should point to for the current thread after
            calling this function.
        '''
        # If the new reference is itself a proxy, we have to ensure that it does
        # not refer to this proxy. If it does, we simply return because updating
        # the reference would result in an inifite loop when trying to use the
        # proxy.
        possible_proxy = new_reference
        while isinstance(possible_proxy, ThreadLocalProxy):
            if possible_proxy is proxy:
                return
            possible_proxy = ThreadLocalProxy.get_reference(possible_proxy)
        thread_local = object.__getattribute__(proxy, '_thread_local')
        thread_local.reference = new_reference
        object.__setattr__(proxy, '_last_reference', new_reference)