classification
Title: profile/cProfile CLI should catch BrokenPipeError
Type: behavior Stage: resolved
Components: Library (Lib) Versions: Python 3.10, Python 3.9, Python 3.8
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: ammar2, miss-islington, vstinner, zmwangx
Priority: normal Keywords: patch

Created on 2020-10-11 05:45 by zmwangx, last changed 2021-01-20 09:19 by miss-islington. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 22643 merged zmwangx, 2020-10-11 05:53
PR 24262 merged miss-islington, 2021-01-20 08:57
PR 24263 merged miss-islington, 2021-01-20 08:57
Messages (5)
msg378421 - (view) Author: Zhiming Wang (zmwangx) * Date: 2020-10-11 05:45
Since profile/cProfile CLI interface prints a sorted list of stats, using head to limit output to the most relevant entries should be a fairly natural thing to do. Unfortunately, BrokenPipeError isn't caught, causing quite a bit of pollution to the output:

$ python3 -m cProfile -m http.server -h | head
usage: http.server [-h] [--cgi] [--bind ADDRESS] [--directory DIRECTORY]
                   [port]

positional arguments:
  port                  Specify alternate port [default: 8000]

optional arguments:
  -h, --help            show this help message and exit
  --cgi                 Run as CGI Server
  --bind ADDRESS, -b ADDRESS
Traceback (most recent call last):
  File "/Users/zmwang/.pyenv/versions/3.9.0/lib/python3.9/runpy.py", line 197, in _run_module_as_main
    return _run_code(code, main_globals, None,
  File "/Users/zmwang/.pyenv/versions/3.9.0/lib/python3.9/runpy.py", line 87, in _run_code
    exec(code, run_globals)
  File "/Users/zmwang/.pyenv/versions/3.9.0/lib/python3.9/cProfile.py", line 180, in <module>
    main()
  File "/Users/zmwang/.pyenv/versions/3.9.0/lib/python3.9/cProfile.py", line 173, in main
    runctx(code, globs, None, options.outfile, options.sort)
  File "/Users/zmwang/.pyenv/versions/3.9.0/lib/python3.9/cProfile.py", line 19, in runctx
    return _pyprofile._Utils(Profile).runctx(statement, globals, locals,
  File "/Users/zmwang/.pyenv/versions/3.9.0/lib/python3.9/profile.py", line 66, in runctx
    self._show(prof, filename, sort)
  File "/Users/zmwang/.pyenv/versions/3.9.0/lib/python3.9/profile.py", line 72, in _show
    prof.print_stats(sort)
  File "/Users/zmwang/.pyenv/versions/3.9.0/lib/python3.9/cProfile.py", line 42, in print_stats
    pstats.Stats(self).strip_dirs().sort_stats(sort).print_stats()
  File "/Users/zmwang/.pyenv/versions/3.9.0/lib/python3.9/pstats.py", line 431, in print_stats
    self.print_line(func)
  File "/Users/zmwang/.pyenv/versions/3.9.0/lib/python3.9/pstats.py", line 513, in print_line
    print(f8(tt/nc), end=' ', file=self.stream)
BrokenPipeError: [Errno 32] Broken pipe
Exception ignored in: <_io.TextIOWrapper name='<stdout>' mode='w' encoding='utf-8'>
BrokenPipeError: [Errno 32] Broken pipe

I think the exception should be caught in order to suppress this noise.
msg378422 - (view) Author: Ammar Askar (ammar2) * (Python committer) Date: 2020-10-11 06:18
Related:

https://bugs.python.org/issue39828
msg385318 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2021-01-20 08:56
New changeset 3554fa4abecfb77ac5fcaa5ce8310eeca5683960 by Zhiming Wang in branch 'master':
bpo-42005: profile and cProfile catch BrokenPipeError (GH-22643)
https://github.com/python/cpython/commit/3554fa4abecfb77ac5fcaa5ce8310eeca5683960
msg385321 - (view) Author: miss-islington (miss-islington) Date: 2021-01-20 09:18
New changeset 648b72900b5039ab46b8b459f921daecb8db2a6b by Miss Islington (bot) in branch '3.8':
bpo-42005: profile and cProfile catch BrokenPipeError (GH-22643)
https://github.com/python/cpython/commit/648b72900b5039ab46b8b459f921daecb8db2a6b
msg385322 - (view) Author: miss-islington (miss-islington) Date: 2021-01-20 09:19
New changeset 0654c4c4cc54a325e878154f8b117159d0105cf7 by Miss Islington (bot) in branch '3.9':
bpo-42005: profile and cProfile catch BrokenPipeError (GH-22643)
https://github.com/python/cpython/commit/0654c4c4cc54a325e878154f8b117159d0105cf7
History
Date User Action Args
2021-01-20 09:19:13miss-islingtonsetmessages: + msg385322
2021-01-20 09:18:25miss-islingtonsetmessages: + msg385321
2021-01-20 09:00:28vstinnersetstatus: open -> closed
stage: patch review -> resolved
resolution: fixed
versions: + Python 3.8, Python 3.9
2021-01-20 08:57:42miss-islingtonsetpull_requests: + pull_request23088
2021-01-20 08:57:04miss-islingtonsetnosy: + miss-islington
pull_requests: + pull_request23087
2021-01-20 08:56:47vstinnersetmessages: + msg385318
2020-10-11 06:18:50ammar2setnosy: + vstinner, ammar2
messages: + msg378422
2020-10-11 05:53:53zmwangxsetkeywords: + patch
stage: patch review
pull_requests: + pull_request21617
2020-10-11 05:45:17zmwangxcreate