Skip to content

Loading…

Add HTTPHeaders.copy() #519

Closed
wants to merge 1 commit into from

3 participants

@simpkins

Update HTTPHeaders to provide its own implementation of copy() that
returns a new HTTPHeaders object. Previously calling copy() used the
underlying dict method, and returned a plain dict.

@simpkins simpkins Add HTTPHeaders.copy()
Update HTTPHeaders to provide its own implementation of copy() that
returns a new HTTPHeaders object.  Previously calling copy() used the
underlying dict method, and returned a plain dict.
4cf903b
@bdarnell
tornadoweb member

I added "copy constructor" behavior to HTTPHeaders in 2f35821#diff-0
(which hasn't yet made it into any release), so I think all that's needed is to make copy() return HTTPHeaders(self) instead of dict(self)

@bdarnell bdarnell closed this in f41306d
@helderm

Is this really closed? I'm still seeing this error on 4.1.

In [0]: from tornado.httputil import HTTPHeaders
In [1]: h = HTTPHeaders()
In [2]: h.add('Set-Cookie', 'A=1')
In [3]: h.add('Set-Cookie', 'B=2')
In [4]: h.get_list('Set-Cookie')
Out[5]: ['A=1', 'B=2']
In [6]: from copy import deepcopy
In [7]: hc = deepcopy(h)
In [8]: hc.get_list('Set-Cookie')
Out[9]: ['A=1,B=2']
@bdarnell
tornadoweb member

This issue is about the method HTTPHeaders.copy(), which overrides the default dict.copy(). The copy module and its functions copy.copy() and copy.deepcopy() are a separate matter. Feel free to open new issues for problems related to the copy module.

@bdarnell
tornadoweb member

FYI I've just fixed the copy.copy issue: 674d7eb

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on May 26, 2012
  1. @simpkins

    Add HTTPHeaders.copy()

    simpkins committed
    Update HTTPHeaders to provide its own implementation of copy() that
    returns a new HTTPHeaders object.  Previously calling copy() used the
    underlying dict method, and returned a plain dict.
Showing with 9 additions and 0 deletions.
  1. +9 −0 tornado/httputil.py
View
9 tornado/httputil.py
@@ -154,6 +154,15 @@ def update(self, *args, **kwargs):
for k, v in dict(*args, **kwargs).iteritems():
self[k] = v
+ def copy(self):
+ # Create a new HTTPHeaders object.
+ # This will initialize the HTTPHeaders with the values in our dict, but
+ # won't populate the full lists as tracked by self._as_list
+ ret = HTTPHeaders(self)
+ # Update ret._as_list separately
+ ret._as_list = self._as_list.copy()
+ return ret
+
_NORMALIZED_HEADER_RE = re.compile(r'^[A-Z0-9][a-z0-9]*(-[A-Z0-9][a-z0-9]*)*$')
_normalized_headers = {}
Something went wrong with that request. Please try again.