Skip to content

Commit d1a325b

Browse files
lun3xclaude
authored andcommitted
fix: Include the library path in the plugin dlopen error message (#29213)
Co-authored-by: Claude Fable 5.1 <[email protected]> (cherry picked from commit 2fec95c)
1 parent 461fe0e commit d1a325b

2 files changed

Lines changed: 46 additions & 3 deletions

File tree

‎crates/polars-plan/src/plans/aexpr/function_expr/plugin.rs‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
#![allow(unsafe_op_in_unsafe_fn)]
22
use std::ffi::CStr;
3+
use std::path::PathBuf;
34
use std::sync::{LazyLock, RwLock};
45

56
use arrow::ffi::{ArrowSchema, import_field_from_c};
67
use libloading::Library;
8+
use polars_utils::io::PathIoError;
79
#[cfg(feature = "python")]
810
use pyo3::{Python, types::PyAnyMethods};
911

@@ -36,7 +38,13 @@ fn get_lib(lib: &str) -> PolarsResult<Arc<PluginAndVersion>> {
3638

3739
let library = unsafe {
3840
Library::new(&load_path).map_err(|e| {
39-
PolarsError::ComputeError(format!("error loading dynamic library: {e}").into())
41+
// Format through `PathIoError` so long paths are truncated consistently with
42+
// other IO errors, and the full path is only shown with `POLARS_VERBOSE=1`.
43+
let err = PathIoError {
44+
path: PathBuf::from(&load_path),
45+
source: std::io::Error::other(e),
46+
};
47+
PolarsError::ComputeError(format!("error loading dynamic library: {err}").into())
4048
})?
4149
};
4250
let version_function: libloading::Symbol<unsafe extern "C" fn() -> u32> = unsafe {

‎py-polars/tests/unit/meta/test_plugins.py‎

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from __future__ import annotations
22

3+
import re
34
import sys
45
from pathlib import Path
56
from typing import Any
@@ -18,7 +19,11 @@
1819

1920

2021
@pytest.mark.write_disk
21-
def test_register_plugin_function_invalid_plugin_path(tmp_path: Path) -> None:
22+
def test_register_plugin_function_invalid_plugin_path(
23+
plmonkeypatch: PlMonkeyPatch, tmp_path: Path
24+
) -> None:
25+
# The full path is shown in verbose mode; tmp_path may be long enough to truncate.
26+
plmonkeypatch.setenv("POLARS_VERBOSE", "1")
2227
tmp_path.mkdir(exist_ok=True)
2328
plugin_path = tmp_path / "lib.so"
2429
plugin_path.touch()
@@ -27,8 +32,38 @@ def test_register_plugin_function_invalid_plugin_path(tmp_path: Path) -> None:
2732
plugin_path=plugin_path, function_name="hello", args=5
2833
)
2934

30-
with pytest.raises(ComputeError, match="error loading dynamic library"):
35+
with pytest.raises(
36+
ComputeError,
37+
match=f"error loading dynamic library: .*: {re.escape(str(plugin_path))}$",
38+
):
39+
pl.select(expr)
40+
41+
42+
@pytest.mark.write_disk
43+
def test_register_plugin_function_invalid_plugin_long_path(
44+
plmonkeypatch: PlMonkeyPatch, tmp_path: Path
45+
) -> None:
46+
plmonkeypatch.setenv("POLARS_VERBOSE", "0")
47+
plugin_dir = tmp_path / ("a" * 100)
48+
plugin_dir.mkdir()
49+
plugin_path = plugin_dir / "lib.so"
50+
plugin_path.touch()
51+
52+
expr = register_plugin_function(
53+
plugin_path=plugin_path, function_name="hello", args=5
54+
)
55+
56+
# Long paths are truncated from the front, keeping the tail and a hint.
57+
# Match either path separator, as Windows joins paths with a backslash.
58+
with pytest.raises(
59+
ComputeError,
60+
match=(
61+
r"error loading dynamic library: .*: \.\.\.a+[\\/]lib\.so "
62+
r"\(set POLARS_VERBOSE=1 to see full path\)"
63+
),
64+
) as exc_info:
3165
pl.select(expr)
66+
assert str(plugin_path) not in str(exc_info.value)
3267

3368

3469
@pytest.mark.parametrize(

0 commit comments

Comments
 (0)