<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
 def render_stats(stats, sort, format):
 """ Returns a StringIO containing the formatted statistics from _statsfile_. _sort_ is a list of fields to sort by. _format_ is the name of the method that pstats uses to format the data. """

    output = StdoutWrapper()
    if hasattr(stats, "stream"):
        stats.stream = output.stream
    stats.sort_stats(*sort)
    getattr(stats, format)()
    return output.stream