Skip to content

Commit 97f1c36

Browse files
committed
Ensure _dl_last_error() does not raise UnicodeDecodeError
1 parent ee5d05e commit 97f1c36

File tree

1 file changed

+6
-3
lines changed

1 file changed

+6
-3
lines changed

cuda_pathfinder/cuda/pathfinder/_dynamic_libs/load_dl_linux.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import ctypes
66
import ctypes.util
77
import os
8-
from typing import Optional
8+
from typing import Optional, cast
99

1010
from cuda.pathfinder._dynamic_libs.load_dl_common import LoadedDL
1111
from cuda.pathfinder._dynamic_libs.supported_nvidia_libs import SUPPORTED_LINUX_SONAMES
@@ -40,8 +40,11 @@ def _load_libdl() -> ctypes.CDLL:
4040

4141

4242
def _dl_last_error() -> Optional[str]:
43-
msg = LIBDL.dlerror()
44-
return msg.decode() if msg else None
43+
msg_bytes = cast(Optional[bytes], LIBDL.dlerror())
44+
if not msg_bytes:
45+
return None # no pending error
46+
# Never raises; undecodable bytes are mapped to U+DC80..U+DCFF
47+
return msg_bytes.decode("utf-8", "surrogateescape")
4548

4649

4750
def abs_path_for_dynamic_library(libname: str, handle: ctypes.CDLL) -> str:

0 commit comments

Comments
 (0)