]> git.ipfire.org Git - thirdparty/tornado.git/commitdiff
Make HTTPHeaders compatible with copy.copy and copy.deepcopy.
authorBen Darnell <ben@bendarnell.com>
Sun, 19 Apr 2015 17:18:46 +0000 (13:18 -0400)
committerBen Darnell <ben@bendarnell.com>
Sun, 19 Apr 2015 17:19:06 +0000 (13:19 -0400)
tornado/httputil.py
tornado/test/httputil_test.py

index a4b1bb661138d7a5b71b4ef84c2b39541ef0062b..f5fea213abf3b297898881467c3173431129671d 100644 (file)
@@ -234,6 +234,14 @@ class HTTPHeaders(dict):
         # default implementation returns dict(self), not the subclass
         return HTTPHeaders(self)
 
+    # Use our overridden copy method for the copy.copy module.
+    __copy__ = copy
+
+    def __deepcopy__(self, memo_dict):
+        # Our values are immutable strings, so our standard copy is
+        # effectively a deep copy.
+        return self.copy()
+
 
 class HTTPServerRequest(object):
     """A single HTTP request.
index 2ddc15fab0d5af5cf0c41257f4b303cbef70a6b5..6e95360174de84b1e2f5f45f27000fcfe80f2dbc 100644 (file)
@@ -9,6 +9,7 @@ from tornado.testing import ExpectLog
 from tornado.test.util import unittest
 from tornado.util import u
 
+import copy
 import datetime
 import logging
 import time
@@ -280,6 +281,24 @@ Foo: even
                           ('Lf', 'lf'),
                           ])
 
+    def test_copy(self):
+        all_pairs = [('A', '1'), ('A', '2'), ('B', 'c')]
+        h1 = HTTPHeaders()
+        for k, v in all_pairs:
+            h1.add(k, v)
+        h2 = h1.copy()
+        h3 = copy.copy(h1)
+        h4 = copy.deepcopy(h1)
+        for headers in [h1, h2, h3, h4]:
+            # All the copies are identical, no matter how they were
+            # constructed.
+            self.assertEqual(list(sorted(headers.get_all())), all_pairs)
+        for headers in [h2, h3, h4]:
+            # Neither the dict or its member lists are reused.
+            self.assertIsNot(headers, h1)
+            self.assertIsNot(headers.get_list('A'), h1.get_list('A'))
+
+
 
 class FormatTimestampTest(unittest.TestCase):
     # Make sure that all the input types are supported.