Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion scalene/scalene_profiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -2027,7 +2027,7 @@ def run_profiler(
raise FileNotFoundError
# Use the full absolute path of the program being profiled, expanding ~ if need be.
prog_name = os.path.abspath(os.path.expanduser(progs[0]))
with open(prog_name, encoding="utf-8") as prog_being_profiled:
with open(prog_name, encoding="utf-8-sig") as prog_being_profiled:
# Read in the code and compile it.
code: Any = ""
try:
Expand Down
50 changes: 50 additions & 0 deletions tests/test_issue1091_bom.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
"""Regression test for issue #1091: UTF-8 BOM in a profiled script."""

from __future__ import annotations

import subprocess
import sys
from pathlib import Path

import pytest


@pytest.mark.parametrize(
("name", "source", "expected"),
[
("plain", b'print("PLAIN_OK")\n', "PLAIN_OK"),
("bom", b'\xef\xbb\xbfprint("BOM_OK")\n', "BOM_OK"),
("utf8_non_ascii", 'print("é_OK")\n'.encode(), "é_OK"),
],
)
def test_cpu_only_cli_accepts_utf8_script_variants(
tmp_path: Path, name: str, source: bytes, expected: str
) -> None:
"""The real CLI must execute BOM and ordinary UTF-8 scripts successfully."""
script = tmp_path / f"{name}.py"
script.write_bytes(source)

result = subprocess.run(
[
sys.executable,
"-m",
"scalene",
"run",
"--cpu-only",
"--no-browser",
str(script),
],
cwd=tmp_path,
check=False,
capture_output=True,
text=True,
timeout=60,
)
output = result.stdout + result.stderr

assert result.returncode == 0, (
f"Scalene failed for {name}: exit={result.returncode}\n{output[-3000:]}"
)
assert expected in result.stdout, (
f"Profiled {name} script did not run to completion:\n{output[-3000:]}"
)
Loading