[Python-Dev] cProfile prints to stdout?

Guido van Rossum gvanrossum at gmail.com
Sun Feb 26 18:12:25 CET 2006

On 2/25/06, skip at pobox.com <skip at pobox.com> wrote:
>     >> It is currently impossible to separate profile output from the
>     >> program's output.
>     Guido> It is if you use the "advanced" use of the profiler -- the
>     Guido> profiling run just saves the profiling data to a file, and the
>     Guido> pstats module invoked separately prints the output.
> Sure, but then it's not "simple".  Your original example was "... > file".
> I'd like it to be (nearly) as easy to do it right yet keep it simple.

OK. I believe the default should be stdout though, and the conveniece
method print_stats() in profile.py should be the only place that
references stderr. The smallest code mod would be to redirect stdout
temporarily inside print_stats(); but I won't complain if you're more
ambitious and modify pstats.py.

    def print_stats(self, sort=-1, stream=None):
        import pstats
        if stream is None:
            stream = sys.stderr
        save = sys.stdout
            if stream is not None:
                sys.stdout = stream
            pstats.Stats(self).strip_dirs().sort_stats(sort). \
            sys.stdout = save

--Guido van Rossum (home page: http://www.python.org/~guido/)

More information about the Python-Dev mailing list