Python object lifetime fixes

Just a couple of fixes for minor problems shown up by the new shutdown checks.