[tor-commits] [stem/master] Revert "Don't modify list of descriptor lines"

atagar at torproject.org atagar at torproject.org
Sun Jan 25 22:37:35 UTC 2015


commit d183c7ab5c65d82c73640132d820dfbc4d3eadf1
Author: Damian Johnson <atagar at torproject.org>
Date:   Sun Jan 25 12:55:52 2015 -0800

    Revert "Don't modify list of descriptor lines"
    
    As discussed in the prior commit it doesn't seem to help performance in
    practice and hurts maintainability, so leaving it out for now.
---
 stem/descriptor/__init__.py |   33 ++++++++++++++-------------------
 1 file changed, 14 insertions(+), 19 deletions(-)

diff --git a/stem/descriptor/__init__.py b/stem/descriptor/__init__.py
index da4e5e7..0d2295d 100644
--- a/stem/descriptor/__init__.py
+++ b/stem/descriptor/__init__.py
@@ -587,25 +587,23 @@ def _read_until_keywords(keywords, descriptor_file, inclusive = False, ignore_fi
     return content
 
 
-def _get_pseudo_pgp_block(lines, i):
+def _get_pseudo_pgp_block(remaining_contents):
   """
   Checks if given contents begins with a pseudo-Open-PGP-style block and, if
   so, pops it off and provides it back to the caller.
 
-  :param list lines: lines to be checked for a public key block
-  :param int i: line to start reading from
+  :param list remaining_contents: lines to be checked for a public key block
 
-  :returns: **tuple** of the (block_type, content, read_until) or None if it
-    doesn't exist
+  :returns: **tuple** of the (block_type, content) or None if it doesn't exist
 
   :raises: **ValueError** if the contents starts with a key block but it's
     malformed (for instance, if it lacks an ending line)
   """
 
-  if len(lines) <= i:
+  if not remaining_contents:
     return None  # nothing left
 
-  block_match = PGP_BLOCK_START.match(lines[i])
+  block_match = PGP_BLOCK_START.match(remaining_contents[0])
 
   if block_match:
     block_type = block_match.groups()[0]
@@ -613,15 +611,14 @@ def _get_pseudo_pgp_block(lines, i):
     end_line = PGP_BLOCK_END % block_type
 
     while True:
-      if len(lines) <= i:
+      if not remaining_contents:
         raise ValueError("Unterminated pgp style block (looking for '%s'):\n%s" % (end_line, '\n'.join(block_lines)))
 
-      line = lines[i]
+      line = remaining_contents.pop(0)
       block_lines.append(line)
-      i += 1
 
       if line == end_line:
-        return (block_type, '\n'.join(block_lines), i)
+        return (block_type, '\n'.join(block_lines))
   else:
     return None
 
@@ -657,12 +654,10 @@ def _get_descriptor_components(raw_contents, validate, extra_keywords = ()):
 
   entries = OrderedDict()
   extra_entries = []  # entries with a keyword in extra_keywords
-  lines = raw_contents.split('\n')
-  skip_until = None  # _get_pseudo_pgp_block() reads ahead, so we should skip lines its read
+  remaining_lines = raw_contents.split('\n')
 
-  for i, line in enumerate(lines):
-    if skip_until and i < skip_until:
-      continue
+  while remaining_lines:
+    line = remaining_lines.pop(0)
 
     # V2 network status documents explicitly can contain blank lines...
     #
@@ -696,12 +691,12 @@ def _get_descriptor_components(raw_contents, validate, extra_keywords = ()):
       value = ''
 
     try:
-      block_attr = _get_pseudo_pgp_block(lines, i + 1)
+      block_attr = _get_pseudo_pgp_block(remaining_lines)
 
       if block_attr:
-        block_type, block_contents, skip_until = block_attr
+        block_type, block_contents = block_attr
       else:
-        block_type, block_contents, skip_until = None, None, None
+        block_type, block_contents = None, None
     except ValueError as exc:
       if not validate:
         continue





More information about the tor-commits mailing list