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