Skip to content
Myr

Fixes #817.

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

Use dicts and lists where necessary but accept both dicts and lists of 2-tuples everywhere.

authored by Ian Cordasco · committed by Ian Cordasco · parent c8f166f · Browse files · 5 files+60 added−47 removed
This commit

State

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

Diff stats

5 files+60 added−47 removed

  • other:+60 added−47 removed

5 files+60 added−47 removed

modified:requests/compat.py+2 added
91 unmodified lines
92 try:93 import cchardet as chardet94 except ImportError:95 from .packages import chardet96 from .packages.urllib3.packages.ordered_dict import OrderedDict9798 builtin_str = str99 bytes = str100 str = unicode
8 unmodified lines
109 from http import cookiejar as cookielib110 from http.cookies import Morsel111 from io import StringIO112 from .packages import chardet2 as chardet113 from collections import OrderedDict114115 builtin_str = str116 str = str117 bytes = bytes
2 unmodified lines
modified:requests/models.py+1 added-1 removed
32 unmodified lines
33 stream_decode_response_unicode, get_netrc_auth, get_environ_proxies,34 to_key_val_list, DEFAULT_CA_BUNDLE_PATH, parse_header_links, iter_slices)35from .compat import (36 cookielib, urlparse, urlunparse, urljoin, urlsplit, urlencode, str, bytes,37 StringIO, is_py2, chardet, json, builtin_str, numeric_types)37 StringIO, is_py2, chardet, json, builtin_str)3839REDIRECT_STATI = (codes.moved, codes.found, codes.other, codes.temporary_moved)40CONTENT_CHUNK_SIZE = 10 * 102441
843 unmodified lines
modified:requests/sessions.py+18 added-29 removed
13 unmodified lines
14from .cookies import cookiejar_from_dict, remove_cookie_by_name15from .defaults import defaults16from .models import Request17from .hooks import dispatch_hook18from .utils import header_expand, to_key_val_list18from .utils import header_expand, from_key_val_list19from .packages.urllib3.poolmanager import PoolManager202122def merge_kwargs(local_kwarg, default_kwarg):
10 unmodified lines
3334 if local_kwarg is None:35 return default_kwarg3637 kwargs = default_kwarg38 # If default_kwargs is a list rather than a dictionary attempt to convert39 # to dictionary. If the check fails, return local_kwargs.40 if isinstance(default_kwarg, list):41 try:42 kwargs = dict(kwargs)43 except ValueError:44 return local_kwarg4537 # Bypass if not a dictionary (e.g. timeout)47 if not hasattr(kwargs, 'items'):38 if not hasattr(default_kwarg, 'items'):39 return local_kwarg4041 default_kwarg = from_key_val_list(default_kwarg)50 local_kwarg = to_key_val_list(local_kwarg)42 local_kwarg = from_key_val_list(local_kwarg)4344 # Update new values.53 kwargs = kwargs.copy()45 kwargs = default_kwarg.copy()46 kwargs.update(local_kwarg)4748 # Remove keys that are set to None.57 for (k, v) in local_kwarg:49 for (k, v) in local_kwarg.items():50 if v is None:51 del kwargs[k]5253 return kwargs
18 unmodified lines
72 prefetch=True,73 verify=True,74 cert=None):7584 #self.headers = to_key_val_list(headers or [])85 self.headers = headers or {}76 self.headers = from_key_val_list(headers or [])77 self.auth = auth78 self.timeout = timeout88 self.proxies = to_key_val_list(proxies or [])89 self.hooks = hooks or {}90 self.params = to_key_val_list(params or [])91 self.config = config or {}79 self.proxies = from_key_val_list(proxies or [])80 self.hooks = from_key_val_list(hooks or {})81 self.params = from_key_val_list(params or [])82 self.config = from_key_val_list(config or {})83 self.prefetch = prefetch84 self.verify = verify85 self.cert = cert86
74 unmodified lines
161 # Default empty dicts for dict params.162 data = [] if data is None else data163 files = [] if files is None else files164 headers = {} if headers is None else headers174 params = [] if params is None else params165 params = {} if params is None else params166 hooks = {} if hooks is None else hooks167 prefetch = prefetch if prefetch is not None else self.prefetch168169 # use session's hooks as defaults
1 unmodified line
171 hooks.setdefault(key, cb)172173 # Expand header values.174 if headers:184 #e = [(k, header_expand(v)) for k, v in to_key_val_list(headers)]185 #headers = e186 for k, v in list(headers.items()) or {}:175 for k, v in list(headers.items() or {}):176 headers[k] = header_expand(v)177178 args = dict(179 method=method,180 url=url,181 data=data,193 params=params,194 headers=headers,182 params=from_key_val_list(params),183 headers=from_key_val_list(headers),184 cookies=cookies,185 files=files,186 auth=auth,198 hooks=hooks,187 hooks=from_key_val_list(hooks),188 timeout=timeout,189 allow_redirects=allow_redirects,201 proxies=to_key_val_list(proxies),202 config=config,190 proxies=from_key_val_list(proxies),191 config=from_key_val_list(config),192 prefetch=prefetch,193 verify=verify,194 cert=cert,195 _poolmanager=self.poolmanager
133 unmodified lines
modified:requests/structures.py+1 added
6 unmodified lines
7Data structures that power Requests.89"""101112class CaseInsensitiveDict(dict):13 """Case-insensitive Dictionary1415 For example, ``headers['content-encoding']`` will return the
52 unmodified lines
modified:requests/utils.py+38 added-17 removed
18 unmodified lines
19from netrc import netrc, NetrcParseError2021from . import __version__22from .compat import parse_http_list as _parse_list_header23from .compat import quote, urlparse, basestring, bytes, str23from .compat import quote, urlparse, basestring, bytes, str, OrderedDict24from .cookies import RequestsCookieJar, cookiejar_from_dict2526_hush_pyflakes = (RequestsCookieJar,)27
85 unmodified lines
113 if name and name[0] != '<' and name[-1] != '>':114 return name115116117def from_key_val_list(value):118 """Take an object and test to see if it can be represented as a119 dictionary. Unless it can not be represented as such, return an120 OrderedDict, e.g.,121122 ::123124 >>> from_key_val_list([('key', 'val')])125 OrderedDict([('key', 'val')])126 >>> from_key_val_list('string')127 ValueError: need more than 1 value to unpack128 >>> from_key_val_list({'key': 'val'})129 OrderedDict([('key', 'val')])130 """131 if value is None:132 return None133134 if isinstance(value, (str, bytes, bool, int)):135 raise ValueError('cannot encode objects that are not 2-tuples')136137 return OrderedDict(value)138139140def to_key_val_list(value):141 """Take an object and test to see if it can be represented as a119 dictionary. Unless it can not be represented as such, return a list of120 tuples, e.g.,:142 dictionary. If it can be, return a list of tuples, e.g.,143122 >>> to_key_val_list([('key', 'val')])123 [('key', 'val')]124 >>> to_key_val_list('string')125 ValueError: ...126 >>> to_key_val_list({'key': 'val'})127 [('key', 'val')]144 ::145146 >>> to_key_val_list([('key', 'val')])147 [('key', 'val')]148 >>> to_key_val_list({'key': 'val'})149 [('key', 'val')]150 >>> to_key_val_list('string')151 ValueError: cannot encode objects that are not 2-tuples.152 """153 if value is None:154 return None155132 try:133 dict(value)134 except ValueError:135 raise ValueError('Unable to encode lists with elements that are not '136 '2-tuples.')156 if isinstance(value, (str, bytes, bool, int)):157 raise ValueError('cannot encode objects that are not 2-tuples')158138 if isinstance(value, dict) or hasattr(value, 'items'):159 if isinstance(value, dict):160 value = value.items()161162 return list(value)163
387 unmodified lines
551552 i.e. Link: <http:/.../front.jpeg>; rel=front; type="image/jpeg",<http://.../back.jpeg>; rel=back;type="image/jpeg"553554 """534 555556 links = []557558 replace_chars = " '\""559
11 unmodified lines
571 try:572 key,value = param.split("=")573 except ValueError:574 break554 575576 link[key.strip(replace_chars)] = value.strip(replace_chars)577578 links.append(link)579
1 unmodified line