Fix thread local storage in python Closes #291