From 700256bbdbf40042ecadad340f7909589a8f7ecc Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Sat, 6 Jul 2013 22:48:20 +0000 Subject: [PATCH] Correctly restore the originally used C locale in wxLocale dtor. Save the original locale used before we changed it instead of "restoring" the same locate that this wxLocale object was using. Add a unit test to verify that this does work as expected. Closes #14873. git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@74426 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- src/common/intl.cpp | 14 ++++++-------- tests/intl/intltest.cpp | 26 ++++++++++++++++++++++++++ 2 files changed, 32 insertions(+), 8 deletions(-) diff --git a/src/common/intl.cpp b/src/common/intl.cpp index ee790a5c07..16bdbd4563 100644 --- a/src/common/intl.cpp +++ b/src/common/intl.cpp @@ -206,7 +206,11 @@ wxLanguageInfoArray *wxLocale::ms_languagesDB = NULL; void wxLocale::DoCommonInit() { - m_pszOldLocale = NULL; + // Store the current locale in order to be able to restore it in the dtor. + m_pszOldLocale = wxSetlocale(LC_ALL, NULL); + if ( m_pszOldLocale ) + m_pszOldLocale = wxStrdup(m_pszOldLocale); + m_pOldLocale = wxSetLocale(this); @@ -285,13 +289,7 @@ bool wxLocale::DoInit(const wxString& name, wxS("no locale to set in wxLocale::Init()") ); } - const char *oldLocale = wxSetlocale(LC_ALL, szLocale); - if ( oldLocale ) - m_pszOldLocale = wxStrdup(oldLocale); - else - m_pszOldLocale = NULL; - - if ( m_pszOldLocale == NULL ) + if ( !wxSetlocale(LC_ALL, szLocale) ) { wxLogError(_("locale '%s' cannot be set."), szLocale); } diff --git a/tests/intl/intltest.cpp b/tests/intl/intltest.cpp index d821e963cf..2563da38fd 100644 --- a/tests/intl/intltest.cpp +++ b/tests/intl/intltest.cpp @@ -39,6 +39,7 @@ public: private: CPPUNIT_TEST_SUITE( IntlTestCase ); + CPPUNIT_TEST( RestoreLocale ); CPPUNIT_TEST( Domain ); CPPUNIT_TEST( Headers ); CPPUNIT_TEST( DateTimeFmtFrench ); @@ -46,12 +47,18 @@ private: CPPUNIT_TEST( IsAvailable ); CPPUNIT_TEST_SUITE_END(); + void RestoreLocale(); void Domain(); void Headers(); void DateTimeFmtFrench(); void DateTimeFmtC(); void IsAvailable(); + static wxString GetDecimalPoint() + { + return wxLocale::GetInfo(wxLOCALE_DECIMAL_POINT, wxLOCALE_CAT_NUMBER); + } + wxLocale *m_locale; DECLARE_NO_COPY_CLASS(IntlTestCase) @@ -92,6 +99,25 @@ void IntlTestCase::tearDown() } } +void IntlTestCase::RestoreLocale() +{ + if ( !m_locale ) + return; + + // We must be using the French locale now, it was changed in setUp(). + CPPUNIT_ASSERT_EQUAL( ",", GetDecimalPoint() ); + + // Switch to the English locale. + { + wxLocale locEn(wxLANGUAGE_ENGLISH); + CPPUNIT_ASSERT_EQUAL( ".", GetDecimalPoint() ); + } + + // Verify that after destroying the English locale object, French locale is + // restored. + CPPUNIT_ASSERT_EQUAL( ",", GetDecimalPoint() ); +} + void IntlTestCase::Domain() { if (!m_locale) -- 2.45.2