diff --git a/include/LightGBM/c_api.h b/include/LightGBM/c_api.h index 87e64a8ada8e..debe163359ae 100644 --- a/include/LightGBM/c_api.h +++ b/include/LightGBM/c_api.h @@ -23,7 +23,6 @@ #include #include #include -#include #endif @@ -434,12 +433,12 @@ LIGHTGBM_C_EXPORT int LGBM_DatasetAddFeaturesFrom(DatasetHandle target, /* --- start Booster interfaces */ /*! -* \brief Get boolean representing whether booster is fitting linear trees. +* \brief Get int representing whether booster is fitting linear trees. * \param handle Handle of booster * \param[out] out The address to hold linear trees indicator * \return 0 when succeed, -1 when failure happens */ -LIGHTGBM_C_EXPORT int LGBM_BoosterGetLinear(BoosterHandle handle, bool* out); +LIGHTGBM_C_EXPORT int LGBM_BoosterGetLinear(BoosterHandle handle, int* out); /*! * \brief Create a new boosting learner. @@ -1361,11 +1360,17 @@ static char* LastErrorMsg() { static THREAD_LOCAL char err_msg[512] = "Everythin #endif /*! * \brief Set string message of the last error. + * \note + * This will call unsafe ``sprintf`` when compiled using C standards before C99. * \param msg Error message */ INLINE_FUNCTION void LGBM_SetLastError(const char* msg) { +#if !defined(__cplusplus) && (!defined(__STDC__) || (__STDC_VERSION__ < 199901L)) + sprintf(LastErrorMsg(), "%s", msg); /* NOLINT(runtime/printf) */ +#else const int err_buf_len = 512; snprintf(LastErrorMsg(), err_buf_len, "%s", msg); +#endif } #endif /* LIGHTGBM_C_API_H_ */ diff --git a/python-package/lightgbm/basic.py b/python-package/lightgbm/basic.py index 83a4b5c071da..a44e5831f9c7 100644 --- a/python-package/lightgbm/basic.py +++ b/python-package/lightgbm/basic.py @@ -3566,7 +3566,7 @@ def refit(self, data, label, decay_rate=0.9, **kwargs): predictor = self._to_predictor(deepcopy(kwargs)) leaf_preds = predictor.predict(data, -1, pred_leaf=True) nrow, ncol = leaf_preds.shape - out_is_linear = ctypes.c_bool(False) + out_is_linear = ctypes.c_int(0) _safe_call(_LIB.LGBM_BoosterGetLinear( self.handle, ctypes.byref(out_is_linear))) @@ -3575,7 +3575,7 @@ def refit(self, data, label, decay_rate=0.9, **kwargs): params=self.params, default_value=None ) - new_params["linear_tree"] = out_is_linear.value + new_params["linear_tree"] = bool(out_is_linear.value) train_set = Dataset(data, label, silent=True, params=new_params) new_params['refit_decay_rate'] = decay_rate new_booster = Booster(new_params, train_set) diff --git a/src/c_api.cpp b/src/c_api.cpp index bc3bfc3b2434..c9972edfa4d7 100644 --- a/src/c_api.cpp +++ b/src/c_api.cpp @@ -1657,10 +1657,14 @@ int LGBM_BoosterGetNumClasses(BoosterHandle handle, int* out_len) { API_END(); } -int LGBM_BoosterGetLinear(BoosterHandle handle, bool* out) { +int LGBM_BoosterGetLinear(BoosterHandle handle, int* out) { API_BEGIN(); Booster* ref_booster = reinterpret_cast(handle); - *out = ref_booster->GetBoosting()->IsLinear(); + if (ref_booster->GetBoosting()->IsLinear()) { + *out = 1; + } else { + *out = 0; + } API_END(); }