Better temp file handling

This PR changes the functions in the utils module related to temporary files to use functions from the builtin Python `tempfile` module for greater portability.  It also adds `try`/`finally` blocks around usages of temporary files to ensure that they get removed even in the case of an exception.  Finally, it fixes a bug in `tmp_file_pattern()` that was preventing the stray temp file cleanup code in `application_start()` from actually working.

I did experiment with replacing the `utils.tmp_filename()` function with a similar function that wrapped `tempfile.NamedTemporaryFile`, but it generally made the calling code messier, so I decided not to go that route.