Fix bugs in :mod:`_ctypes` where exceptions could end up being overwritten.