Close various files when finished writing to them. This avoids ResourceWarnings on Python 3, and better supports doctests under PyPy.