rewrite_file(): Delete both .pyc and .pyo leftovers, and explicitly close the new source file after writing to it.
Original by Alex Coventry (in SF patch #441791), adapted to the standard regression test framework.