Move to_native_string to _internal_utils.py to avoid circular dependency
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Brian Bamsch · committed by Brian Bamsch · parent 2af0597 · Browse files · 3 files·+23 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
3 files+23 added−21 removed
- other:+23 added−21 removed
Provenance
- Commit
f002b73- Tree
15ad7c7- Author
Brian Bamsch <bbamsch32@gmail.com>- Committer
Brian Bamsch <bbamsch32@gmail.com>- Parent
2af0597
3 files changed+23 added−21 removed
added:requests/_internal_utils.py+17 added
1+from .compat import is_py2, builtin_str2+3+4+def to_native_string(string, encoding='ascii'):5+ """Given a string object, regardless of type, returns a representation of6+ that string in the native string type, encoding and decoding where7+ necessary. This assumes ASCII unless told otherwise.8+ """9+ if isinstance(string, builtin_str):10+ out = string11+ else:12+ if is_py2:13+ out = string.encode(encoding)14+ else:15+ out = string.decode(encoding)16+17+ return outmodified:requests/cookies.py+2 added-2 removed
12 unmodified lines13 import time14 import calendar15 import collections16 17-from . import utils17+from ._internal_utils import to_native_string18 from .compat import cookielib, urlparse, urlunparse, Morsel19 20 try:21 import threading34 unmodified lines56 # header57 if not self._r.headers.get('Host'):58 return self._r.url59 # If they did set it, retrieve it and reconstruct the expected domain60- host = utils.to_native_string(self._r.headers['Host'], encoding='utf-8')60+ host = to_native_string(self._r.headers['Host'], encoding='utf-8')61 parsed = urlparse(self._r.url)62 # Reconstruct the URL as we expect it63 return urlunparse([64 parsed.scheme, host, parsed.path, parsed.params, parsed.query,478 unmodified linesmodified:requests/utils.py+4 added-19 removed
18 unmodified lines19 import warnings20 21 from . import __version__22 from . import certs23+# noinspection PyUnresolvedReferences24+from ._internal_utils import to_native_string25 from .compat import parse_http_list as _parse_list_header24-from .compat import (quote, urlparse, bytes, str, OrderedDict, unquote, is_py2,25- builtin_str, getproxies, proxy_bypass, urlunparse,26- basestring)26+from .compat import (quote, urlparse, bytes, str, OrderedDict, unquote,27+ getproxies, proxy_bypass, urlunparse, basestring)28 from .cookies import RequestsCookieJar, cookiejar_from_dict29 from .structures import CaseInsensitiveDict30 from .exceptions import InvalidURL, InvalidHeader, FileModeWarning31 738 unmodified lines770 771 return auth772 773 773-def to_native_string(string, encoding='ascii'):774- """Given a string object, regardless of type, returns a representation of775- that string in the native string type, encoding and decoding where776- necessary. This assumes ASCII unless told otherwise.777- """778- if isinstance(string, builtin_str):779- out = string780- else:781- if is_py2:782- out = string.encode(encoding)783- else:784- out = string.decode(encoding)785-786- return out787-788-774 # Moved outside of function to avoid recompile every call775 _CLEAN_HEADER_REGEX_BYTE = re.compile(b'^\\S[^\\r\\n]*$|^$')776 _CLEAN_HEADER_REGEX_STR = re.compile(r'^\S[^\r\n]*$|^$')777 36 unmodified lines