Skip to content

Commit 732480a

Browse files
committed
Fixed: Correctly escapes backslashes in gnuplot strings
Fixes an issue where backslashes were not properly escaped when generating strings for gnuplot, leading to incorrect rendering. The gnuplot quoting function now escapes both backslashes and single quotes to ensure correct string interpretation by gnuplot. Refs: docs/benchmark
1 parent 8224d86 commit 732480a

2 files changed

Lines changed: 12 additions & 5 deletions

File tree

scripts/criterion_dim_plot.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@ class PlotRequest:
4343
log_y: bool
4444

4545

46+
Row = tuple[int, float, float, float, float, float, float, float, float, float]
47+
48+
4649
METRICS: Final[dict[str, Metric]] = {
4750
"det_via_lu": Metric(
4851
la_bench="la_stack_det_via_lu",
@@ -201,8 +204,8 @@ def _update_readme_table(readme_path: Path, marker_begin: str, marker_end: str,
201204

202205

203206
def _gp_quote(s: str) -> str:
204-
# gnuplot supports single-quoted strings; escape single quotes.
205-
return "'" + s.replace("'", "\\'") + "'"
207+
# gnuplot supports single-quoted strings; escape backslashes and single quotes.
208+
return "'" + s.replace("\\", "\\\\").replace("'", "\\'") + "'"
206209

207210

208211
def _render_svg_with_gnuplot(req: PlotRequest) -> None:
@@ -310,9 +313,6 @@ def _parse_args(argv: list[str]) -> argparse.Namespace:
310313
return parser.parse_args(argv)
311314

312315

313-
Row = tuple[int, float, float, float, float, float, float, float, float, float]
314-
315-
316316
def _resolve_under_root(root: Path, arg: str) -> Path:
317317
path = Path(arg)
318318
return path if path.is_absolute() else root / path

scripts/tests/test_criterion_dim_plot.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,13 @@ def test_markdown_table_handles_zero_nalgebra_time() -> None:
4242
assert "| 2 | 10.000 | 0.000 | 100.000 | n/a | +90.0% |" in table
4343

4444

45+
def test_gp_quote_escapes_backslashes_and_quotes() -> None:
46+
assert criterion_dim_plot._gp_quote("plain") == "'plain'"
47+
assert criterion_dim_plot._gp_quote("a'b") == "'a\\'b'"
48+
assert criterion_dim_plot._gp_quote("a\\b") == "'a\\\\b'"
49+
assert criterion_dim_plot._gp_quote("a\\'b") == "'a\\\\\\'b'"
50+
51+
4552
def test_maybe_render_plot_handles_gnuplot_failure(capsys: pytest.CaptureFixture[str], monkeypatch: pytest.MonkeyPatch) -> None:
4653
# Simulate gnuplot existing but failing to run (CalledProcessError).
4754
def boom(_req: object) -> None:

0 commit comments

Comments
 (0)