gemini-code-assist[bot] commented on code in PR #336:
URL: https://github.com/apache/tvm-ffi/pull/336#discussion_r2613008744


##########
python/tvm_ffi/libinfo.py:
##########
@@ -169,6 +172,33 @@ def load_lib_ctypes(package: str, target_name: str, mode: 
str) -> ctypes.CDLL:
     return ctypes.CDLL(str(lib_path), getattr(ctypes, mode))
 
 
+def load_lib_module(package: str, target_name: str, keep_module_alive: bool = 
True) -> Module:
+    """Load the tvm_ffi shared library by searching likely paths.
+
+    Parameters
+    ----------
+    package
+        The package name where the library is expected to be found. For 
example,
+        ``"apache-tvm-ffi"`` is the package name of `tvm-ffi`.
+
+    target_name
+        Name of the CMake target, e.g., ``"tvm_ffi"``. It is used to derive 
the platform-specific
+        shared library name, e.g., ``"libtvm_ffi.so"`` on Linux, 
``"tvm_ffi.dll"`` on Windows.
+
+    keep_module_alive
+        Whether to keep the loaded module alive to prevent it from being 
unloaded.
+
+    Returns
+    -------
+    The loaded shared library.
+
+    """
+    from .module import load_module  # noqa: PLC0415
+
+    lib_path: Path = _find_library_by_basename(package, target_name)
+    return load_module(lib_path, keep_module_alive=keep_module_alive)

Review Comment:
   ![medium](https://www.gstatic.com/codereviewagent/medium-priority.svg)
   
   On Windows, if the loaded shared library has dependencies on other DLLs 
located in the same directory, they might not be found at runtime. The 
`load_lib_ctypes` function includes logic to add the library's parent directory 
to the DLL search path using `os.add_dll_directory`. It would be safer to 
include similar logic here to ensure robust loading of extension modules on 
Windows.
   
   ```suggestion
       lib_path: Path = _find_library_by_basename(package, target_name)
       if sys.platform.startswith("win32"):
           os.add_dll_directory(str(lib_path.parent))
       return load_module(lib_path, keep_module_alive=keep_module_alive)
   ```



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to