From 63af1fe9f0ca036fd5e1fc0680d86407d40331d3 Mon Sep 17 00:00:00 2001 From: Emery Berger Date: Mon, 24 Jul 2023 23:57:30 -0700 Subject: [PATCH] Force profile output when triggered by --off. (#641) * Force profile output when triggered by --off. * Fixed for Windows. --- scalene/scalene_profiler.py | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/scalene/scalene_profiler.py b/scalene/scalene_profiler.py index e04b86859..cdad82d36 100644 --- a/scalene/scalene_profiler.py +++ b/scalene/scalene_profiler.py @@ -951,9 +951,15 @@ def output_profile() -> bool: # be used by the parent process if "is_child" in json_output: return True - if not Scalene.__output.output_file: - Scalene.__output.output_file = "/dev/stdout" - with open(Scalene.__output.output_file, "w") as f: + outfile = Scalene.__output.output_file + # If there was no output file specified, print to the console. + if not outfile: + if sys.platform == "win32": + outfile = "CON" + else: + outfile = "/dev/stdout" + # Write the JSON to the output file (or console). + with open(outfile, "w") as f: f.write( json.dumps(json_output, sort_keys=True, indent=4) + "\n" ) @@ -1760,6 +1766,9 @@ def stop_signal_handler( for pid in Scalene.child_pids: Scalene.__orig_kill(pid, Scalene.__signals.stop_profiling_signal) Scalene.stop() + # Output the profile if `--outfile` was set to a file. + if Scalene.__output.output_file: + Scalene.output_profile() @staticmethod def disable_signals(retry: bool = True) -> None: