From 263a02b6d66fc437ab915cce7194ff76bd227b3e Mon Sep 17 00:00:00 2001 From: "alberto.ferreira" Date: Sun, 8 Mar 2020 22:05:34 +0000 Subject: [PATCH 1/3] Fix Booster read/write locale dependency --- include/LightGBM/utils/LocaleContext.h | 38 ++++++++++++++++++++++++++ src/c_api.cpp | 6 ++++ 2 files changed, 44 insertions(+) create mode 100644 include/LightGBM/utils/LocaleContext.h diff --git a/include/LightGBM/utils/LocaleContext.h b/include/LightGBM/utils/LocaleContext.h new file mode 100644 index 000000000000..dc5a84ca848a --- /dev/null +++ b/include/LightGBM/utils/LocaleContext.h @@ -0,0 +1,38 @@ +#ifndef __LOCALE_CONTEXT_H__ +#define __LOCALE_CONTEXT_H__ + +#include +#include + +/** + * 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. + */ + 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 // __LOCALE_CONTEXT_H__ diff --git a/src/c_api.cpp b/src/c_api.cpp index e684ec1aea9b..dba383b39a83 100644 --- a/src/c_api.cpp +++ b/src/c_api.cpp @@ -17,6 +17,7 @@ #include #include #include +#include #include #include @@ -1208,6 +1209,7 @@ int LGBM_BoosterCreateFromModelfile( int* out_num_iterations, BoosterHandle* out) { API_BEGIN(); + LocaleContext withLocaleContext("C"); auto ret = std::unique_ptr(new Booster(filename)); *out_num_iterations = ret->GetBoosting()->GetCurrentIteration(); *out = ret.release(); @@ -1219,6 +1221,7 @@ int LGBM_BoosterLoadModelFromString( int* out_num_iterations, BoosterHandle* out) { API_BEGIN(); + LocaleContext withLocaleContext("C"); auto ret = std::unique_ptr(new Booster(nullptr)); ret->LoadModelFromString(model_str); *out_num_iterations = ret->GetBoosting()->GetCurrentIteration(); @@ -1631,6 +1634,7 @@ int LGBM_BoosterSaveModel(BoosterHandle handle, int num_iteration, const char* filename) { API_BEGIN(); + LocaleContext withLocaleContext("C"); Booster* ref_booster = reinterpret_cast(handle); ref_booster->SaveModelToFile(start_iteration, num_iteration, filename); API_END(); @@ -1643,6 +1647,7 @@ int LGBM_BoosterSaveModelToString(BoosterHandle handle, int64_t* out_len, char* out_str) { API_BEGIN(); + LocaleContext withLocaleContext("C"); Booster* ref_booster = reinterpret_cast(handle); std::string model = ref_booster->SaveModelToString(start_iteration, num_iteration); *out_len = static_cast(model.size()) + 1; @@ -1659,6 +1664,7 @@ int LGBM_BoosterDumpModel(BoosterHandle handle, int64_t* out_len, char* out_str) { API_BEGIN(); + LocaleContext withLocaleContext("C"); Booster* ref_booster = reinterpret_cast(handle); std::string model = ref_booster->DumpModel(start_iteration, num_iteration); *out_len = static_cast(model.size()) + 1; From 6988a7cb234e7848861398ecd8395034185bc773 Mon Sep 17 00:00:00 2001 From: "alberto.ferreira" Date: Tue, 17 Mar 2020 18:28:14 +0000 Subject: [PATCH 2/3] Address review comments --- include/LightGBM/utils/LocaleContext.h | 30 ++++++++++++++------------ src/c_api.cpp | 2 +- 2 files changed, 17 insertions(+), 15 deletions(-) diff --git a/include/LightGBM/utils/LocaleContext.h b/include/LightGBM/utils/LocaleContext.h index dc5a84ca848a..f8f0073fb5cc 100644 --- a/include/LightGBM/utils/LocaleContext.h +++ b/include/LightGBM/utils/LocaleContext.h @@ -1,38 +1,40 @@ -#ifndef __LOCALE_CONTEXT_H__ -#define __LOCALE_CONTEXT_H__ +/*! + * 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 #include -/** +/*! * 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: - - /** + */ +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. */ - LocaleContext(const char* target_locale = "C") { + 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. + private: + std::locale _saved_global_locale; //!< Stores global locale at initialization. }; -#endif // __LOCALE_CONTEXT_H__ +#endif // LIGHTGBM_LOCALE_CONTEXT_H_ diff --git a/src/c_api.cpp b/src/c_api.cpp index dba383b39a83..a51c83896707 100644 --- a/src/c_api.cpp +++ b/src/c_api.cpp @@ -13,11 +13,11 @@ #include #include #include +#include #include #include #include #include -#include #include #include From a39e12a57e2197c06324624a6887c1445a4a2640 Mon Sep 17 00:00:00 2001 From: "alberto.ferreira" Date: Tue, 17 Mar 2020 18:30:30 +0000 Subject: [PATCH 3/3] Move LocaleContext.h->locale_context.h --- include/LightGBM/utils/{LocaleContext.h => locale_context.h} | 0 src/c_api.cpp | 2 +- 2 files changed, 1 insertion(+), 1 deletion(-) rename include/LightGBM/utils/{LocaleContext.h => locale_context.h} (100%) diff --git a/include/LightGBM/utils/LocaleContext.h b/include/LightGBM/utils/locale_context.h similarity index 100% rename from include/LightGBM/utils/LocaleContext.h rename to include/LightGBM/utils/locale_context.h diff --git a/src/c_api.cpp b/src/c_api.cpp index a51c83896707..b82af504c513 100644 --- a/src/c_api.cpp +++ b/src/c_api.cpp @@ -13,7 +13,7 @@ #include #include #include -#include +#include #include #include #include