Skip to content
Myr

Lazily encode data, params, files

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

Previously, data, params, and files were encoded and stored in Request.__init__, and subsequently put into service during Request.send. The problem with this approach is that hooks and auth callables need to be aware of the eager encoding, and if they touch the originals, make sure to update the encoded versions. A better approach is to only encode late in the sending process. This way, hooks and auth callables can safely make changes without fear of the old, encoded variant overriding it.

authored by Idan Gazit · committed by Idan Gazit · parent d240b1f · Browse files · 1 file+15 added−21 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+15 added−21 removed

  • other:+15 added−21 removed

1 file+15 added−21 removed

modified:requests/models.py+15 added-21 removed
112 unmodified lines
113 self.proxies['http'] = os.environ['HTTP_PROXY']114 if 'HTTPS_PROXY' in os.environ:115 self.proxies['https'] = os.environ['HTTPS_PROXY']116117 self.data, self._enc_data = self._encode_params(data)118 self.params, self._enc_params = self._encode_params(params)119 self.files, self._enc_files = self._encode_files(files)117 self.data = data118 self.params = params119 self.files = files120121 #: :class:`Response <Response>` instance, containing122 #: content and metadata of HTTP Response, once :attr:`sent <send>`.123 self.response = Response()
189 unmodified lines
313314 Will successfully encode parameters when passed as a dict or a list of315 2-tuples. Order is retained if data is a list of 2-tuples but abritrary316 if parameters are supplied as a dict.317318 If the data supplied contains parameters, encodes each parameter in it,319 and returns a list of tuples containing the encoded parameters, and a320 urlencoded version of that.321322 Otherwise, assumes the data is already encoded appropriately, and323 returns it twice.317 """318319 if isinstance(data, bytes):327 return data, data320 return data321 if isinstance(data, str):329 return data, data322 return data323 elif hasattr(data, '__iter__'):324 try:325 dict(data)326 except ValueError:
5 unmodified lines
332 for v in isinstance(vs, list) and vs or [vs]:333 result.append(334 (k.encode('utf-8') if isinstance(k, str) else k,335 v.encode('utf-8') if isinstance(v, str) else v))343 return result, urlencode(result, doseq=True)336 return urlencode(result, doseq=True)337 else:345 return data, data338 return data339340 def _encode_files(self, files):341342 if (not files) or isinstance(self.data, str):350 return None, None343 return None344345 try:346 fields = self.data.copy()347 except AttributeError:
9 unmodified lines
357 fields.update({k: (fn, fp.read())})358359 (body, content_type) = encode_multipart_formdata(fields)360368 return files, (body, content_type)361 return (body, content_type)362363 @property364 def full_url(self):365 """Build the actual URL to use."""
32 unmodified lines
398 fragment = fragment.encode('utf-8')399400 url = (urlunparse([scheme, netloc, path, params, query, fragment]))401402 enc_params = self._encode_params(self.params)409 if self._enc_params:403 if enc_params:404 if urlparse(url).query:411 url = '%s&%s' % (url, self._enc_params)405 url = '%s&%s' % (url, enc_params)406 else:413 url = '%s?%s' % (url, self._enc_params)407 url = '%s?%s' % (url, enc_params)408409 if self.config.get('encode_uri', True):410 url = requote_uri(url)411
80 unmodified lines
492 self.__dict__.update(r.__dict__)493494 # Multi-part file uploads.495 if self.files:502 (body, content_type) = self._enc_files496 (body, content_type) = self._encode_files(self.files)497 else:498 if self.data:499506 body = self._enc_data500 body = self._encode_params(self.data)501 if isinstance(self.data, str):502 content_type = None503 else:504 content_type = 'application/x-www-form-urlencoded'
340 unmodified lines