[tor-commits] [stem/master] Python3 regression in socket's recv_message()

atagar at torproject.org atagar at torproject.org
Tue Feb 2 15:53:20 UTC 2016


commit 05bd17003bf86362b03cb487f4277f312b428fc2
Author: Damian Johnson <atagar at torproject.org>
Date:   Tue Feb 2 07:52:20 2016 -0800

    Python3 regression in socket's recv_message()
    
    Oops, shame on me for not testing with python3. Recent log truncation change
    introduced a regression...
    
      ======================================================================
      ERROR: test_hs_desc_content_event
      ----------------------------------------------------------------------
      Traceback (most recent call last):
        File "/home/atagar/Desktop/stem/test/unit/response/events.py", line 877, in test_hs_desc_content_event
          event = _get_event(HS_DESC_CONTENT_EVENT)
        File "/home/atagar/Desktop/stem/test/unit/response/events.py", line 458, in _get_event
          controller_event = mocking.get_message(content)
        File "/home/atagar/Desktop/stem/test/mocking.py", line 259, in get_message
          return stem.response.ControlMessage.from_str(content)
        File "/home/atagar/Desktop/stem/stem/response/__init__.py", line 160, in from_str
          msg = stem.socket.recv_message(StringIO(content))
        File "/home/atagar/Desktop/stem/stem/socket.py", line 601, in recv_message
          print([b'... %i more lines...' % (len(log_message_lines) - TRUNCATE_LOGS)])
      TypeError: unsupported operand type(s) for %: 'bytes' and 'int'
    
      ----------------------------------------------------------------------
    
    Thanks to toralf for pointing this out. Trouble is that under python3 bytes
    doesn't support % formatting at all...
    
      >>> b'hi %i' % 5
      Traceback (most recent call last):
        File "<stdin>", line 1, in <module>
      TypeError: unsupported operand type(s) for %: 'bytes' and 'int'
    
    This is pretty pesky, and something they plan to change...
    
      http://legacy.python.org/dev/peps/pep-0461/
---
 stem/socket.py | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)

diff --git a/stem/socket.py b/stem/socket.py
index de57c7d..4537ea6 100644
--- a/stem/socket.py
+++ b/stem/socket.py
@@ -592,15 +592,15 @@ def recv_message(control_file):
       parsed_content.append((status_code, divider, content))
 
       raw_content_str = b''.join(raw_content)
-      log_message = raw_content_str.replace(b'\r\n', b'\n').rstrip()
+      log_message = stem.util.str_tools._to_unicode(raw_content_str.replace(b'\r\n', b'\n').rstrip())
 
       if TRUNCATE_LOGS:
-        log_message_lines = log_message.split(b'\n')
+        log_message_lines = log_message.split('\n')
 
         if len(log_message_lines) > TRUNCATE_LOGS:
-          log_message = b'\n'.join(log_message_lines[:TRUNCATE_LOGS] + [b'... %i more lines...' % (len(log_message_lines) - TRUNCATE_LOGS)])
+          log_message = '\n'.join(log_message_lines[:TRUNCATE_LOGS] + ['... %i more lines...' % (len(log_message_lines) - TRUNCATE_LOGS)])
 
-      log.trace('Received from tor:\n' + stem.util.str_tools._to_unicode(log_message))
+      log.trace('Received from tor:\n' + log_message)
 
       return stem.response.ControlMessage(parsed_content, raw_content_str)
     elif divider == '+':



More information about the tor-commits mailing list