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
from collections import OrderedDict
from copy import deepcopy
from functools import wraps
from os import SEEK_END
from os import SEEK_END, environ
from os.path import getsize
from pathlib import Path
from tempfile import NamedTemporaryFile
......@@ -108,11 +108,9 @@ def _log_callback(msg: bytes) -> None:
_log_native(str(msg.decode('utf-8')))
def _load_lib() -> Optional[ctypes.CDLL]:
def _load_lib() -> ctypes.CDLL:
"""Load LightGBM library."""
lib_path = find_lib_path()
if len(lib_path) == 0:
return None
lib = ctypes.cdll.LoadLibrary(lib_path[0])
lib.LGBM_GetLastError.restype = ctypes.c_char_p
callback = ctypes.CFUNCTYPE(None, ctypes.c_char_p)
......@@ -122,7 +120,13 @@ def _load_lib() -> Optional[ctypes.CDLL]:
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)
......
# coding: utf-8
"""Find the path to LightGBM dynamic library files."""
from os import environ
from pathlib import Path
from platform import system
from typing import List
......@@ -14,10 +13,6 @@ def find_lib_path() -> List[str]:
lib_path: list of str
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
dll_path = [curr_path,
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