[wxPython-dev] Strange problem with python's built-in functions

(Replying to this list because my problem became 'user-level')

wxGTK changes the locale to match the system's locale, and wxPython's
startup code also changes the local so Python will match the C lib. So
it is probably being changed to a locale where ',' is the decimal
separator instead of '.'.

Yes, this seems to be the root of my problem because I'm using lt_LT.UTF-8
locale on this machine. That meens I need to modify my program not to use
it, but use some another language instead to get the 'normal' eval function.

I'm trying the following code:

   class MyFrame(wx.Frame):
      def __init__(self, parent, title):
         wx.Frame.__init__(self, parent, -1, title)

         l = wxLocale()
         l.Init(wxLANGUAGE_ENGLISH)
         p = wx.Panel(self, -1)
         b = wx.Button(p, -1, "Do It", (10,10))

         print "Bad:", eval("-5.323")
         print float("3.324")

But this doesn't help and 'flaot' requires '3,324' instead of '3.324'. Is it
possible to override system language inside my application? I need to use
float() and eval() with "." separator with the application on any system not
depending on it's locale. Thanks.

Martynas

···

On Sun, May 16, 2004 at 11:28:41AM -0700, Robin Dunn wrote:
--
The best way of learning is learning by doing.
http://mjoc.sig.lt

> wxGTK changes the locale to match the system's locale, and

wxPython's

> startup code also changes the local so Python will match the C lib.

So

> it is probably being changed to a locale where ',' is the decimal
> separator instead of '.'.

Yes, this seems to be the root of my problem because I'm
using lt_LT.UTF-8
locale on this machine. That meens I need to modify my
program not to use
it, but use some another language instead to get the 'normal'
eval function.

I'm trying the following code:

   class MyFrame(wx.Frame):
      def __init__(self, parent, title):
         wx.Frame.__init__(self, parent, -1, title)

         l = wxLocale()
         l.Init(wxLANGUAGE_ENGLISH)
         p = wx.Panel(self, -1)
         b = wx.Button(p, -1, "Do It", (10,10))

         print "Bad:", eval("-5.323")
         print float("3.324")

But this doesn't help and 'flaot' requires '3,324' instead of
'3.324'. Is it
possible to override system language inside my application? I
need to use
float() and eval() with "." separator with the application on
any system not
depending on it's locale. Thanks.

Have you tried using locale.setlocale?

Another idea: I noticed that wx.py (a few months ago, anyway)
for GTK but not for Windows calls

locale.setlocale( locale.LC_ALL, '' )

Thus, in GTK, a wx program automatically imports the OS
locale; however, under Windows, you need to include that line in
your program to import the OS locale.
Perhaps you could try commenting that line out in wx.py and see
what happens.

Also, I would use Python's locale-related functions whenever
possible instead of wxPython's.

···

On Sun, May 16, 2004 at 11:28:41AM -0700, Robin Dunn wrote:

Martynas Jocius wrote:

(Replying to this list because my problem became 'user-level')

wxGTK changes the locale to match the system's locale, and wxPython's startup code also changes the local so Python will match the C lib. So it is probably being changed to a locale where ',' is the decimal separator instead of '.'.

Yes, this seems to be the root of my problem because I'm using lt_LT.UTF-8
locale on this machine. That meens I need to modify my program not to use
it, but use some another language instead to get the 'normal' eval function.

I'm trying the following code:

   class MyFrame(wx.Frame):
      def __init__(self, parent, title):
         wx.Frame.__init__(self, parent, -1, title)

         l = wxLocale()
         l.Init(wxLANGUAGE_ENGLISH)
         p = wx.Panel(self, -1)
         b = wx.Button(p, -1, "Do It", (10,10))
          print "Bad:", eval("-5.323")
         print float("3.324")

But this doesn't help and 'flaot' requires '3,324' instead of '3.324'. Is it
possible to override system language inside my application? I need to use
float() and eval() with "." separator with the application on any system not
depending on it's locale. Thanks.

Use Python's locale.setlocale(). You could perhaps only change the numeric settings and get it to work as you expect:

  locale.setlocale(locale.LC_NUMERIC, "C")

Or you could reset the LANG* and/or LC* environment vars before running the app.

···

On Sun, May 16, 2004 at 11:28:41AM -0700, Robin Dunn wrote:

--
Robin Dunn
Software Craftsman
http://wxPython.org Java give you jitters? Relax with wxPython!