Skip to content

Prevent --browser from opening when no report is generated #145306

@misa-j

Description

@misa-j

Bug report

Bug description:

When profiling a script that does not produce HTML output, the following warning is displayed:
Warning: No functions found in profiling data. Check if sampling captured any data.
However, when the --browser option is specified, the program still attempts to open the output file in a browser.

./python -m profiling.sampling run --flamegraph --browser script.py
# script.py
print("hello world")

Suggested Fix

diff --git a/Lib/profiling/sampling/cli.py b/Lib/profiling/sampling/cli.py
index f4b31aad45b..75a21429705 100644
--- a/Lib/profiling/sampling/cli.py
+++ b/Lib/profiling/sampling/cli.py
@@ -607,6 +607,10 @@ def _open_in_browser(path):
     """
     abs_path = os.path.abspath(path)
 
+    if not os.path.exists(abs_path):
+        print(f"Warning: Could not find {abs_path}", file=sys.stderr)
+        return
+
     # For heatmap directories, open the index.html file
     if os.path.isdir(abs_path):
         index_path = os.path.join(abs_path, 'index.html')

I can open a PR with a proposed fix

CPython versions tested on:

CPython main branch

Operating systems tested on:

Linux

Metadata

Metadata

Assignees

No one assigned

    Labels

    stdlibStandard Library Python modules in the Lib/ directorytopic-profilingtype-bugAn unexpected behavior, bug, or error

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions