diff --git a/python/cudf/cudf/pandas/__main__.py b/python/cudf/cudf/pandas/__main__.py index 8d9c0e280840..7410f26229b4 100644 --- a/python/cudf/cudf/pandas/__main__.py +++ b/python/cudf/cudf/pandas/__main__.py @@ -52,7 +52,9 @@ def profile(function_profile, line_profile, fn): raise RuntimeError("Enabling the profiler requires a script name.") if line_profile: with open(fn) as f: - lines = f.readlines() + # lines_with_profiling joins the lines with "\n", so they must not + # keep their own line endings (the cell magic splits on "\n" too). + lines = f.read().split("\n") with tempfile.NamedTemporaryFile(mode="w+b", suffix=".py") as f: f.write(lines_with_profiling(lines, function_profile).encode()) diff --git a/python/cudf/cudf_pandas_tests/test_main.py b/python/cudf/cudf_pandas_tests/test_main.py index 8448c0756e46..a0eb754e4e35 100644 --- a/python/cudf/cudf_pandas_tests/test_main.py +++ b/python/cudf/cudf_pandas_tests/test_main.py @@ -1,6 +1,7 @@ # SPDX-FileCopyrightText: Copyright (c) 2024-2026, NVIDIA CORPORATION & AFFILIATES. All rights reserved. # SPDX-License-Identifier: Apache-2.0 +import re import subprocess import tempfile import textwrap @@ -59,6 +60,16 @@ def test_run_cudf_pandas_line_profile_preserves_file(tmp_path): assert str(script) in res +def test_run_cudf_pandas_line_profile_reports_script_line_numbers(tmp_path): + # ``--line-profile`` must report the line numbers of the original script. + script = tmp_path / "script.py" + script.write_text("x = 1\ny = 2\nz = 3\nprint(x + y + z)\n") + + res = _run_python(cudf_pandas=True, command=f"--line-profile {script}") + reported = re.findall(r"^[│|]\s*(\d+)\s*[│|]", res, re.MULTILINE) + assert reported == ["1", "2", "3", "4"] + + def test_run_cudf_pandas_with_script_with_cmd_args(): input_args_and_code = """-c 'import pandas as pd; df = pd.DataFrame({"a": [1, 2, 3]}); print(df["a"].sum())'"""