Uniformize UTF-8 naming (#1115)

* Uniformize UTF-8 naming

Replace `utf8` -> `utf-8` everywhere.
It should have no impact, `utf8` is an alias of `utf-8` [1].

[1] https://github.com/python/cpython/blob/ee03bad25e83b00ba5fc2a0265b48c6286e6b3f7/Lib/encodings/aliases.py#L534

* Always specify the encoding

Let's be explicit over implicit. And prevent future warnings from PEP-597 [1].

[1] https://www.python.org/dev/peps/pep-0597/#using-the-default-encoding-is-a-common-mistake

* Update `UTF8` constant (`utf-8` -> `utf_8`)

* Remove default argument from `str.encode()` and `bytes.decode()`

* Clean-up
This commit is contained in:
Mickaël Schoentgen authored and GitHub committed 2021-08-05 20:58:43 +02:00
1 parent 11399dde76
commit c6cbc7dfa5
21 files changed
+54 -42

No files matched your search

+1 -1
View File
@@ -301,7 +301,7 @@ class HTTPieArgumentParser(argparse.ArgumentParser):
"""
self._ensure_one_data_source(self.has_stdin_data, self.args.data,
self.args.files)
self.args.data = data.encode('utf-8')
self.args.data = data.encode()
def _ensure_one_data_source(self, *other_sources):
"""There can only be one source of input request data.
+1 -1
View File
@@ -144,7 +144,7 @@ def load_text_file(item: KeyValueArg) -> str:
except UnicodeDecodeError:
raise ParseError(
f'{item.orig!r}: cannot embed the content of {item.value!r},'
' not a UTF8 or ASCII-encoded text file'
' not a UTF-8 or ASCII-encoded text file'
)
+2 -2
View File
@@ -24,7 +24,7 @@ from .utils import get_expired_cookies, repr_dict
urllib3.disable_warnings()
FORM_CONTENT_TYPE = 'application/x-www-form-urlencoded; charset=utf-8'
FORM_CONTENT_TYPE = 'application/x-www-form-urlencoded; charset=UTF-8'
JSON_CONTENT_TYPE = 'application/json'
JSON_ACCEPT = f'{JSON_CONTENT_TYPE}, */*;q=0.5'
DEFAULT_UA = f'HTTPie/{__version__}'
@@ -188,7 +188,7 @@ def finalize_headers(headers: RequestHeadersDict) -> RequestHeadersDict:
value = value.strip()
if isinstance(value, str):
# See <https://github.com/httpie/httpie/issues/212>
value = value.encode('utf8')
value = value.encode()
final_headers[name] = value
return final_headers
+3 -2
View File
@@ -5,6 +5,7 @@ from typing import Union
from . import __version__
from .compat import is_windows
from .constants import UTF8
ENV_XDG_CONFIG_HOME = 'XDG_CONFIG_HOME'
@@ -79,7 +80,7 @@ class BaseConfigDict(dict):
def load(self):
config_type = type(self).__name__.lower()
try:
with self.path.open() as f:
with self.path.open(encoding=UTF8) as f:
try:
data = json.load(f)
except ValueError as e:
@@ -111,7 +112,7 @@ class BaseConfigDict(dict):
ensure_ascii=True,
)
try:
self.path.write_text(json_string + '\n')
self.path.write_text(json_string + '\n', encoding=UTF8)
except OSError:
if not fail_silently:
raise
+2
View File
@@ -0,0 +1,2 @@
# UTF-8 encoding name
UTF8 = 'utf-8'
+4 -3
View File
@@ -11,6 +11,7 @@ except ImportError:
from .compat import is_windows
from .config import DEFAULT_CONFIG_DIR, Config, ConfigFileError
from .constants import UTF8
from .utils import repr_dict
@@ -70,10 +71,10 @@ class Environment:
self._orig_stderr = self.stderr
self._devnull = devnull
# Keyword arguments > stream.encoding > default utf8
# Keyword arguments > stream.encoding > default UTF-8
if self.stdin and self.stdin_encoding is None:
self.stdin_encoding = getattr(
self.stdin, 'encoding', None) or 'utf8'
self.stdin, 'encoding', None) or UTF8
if self.stdout_encoding is None:
actual_stdout = self.stdout
if is_windows:
@@ -83,7 +84,7 @@ class Environment:
# noinspection PyUnresolvedReferences
actual_stdout = self.stdout.wrapped
self.stdout_encoding = getattr(
actual_stdout, 'encoding', None) or 'utf8'
actual_stdout, 'encoding', None) or UTF8
def __str__(self):
defaults = dict(type(self).__dict__)
+6 -5
View File
@@ -2,6 +2,7 @@ from abc import ABCMeta, abstractmethod
from typing import Iterable, Optional
from urllib.parse import urlsplit
from .constants import UTF8
from .utils import split_cookies
@@ -39,7 +40,7 @@ class HTTPMessage(metaclass=ABCMeta):
"""Return the message content type."""
ct = self._orig.headers.get('Content-Type', '')
if not isinstance(ct, str):
ct = ct.decode('utf8')
ct = ct.decode()
return ct
@@ -83,7 +84,7 @@ class HTTPResponse(HTTPMessage):
@property
def encoding(self):
return self._orig.encoding or 'utf8'
return self._orig.encoding or UTF8
@property
def body(self):
@@ -116,7 +117,7 @@ class HTTPRequest(HTTPMessage):
headers['Host'] = url.netloc.split('@')[-1]
headers = [
f'{name}: {value if isinstance(value, str) else value.decode("utf-8")}'
f'{name}: {value if isinstance(value, str) else value.decode()}'
for name, value in headers.items()
]
@@ -126,12 +127,12 @@ class HTTPRequest(HTTPMessage):
@property
def encoding(self):
return 'utf8'
return UTF8
@property
def body(self):
body = self._orig.body
if isinstance(body, str):
# Happens with JSON/form request data parsed from the command line.
body = body.encode('utf8')
body = body.encode()
return body or b''
+4 -3
View File
@@ -3,6 +3,7 @@ from itertools import chain
from typing import Callable, Iterable, Union
from ..context import Environment
from ..constants import UTF8
from ..models import HTTPMessage
from .processing import Conversion, Formatting
@@ -49,7 +50,7 @@ class BaseStream(metaclass=ABCMeta):
def get_headers(self) -> bytes:
"""Return the headers' bytes."""
return self.msg.headers.encode('utf8')
return self.msg.headers.encode()
@abstractmethod
def iter_body(self) -> Iterable[bytes]:
@@ -105,8 +106,8 @@ class EncodedStream(BaseStream):
else:
# Preserve the message encoding.
output_encoding = self.msg.encoding
# Default to utf8 when unsure.
self.output_encoding = output_encoding or 'utf8'
# Default to UTF-8 when unsure.
self.output_encoding = output_encoding or UTF8
def iter_body(self) -> Iterable[bytes]:
for line, lf in self.msg.iter_lines(self.CHUNK_SIZE):
+1 -1
View File
@@ -30,7 +30,7 @@ class HTTPBasicAuth(requests.auth.HTTPBasicAuth):
@staticmethod
def make_header(username: str, password: str) -> str:
credentials = f'{username}:{password}'
token = b64encode(credentials.encode('utf-8')).strip().decode('latin1')
token = b64encode(credentials.encode()).strip().decode('latin1')
return f'Basic {token}'
+1 -1
View File
@@ -78,7 +78,7 @@ class Session(BaseConfigDict):
continue # Ignore explicitly unset headers
if type(value) is not str:
value = value.decode('utf8')
value = value.decode()
if name.lower() == 'user-agent' and value.startswith('HTTPie/'):
continue