"vscode:/vscode.git/clone" did not exist on "37ce3eb2a9cf867a04e8b2e8f9cf7d50147c4ef8"
Unverified Commit 6b68967d authored by Alberto Ferreira's avatar Alberto Ferreira Committed by GitHub
Browse files

Fix Booster read/write locale dependency (#2891)



* Fix Booster read/write locale dependency

* Address review comments

* Move LocaleContext.h->locale_context.h
Co-authored-by: default avataralberto.ferreira <alberto.ferreira@feedzai.com>
parent fc0f132f
/*!
* Copyright (c) 2020 Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See LICENSE file in the project root for license information.
*/
#ifndef LIGHTGBM_LOCALE_CONTEXT_H_
#define LIGHTGBM_LOCALE_CONTEXT_H_
#include <clocale>
#include <locale>
/*!
* Class to override the program global locale during this object lifetime.
* After the object is destroyed, the locale is returned to its original state.
*
* @warn This is not thread-safe.
*/
class LocaleContext {
public:
/*!
* Override the current program global locale during this object lifetime.
*
* @param target_locale override the locale to this locale setting.
* @warn This is not thread-safe.
* @note This doesn't override cout, cerr, etc.
*/
explicit LocaleContext(const char* target_locale = "C") {
std::locale::global(std::locale(target_locale));
}
/*!
* Restores the old global locale.
*/
~LocaleContext() {
std::locale::global(_saved_global_locale);
}
private:
std::locale _saved_global_locale; //!< Stores global locale at initialization.
};
#endif // LIGHTGBM_LOCALE_CONTEXT_H_
......@@ -13,6 +13,7 @@
#include <LightGBM/objective_function.h>
#include <LightGBM/prediction_early_stop.h>
#include <LightGBM/utils/common.h>
#include <LightGBM/utils/locale_context.h>
#include <LightGBM/utils/log.h>
#include <LightGBM/utils/openmp_wrapper.h>
#include <LightGBM/utils/random.h>
......@@ -1210,6 +1211,7 @@ int LGBM_BoosterCreateFromModelfile(
int* out_num_iterations,
BoosterHandle* out) {
API_BEGIN();
LocaleContext withLocaleContext("C");
auto ret = std::unique_ptr<Booster>(new Booster(filename));
*out_num_iterations = ret->GetBoosting()->GetCurrentIteration();
*out = ret.release();
......@@ -1221,6 +1223,7 @@ int LGBM_BoosterLoadModelFromString(
int* out_num_iterations,
BoosterHandle* out) {
API_BEGIN();
LocaleContext withLocaleContext("C");
auto ret = std::unique_ptr<Booster>(new Booster(nullptr));
ret->LoadModelFromString(model_str);
*out_num_iterations = ret->GetBoosting()->GetCurrentIteration();
......@@ -1635,6 +1638,7 @@ int LGBM_BoosterSaveModel(BoosterHandle handle,
int num_iteration,
const char* filename) {
API_BEGIN();
LocaleContext withLocaleContext("C");
Booster* ref_booster = reinterpret_cast<Booster*>(handle);
ref_booster->SaveModelToFile(start_iteration, num_iteration, filename);
API_END();
......@@ -1647,6 +1651,7 @@ int LGBM_BoosterSaveModelToString(BoosterHandle handle,
int64_t* out_len,
char* out_str) {
API_BEGIN();
LocaleContext withLocaleContext("C");
Booster* ref_booster = reinterpret_cast<Booster*>(handle);
std::string model = ref_booster->SaveModelToString(start_iteration, num_iteration);
*out_len = static_cast<int64_t>(model.size()) + 1;
......@@ -1663,6 +1668,7 @@ int LGBM_BoosterDumpModel(BoosterHandle handle,
int64_t* out_len,
char* out_str) {
API_BEGIN();
LocaleContext withLocaleContext("C");
Booster* ref_booster = reinterpret_cast<Booster*>(handle);
std::string model = ref_booster->DumpModel(start_iteration, num_iteration);
*out_len = static_cast<int64_t>(model.size()) + 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