From 1dfec4ac839f967ae67cbe45bb0f13cb51d95135 Mon Sep 17 00:00:00 2001 From: Emery Berger Date: Sun, 10 Nov 2024 12:33:49 -0500 Subject: [PATCH 1/4] Updated logic. --- scalene/scalene_profiler.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/scalene/scalene_profiler.py b/scalene/scalene_profiler.py index 615f8d798..bcb573f02 100644 --- a/scalene/scalene_profiler.py +++ b/scalene/scalene_profiler.py @@ -1826,6 +1826,7 @@ def profile_code( "Scalene can only profile code that runs for at least one second or allocates at least 10MB.", file=sys.stderr, ) + if not ( did_output and Scalene.__args.web @@ -1834,12 +1835,12 @@ def profile_code( ): return exit_status - if Scalene.__args.web: - # Only generate HTML file if needed for display in the browser. + if Scalene.__args.web or Scalene.__args.html: generate_html( profile_fname=Scalene.__profile_filename, output_fname=( - Scalene.__profiler_html + Scalene.__profiler_html if not Scalene.__args.outfile + else Scalene.__args.outfile ), ) if Scalene.in_jupyter(): From 7c0b21ef29698eb19cb7f37c2f1f16411f7e8152 Mon Sep 17 00:00:00 2001 From: Emery Berger Date: Sun, 10 Nov 2024 19:13:02 -0500 Subject: [PATCH 2/4] Fixed filename handling when --outfile is specified. --- 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 bcb573f02..3e56c1929 100644 --- a/scalene/scalene_profiler.py +++ b/scalene/scalene_profiler.py @@ -106,7 +106,6 @@ console = Console(style="white on blue") - # Assigning to `nada` disables any console.log commands. def nada(*args: Any) -> None: pass @@ -857,7 +856,11 @@ def output_profile(program_args: Optional[List[str]] = None) -> bool: return True outfile = Scalene.__output.output_file if Scalene.__args.outfile: - outfile = Scalene.__args.outfile + outfile = os.path.join( + os.path.dirname(Scalene.__args.outfile), + os.path.splitext(os.path.basename(Scalene.__args.outfile))[0] + ".json" + ) + # outfile = Scalene.__args.outfile # If there was no output file specified, print to the console. if not outfile: if sys.platform == "win32": @@ -1836,8 +1839,14 @@ def profile_code( return exit_status if Scalene.__args.web or Scalene.__args.html: + profile_filename = Scalene.__profile_filename + if Scalene.__args.outfile: + profile_filename = os.path.join( + os.path.dirname(Scalene.__args.outfile), + os.path.splitext(os.path.basename(Scalene.__args.outfile))[0] + ".json" + ) generate_html( - profile_fname=Scalene.__profile_filename, + profile_fname=profile_filename, output_fname=( Scalene.__profiler_html if not Scalene.__args.outfile else Scalene.__args.outfile From af6b4786e197576fa07160cefde8853029195c47 Mon Sep 17 00:00:00 2001 From: Emery Berger Date: Sun, 10 Nov 2024 20:18:45 -0500 Subject: [PATCH 3/4] Use a real file name instead of /dev/stderr. --- test/smoketest.py | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/test/smoketest.py b/test/smoketest.py index fb01f9306..fb97e6c36 100644 --- a/test/smoketest.py +++ b/test/smoketest.py @@ -1,10 +1,13 @@ #!/usr/bin/env python3 +import json +import pathlib +import tempfile import subprocess import sys -import json def smoketest(fname, rest): - cmd = [sys.executable, "-m", "scalene", "--cli", "--json", "--outfile", "/dev/stderr", *rest, fname] + outfile = pathlib.Path(tempfile.mkdtemp(prefix="scalene") / pathlib.Path("smoketest.json")) + cmd = [sys.executable, "-m", "scalene", "--cli", "--json", "--outfile", str(outfile), *rest, fname] print("COMMAND", ' '.join(cmd)) proc = subprocess.run(cmd ,capture_output=True) stdout = proc.stdout.decode('utf-8') @@ -19,7 +22,9 @@ def smoketest(fname, rest): # print("STDOUT", stdout) # print("\nSTDERR", stderr) try: - scalene_json = json.loads(stderr) + with open(outfile, "r") as f: + outfile_contents = f.read() + scalene_json = json.loads(outfile_contents) except json.JSONDecodeError: print("Invalid JSON", stderr) print("STDOUT", stdout) From b9d6ee5157d7b72697c34742c907b2a018a8dfd9 Mon Sep 17 00:00:00 2001 From: Emery Berger Date: Sun, 10 Nov 2024 20:25:45 -0500 Subject: [PATCH 4/4] Use a real file name instead of /dev/stderr. --- test/smoketest_profile_decorator.py | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/test/smoketest_profile_decorator.py b/test/smoketest_profile_decorator.py index dbbfa0a50..34f1bac2b 100644 --- a/test/smoketest_profile_decorator.py +++ b/test/smoketest_profile_decorator.py @@ -1,10 +1,13 @@ #!/usr/bin/env python3 +import json +import pathlib import subprocess import sys -import json +import tempfile def smoketest(fname): - proc = subprocess.run( [sys.executable, "-m", "scalene", "--cli", "--json", "--outfile", "/dev/stderr", fname] ,capture_output=True) + outfile = pathlib.Path(tempfile.mkdtemp(prefix="scalene") / pathlib.Path("smoketest.json")) + proc = subprocess.run( [sys.executable, "-m", "scalene", "--cli", "--json", "--outfile", str(outfile), fname] ,capture_output=True) if proc.returncode != 0: print("Exited with a non-zero code:", proc.returncode) print("Stdout:", proc.stdout.decode('utf-8')) @@ -14,7 +17,9 @@ def smoketest(fname): stderr = proc.stderr.decode('utf-8') try: - scalene_json = json.loads(stderr) + with open(outfile, "r") as f: + outfile_contents = f.read() + scalene_json = json.loads(outfile_contents) except json.JSONDecodeError: print("Invalid JSON", stderr) exit(1)