[Pkg-python-debian-commits] r84 trunk: - Fix the copy method of Deb822Dict so that it returns an object of the

jsw at debian.org jsw at debian.org
Thu Sep 27 04:59:18 UTC 2007


------------------------------------------------------------
revno: 84
committer: jsw at debian.org
branch nick: trunk
timestamp: Wed 2007-09-26 22:59:18 -0600
message:
    - Fix the copy method of Deb822Dict so that it returns an object of the
      same type, rather than explicitly instantiating Deb822Dict (so that
      subclasses like Deb822 can directly use the copy method and have it "just
      work").  Added a test case.
modified:
  debian/changelog
  debian_bundle/deb822.py
  debian_bundle/test_deb822.py
-------------- next part --------------
=== modified file 'debian/changelog'
--- a/debian/changelog	2007-09-12 00:16:40 +0000
+++ b/debian/changelog	2007-09-27 04:59:18 +0000
@@ -6,8 +6,12 @@
       these were parsed fine, but if you tried to dump the resulting Deb822
       object, you'd get an IndexError (because of how we handled fields whose
       values start with newlines).  Added a test case.
+    - Fix the copy method of Deb822Dict so that it returns an object of the
+      same type, rather than explicitly instantiating Deb822Dict (so that
+      subclasses like Deb822 can directly use the copy method and have it "just
+      work").  Added a test case.
 
- -- John Wright <jsw at debian.org>  Tue, 11 Sep 2007 18:09:21 -0600
+ -- John Wright <jsw at debian.org>  Wed, 26 Sep 2007 22:50:52 -0600
 
 python-debian (0.1.6) unstable; urgency=low
 

=== modified file 'debian_bundle/deb822.py'
--- a/debian_bundle/deb822.py	2007-09-12 00:16:40 +0000
+++ b/debian_bundle/deb822.py	2007-09-27 04:59:18 +0000
@@ -36,12 +36,6 @@
     # Subclassing UserDict.DictMixin because we're overriding so much dict
     # functionality that subclassing dict requires overriding many more than
     # the four methods that DictMixin requires.
-    #
-    # HACK: I'm also subclassing dict, but all the methods we care about will
-    # already be provided by DictMixin.  This way, Deb822Dict is a "real"
-    # subclass of dict, i.e.
-    #     isinstance(Deb822Dict(), dict) == True
-    # but we get out of implementing lots of methods.
     """A dictionary-like object suitable for storing RFC822-like data.
 
     Deb822Dict behaves like a normal dict, except:
@@ -132,7 +126,8 @@
         return True
 
     def copy(self):
-        copy = Deb822Dict(self)
+        # Use self.__class__ so this works as expected for subclasses
+        copy = self.__class__(self)
         return copy
 
     # TODO implement __str__() and make dump() use that?

=== modified file 'debian_bundle/test_deb822.py'
--- a/debian_bundle/test_deb822.py	2007-09-12 00:16:40 +0000
+++ b/debian_bundle/test_deb822.py	2007-09-27 04:59:18 +0000
@@ -9,9 +9,7 @@
 # dated June, 1991.
 #
 # This program is distributed in the hope that it will be useful,
-# but WITHOUT ANY WARRANTY; without even the implied warranty of
-# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-# GNU General Public License for more details.
+# but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the # GNU General Public License for more details.
 #
 # You should have received a copy of the GNU General Public License
 # along with this program; if not, write to the Free Software
@@ -430,6 +428,16 @@
         expected = "Foo: bar\nBaz:\nAnother-Key: another value\n"
         self.assertEqual(dumped, expected)
 
+    def test_copy(self):
+        """The copy method of Deb822 should return another Deb822 object"""
+        d = deb822.Deb822()
+        d['Foo'] = 'bar'
+        d['Bar'] = 'baz'
+        d_copy = d.copy()
+
+        self.assert_(isinstance(d_copy, deb822.Deb822))
+        expected_dump = "Foo: bar\nBar: baz\n"
+        self.assertEqual(d_copy.dump(), expected_dump)
 
 if __name__ == '__main__':
     unittest.main()



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