[1.7.x] Made the vendored NamedTemporaryFile work as a context manager. Refs #22680.

This fixes a regression on Windows introduced by b7de5f5.

Thanks Tim Graham for the report and review.

Backport of 6e8d614acd from master