port to python3
Don't rely on implicit close during gc, use with statement.

amends 1e39deab40e7f4