From: Ben Darnell Date: Sun, 19 Apr 2015 17:18:46 +0000 (-0400) Subject: Make HTTPHeaders compatible with copy.copy and copy.deepcopy. X-Git-Tag: v4.2.0b1~12 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=674d7eb506b5569f8be19fd3cd363421dc095af4;p=thirdparty%2Ftornado.git Make HTTPHeaders compatible with copy.copy and copy.deepcopy. --- diff --git a/tornado/httputil.py b/tornado/httputil.py index a4b1bb661..f5fea213a 100644 --- a/tornado/httputil.py +++ b/tornado/httputil.py @@ -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. diff --git a/tornado/test/httputil_test.py b/tornado/test/httputil_test.py index 2ddc15fab..6e9536017 100644 --- a/tornado/test/httputil_test.py +++ b/tornado/test/httputil_test.py @@ -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.