Define macros to abstract away the details of thread local storage
Fix thread local storage in python Closes #291