I wonder if it's worth allow injection of a file-like object into this function as a param, the default being sys.stdout?  That way we don't need to patch anything and the `StringIO` can just be passed in.