[I18n-sig] Locales module

Peter Funk pf@artcom-gmbh.de
Tue, 15 Feb 2000 02:18:35 +0100 (MET)


Hi!

I wrote:
[...]
> >about 'locale.py', which already comes included with Python 1.5.2
> >and works very well for me together with time.strftime.

Andy Robinson:
> Wow!  No, I did not know about this at all.  I tested it and it works
> fine on Windows.  I don't know about the POSIX API, but this is
> essentially a static database so I presume one could dump the contents
> into a data structure which the Mac etc. used if "from _locale import
> *" fails.  (I suspect it could do with some more convenience stuff
> layered on top to do number and string formatting, and a bit more
> documentation - but non-critical).

I don't, if this will work, since this stuff depends on some ANSI-C
library features.  I took a deeper look into the sources by
Martin von Loewis, who has contributed locale.py and _localemodule.c.  

There is already some #ifdef macintosh in 'Modules/_localemodule.c'.
So I really don't know, why Jack Jansens Python 1.5.2c1 binary
distribution for the mac doesn't contain the _locale module.  May be
it was simply forgotten during the build, since it is disabled in
Modules/Setup by default?

I wonder whether it would make sense, to fill 'locale.py' with dummy
stubs, that will be put in if an ImportError exception occurs due
to a missing _locale builtin module?  The following patch against a
recent CVS version will do that.  But I am very unsure, whether this
behaviour is desired.  Better i18n-applications shouldn't depend on
the availability of 'locale' and should contain their own fallback,
if importing locale fails.

Regards, Peter
-- 
Peter Funk, Oldenburger Str.86, 27777 Ganderkesee, Tel: 04222 9502 70, Fax: -60
*** ../../../Python-CVS_10_02_00-orig/dist/src/Lib/locale.py    Sat Feb  5 10:45:31 2000
--- Lib/locale.py       Tue Feb 15 01:55:35 2000
***************
*** 1,9 ****
  """Support for number formatting using the current locale settings."""
  
  # Author: Martin von Loewis
  
- from _locale import *
  import string
  
  #perform the grouping from right to left
  def _group(s):
--- 1,36 ----
  """Support for number formatting using the current locale settings."""
  
  # Author: Martin von Loewis
+ # Fallback stubs added by Peter Funk
  
  import string
+ try:
+     from _locale import *
+ except ImportError:
+     # this may happen on MacOS or on Unices where the locale support 
+     # in Modules/Setup wasn't uncommented during the build of python
+     # we add some dummy stubs here in order not to break any apps:
+     CHAR_MAX=127
+     LC_CTYPE, LC_NUMERIC, LC_TIME, LC_COLLATE, \
+     LC_MONETARY, LC_MESSAGES, LC_ALL = tuple(range(7))
+     def localeconv():
+         return {'grouping': [127], 'currency_symbol': '', 'n_sign_posn': 127, 
+                 'p_cs_precedes': 127, 'n_cs_precedes': 127, 
+                 'mon_grouping': [], 'n_sep_by_space': 127, 
+                 'decimal_point': '.', 'negative_sign': '', 
+                 'positive_sign': '', 'p_sep_by_space': 127, 
+                 'int_curr_symbol': '', 'p_sign_posn': 127, 
+                 'thousands_sep': '', 'mon_thousands_sep': '', 
+                 'frac_digits': 127, 'mon_decimal_point': '', 
+                 'int_frac_digits': 127}
+     def setlocale(category, arg):
+         if category == LC_ALL:
+             return \
+   "LC_CTYPE=C;LC_NUMERIC=C;LC_TIME=C;LC_COLLATE=C;LC_MONETARY=C;LC_MESSAGES=C"
+         else:
+             return 'C'
+     def strcoll(s1, s2): return cmp(s1, s2)
+     def strxfrm(s): return s
  
  #perform the grouping from right to left
  def _group(s):