[tor-commits] [stem/master] Using binary mode for the controller socket file

atagar at torproject.org atagar at torproject.org
Sat Feb 2 23:53:28 UTC 2013


commit 9cd4c9feb479fe00987526c272a1b2a1853bd295
Author: Damian Johnson <atagar at torproject.org>
Date:   Sat Feb 2 12:23:07 2013 -0800

    Using binary mode for the controller socket file
    
    Yay! Now that I have a version of python 3 that doesn't segfault I can finish
    making our integ tests work.
    
    The socket file used for controller connections should be normalized to use
    binary mode. This is its behavior in python 2.x, and in 3.x having it in text
    mode can cause sadness.
    
    Exception in thread Tor Listener:
    Traceback (most recent call last):
      File "/home/atagar/Desktop/Python-3.3.0/Lib/threading.py", line 639, in _bootstrap_inner
        self.run()
      File "/home/atagar/Desktop/Python-3.3.0/Lib/threading.py", line 596, in run
        self._target(*self._args, **self._kwargs)
      File "/home/atagar/Desktop/stem/test/data/python3/stem/control.py", line 573, in _reader_loop
        control_message = self._socket.recv()
      File "/home/atagar/Desktop/stem/test/data/python3/stem/socket.py", line 115, in recv
        return recv_message(socket_file)
      File "/home/atagar/Desktop/stem/test/data/python3/stem/socket.py", line 539, in recv_message
        line = control_file.readline()
      File "/home/atagar/Desktop/Python-3.3.0/Lib/codecs.py", line 300, in decode
        (result, consumed) = self._buffer_decode(data, self.errors, final)
    UnicodeDecodeError: 'utf-8' codec can't decode byte 0xc3 in position 2005: invalid continuation byte
    
    After addressing this and a few encoding issues the controller integ tests now
    pass, but after we're done testing python spews out a dump following...
    
    *** glibc detected *** python3: munmap_chunk(): invalid pointer: 0x097f1620 ***
    
    At this point I'm pretty well persuaded that the python 3.x series leaves
    something to be desired in terms of stability.
---
 stem/socket.py                   |   16 +++++++++-------
 test/integ/control/controller.py |    2 +-
 2 files changed, 10 insertions(+), 8 deletions(-)

diff --git a/stem/socket.py b/stem/socket.py
index f0786ab..4277fa1 100644
--- a/stem/socket.py
+++ b/stem/socket.py
@@ -37,6 +37,7 @@ import threading
 
 import stem.prereq
 import stem.response
+import stem.util.str_tools
 
 from stem.util import log
 
@@ -182,12 +183,7 @@ class ControlSocket(object):
 
       with self._recv_lock:
         self._socket = self._make_socket()
-
-        if stem.prereq.is_python_3():
-          self._socket_file = self._socket.makefile(mode = "rw", newline = "")
-        else:
-          self._socket_file = self._socket.makefile(mode = "rw")
-
+        self._socket_file = self._socket.makefile(mode = "rwb")
         self._is_alive = True
 
         # It's possible for this to have a transient failure...
@@ -425,7 +421,7 @@ def send_message(control_file, message, raw = False):
     message = send_formatting(message)
 
   try:
-    control_file.write(message)
+    control_file.write(stem.util.str_tools.to_bytes(message))
     control_file.flush()
 
     log_message = message.replace("\r\n", "\n").rstrip()
@@ -471,6 +467,9 @@ def recv_message(control_file):
   while True:
     try:
       line = control_file.readline()
+
+      if stem.prereq.is_python_3():
+        line = stem.util.str_tools.to_unicode(line)
     except AttributeError:
       # if the control_file has been closed then we will receive:
       # AttributeError: 'NoneType' object has no attribute 'recv'
@@ -537,6 +536,9 @@ def recv_message(control_file):
       while True:
         try:
           line = control_file.readline()
+
+          if stem.prereq.is_python_3():
+            line = stem.util.str_tools.to_unicode(line)
         except socket.error, exc:
           prefix = logging_prefix % "SocketClosed"
           log.info(prefix + "received an exception while mid-way through a data reply (exception: \"%s\", read content: \"%s\")" % (exc, log.escape(raw_content)))
diff --git a/test/integ/control/controller.py b/test/integ/control/controller.py
index 600403b..15168a4 100644
--- a/test/integ/control/controller.py
+++ b/test/integ/control/controller.py
@@ -568,7 +568,7 @@ class TestController(unittest.TestCase):
         controller.set_conf("ContactInfo", "confsaved")
         controller.save_conf()
 
-        with file(runner.get_torrc_path()) as torrcfile:
+        with open(runner.get_torrc_path()) as torrcfile:
           self.assertTrue("\nContactInfo confsaved\n" in torrcfile.read())
       finally:
         controller.load_conf(oldconf)



More information about the tor-commits mailing list