mirror of
https://github.com/kennethreitz/requests.git
synced 2026-06-05 22:50:18 +00:00
Shallow copy of Request fields in Request.copy()
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?
This commit is contained in:
+12
-6
@@ -28,6 +28,8 @@ from .compat import (
|
||||
cookielib, urlunparse, urlsplit, urlencode, str, bytes, StringIO,
|
||||
is_py2, chardet, json, builtin_str, basestring)
|
||||
|
||||
from copy import copy as shallowcopy
|
||||
|
||||
CONTENT_CHUNK_SIZE = 10 * 1024
|
||||
ITER_CHUNK_SIZE = 512
|
||||
|
||||
@@ -218,13 +220,17 @@ class Request(RequestHooksMixin):
|
||||
return Request(
|
||||
method = self.method,
|
||||
url = self.url,
|
||||
headers = self.headers,
|
||||
files = self.files,
|
||||
data = self.data,
|
||||
params = self.params,
|
||||
auth = self.auth,
|
||||
cookies = self.cookies,
|
||||
hooks = self.hooks,
|
||||
|
||||
# Copy mutable dict/list parameters so that altering one request
|
||||
# does not alter the copy.
|
||||
headers = shallowcopy(self.headers),
|
||||
params = shallowcopy(self.params),
|
||||
cookies = shallowcopy(self.cookies),
|
||||
hooks = shallowcopy(self.hooks),
|
||||
|
||||
files = shallowcopy(self.files),
|
||||
data = shallowcopy(self.data),
|
||||
)
|
||||
|
||||
def prepare(self):
|
||||
|
||||
Reference in New Issue
Block a user