Use 'with file' more.

This helps to minimize ResourceWarning triggered by ^C with python3.
_setup_pipes: close unnecessary duplicate fds