Fix execution in non-Python environments

The Python thread state can only be saved when we are actually running inside
something that looks like Python. Hence, we check first and save then.