[vtkusers] wx/vtk weirdness

Marcus D. Hanwell marcus.hanwell at kitware.com
Fri Nov 27 14:26:30 EST 2009


This issue hit us on a project I worked on previously. After a quick search a 
few relevant pages may help to clear up the situation. I know from an i18n 
point of view, and from being a good library (where readers are often called 
in separate threads so as not to lock the GUI) we should not set the global 
locale.

Instead the C++ functions and classes should be used in combination with 
imbue. See for example,

http://stackoverflow.com/questions/1333451/c-locale-independent-atof

This means that the C functions such as scanf cannot be used if you are 
relying on the imbue functionality.

Marcus

On Friday 27 November 2009 13:29:29 Francois Bertel wrote:
> Actually, the documentation of setlocale() says that:
> 
> "
> On startup of the main program, the portable "C" locale is selected as
>  default. "
> (this is probably what Markus merely remembered when he said "The C
> functions as I remember set the locale globally")
> 
> It means that changing the LC_ALL environment variable in a dashboard
> script is pointless.
> 
> It also means, as stated at the beginning of this discussion (but I
> haven't read it that way) that, in the combination with wx and vtk,
> the wx component is the probably the one forcing the locales to be
> something like:
> 
>  setlocale(LC_ALL, ""); // Make the program portable to all locales
> (LC_NUMERIC, LC_TIME, ...)
> 
> 
> So we have to go back to the first approach for regression testing:
> forcing a non English locale at the beginning of the test.
> 
> 
> On Fri, Nov 27, 2009 at 1:09 PM, Mathieu Malaterre
> 
> <mathieu.malaterre at gmail.com> wrote:
> > I do not know for Windows. But for UNIX machine if the fr_FR locale is
> > not installed , I think setlocale() is simply a no-OP. Part of the
> > test should make sure that "," is indeed the LC_NUMERIC separator.
> >
> > 2cts
> >
> > On Fri, Nov 27, 2009 at 7:05 PM, Francois Bertel
> >
> > <francois.bertel at kitware.com> wrote:
> >> Actually, forcing a non English locale in a test is probably the wrong
> >> approach because the locale might not exist on the system.
> >> Instead, it is probably better to change the environment variable on a
> >> dashboard machine.
> >>
> >> I just changed the script of arkadia on VTK to run the tests under
> >> French locales, with the following lines:
> >>
> >> # save LC_ALL
> >> set(saved_lc_all "$ENV{LC_ALL}")
> >> [...]
> >> # Change locales to make sure the text file readers and writers are
> >> locale independent:
> >> # Before writing the following line, we made sure that "fr_FR.utf8" is
> >> listed in # "locale -a", if not, install package "language-support-fr"
> >> set(ENV{LC_ALL} "fr_FR.utf8")
> >>
> >> [...]
> >> ctest_test(BUILD "${CTEST_BINARY_DIRECTORY}")
> >> [...]
> >> # restore LC_ALL
> >> set(ENV{LC_ALL} "${saved_lc_all}")
> >>
> >>
> >> We'll see how bad is it for VTK tomorrow.
> >>
> >>
> >> On Fri, Nov 27, 2009 at 12:17 PM, Francois Bertel
> >>
> >> <francois.bertel at kitware.com> wrote:
> >>> ... starting with regression tests that do the following:
> >>>
> >>> For a text reader:
> >>>
> >>> 1. switch to a locale known to be different than English regarding
> >>> numerical rules, like French where "," and "." have opposite meaning.
> >>> 2. read the file (assuming the file to read is encoded in C|POSIX
> >>> locales)
> >>>
> >>> For a text writer:
> >>> 1. switch to a non English locale
> >>> 2. write a file
> >>> 3. switch to an English locale
> >>> 4. read the file
> >>>
> >>> On Fri, Nov 27, 2009 at 12:07 PM, Bill Lorensen 
<bill.lorensen at gmail.com> wrote:
> >>>> Looks like we have some work to do in both itk and vtk.
> >>>>
> >>>> Bill
> >>>>
> >>>> On Fri, Nov 27, 2009 at 12:05 PM, Francois Bertel
> >>>>
> >>>> <francois.bertel at kitware.com> wrote:
> >>>>> note: it is necessary to duplicate the return value of setlocale() as
> >>>>> it can be a pointer to a static string.
> >>>>>
> >>>>> On Fri, Nov 27, 2009 at 12:04 PM, Francois Bertel
> >>>>>
> >>>>> <francois.bertel at kitware.com> wrote:
> >>>>>> Something like that:
> >>>>>>
> >>>>>> // save the current locale
> >>>>>>  prev_locale = setlocale(LC_NUMERIC, NULL);
> >>>>>>  prev_locale = dupstring(setlocale(LC_NUMERIC, NULL));
> >>>>>> setlocale(LC_NUMERIC, "POSIX");
> >>>>>>
> >>>>>> <reader code here>
> >>>>>>
> >>>>>> // Restore locale
> >>>>>> setlocale(LC_NUMERIC, prev_locale);
> >>>>>> if(prev_locale)
> >>>>>> {
> >>>>>>  setlocale(LC_NUMERIC, prev_locale);
> >>>>>> }
> >>>>>>
> >>>>>> ref: http://www.zsh.org/mla/workers/2003/msg00210.html
> >>>>>>
> >>>>>> I don't think bug 5891 is relevant as it elimitates the use of
> >>>>>> interpretation of text into numerical values.
> >>>>>> Patch in bug 1431 was using the selocale() function (as above).
> >>>>>>
> >>>>>>
> >>>>>> On Fri, Nov 27, 2009 at 11:58 AM, Mathieu Malaterre
> >>>>>>
> >>>>>> <mathieu.malaterre at gmail.com> wrote:
> >>>>>>> On Fri, Nov 27, 2009 at 5:57 PM, Marcus D. Hanwell
> >>>>>>>
> >>>>>>> <marcus.hanwell at kitware.com> wrote:
> >>>>>>>> The C functions as I remember set the locale globally which can
> >>>>>>>> also cause unexpected bugs if a library changes this internally.
> >>>>>>>
> >>>>>>> Ah ! That answer my question :)
> >>>>>>>
> >>>>>>> Thx
> >>>>>>> --
> >>>>>>> Mathieu
> >>>>>>> _______________________________________________
> >>>>>>> Powered by www.kitware.com
> >>>>>>>
> >>>>>>> Visit other Kitware open-source projects at
> >>>>>>> http://www.kitware.com/opensource/opensource.html
> >>>>>>>
> >>>>>>> Please keep messages on-topic and check the VTK FAQ at:
> >>>>>>> http://www.vtk.org/Wiki/VTK_FAQ
> >>>>>>>
> >>>>>>> Follow this link to subscribe/unsubscribe:
> >>>>>>> http://www.vtk.org/mailman/listinfo/vtkusers
> >>>>>>
> >>>>>> --
> >>>>>> François Bertel, PhD  | Kitware Inc. Suite 204
> >>>>>> 1 (518) 371 3971 x113 | 28 Corporate Drive
> >>>>>>                      | Clifton Park NY 12065, USA
> >>>>>
> >>>>> --
> >>>>> François Bertel, PhD  | Kitware Inc. Suite 204
> >>>>> 1 (518) 371 3971 x113 | 28 Corporate Drive
> >>>>>                      | Clifton Park NY 12065, USA
> >>>>> _______________________________________________
> >>>>> Powered by www.kitware.com
> >>>>>
> >>>>> Visit other Kitware open-source projects at
> >>>>> http://www.kitware.com/opensource/opensource.html
> >>>>>
> >>>>> Please keep messages on-topic and check the VTK FAQ at:
> >>>>> http://www.vtk.org/Wiki/VTK_FAQ
> >>>>>
> >>>>> Follow this link to subscribe/unsubscribe:
> >>>>> http://www.vtk.org/mailman/listinfo/vtkusers
> >>>
> >>> --
> >>> François Bertel, PhD  | Kitware Inc. Suite 204
> >>> 1 (518) 371 3971 x113 | 28 Corporate Drive
> >>>                      | Clifton Park NY 12065, USA
> >>
> >> --
> >> François Bertel, PhD  | Kitware Inc. Suite 204
> >> 1 (518) 371 3971 x113 | 28 Corporate Drive
> >>                      | Clifton Park NY 12065, USA
> >> _______________________________________________
> >> Powered by www.kitware.com
> >>
> >> Visit other Kitware open-source projects at
> >> http://www.kitware.com/opensource/opensource.html
> >>
> >> Please keep messages on-topic and check the VTK FAQ at:
> >> http://www.vtk.org/Wiki/VTK_FAQ
> >>
> >> Follow this link to subscribe/unsubscribe:
> >> http://www.vtk.org/mailman/listinfo/vtkusers
> >
> > --
> > Mathieu
> 

-- 
Marcus D. Hanwell, Ph.D.
R&D Engineer, Kitware Inc.
(518) 881-4937



More information about the vtkusers mailing list