[PATCH] support commented-out stanzas in debian/control

Philipp Kern pkern at debian.org
Wed May 6 20:36:57 UTC 2009


In some packages you can find commented-out stanzas that contain
VCS ids, build-dependency explanations or just commented-out binary
packages.  This hit the breaking condition in the pure Python
parser.  This patch should fix this by catching the EOFError
exception instead.  (Please note that the previous code at that
place was elegant but as one cannot check if an iterator is
exhausted, it could not be kept.)

The apt_pkg parser fails even more by reporting a key '# ' which
is later not found in the dictionary.  So the pure Python parser
already caught the commented out case correctly except for the
commented lines in a stanza of their own case.

Signed-off-by: Philipp Kern <pkern at debian.org>
---
 debian_bundle/deb822.py |   20 ++++++++++++++------
 tests/test_deb822.py    |   22 ++++++++++++++++++++++
 2 files changed, 36 insertions(+), 6 deletions(-)

diff --git a/debian_bundle/deb822.py b/debian_bundle/deb822.py
index 125f451..34fb80c 100644
--- a/debian_bundle/deb822.py
+++ b/debian_bundle/deb822.py
@@ -190,7 +190,7 @@ class Deb822Dict(object, UserDict.DictMixin):
 
 class Deb822(Deb822Dict):
 
-    def __init__(self, sequence=None, fields=None, _parsed=None):
+    def __init__(self, sequence=None, fields=None, _parsed=None, _raise=False):
         """Create a new Deb822 instance.
 
         :param sequence: a string, or any any object that returns a line of
@@ -214,7 +214,9 @@ class Deb822(Deb822Dict):
             try:
                 self._internal_parser(sequence, fields)
             except EOFError:
-                pass
+                # Sequence is exhausted.
+                if _raise:
+                    raise
 
         self.gpg_info = None
 
@@ -256,10 +258,16 @@ class Deb822(Deb822Dict):
 
         else:
             iterable = iter(sequence)
-            x = cls(iterable, fields)
-            while len(x) != 0:
-                yield x
-                x = cls(iterable, fields)
+            try:
+                x = cls(iterable, fields, _raise=True)
+                while True:
+                    # Ignore empty stanzas.
+                    if len(x) != 0:
+                        yield x
+                    x = cls(iterable, fields, _raise=True)
+            except EOFError:
+                # The loop breaking condition.
+                pass
 
     iter_paragraphs = classmethod(iter_paragraphs)
 
diff --git a/tests/test_deb822.py b/tests/test_deb822.py
index 10e98c5..89e58cd 100755
--- a/tests/test_deb822.py
+++ b/tests/test_deb822.py
@@ -571,6 +571,28 @@ Description: python modules to work with Debian-related data formats
                              "should have a space between the colon and the "
                              "beginning of the value")
 
+    def test_commented_out_stanza(self):
+        """Commented out stanzas
+
+	Some debian/control have stanzas that just consist out of a set of
+        commented lines.  They could be VCS IDs, commented out binary packages
+        or further explanations about (build-)dependencies.  All of them
+        were found in the wild.
+        """
+        control_paragraphs = """# $Id$
+
+Package: python-debian
+Architecture: all
+
+# It's architecture all because it does not contain any architecture-
+# specific data.
+"""
+        lines = control_paragraphs.splitlines()
+        counter = 0
+        for d in deb822.Deb822.iter_paragraphs(lines):
+            counter += 1
+        self.assert_(counter == 1, "Invalid paragraph count.")
+
     def test_blank_value(self):
         """Fields with blank values are parsable--so they should be dumpable"""
 
-- 
1.5.6.5




More information about the pkg-python-debian-maint mailing list