[Python-3000-checkins] r56290 - in python/branches/py3k-struni/Lib: asynchat.py test/test_asynchat.py

guido.van.rossum python-3000-checkins at python.org
Thu Jul 12 09:58:54 CEST 2007


Author: guido.van.rossum
Date: Thu Jul 12 09:58:54 2007
New Revision: 56290

Modified:
   python/branches/py3k-struni/Lib/asynchat.py
   python/branches/py3k-struni/Lib/test/test_asynchat.py
Log:
Fix asynchat to use bytes instead of strings.
Fix by Alexandre Vassalotti, SF# 1752173.


Modified: python/branches/py3k-struni/Lib/asynchat.py
==============================================================================
--- python/branches/py3k-struni/Lib/asynchat.py	(original)
+++ python/branches/py3k-struni/Lib/asynchat.py	Thu Jul 12 09:58:54 2007
@@ -60,8 +60,8 @@
     ac_out_buffer_size      = 4096
 
     def __init__ (self, conn=None):
-        self.ac_in_buffer = ''
-        self.ac_out_buffer = ''
+        self.ac_in_buffer = b''
+        self.ac_out_buffer = b''
         self.producer_fifo = fifo()
         asyncore.dispatcher.__init__ (self, conn)
 
@@ -91,7 +91,7 @@
             self.handle_error()
             return
 
-        self.ac_in_buffer = self.ac_in_buffer + data
+        self.ac_in_buffer = self.ac_in_buffer + bytes(data)
 
         # Continue to search for self.terminator in self.ac_in_buffer,
         # while calling self.collect_incoming_data.  The while loop
@@ -110,7 +110,7 @@
                 n = terminator
                 if lb < n:
                     self.collect_incoming_data (self.ac_in_buffer)
-                    self.ac_in_buffer = ''
+                    self.ac_in_buffer = b''
                     self.terminator = self.terminator - lb
                 else:
                     self.collect_incoming_data (self.ac_in_buffer[:n])
@@ -147,7 +147,7 @@
                     else:
                         # no prefix, collect it all
                         self.collect_incoming_data (self.ac_in_buffer)
-                        self.ac_in_buffer = ''
+                        self.ac_in_buffer = b''
 
     def handle_write (self):
         self.initiate_send ()
@@ -172,7 +172,7 @@
         # return len(self.ac_out_buffer) or len(self.producer_fifo) or (not self.connected)
         # this is about twice as fast, though not as clear.
         return not (
-                (self.ac_out_buffer == '') and
+                (self.ac_out_buffer == b'') and
                 self.producer_fifo.is_empty() and
                 self.connected
                 )
@@ -194,13 +194,13 @@
                         self.producer_fifo.pop()
                         self.close()
                     return
-                elif isinstance(p, str):
+                elif isinstance(p, str) or isinstance(p, bytes):
                     self.producer_fifo.pop()
-                    self.ac_out_buffer = self.ac_out_buffer + p
+                    self.ac_out_buffer = self.ac_out_buffer + bytes(p)
                     return
                 data = p.more()
                 if data:
-                    self.ac_out_buffer = self.ac_out_buffer + data
+                    self.ac_out_buffer = self.ac_out_buffer + bytes(data)
                     return
                 else:
                     self.producer_fifo.pop()
@@ -226,8 +226,8 @@
 
     def discard_buffers (self):
         # Emergencies only!
-        self.ac_in_buffer = ''
-        self.ac_out_buffer = ''
+        self.ac_in_buffer = b''
+        self.ac_out_buffer = b''
         while self.producer_fifo:
             self.producer_fifo.pop()
 
@@ -245,7 +245,7 @@
             return result
         else:
             result = self.data
-            self.data = ''
+            self.data = b''
             return result
 
 class fifo:

Modified: python/branches/py3k-struni/Lib/test/test_asynchat.py
==============================================================================
--- python/branches/py3k-struni/Lib/test/test_asynchat.py	(original)
+++ python/branches/py3k-struni/Lib/test/test_asynchat.py	Thu Jul 12 09:58:54 2007
@@ -17,8 +17,8 @@
         PORT = test_support.bind_port(sock, HOST, PORT)
         sock.listen(1)
         conn, client = sock.accept()
-        buffer = ""
-        while "\n" not in buffer:
+        buffer = b""
+        while b"\n" not in buffer:
             data = conn.recv(1)
             if not data:
                 break
@@ -37,7 +37,7 @@
         self.create_socket(socket.AF_INET, socket.SOCK_STREAM)
         self.connect((HOST, PORT))
         self.set_terminator(terminator)
-        self.buffer = ""
+        self.buffer = b""
 
     def handle_connect(self):
         pass
@@ -49,7 +49,7 @@
     def found_terminator(self):
         #print "Received:", repr(self.buffer)
         self.contents = self.buffer
-        self.buffer = ""
+        self.buffer = b""
         self.close()
 
 
@@ -70,7 +70,7 @@
         asyncore.loop()
         s.join()
 
-        self.assertEqual(c.contents, 'hello world')
+        self.assertEqual(c.contents, b'hello world')
 
     def test_numeric_terminator(self):
         # Try reading a fixed number of bytes
@@ -83,7 +83,7 @@
         asyncore.loop()
         s.join()
 
-        self.assertEqual(c.contents, 'hello ')
+        self.assertEqual(c.contents, b'hello ')
 
 
 def test_main(verbose=None):


More information about the Python-3000-checkins mailing list