From 92a86642c2c1e8e91145e3112d7f7bdb7622ee7d Mon Sep 17 00:00:00 2001 From: James Lamb Date: Thu, 2 Mar 2023 20:32:59 -0600 Subject: [PATCH 1/2] [ci] isolate c_api_test library-loading from Python source tree --- tests/c_api_test/test_.py | 42 +++++++-------------------------------- 1 file changed, 7 insertions(+), 35 deletions(-) diff --git a/tests/c_api_test/test_.py b/tests/c_api_test/test_.py index 55e4af906dd9..fa4517a1e56b 100644 --- a/tests/c_api_test/test_.py +++ b/tests/c_api_test/test_.py @@ -1,48 +1,20 @@ # coding: utf-8 import ctypes -from os import environ from pathlib import Path from platform import system import numpy as np from scipy import sparse - -def find_lib_path(): - 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], - curr_path.parents[1] / 'python-package' / 'lightgbm' / 'compile', - curr_path.parents[1] / 'python-package' / 'compile', - curr_path.parents[1] / 'lib'] +try: + from lightgbm.basic import _LIB as LIB +except ModuleNotFoundError: + print(f"Could not import lightgbm Python package, looking for lib_lightgbm at the repo root") if system() in ('Windows', 'Microsoft'): - dll_path.append(curr_path.parents[1] / 'python-package' / 'compile' / 'Release/') - dll_path.append(curr_path.parents[1] / 'python-package' / 'compile' / 'windows' / 'x64' / 'DLL') - dll_path.append(curr_path.parents[1] / 'Release') - dll_path.append(curr_path.parents[1] / 'windows' / 'x64' / 'DLL') - dll_path = [p / 'lib_lightgbm.dll' for p in dll_path] + lib_file = Path(__file__).absolute().parents[2] / "Release"/ "lib_lightgbm.dll" else: - dll_path = [p / 'lib_lightgbm.so' for p in dll_path] - lib_path = [str(p) for p in dll_path if p.is_file()] - if not lib_path: - dll_path_joined = '\n'.join(map(str, dll_path)) - raise Exception(f'Cannot find lightgbm library file in following paths:\n{dll_path_joined}') - return lib_path - - -def LoadDll(): - lib_path = find_lib_path() - if len(lib_path) == 0: - return None - lib = ctypes.cdll.LoadLibrary(lib_path[0]) - return lib - - -LIB = LoadDll() + lib_file = Path(__file__).absolute().parents[2] / "lib_lightgbm.so" + LIB = ctypes.cdll.LoadLibrary(lib_file) LIB.LGBM_GetLastError.restype = ctypes.c_char_p From f034e2c7ce68e0f5f3760c981f74a8f8b7c0ec15 Mon Sep 17 00:00:00 2001 From: James Lamb Date: Thu, 2 Mar 2023 21:28:37 -0600 Subject: [PATCH 2/2] linting --- tests/c_api_test/test_.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/c_api_test/test_.py b/tests/c_api_test/test_.py index fa4517a1e56b..4bb76e4aba19 100644 --- a/tests/c_api_test/test_.py +++ b/tests/c_api_test/test_.py @@ -9,9 +9,9 @@ try: from lightgbm.basic import _LIB as LIB except ModuleNotFoundError: - print(f"Could not import lightgbm Python package, looking for lib_lightgbm at the repo root") + print("Could not import lightgbm Python package, looking for lib_lightgbm at the repo root") if system() in ('Windows', 'Microsoft'): - lib_file = Path(__file__).absolute().parents[2] / "Release"/ "lib_lightgbm.dll" + lib_file = Path(__file__).absolute().parents[2] / "Release" / "lib_lightgbm.dll" else: lib_file = Path(__file__).absolute().parents[2] / "lib_lightgbm.so" LIB = ctypes.cdll.LoadLibrary(lib_file)