[python-debian/master] deb822: Don't ignore leading newlines on field data with apt_pkg

John Wright john at johnwright.org
Sun Jul 26 15:20:20 UTC 2009


When using Deb822.iter_paragraphs with apt_pkg=True on certain data,
whitespace preservation would be broken.  For example,

    Foo:
     bar baz

would be parsed identically to

    Foo: bar baz

To correct this, we use a feature introduced to python-apt in version
0.7.11.1 that allows us to get the full tag, including the field name,
without any whitespace stripping magic.  Then, we just strip off leading
spaces and tabs, *but not newlines*, from the input.

The test_Sources file is also updated to check for regressions against
this bug.

Closes: #466753
---
 debian/changelog        |    7 +++++++
 debian/control          |    1 +
 debian_bundle/deb822.py |   39 ++++++++++++++++++++++++++++++---------
 tests/test_Sources      |   19 +++++++++++++++++++
 4 files changed, 57 insertions(+), 9 deletions(-)

diff --git a/debian/changelog b/debian/changelog
index e2d63f5..fe95309 100644
--- a/debian/changelog
+++ b/debian/changelog
@@ -1,3 +1,10 @@
+python-debian (0.1.15) UNRELEASED; urgency=low
+
+  * deb822: Don't ignore leading newlines on field data with apt_pkg
+    (Closes: #466753)
+
+ -- John Wright <jsw at debian.org>  Thu, 30 Jul 2009 01:03:49 +0200
+
 python-debian (0.1.14) unstable; urgency=low
 
   [ Stefano Zacchiroli ]
diff --git a/debian/control b/debian/control
index df2830e..521b588 100644
--- a/debian/control
+++ b/debian/control
@@ -18,6 +18,7 @@ Package: python-debian
 Architecture: all
 Depends: ${python:Depends}
 Recommends: python-apt
+Conflicts: python-apt (< 0.7.11.1)
 Suggests: gpgv
 Provides: python-deb822
 Conflicts: python-deb822
diff --git a/debian_bundle/deb822.py b/debian_bundle/deb822.py
index fb1ed7c..c1301f1 100644
--- a/debian_bundle/deb822.py
+++ b/debian_bundle/deb822.py
@@ -37,6 +37,33 @@ import sys
 import StringIO
 import UserDict
 
+class TagFileWrapper(object, UserDict.DictMixin):
+    """Wrap a TagFile object, using its Section.FindRaw method instead of Find
+
+    This allows us to pick which whitespace to strip off the beginning and end
+    of the data, so we don't lose leading newlines.
+    """
+
+    def __init__(self, parser):
+        self.__parser = parser
+
+    def keys(self):
+        return self.__parser.Section.keys()
+
+    def __getitem__(self, key):
+        s = self.__parser.Section.FindRaw(key)
+
+        if s is None:
+            raise KeyError(key)
+
+        # Get just the stuff after the first ':'
+        # Could use s.partition if we only supported python >= 2.5
+        data = s[s.find(':')+1:]
+
+        # Get rid of spaces and tabs after the ':', but not newlines, and strip
+        # off any newline at the end of the data.
+        return data.lstrip(' \t').rstrip('\n')
+
 class OrderedSet(object):
     """A set-like object that preserves order when iterating over it
 
@@ -243,15 +270,9 @@ class Deb822(Deb822Dict):
         if _have_apt_pkg and use_apt_pkg and isinstance(sequence, file):
             parser = apt_pkg.ParseTagFile(sequence)
             while parser.Step() == 1:
-                if shared_storage:
-                    parsed = parser.Section
-                else:
-                    # Since parser.Section doesn't have an items method, we
-                    # need to imitate that method here and make a Deb822Dict
-                    # from the result in order to preserve order.
-                    items = [(key, parser.Section[key])
-                             for key in parser.Section.keys()]
-                    parsed = Deb822Dict(items)
+                parsed = TagFileWrapper(parser)
+                if not shared_storage:
+                    parsed = Deb822Dict(parsed)
                 yield cls(fields=fields, _parsed=parsed)
 
         else:
diff --git a/tests/test_Sources b/tests/test_Sources
index 0f3c67d..254f9fb 100644
--- a/tests/test_Sources
+++ b/tests/test_Sources
@@ -43,3 +43,22 @@ Checksums-Sha256:
  bf2e12578caf3b79be6182bb109b3a0215d8a3b89ae2049773f56f286eeb8ad3 19746003 binutils_2.18.1~cvs20080103.orig.tar.gz
  44e346757ce336f6a16879264b6cc4ff4e2b4da517e4b5db6cc7f1d254f302dd 64334 binutils_2.18.1~cvs20080103-6.diff.gz
 
+Package: debian-archive-keyring
+Binary: debian-archive-keyring, debian-archive-keyring-udeb
+Version: 2009.01.31
+Priority: important
+Section: misc
+Maintainer: Debian Release Team <packages at release.debian.org>
+Architecture: all
+Standards-Version: 3.7.3
+Format: 1.0
+Directory: pool/main/d/debian-archive-keyring
+Files:
+ 2675031b2286ca8dfc085e2a9c9d38ed 838 debian-archive-keyring_2009.01.31.dsc
+ 5365c07ddcf639544933552e31a571ee 13627 debian-archive-keyring_2009.01.31.tar.gz
+Uploaders: Luk Claes <luk at debian.org>
+Checksums-Sha1: 
+ dd987a5ee85d8c4fc3e115f60a6a021e78c626dd 13627 debian-archive-keyring_2009.01.31.tar.gz
+Checksums-Sha256: 
+ 9bdfe720407f5c134842eeaa8cfc9932be33150a5a3c041426216a90cce9e32d 13627 debian-archive-keyring_2009.01.31.tar.gz
+
-- 
1.5.6.5




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