[Zope3-checkins] SVN: Zope3/branches/3.2/src/zope/server/ Changed channels to accept non-string iterables.

Jim Fulton jim at zope.com
Tue Dec 20 13:24:47 EST 2005


Log message for revision 40915:
  Changed channels to accept non-string iterables.
  

Changed:
  U   Zope3/branches/3.2/src/zope/server/dualmodechannel.py
  U   Zope3/branches/3.2/src/zope/server/tests/test_serverbase.py

-=-
Modified: Zope3/branches/3.2/src/zope/server/dualmodechannel.py
===================================================================
--- Zope3/branches/3.2/src/zope/server/dualmodechannel.py	2005-12-20 18:24:44 UTC (rev 40914)
+++ Zope3/branches/3.2/src/zope/server/dualmodechannel.py	2005-12-20 18:24:47 UTC (rev 40915)
@@ -152,8 +152,14 @@
     #
 
     def write(self, data):
-        if data:
-            self.outbuf.append(data)
+        if isinstance(data, str):
+            if data:
+                self.outbuf.append(data)
+        else:
+            for v in data:
+                if v:
+                    self.outbuf.append(v)
+
         while len(self.outbuf) >= self.adj.send_bytes:
             # Send what we can without blocking.
             # We propagate errors to the application on purpose

Modified: Zope3/branches/3.2/src/zope/server/tests/test_serverbase.py
===================================================================
--- Zope3/branches/3.2/src/zope/server/tests/test_serverbase.py	2005-12-20 18:24:44 UTC (rev 40914)
+++ Zope3/branches/3.2/src/zope/server/tests/test_serverbase.py	2005-12-20 18:24:47 UTC (rev 40915)
@@ -39,7 +39,54 @@
 
     """
 
+class FakeSocket:
+    data        = ''
+    setblocking = lambda *_: None
+    fileno      = lambda *_: 42
+    getpeername = lambda *_: ('localhost', 42)
 
+    def send(self, data):
+        self.data += data
+        return len(data)
+    
+
+def channels_accept_iterables():
+    r"""
+Channels accept iterables (they special-case strings).
+
+    >>> from zope.server.dualmodechannel import DualModeChannel
+    >>> socket = FakeSocket()
+    >>> channel = DualModeChannel(socket, ('localhost', 42))
+
+    >> channel.write("First")
+    >> channel.flush()
+    >> print socket.data
+    First
+
+    >>> channel.write(["First", "\n", "Second", "\n", "Third"])
+    >>> channel.flush()
+    >>> print socket.data
+    First
+    Second
+    Third
+
+    >>> def count():
+    ...     yield '\n1\n2\n3\n'
+    ...     yield 'I love to count. Ha ha ha.'
+
+    >>> channel.write(count())
+    >>> channel.flush()
+    >>> print socket.data
+    First
+    Second
+    Third
+    1
+    2
+    3
+    I love to count. Ha ha ha.
+    
+"""
+
 def test_suite():
     return doctest.DocTestSuite()
 



More information about the Zope3-Checkins mailing list