Unverified Commit 58bc3af7 authored by James Lamb's avatar James Lamb Committed by GitHub
Browse files

[python-package] make library-loading stricter (#5357)

parent 68b7315f
...@@ -7,7 +7,7 @@ import warnings ...@@ -7,7 +7,7 @@ import warnings
from collections import OrderedDict from collections import OrderedDict
from copy import deepcopy from copy import deepcopy
from functools import wraps from functools import wraps
from os import SEEK_END from os import SEEK_END, environ
from os.path import getsize from os.path import getsize
from pathlib import Path from pathlib import Path
from tempfile import NamedTemporaryFile from tempfile import NamedTemporaryFile
...@@ -108,11 +108,9 @@ def _log_callback(msg: bytes) -> None: ...@@ -108,11 +108,9 @@ def _log_callback(msg: bytes) -> None:
_log_native(str(msg.decode('utf-8'))) _log_native(str(msg.decode('utf-8')))
def _load_lib() -> Optional[ctypes.CDLL]: def _load_lib() -> ctypes.CDLL:
"""Load LightGBM library.""" """Load LightGBM library."""
lib_path = find_lib_path() lib_path = find_lib_path()
if len(lib_path) == 0:
return None
lib = ctypes.cdll.LoadLibrary(lib_path[0]) lib = ctypes.cdll.LoadLibrary(lib_path[0])
lib.LGBM_GetLastError.restype = ctypes.c_char_p lib.LGBM_GetLastError.restype = ctypes.c_char_p
callback = ctypes.CFUNCTYPE(None, ctypes.c_char_p) callback = ctypes.CFUNCTYPE(None, ctypes.c_char_p)
...@@ -122,7 +120,13 @@ def _load_lib() -> Optional[ctypes.CDLL]: ...@@ -122,7 +120,13 @@ def _load_lib() -> Optional[ctypes.CDLL]:
return lib return lib
_LIB = _load_lib() # we don't need lib_lightgbm while building docs
_LIB: ctypes.CDLL
if environ.get('LIGHTGBM_BUILD_DOC', False):
from unittest.mock import Mock # isort: skip
_LIB = Mock(ctypes.CDLL) # type: ignore
else:
_LIB = _load_lib()
NUMERIC_TYPES = (int, float, bool) NUMERIC_TYPES = (int, float, bool)
......
# coding: utf-8 # coding: utf-8
"""Find the path to LightGBM dynamic library files.""" """Find the path to LightGBM dynamic library files."""
from os import environ
from pathlib import Path from pathlib import Path
from platform import system from platform import system
from typing import List from typing import List
...@@ -14,10 +13,6 @@ def find_lib_path() -> List[str]: ...@@ -14,10 +13,6 @@ def find_lib_path() -> List[str]:
lib_path: list of str lib_path: list of str
List of all found library paths to LightGBM. List of all found library paths to LightGBM.
""" """
if environ.get('LIGHTGBM_BUILD_DOC', False):
# we don't need lib_lightgbm while building docs
return []
curr_path = Path(__file__).absolute().parent curr_path = Path(__file__).absolute().parent
dll_path = [curr_path, dll_path = [curr_path,
curr_path.parents[1], curr_path.parents[1],
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment