Skip to content
Myr

Shallow copy of Request fields in Request.copy()

Before adoption: this commit came in with the history the project adopted, so no landing records it.

This prevents e.g. modifying the headers of a copied request from affecting the headers of its source and vice versa. Copying is used with the intent to mutuate, so allowing this kind of mutation of fields makes sense. Is a deep copy better?

authored by Robert Estelle · committed by Robert Estelle · parent 9edba83 · Browse files · 1 file+12 added−6 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

1 file+12 added−6 removed

  • other:+12 added−6 removed

1 file+12 added−6 removed

modified:requests/models.py+12 added-6 removed
26 unmodified lines
27from .compat import (28 cookielib, urlunparse, urlsplit, urlencode, str, bytes, StringIO,29 is_py2, chardet, json, builtin_str, basestring)3031from copy import copy as shallowcopy3233CONTENT_CHUNK_SIZE = 10 * 102434ITER_CHUNK_SIZE = 5123536log = logging.getLogger(__name__)
182 unmodified lines
219 def copy(self):220 return Request(221 method = self.method,222 url = self.url,221 headers = self.headers,222 files = self.files,223 data = self.data,224 params = self.params,223 auth = self.auth,226 cookies = self.cookies,227 hooks = self.hooks,224225 # Copy mutable dict/list parameters so that altering one request226 # does not alter the copy.227 headers = shallowcopy(self.headers),228 params = shallowcopy(self.params),229 cookies = shallowcopy(self.cookies),230 hooks = shallowcopy(self.hooks),231232 files = shallowcopy(self.files),233 data = shallowcopy(self.data),234 )235236 def prepare(self):237 """Constructs a :class:`PreparedRequest <PreparedRequest>` for transmission and returns it."""
488 unmodified lines