[Python-bugs-list] [ python-Bugs-616002 ] getdefaultlocale failure on OS X

noreply@sourceforge.net noreply@sourceforge.net
Fri, 04 Oct 2002 00:28:23 -0700


Bugs item #616002, was opened at 2002-09-29 02:20
You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=616002&group_id=5470

Category: IDLE
Group: Python 2.3
>Status: Closed
>Resolution: Wont Fix
Priority: 5
Submitted By: Brian Lenihan (brianl)
Assigned to: Nobody/Anonymous (nobody)
Summary: getdefaultlocale failure on OS X

Initial Comment:
My OS X laptop did not have LANG  set in the bash
envrionment and idle (AquaTK) failed to launch with an
AttributeError in IOBinding.py

        try:
            encoding = locale.getdefaultlocale()[1]
            codecs.lookup(encoding)
        except (ValueError, LookupError):
            pass

encoding = encoding.lower()
^^^^^

encoding is None here because locale.getdefaultlocale()
returns (None, None) when the locale can't be determined.

After finding that adding LANG=en_US to my .bashrc
didn't help, I Iearned OS X GUI apps don't use the
shell's environment, but do use whatever they find in
~/.MacOSX/environment.plist.  Adding a couple lines:

        <key>LANG</key>
        <string>en_US</string>

makes idle happy.   An alternative would be to make
sure encoding never gets set to None.

--- IOBinding.py.orig   Sat Sep 28 17:17:02 2002
+++ IOBinding.py        Sat Sep 28 17:17:42 2002
@@ -64,7 +64,7 @@
         # which may give a clue. Unfortunately,
getdefaultlocale has
         # bugs that can cause ValueError.
         try:
-            encoding = locale.getdefaultlocale()[1]
+            encoding = locale.getdefaultlocale()[1] or
'ascii'
             codecs.lookup(encoding)
         except (ValueError, LookupError):
             pass
 




 
 



----------------------------------------------------------------------

>Comment By: Martin v. Löwis (loewis)
Date: 2002-10-04 09:28

Message:
Logged In: YES 
user_id=21627

You can probably guess that I dislike the change being done
to IDLE. Oh well.

----------------------------------------------------------------------

Comment By: Brian Lenihan (brianl)
Date: 2002-10-04 08:36

Message:
Logged In: YES 
user_id=17671

This may as well be closed.  I see in the latest Python-dev
summary that Guido asked for all future changes to be done
in the idle-fork project.

Tony Lownds checked in a work-around to the idle-fork project
on 09/23 which is similar to what I was suggesting.

----------------------------------------------------------------------

Comment By: Martin v. Löwis (loewis)
Date: 2002-09-30 10:46

Message:
Logged In: YES 
user_id=21627

Feel free to use any work-around you feel comfortable with.
However, I won't approve a fix that I know is wrong.

----------------------------------------------------------------------

Comment By: Brian Lenihan (brianl)
Date: 2002-09-30 10:35

Message:
Logged In: YES 
user_id=17671

I was surprised falling back to ascii actually worked.  I
found a bunch of docs at the devoper.apple.com site.  As far
as I can tell, the proper way to fix this is to add the
necessary support to macglue.h, etc. so the functions in
_locale work on OS X as well.

I found out why this only just now bit me: I had compatible
libs in /usr/local/lib which made nl_langinfo() available in
_locale until I upgraded to 10.2.1.

The proper fix is going to require some effort by someone
who understands Apple API's much better than I do.

Since falling back to ascii works (at least for en_US) as
you intended - you were the one who added the locale code to
IOBinding.py - perhaps a temporary fix is to make sure the
fallback works as you intended?

----------------------------------------------------------------------

Comment By: Martin v. Löwis (loewis)
Date: 2002-09-30 09:38

Message:
Logged In: YES 
user_id=21627

I agree that there is a bug somewhere. I also suspect that
the bug is in IDLE, but I think it is wrong to fall-back to
ASCII on OS X (I'm almost certain that the user's encoding
is never just ASCII on that system). I suspect that it is
MacRoman or some such, but I also suspect that this either
might vary with the localized version of OS X (assuming
there are localized versions), or with a per-user setting.

----------------------------------------------------------------------

Comment By: Brian Lenihan (brianl)
Date: 2002-09-30 04:18

Message:
Logged In: YES 
user_id=17671

I've done a little digging and have yet to find a
satisfactory answer.

This article: 
http://developer.apple.com/internet/macosx/perl.html has
instructions for upgrading perl to 5.8.0

"OS X has one of the necessary environment variables set
(LANG), but without the above, you'll get annoying error
messages each time you try to run a script. The above
assumes the tcsh shell; if you prefer bash, use echo "export
LC_ALL=C" >> ~/.bash_profile instead."

I'll keep digging for a good answer, but I still believe
there is a bug here.  If you look in IOBinding.py, you'll
see that encoding is given a default value of "ascii".   It
should leave the section with that value intact or with
ascii replaced with the proper locale.

The possibility that getdefaultlocale will punt and return
(None, None) is not taken into account in IOBinding.py

----------------------------------------------------------------------

Comment By: Martin v. Löwis (loewis)
Date: 2002-09-30 00:17

Message:
Logged In: YES 
user_id=21627

Can you please find out how to determine the user's
prefererred encoding on OS X?

Yes, getdefaultlocale is broken, but that is by design, and
cannot be fixed. So we should reduce its usage instead of
trying to correct it.

----------------------------------------------------------------------

You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=616002&group_id=5470