Move locale-switching code in tests to a shared header
QLocale and QString tests had copies of a TransientLocale; we've recently improved the QLocale one. Rather than duplicating those rather complicated improvements, finally share a common version. In the process, I noticed that setlocale() only returns the prior value when passed nullptr as the new value; so rework the implementation to get that right, so that it now correctly restores the prior locale. That, in turn, means there's now a later call to setlocale(), when we actually set the changed setting, which may invalidate the earlier return; so copy it to a QByteArray before the second call. Included Ivan Solovev's improved version of how to reset the locale, since TransientLocale needs it. Pick-to: 6.2 Change-Id: I4cb1efbda42f0e2cdd934e04b3b3732ce0f45a06 Reviewed-by: Ivan Solovev <ivan.solovev@qt.io> Reviewed-by: Mårten Nordheim <marten.nordheim@qt.io>
This commit is contained in:
parent
5e84e9fe3a
commit
458d53f572
@ -39,12 +39,12 @@
|
|||||||
# include <qprocess.h>
|
# include <qprocess.h>
|
||||||
#endif
|
#endif
|
||||||
#include <float.h>
|
#include <float.h>
|
||||||
#include <locale.h>
|
|
||||||
|
|
||||||
#include <qlocale.h>
|
#include <qlocale.h>
|
||||||
#include <private/qlocale_p.h>
|
#include <private/qlocale_p.h>
|
||||||
#include <private/qlocale_tools_p.h>
|
#include <private/qlocale_tools_p.h>
|
||||||
#include <qnumeric.h>
|
#include <qnumeric.h>
|
||||||
|
#include "../../../../shared/localechange.h"
|
||||||
|
|
||||||
#if defined(Q_OS_LINUX) && !defined(__UCLIBC__)
|
#if defined(Q_OS_LINUX) && !defined(__UCLIBC__)
|
||||||
# define QT_USE_FENV
|
# define QT_USE_FENV
|
||||||
@ -176,58 +176,7 @@ private:
|
|||||||
bool europeanTimeZone;
|
bool europeanTimeZone;
|
||||||
void toReal_data();
|
void toReal_data();
|
||||||
|
|
||||||
class TransientLocale
|
using TransientLocale = QTestLocaleChange::TransientLocale;
|
||||||
{
|
|
||||||
const int m_category;
|
|
||||||
const char *const m_prior;
|
|
||||||
#if !defined(QT_NO_SYSTEMLOCALE) && defined(Q_OS_UNIX) \
|
|
||||||
&& (!defined(Q_OS_DARWIN) || defined(Q_OS_NACL))
|
|
||||||
#define TRANSIENT_ENV
|
|
||||||
// Unix system locale consults environment variables, so we need to set
|
|
||||||
// the appropriate one, too.
|
|
||||||
const QByteArray m_envVar, m_envPrior;
|
|
||||||
const bool m_envSet;
|
|
||||||
static QByteArray categoryToEnv(int category)
|
|
||||||
{
|
|
||||||
switch (category) {
|
|
||||||
#define CASE(cat) case cat: return #cat
|
|
||||||
CASE(LC_ALL); CASE(LC_NUMERIC); CASE(LC_TIME); CASE(LC_MONETARY);
|
|
||||||
CASE(LC_MESSAGES); CASE(LC_MEASUREMENT); CASE(LC_COLLATE);
|
|
||||||
#undef CASE
|
|
||||||
// Nothing in our code pays attention to any other LC_*
|
|
||||||
default:
|
|
||||||
Q_UNREACHABLE();
|
|
||||||
qFatal("You need to add a case for this category");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
#endif // TRANSIENT_ENV
|
|
||||||
public:
|
|
||||||
TransientLocale(int category, const char *locale)
|
|
||||||
: m_category(category),
|
|
||||||
m_prior(setlocale(category, locale))
|
|
||||||
#ifdef TRANSIENT_ENV
|
|
||||||
, m_envVar(categoryToEnv(category)),
|
|
||||||
m_envPrior(qgetenv(m_envVar.constData())),
|
|
||||||
m_envSet(qputenv(m_envVar.constData(), locale))
|
|
||||||
#endif
|
|
||||||
{
|
|
||||||
QSystemLocale dummy; // to provoke a refresh of the system locale
|
|
||||||
}
|
|
||||||
~TransientLocale()
|
|
||||||
{
|
|
||||||
#ifdef TRANSIENT_ENV
|
|
||||||
if (m_envSet) {
|
|
||||||
if (m_envPrior.isEmpty())
|
|
||||||
qunsetenv(m_envVar.constData());
|
|
||||||
else
|
|
||||||
qputenv(m_envVar.constData(), m_envPrior);
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
setlocale(m_category, m_prior);
|
|
||||||
QSystemLocale dummy; // to provoke a refresh of the system locale
|
|
||||||
}
|
|
||||||
#undef TRANSIENT_ENV
|
|
||||||
};
|
|
||||||
};
|
};
|
||||||
|
|
||||||
tst_QLocale::tst_QLocale()
|
tst_QLocale::tst_QLocale()
|
||||||
@ -2242,7 +2191,7 @@ public:
|
|||||||
setWinLocaleInfo(LOCALE_SNATIVEDIGITS, m_digits);
|
setWinLocaleInfo(LOCALE_SNATIVEDIGITS, m_digits);
|
||||||
setWinLocaleInfo(LOCALE_IDIGITSUBSTITUTION, m_subst);
|
setWinLocaleInfo(LOCALE_IDIGITSUBSTITUTION, m_subst);
|
||||||
|
|
||||||
QSystemLocale dummy; // to provoke a refresh of the system locale
|
QTestLocaleChange::resetSystemLocale();
|
||||||
}
|
}
|
||||||
|
|
||||||
QString m_decimal, m_thousand, m_sdate, m_ldate, m_stime, m_ltime, m_digits, m_subst;
|
QString m_decimal, m_thousand, m_sdate, m_ldate, m_stime, m_ltime, m_digits, m_subst;
|
||||||
@ -2268,8 +2217,7 @@ void tst_QLocale::windowsDefaultLocale()
|
|||||||
setWinLocaleInfo(LOCALE_IDIGITSUBSTITUTION, u"2");
|
setWinLocaleInfo(LOCALE_IDIGITSUBSTITUTION, u"2");
|
||||||
// NB: when adding to the system things being set, be sure to update RestoreLocaleHelper, too.
|
// NB: when adding to the system things being set, be sure to update RestoreLocaleHelper, too.
|
||||||
|
|
||||||
QSystemLocale dummy; // to provoke a refresh of the system locale
|
QLocale locale = QTestLocaleChange::resetSystemLocale();
|
||||||
QLocale locale = QLocale::system();
|
|
||||||
|
|
||||||
// Make sure we are seeing the system's format strings
|
// Make sure we are seeing the system's format strings
|
||||||
QCOMPARE(locale.zeroDigit(), QStringView(u"\u3007"));
|
QCOMPARE(locale.zeroDigit(), QStringView(u"\u3007"));
|
||||||
|
@ -60,6 +60,7 @@
|
|||||||
#include <ctype.h>
|
#include <ctype.h>
|
||||||
|
|
||||||
#include "../shared/test_number_shared.h"
|
#include "../shared/test_number_shared.h"
|
||||||
|
#include "../../../../shared/localechange.h"
|
||||||
|
|
||||||
#define CREATE_VIEW(string) \
|
#define CREATE_VIEW(string) \
|
||||||
const QString padded = QLatin1Char(' ') + string + QLatin1Char(' '); \
|
const QString padded = QLatin1Char(' ') + string + QLatin1Char(' '); \
|
||||||
@ -329,16 +330,7 @@ class tst_QString : public QObject
|
|||||||
void insert_impl() const { insert_impl<ArgType, QString &(QString::*)(qsizetype, const ArgType&)>(); }
|
void insert_impl() const { insert_impl<ArgType, QString &(QString::*)(qsizetype, const ArgType&)>(); }
|
||||||
void insert_data(bool emptyIsNoop = false);
|
void insert_data(bool emptyIsNoop = false);
|
||||||
|
|
||||||
class TransientLocale
|
using TransientLocale = QTestLocaleChange::TransientLocale;
|
||||||
{
|
|
||||||
const int m_category;
|
|
||||||
const char *const m_prior;
|
|
||||||
public:
|
|
||||||
TransientLocale(int category, const char *locale)
|
|
||||||
: m_category(category), m_prior(setlocale(category, locale)) {}
|
|
||||||
bool isValid() const { return !!m_prior; }
|
|
||||||
~TransientLocale() { if (m_prior) setlocale(m_category, m_prior); }
|
|
||||||
};
|
|
||||||
|
|
||||||
class TransientDefaultLocale
|
class TransientDefaultLocale
|
||||||
{
|
{
|
||||||
|
111
tests/shared/localechange.h
Normal file
111
tests/shared/localechange.h
Normal file
@ -0,0 +1,111 @@
|
|||||||
|
/****************************************************************************
|
||||||
|
**
|
||||||
|
** Copyright (C) 2021 The Qt Company Ltd.
|
||||||
|
** Contact: https://www.qt.io/licensing/
|
||||||
|
**
|
||||||
|
** This file is part of the test suite of the Qt Toolkit.
|
||||||
|
**
|
||||||
|
** $QT_BEGIN_LICENSE:GPL-EXCEPT$
|
||||||
|
** Commercial License Usage
|
||||||
|
** Licensees holding valid commercial Qt licenses may use this file in
|
||||||
|
** accordance with the commercial license agreement provided with the
|
||||||
|
** Software or, alternatively, in accordance with the terms contained in
|
||||||
|
** a written agreement between you and The Qt Company. For licensing terms
|
||||||
|
** and conditions see https://www.qt.io/terms-conditions. For further
|
||||||
|
** information use the contact form at https://www.qt.io/contact-us.
|
||||||
|
**
|
||||||
|
** GNU General Public License Usage
|
||||||
|
** Alternatively, this file may be used under the terms of the GNU
|
||||||
|
** General Public License version 3 as published by the Free Software
|
||||||
|
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
|
||||||
|
** included in the packaging of this file. Please review the following
|
||||||
|
** information to ensure the GNU General Public License requirements will
|
||||||
|
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
|
||||||
|
**
|
||||||
|
** $QT_END_LICENSE$
|
||||||
|
**
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
#ifndef QT_TESTS_SHARED_LOCALE_CHANGE_H
|
||||||
|
#define QT_TESTS_SHARED_LOCALE_CHANGE_H
|
||||||
|
#include <qglobal.h>
|
||||||
|
#include <QtCore/QByteArray>
|
||||||
|
#include <QtCore/QLocale>
|
||||||
|
#include <private/qlocale_p.h>
|
||||||
|
|
||||||
|
#include <locale.h>
|
||||||
|
|
||||||
|
namespace QTestLocaleChange {
|
||||||
|
|
||||||
|
QLocale resetSystemLocale()
|
||||||
|
{
|
||||||
|
#ifndef QT_NO_SYSTEMLOCALE
|
||||||
|
{ // Transient instance marks system locale data as stale:
|
||||||
|
QSystemLocale dummy;
|
||||||
|
} // Now we can reinitialize:
|
||||||
|
#endif
|
||||||
|
return QLocale::system();
|
||||||
|
}
|
||||||
|
|
||||||
|
class TransientLocale
|
||||||
|
{
|
||||||
|
const int m_category;
|
||||||
|
const QByteArray m_prior;
|
||||||
|
const bool m_didSet;
|
||||||
|
#if !defined(QT_NO_SYSTEMLOCALE) && defined(Q_OS_UNIX) \
|
||||||
|
&& (!defined(Q_OS_DARWIN) || defined(Q_OS_NACL))
|
||||||
|
#define TRANSIENT_ENV
|
||||||
|
// Unix system locale consults environment variables, so we need to set
|
||||||
|
// the appropriate one, too.
|
||||||
|
const QByteArray m_envVar, m_envPrior;
|
||||||
|
const bool m_envSet;
|
||||||
|
static QByteArray categoryToEnv(int category)
|
||||||
|
{
|
||||||
|
switch (category) {
|
||||||
|
#define CASE(cat) case cat: return #cat
|
||||||
|
CASE(LC_ALL); CASE(LC_NUMERIC); CASE(LC_TIME); CASE(LC_MONETARY);
|
||||||
|
CASE(LC_MESSAGES); CASE(LC_MEASUREMENT); CASE(LC_COLLATE);
|
||||||
|
#undef CASE
|
||||||
|
// Nothing in our code pays attention to any other LC_*
|
||||||
|
default:
|
||||||
|
Q_UNREACHABLE();
|
||||||
|
qFatal("You need to add a case for this category");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
#endif // TRANSIENT_ENV
|
||||||
|
public:
|
||||||
|
TransientLocale(int category, const char *locale)
|
||||||
|
: m_category(category),
|
||||||
|
m_prior(setlocale(category, nullptr)),
|
||||||
|
// That return value may be stomped by this later call, so we copy
|
||||||
|
// it to a QByteArray for safe keeping.
|
||||||
|
m_didSet(setlocale(category, locale) != nullptr)
|
||||||
|
#ifdef TRANSIENT_ENV
|
||||||
|
, m_envVar(categoryToEnv(category)),
|
||||||
|
m_envPrior(qgetenv(m_envVar.constData())),
|
||||||
|
m_envSet(qputenv(m_envVar.constData(), locale))
|
||||||
|
#endif
|
||||||
|
{
|
||||||
|
resetSystemLocale();
|
||||||
|
}
|
||||||
|
~TransientLocale()
|
||||||
|
{
|
||||||
|
#ifdef TRANSIENT_ENV
|
||||||
|
if (m_envSet) {
|
||||||
|
if (m_envPrior.isEmpty())
|
||||||
|
qunsetenv(m_envVar.constData());
|
||||||
|
else
|
||||||
|
qputenv(m_envVar.constData(), m_envPrior);
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
if (m_prior.size())
|
||||||
|
setlocale(m_category, m_prior.constData());
|
||||||
|
resetSystemLocale();
|
||||||
|
}
|
||||||
|
#undef TRANSIENT_ENV
|
||||||
|
|
||||||
|
bool isValid() const { return m_didSet; }
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif // QT_TESTS_SHARED_LOCALE_CHANGE_H
|
Loading…
x
Reference in New Issue
Block a user