Avoid at exit errors due to logging module bugs also with Jython.

Commit 37e94b1227260d5edc136679c8c999f30421b0ea fixed at exit errors
caused by an earlier fix to #1821 but the fix didn't work with Jython.
More workarounds here.