Fix getting attribute from local thread.

When using thread-locals, we have to check whether the attribute of the thread local is set before using it.
Todo so always use hasattr or getattr.
Well, actually this checks if it is not None which is stronger than the existence of the attribute so it is fine.