[Python-checkins] python/dist/src/Lib locale.py,1.21,1.22

loewis@users.sourceforge.net loewis@users.sourceforge.net
Sun, 03 Nov 2002 09:20:41 -0800


Update of /cvsroot/python/python/dist/src/Lib
In directory usw-pr-cvs1:/tmp/cvs-serv11450/Lib

Modified Files:
	locale.py 
Log Message:
Add getpreferredencoding. Support @euro modifiers. Fixes #554676.
The @euro part is backported to 2.2.3.


Index: locale.py
===================================================================
RCS file: /cvsroot/python/python/dist/src/Lib/locale.py,v
retrieving revision 1.21
retrieving revision 1.22
diff -C2 -d -r1.21 -r1.22
*** locale.py	19 Oct 2002 20:19:10 -0000	1.21
--- locale.py	3 Nov 2002 17:20:08 -0000	1.22
***************
*** 265,268 ****
--- 265,277 ----
      """
      code = normalize(localename)
+     if '@' in localename:
+         # Deal with locale modifiers
+         code, modifier = code.split('@')
+         if modifier == 'euro' and '.' not in code:
+             # Assume Latin-9 for @euro locales. This is bogus,
+             # since some systems may use other encodings for these
+             # locales. Also, we ignore other modifiers.
+             return code, 'iso-8859-15'
+             
      if '.' in code:
          return code.split('.')[:2]
***************
*** 381,384 ****
--- 390,425 ----
      """
      _setlocale(category, _build_localename(getdefaultlocale()))
+ 
+ if sys.platform in ('win32', 'darwin', 'mac'):
+     # On Win32, this will return the ANSI code page
+     # On the Mac, it should return the system encoding;
+     # it might return "ascii" instead
+     def getpreferredencoding(do_setlocale = True):
+         """Return the charset that the user is likely using."""
+         import _locale
+         return _locale.getdefaultlocale()[1]
+ else:
+     # On Unix, if CODESET is available, use that.
+     try:
+         CODESET
+     except NameError:
+         # Fall back to parsing environment variables :-(
+         def getpreferredencoding(do_setlocale = True):
+             """Return the charset that the user is likely using,
+             by looking at environment variables."""
+             return getdefaultlocale()[1]
+     else:
+         def getpreferredencoding(do_setlocale = True):
+             """Return the charset that the user is likely using,
+             according to the system configuration."""
+             if do_setlocale:
+                 oldloc = setlocale(LC_CTYPE)
+                 setlocale(LC_CTYPE, "")
+                 result = nl_langinfo(CODESET)
+                 setlocale(LC_CTYPE, oldloc)
+                 return result
+             else:
+                 return nl_langinfo(CODESET)
+                 
  
  ### Database