Hi,
I added some code in Main.cxx to check the values of C and C++ locale settings:
int SlicerAppMain(int argc, char* argv[])
{
typedef qSlicerAppMainWindow SlicerMainWindowType;
typedef qSlicerStyle SlicerAppStyle;
std::cout << "C locale: " << std::setlocale(LC_ALL, nullptr) << std::endl;
std::cout << "Cpp locale: " << std::locale().name() << std::endl;
qSlicerApplicationHelper::preInitializeApplication(argv[0], new SlicerAppStyle);
std::cout << "C locale: " << std::setlocale(LC_ALL, nullptr) << std::endl;
std::cout << "Cpp locale: " << std::locale().name() << std::endl;
qSlicerApplication app(argc, argv);
if (app.returnCode() != -1)
{
return app.returnCode();
}
std::cout << "C locale: " << std::setlocale(LC_ALL, nullptr) << std::endl;
std::cout << "Cpp locale: " << std::locale().name() << std::endl;
On my PC it prints:
C locale: C
Cpp locale: C
C locale: C
Cpp locale: C
C locale: LC_CTYPE=en_US.UTF-8;LC_NUMERIC=C;LC_TIME=C;LC_COLLATE=C;LC_MONETARY=C;LC_MESSAGES=C;LC_PAPER=C;LC_NAME=C;LC_ADDRESS=C;LC_TELEPHONE=C;LC_MEASUREMENT=C;LC_IDENTIFICATION=C
Cpp locale: C
So qSlicerApplication construction set LC_CTYPe to en_US.UTF-8
It is done during Python initialization line 426 in Python-3.9.10/Python/pylifecycle.c
This difference between C and C++ locale can lead to issues if C++ locale settings are saved/restored using std::global as it is done in vtkDataReader::OpenVTKFile and vtkDataReader::CloseVTKFile:
Before open & save calls:
C locale: LC_CTYPE=en_US.UTF-8;LC_NUMERIC=C;LC_TIME=C;LC_COLLATE=C;LC_MONETARY=C;LC_MESSAGES=C;LC_PAPER=C;LC_NAME=C;LC_ADDRESS=C;LC_TELEPHONE=C;LC_MEASUREMENT=C;LC_IDENTIFICATION=C
Cpp locale: C
After open & save calls:
C locale: C
Cpp locale: C
Because std::global set C++ locale AND C locale.
So I have several questions:
- Is it ok to let python initialization set LC_CTYPE?
- If yes: should we synchronize C++ locale using this line after qSlicerApplication creation: std::locale::global(std::locale(std::setlocale(LC_ALL, nullptr))); (this is what we did in our custom app)
- If no: so C and C++ locale should always be C, is it not a problem if users type special characters as input ?
Regards