cStringIO is very handy for testing -- you might try using it directly in your tests without this function that both uses in and writes to a file -- no need to write to a file for the tests.