tags 539316 + patch
thanks

Hi Michael,

On Thu, Jul 30, 2009 at 06:34:20PM +0200, Michael Bienia wrote:
> Package: python-debian
> Version: 0.1.14
> Severity: normal

Thanks for the report.

> I was looking if I can use the Changelog class to access changelog
> entries from a existing changelogs. The parsing of a existing changelog
> went fine but then I found out that there are no methods to iterate over
> (or access) existing changelog blocks (I'd should file a wishlist bug
> about this instead of accessing internal structures)

Yes, please file this as a separate bug...

> but the more
> important problem was that I got to many newlines when I tried to get a
> string representation of a changelog block.
> (Note: this was with pyhton 2.6 on Ubuntu if it's important)
> 
> >>> c = debian_bundle.changelog.Changelog(file('changelog.txt', 'r'))
> >>> print c

To work around this for now, please pass the whole file in as a string:

>>> c = debian_bundle.changelog.Changelog(file('changelog.txt', 'r').read())

I'm attaching a patch that fixes the bug and adds a testcase.  James,
would you review and apply if you agree?

-- 
John Wright <j...@debian.org>
commit 1be5f14ee2e432ede73e3db70f12fd7beb0eeb4c
Author: John Wright <j...@johnwright.org>
Date:   Thu Jul 30 19:44:51 2009 +0200

    changelog: Consistently parse different types of inputs
    
    The previous implementation added extra newlines when given a file as
    input, rather than the result of whole_string.splitlines().

diff --git a/debian_bundle/changelog.py b/debian_bundle/changelog.py
index 07fe4fa..ae72a90 100644
--- a/debian_bundle/changelog.py
+++ b/debian_bundle/changelog.py
@@ -298,10 +298,12 @@ class Changelog(object):
                 self._parse_error('Empty changelog file.', strict)
                 return
 
-            if file[-1] != '\n':
-                file += '\n'
-            file = file.split('\n')[:-1]
+            file = file.splitlines()
         for line in file:
+            # Support both lists of lines without the trailing newline and
+            # those with trailing newlines (e.g. when given a file object
+            # directly)
+            line = line.rstrip('\n')
             if state == first_heading or state == next_heading_or_eof:
                 top_match = topline.match(line)
                 blank_match = blankline.match(line)
@@ -645,6 +647,17 @@ class ChangelogTests(unittest.TestCase):
         self.assertEqual(c.full_version, '1.2.3')
         self.assertEqual(str(c.version), c.full_version)
 
+    def test_str_consistent(self):
+        # The parsing of the changelog (including the string representation)
+        # should be consistent whether we give a single string, a list of
+        # lines, or a file object to the Changelog initializer
+        cl_data = open('test_changelog').read()
+        c1 = Changelog(open('test_changelog'))
+        c2 = Changelog(cl_data)
+        c3 = Changelog(cl_data.splitlines())
+        for c in (c1, c2, c3):
+            self.assertEqual(str(c), cl_data)
+
 class VersionTests(unittest.TestCase):
 
     def _test_version(self, full_version, epoch, upstream, debian):

Reply via email to