From: Ben Darnell Date: Sat, 4 Jul 2015 22:24:24 +0000 (-0400) Subject: Support pickling of HTTPHeaders. X-Git-Tag: v4.3.0b1~85 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=433e7f75cc0d6d1ee573a8b38e0768f1c46c225d;p=thirdparty%2Ftornado.git Support pickling of HTTPHeaders. Closes #1445 --- diff --git a/tornado/httputil.py b/tornado/httputil.py index fa5e697c1..747dfc400 100644 --- a/tornado/httputil.py +++ b/tornado/httputil.py @@ -242,6 +242,15 @@ class HTTPHeaders(dict): # effectively a deep copy. return self.copy() + def __reduce_ex__(self, v): + # We must override dict.__reduce_ex__ to pickle ourselves + # correctly. + return HTTPHeaders, (), list(self.get_all()) + + def __setstate__(self, state): + for k, v in state: + self.add(k, v) + class HTTPServerRequest(object): """A single HTTP request. diff --git a/tornado/test/httputil_test.py b/tornado/test/httputil_test.py index 6e9536017..ca60b45ef 100644 --- a/tornado/test/httputil_test.py +++ b/tornado/test/httputil_test.py @@ -12,11 +12,11 @@ from tornado.util import u import copy import datetime import logging +import pickle import time class TestUrlConcat(unittest.TestCase): - def test_url_concat_no_query_params(self): url = url_concat( "https://localhost/path", @@ -298,6 +298,15 @@ Foo: even self.assertIsNot(headers, h1) self.assertIsNot(headers.get_list('A'), h1.get_list('A')) + def test_pickle_roundtrip(self): + headers = HTTPHeaders() + headers.add('Set-Cookie', 'a=b') + headers.add('Set-Cookie', 'c=d') + headers.add('Content-Type', 'text/html') + pickled = pickle.dumps(headers) + unpickled = pickle.loads(pickled) + self.assertEqual(sorted(headers.get_all()), sorted(unpickled.get_all())) + self.assertEqual(sorted(headers.items()), sorted(unpickled.items())) class FormatTimestampTest(unittest.TestCase):