Re: getting rid of some of the LOCALE magic

Tobias Oetiker <[email protected]>
Newsgroups gmane.comp.db.rrdtool.devel
Message-ID <[email protected]>
Hi Pawe,

oops ... bad one ... this exists in many more places ...

thanks!

cheers
tobi

Today Pawe? 'Reef' Polewicz wrote:

>
> Hi
>
> I think I have found a bug in rrd_graph.c related to the LOCALE magic
> mentioned in this thread. I have patched it and tested it and now it looks
> to be working as it should. Here is the patch:
>
> $ svn diff
> Index: rrd_graph.c
> ===================================================================
> --- rrd_graph.c (wersja 2126)
> +++ rrd_graph.c (kopia robocza)
> @@ -3991,7 +3991,8 @@
>      char *old_locale;
>      rrd_graph_init(&im);
>      /* a dummy surface so that we can measure text sizes for placements */
> -    old_locale = setlocale(LC_NUMERIC, "C");
> +    old_locale = setlocale(LC_NUMERIC, NULL);
> +    setlocale(LC_NUMERIC, "C");
>      rrd_graph_options(argc, argv, &im);
>      if (rrd_test_error()) {
>          rrd_info_free(im.grinfo);
>
>
>
> Why did I change this?
>
> setlocale() manual says here:
> http://www.cplusplus.com/reference/clibrary/clocale/setlocale/
>
> """
> Return Value:
> On success, A pointer to a C string identifying the locale currently set for
> the category. If category is LC_ALL and different parts of the locale are
> set to different values, the string returned gives this information in a
> format which may vary between compiler implementations.
>
> If the function failed to set a new locale, this is not modified and a null
> pointer is returned.
> """
>
> So it returns a pointer to a C string that represents the _new_ locale that
> was just set, not the old one.
>
>
>
>
> A test on a patched version:
>
> > /tmp/RRD.py(173)draw()
> -> rrdtool.graph( *args )
> (Pdb) import locale; locale.getlocale(locale.LC_TIME)
> (None, None)
> (Pdb) n
> --Return--
> > /tmp/RRD.py(173)draw()->None
> -> rrdtool.graph( *args )
> (Pdb) import locale; locale.getlocale(locale.LC_TIME)
> (None, None)
>
> Please note that rrdtool.graph() didn't unexpectedly set the locale.
> Now downgrade to debian stable (lenny):
> apt-get install librrd4=1.3.1-4 python-rrdtool=1.3.1-4
>
> > /tmp/RRD.py(173)draw()
> -> rrdtool.graph( *args )
> (Pdb) import locale; locale.getlocale(locale.LC_TIME)
> (None, None)
> (Pdb) n
> --Return--
> > /tmp/RRD.py(173)draw()->None
> -> rrdtool.graph( *args )
> (Pdb) import locale; locale.getlocale(locale.LC_TIME)
> ('pl_PL', 'UTF8')
> (Pdb)
>
> My code has used the python "time.strptime()" method that (indirectly,
> through "calendar" module) uses locale in order to parse dates. When the
> program gathered enough data, it wrote it to rrd, after which the date
> parser suddenly raised exception: ValueError: time data did not match
> format:  data=16/Aug/2010:00:03:20  fmt=%d/%b/%Y:%H:%M:%S.</search-bot-food>
>

-- 
Tobi Oetiker, OETIKER+PARTNER AG, Aarweg 15 CH-4600 Olten, Switzerland
http://it.oetiker.ch [email protected] ++41 62 775 9902 / sb: -9900
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.