[py-dev] svn diff for producer.py and consumer.py

Grig Gheorghiu grig at gheorghiu.net
Mon Jul 18 22:54:36 CEST 2005


Holger,

I'm including 2 diffs for consumer.py and producer.py. I added some
initial unicode support, with a default encoding of 'utf-8'. 

The only other modification is that the prefix() method of a Message
only uses square brackets when there are actual keywords to be printed.
If you instantiate a log object via log = py.log.Producer(""), then
log('blah') will now produce 'blah', not '[] blah'.

I didn't want to commit these changes, since you may have other ideas
on the unicode stuff.

Grig

# svn diff producer.py
Index: producer.py
===================================================================
--- producer.py (revision 14743)
+++ producer.py (working copy)
@@ -16,14 +16,20 @@
         self.args = args

     def content(self):
-        return " ".join(map(str, self.args))
+        return " ".join(map(unicode, self.args))

-    def prefix(self):
-        return "[%s] " % (":".join(self.keywords))
+    def prefix(self):
+        if not self.keywords:
+            return u""
+        return "[%s] " % (u":".join(self.keywords))

     def __str__(self):
-        return self.prefix() + self.content()
+        s = self.prefix() + self.content()
+        return s.encode('utf-8')

+    def __unicode__(self):
+        return self.prefix() + self.content()
+
 class Producer(object):
     """ Log producer API which sends messages to be logged
         to a 'consumer' object, which then prints them to stdout,
@@ -71,6 +77,6 @@
     Producer.keywords2consumer.update(state)

 def default_consumer(msg):
-    print str(msg)
+    print unicode(msg).encode('utf-8')

 Producer.keywords2consumer['default'] = default_consumer

# svn diff consumer.py
Index: consumer.py
===================================================================
--- consumer.py (revision 14743)
+++ consumer.py (working copy)
@@ -1,6 +1,8 @@
 import py
 import sys

+DEFAULT_ENCODING = 'utf-8'
+
 class File(object):
     def __init__(self, f):
         assert hasattr(f, 'write')
@@ -8,7 +10,7 @@
         self._file = f

     def __call__(self, msg):
-        print >>self._file, str(msg)
+        print >>self._file, unicode(msg).encode(DEFAULT_ENCODING)

 class Path(File):
     def __init__(self, filename, append=False):
@@ -17,10 +19,10 @@
         super(Path, self).__init__(f)

 def STDOUT(msg):
-    print >>sys.stdout, str(msg)
+    print >>sys.stdout, unicode(msg).encode(DEFAULT_ENCODING)

 def STDERR(msg):
-    print >>sys.stderr, str(msg)
+    print >>sys.stderr, unicode(msg).encode(DEFAULT_ENCODING)

 def setconsumer(keywords, consumer):
     # normalize to tuples



More information about the py-dev mailing list