Skip to content
Myr

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

3 files+23 added−21 removed

added:requests/_internal_utils.py+17 added
1from .compat import is_py2, builtin_str234def 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)1617 return out
modified:requests/cookies.py+2 added-2 removed
12 unmodified lines
13import time14import calendar15import collections1617from . import utils17from ._internal_utils import to_native_string18from .compat import cookielib, urlparse, urlunparse, Morsel1920try:21 import threading
34 unmodified lines
56 # 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 lines
modified:requests/utils.py+4 added-19 removed
18 unmodified lines
19import warnings2021from . import __version__22from . import certs23# noinspection PyUnresolvedReferences24from ._internal_utils import to_native_string25from .compat import parse_http_list as _parse_list_header24from .compat import (quote, urlparse, bytes, str, OrderedDict, unquote, is_py2,25 builtin_str, getproxies, proxy_bypass, urlunparse,26 basestring)26from .compat import (quote, urlparse, bytes, str, OrderedDict, unquote,27 getproxies, proxy_bypass, urlunparse, basestring)28from .cookies import RequestsCookieJar, cookiejar_from_dict29from .structures import CaseInsensitiveDict30from .exceptions import InvalidURL, InvalidHeader, FileModeWarning31
738 unmodified lines
770771 return auth772773773def 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)785786 return out787788774# 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