[Python-Dev] logging module broken because of locale

Mihai Ibanescu mihaiibanescu at yahoo.com
Thu Jul 20 15:49:05 CEST 2006


On Thu, Jul 20, 2006 at 05:09:38AM -0500, skip at pobox.com wrote:
> 
>     Misa> Good point. Does the attached patch look reasonable?
> 
>     ...
>     Misa> -        self.when = string.upper(when)
>     Misa> +        self.when = unicode(when).upper()
>     ...
> 
> The use of the string module instead of string methods suggests to me that
> the logging package attempts to work with older versions of Python.  Looking
> at PEP 291 it looks like 1.5.2 compatibility is desired (no string methods,
> no unicode).  I think a conscious decision by someone (probably Vinay Sajip)
> to give up that compatibility would be required.

Agreed. There is a note that the module should work with python >= 1.5.2 at
the top of the module.

It's up to Vinay to decide if we want to drop support for 1.5.2 in the module
included in newer pythons, or the attached patch would make it work for 1.5.2
as well (as in "it's not more broken than before"). 

I would like to redo the patch once more to get rid of the try-except and use
__builtins__ instead (but for some reason it kept jumping from being a module
to being a dictionary and I just wanted the proof of concept).

Misa
-------------- next part --------------
--- Python-2.4.3/Lib/logging/handlers.py.nolocale	2006-07-19 12:15:46.000000000 -0400
+++ Python-2.4.3/Lib/logging/handlers.py	2006-07-20 09:45:57.000000000 -0400
@@ -162,7 +162,7 @@
     """
     def __init__(self, filename, when='h', interval=1, backupCount=0, encoding=None):
         BaseRotatingHandler.__init__(self, filename, 'a', encoding)
-        self.when = string.upper(when)
+        self.when = _upper(when)
         self.backupCount = backupCount
         # Calculate the real rollover interval, which is just the number of
         # seconds between rollovers.  Also set the filename suffix used when
@@ -645,7 +645,7 @@
         """
         msg = self.log_format_string % (
             self.encodePriority(self.facility,
-                                string.lower(record.levelname)),
+                                _lower(record.levelname)),
             msg)
         try:
             if self.unixsocket:
@@ -854,7 +854,7 @@
         ("GET" or "POST")
         """
         logging.Handler.__init__(self)
-        method = string.upper(method)
+        method = _upper(method)
         if method not in ["GET", "POST"]:
             raise ValueError, "method must be GET or POST"
         self.host = host
@@ -1007,3 +1007,25 @@
         self.flush()
         self.target = None
         BufferingHandler.close(self)
+
+def _upper(s):
+    """A version of upper() that tries to be locale-independent by converting
+    the string to unicode (which is not subject to case conversion being
+    locale specific)
+    """
+    try:
+        ret = str(unicode(s).upper())
+    except NameError:
+        ret = string.upper(s)
+    return ret
+
+def _lower(s):
+    """A version of lower() that tries to be locale-independent by converting
+    the string to unicode (which is not subject to case conversion being
+    locale specific)
+    """
+    try:
+        ret = str(unicode(s).lower())
+    except NameError:
+        ret = string.lower(s)
+    return ret


More information about the Python-Dev mailing list