
commit 4e458ee979569f2be2cf1cacd1f653b5bb8a9667 Author: Damian Johnson <atagar@torproject.org> Date: Tue Jun 3 08:10:22 2014 -0700 Fixing /info for python3 On irc zoltan reported that the /info command balks under python3... >>> /info moria1 Traceback (most recent call last): File "/usr/local/bin/tor-prompt", line 8, in <module> stem.interpreter.main() File "/usr/local/lib/python3.2/dist-packages/stem/interpreter/__init__.py", line 116, in main response = interpreter.run_command(user_input) File "/usr/local/lib/python3.2/dist-packages/stem/util/conf.py", line 276, in wrapped return func(*args, config = config, **kwargs) File "/usr/local/lib/python3.2/dist-packages/stem/interpreter/commands.py", line 317, in run_command output = self.do_info(arg) File "/usr/local/lib/python3.2/dist-packages/stem/interpreter/commands.py", line 184, in do_info for desc in server_desc_query: File "/usr/local/lib/python3.2/dist-packages/stem/descriptor/remote.py", line 328, in __iter__ for desc in self._run(True): File "/usr/local/lib/python3.2/dist-packages/stem/descriptor/remote.py", line 317, in _run for desc in results: File "/usr/local/lib/python3.2/dist-packages/stem/descriptor/__init__.py", line 211, in parse_file for desc in file_parser(descriptor_file): File "/usr/local/lib/python3.2/dist-packages/stem/descriptor/__init__.py", line 261, in _parse_metrics_file for desc in stem.descriptor.server_descriptor._parse_file(descriptor_file, is_bridge = False, validate = validate, **kwargs): File "/usr/local/lib/python3.2/dist-packages/stem/descriptor/server_descriptor.py", line 146, in _parse_file if descriptor_content[0].startswith('@type'): TypeError: startswith first arg must be bytes or a tuple of bytes, not str https://trac.torproject.org/projects/tor/ticket/12185 There's a couple issues here. First is a last minute change I added to strip @type annotations. It ran into the normal bytes/unicode python3 gotcha resulting in the stacktrace above. The other is that our contact field is bytes, so it needs to be converted to unicode before mingling with the other lines. --- stem/descriptor/extrainfo_descriptor.py | 2 +- stem/descriptor/microdescriptor.py | 2 +- stem/descriptor/networkstatus.py | 2 +- stem/descriptor/server_descriptor.py | 2 +- stem/interpreter/commands.py | 3 ++- 5 files changed, 6 insertions(+), 5 deletions(-) diff --git a/stem/descriptor/extrainfo_descriptor.py b/stem/descriptor/extrainfo_descriptor.py index 000b4ec..da81567 100644 --- a/stem/descriptor/extrainfo_descriptor.py +++ b/stem/descriptor/extrainfo_descriptor.py @@ -175,7 +175,7 @@ def _parse_file(descriptor_file, is_bridge = False, validate = True, **kwargs): extrainfo_content += _read_until_keywords(block_end_prefix, descriptor_file, True) if extrainfo_content: - if extrainfo_content[0].startswith('@type'): + if extrainfo_content[0].startswith(b'@type'): extrainfo_content = extrainfo_content[1:] if is_bridge: diff --git a/stem/descriptor/microdescriptor.py b/stem/descriptor/microdescriptor.py index 33ad4e0..3c92aca 100644 --- a/stem/descriptor/microdescriptor.py +++ b/stem/descriptor/microdescriptor.py @@ -138,7 +138,7 @@ def _parse_file(descriptor_file, validate = True, **kwargs): descriptor_lines.append(line) if descriptor_lines: - if descriptor_lines[0].startswith('@type'): + if descriptor_lines[0].startswith(b'@type'): descriptor_lines = descriptor_lines[1:] # strip newlines from annotations diff --git a/stem/descriptor/networkstatus.py b/stem/descriptor/networkstatus.py index fbe96fb..6cf0880 100644 --- a/stem/descriptor/networkstatus.py +++ b/stem/descriptor/networkstatus.py @@ -198,7 +198,7 @@ def _parse_file(document_file, document_type = None, validate = True, is_microde header = _read_until_keywords((ROUTERS_START, FOOTER_START, V2_FOOTER_START), document_file) - if header and header[0].startswith('@type'): + if header and header[0].startswith(b'@type'): header = header[1:] routers_start = document_file.tell() diff --git a/stem/descriptor/server_descriptor.py b/stem/descriptor/server_descriptor.py index 97781da..9416582 100644 --- a/stem/descriptor/server_descriptor.py +++ b/stem/descriptor/server_descriptor.py @@ -143,7 +143,7 @@ def _parse_file(descriptor_file, is_bridge = False, validate = True, **kwargs): descriptor_content += _read_until_keywords(block_end_prefix, descriptor_file, True) if descriptor_content: - if descriptor_content[0].startswith('@type'): + if descriptor_content[0].startswith(b'@type'): descriptor_content = descriptor_content[1:] # strip newlines from annotations diff --git a/stem/interpreter/commands.py b/stem/interpreter/commands.py index f7de765..5473628 100644 --- a/stem/interpreter/commands.py +++ b/stem/interpreter/commands.py @@ -10,6 +10,7 @@ import stem.control import stem.descriptor.remote import stem.interpreter.help import stem.util.connection +import stem.util.str_tools import stem.util.tor_tools from stem.interpreter import STANDARD_OUTPUT, BOLD_OUTPUT, ERROR_OUTPUT, uses_settings, msg @@ -220,7 +221,7 @@ class ControlInterpretor(code.InteractiveConsole): lines.append(format('exit policy: ', *BOLD_OUTPUT) + exit_policy_label) if server_desc: - contact = server_desc.contact + contact = stem.util.str_tools._to_unicode(server_desc.contact) # clears up some highly common obscuring