[or-cvs] It looks like socket.makefile is broken under Python 2.2. ...

Nick Mathewson nickm at seul.org
Wed Jul 13 06:12:14 UTC 2005


Update of /home/or/cvsroot/control/python
In directory moria:/tmp/cvs-serv29165

Modified Files:
	TorCtl1.py 
Log Message:
It looks like socket.makefile is broken under Python 2.2.  So emulate the one part we need (readline), and leave the rest alone.

Index: TorCtl1.py
===================================================================
RCS file: /home/or/cvsroot/control/python/TorCtl1.py,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -d -r1.3 -r1.4
--- TorCtl1.py	11 Jul 2005 19:16:40 -0000	1.3
+++ TorCtl1.py	13 Jul 2005 06:12:12 -0000	1.4
@@ -44,6 +44,40 @@
     else:
         return "\r\n".join(lines)
 
+class _BufSock:
+    def __init__(self, s):
+        self._s = s
+        self._buf = []
+
+    def readline(self):
+        if self._buf:
+            idx = self._buf[0].find('\n')
+            if idx >= 0:
+                result = self._buf[0][:idx+1]
+                self._buf[0] = self._buf[0][idx+1:]
+                return result
+
+        while 1:
+            s = self._s.recv(128)
+            idx = s.find('\n')
+            if idx >= 0:
+                self._buf.append(s[:idx+1])
+                result = "".join(self._buf)
+                rest = s[idx+1:]
+                if rest:
+                    self._buf = [ rest ]
+                else:
+                    del self._buf[:]
+                return result
+            else:
+                self._buf.append(s)
+
+    def write(self, s):
+        self._s.send(s)
+
+    def close(self):
+        self._s.close()
+
 def _read_reply(f,debugFile=None):
     lines = []
     while 1:
@@ -75,15 +109,11 @@
 
 class Connection:
     """A Connection represents a connection to the Tor process."""
-    def __init__(self, sock, file=None):
+    def __init__(self, sock):
         """Create a Connection to communicate with the Tor process over the
            socket 'sock'.
         """
-        if file:
-            self._s = file
-        else:
-            assert sock
-            self._s = sock.makefile("r+b");
+        self._s = _BufSock(sock)
         self._debugFile = None
         self._handler = None
         self._sendLock = threading.RLock()
@@ -169,7 +199,6 @@
             if self._debugFile:
                 self._debugFile.write(">>> %s" % msg)
             self._s.write(msg)
-            self._s.flush()
         finally:
             self._sendLock.release()
 



More information about the tor-commits mailing list