[PATCH] Fix the copy method for subclasses of _multivalued

John Wright jsw at debian.org
Sat Sep 18 08:17:45 UTC 2010


The default copy method just passes the original object as the first
argument to the constructor.  But _multivalued's constructor does some
post-processing after Deb822's that assumes that the values are all
string (or unicode), rather than lists.  Rather than add this special
case to the constructor, I get all the key-value pairs as strings,
create a Deb822Dict out of them, and send that as a _parsed argument to
the class constructor.
---
 lib/debian/deb822.py |    7 +++++++
 tests/test_deb822.py |    9 +++++++++
 2 files changed, 16 insertions(+), 0 deletions(-)

diff --git a/lib/debian/deb822.py b/lib/debian/deb822.py
index a0cad69..97684f5 100644
--- a/lib/debian/deb822.py
+++ b/lib/debian/deb822.py
@@ -922,6 +922,13 @@ class _multivalued(Deb822):
         else:
             return Deb822.get_as_string(self, key)
 
+    def copy(self):
+        # Passing this object directly to its constructor will fail because the
+        # values for multivalued fields are stored as lists, and the
+        # constructor expects strings.
+        items_iter = ((key, self.get_as_string(key)) for key in self)
+        return self.__class__(_parsed=Deb822Dict(items_iter))
+
 ###
 
 
diff --git a/tests/test_deb822.py b/tests/test_deb822.py
index 891f4cd..9dadb15 100755
--- a/tests/test_deb822.py
+++ b/tests/test_deb822.py
@@ -610,6 +610,15 @@ Description: python modules to work with Debian-related data formats
         expected_dump = "Foo: bar\nBar: baz\n"
         self.assertEqual(d_copy.dump(), expected_dump)
 
+    def test_copy_multivalued(self):
+        """The copy method should also work with multivalued-field objects"""
+        d = deb822.Dsc()
+        d['Files'] = [{'md5sum': 'deadbeef', 'size': '9605', 'name': 'foo'}]
+
+        d_copy = d1.copy()
+        self.assertTrue(isinstance(d_copy, deb822.Dsc))
+        self.assertEqual(d_copy.dump(), d.dump())
+
     def test_bug457929_multivalued_dump_works(self):
         """dump() was not working in multivalued classes, see #457929."""
         changesobj = deb822.Changes(CHANGES_FILE.splitlines())
-- 
1.7.1




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