python: Remove default "utf8" argument for encode(), decode().

Partially generated by pyupgrade.

Signed-off-by: Anders Kaseorg <anders@zulip.com>
This commit is contained in:
Anders Kaseorg 2021-08-02 14:20:39 -07:00 committed by Tim Abbott
parent 1760897a8c
commit ad5f0c05b5
32 changed files with 54 additions and 56 deletions

View File

@ -411,13 +411,11 @@ class StripeTestCase(ZulipTestCase):
self.next_year = datetime(2013, 1, 2, 3, 4, 5, tzinfo=timezone.utc)
def get_signed_seat_count_from_response(self, response: HttpResponse) -> Optional[str]:
match = re.search(
r"name=\"signed_seat_count\" value=\"(.+)\"", response.content.decode("utf-8")
)
match = re.search(r"name=\"signed_seat_count\" value=\"(.+)\"", response.content.decode())
return match.group(1) if match else None
def get_salt_from_response(self, response: HttpResponse) -> Optional[str]:
match = re.search(r"name=\"salt\" value=\"(\w+)\"", response.content.decode("utf-8"))
match = re.search(r"name=\"salt\" value=\"(\w+)\"", response.content.decode())
return match.group(1) if match else None
def upgrade(

View File

@ -156,7 +156,7 @@ def send_email_mirror(
"msg_base64": base64.b64encode(msg_bytes).decode(),
"secret": shared_secret,
}
req = Request(url=urljoin(host, url), data=urlencode(data).encode("utf8"))
req = Request(url=urljoin(host, url), data=urlencode(data).encode())
try:
urlopen(req, context=context)
except HTTPError as err:

View File

@ -40,7 +40,7 @@ def python_version() -> str:
def hash_deps(deps: Iterable[str]) -> str:
deps_str = "\n".join(deps) + "\n" + python_version()
return hashlib.sha1(deps_str.encode("utf-8")).hexdigest()
return hashlib.sha1(deps_str.encode()).hexdigest()
def main() -> int:

View File

@ -48,7 +48,7 @@ def generate_sha1sum_node_modules(
data["yarn-args"] = get_yarn_args(production=production)
sha1sum = hashlib.sha1()
sha1sum.update(json.dumps(data, sort_keys=True).encode("utf-8"))
sha1sum.update(json.dumps(data, sort_keys=True).encode())
return sha1sum.hexdigest()

View File

@ -27,7 +27,7 @@ def generate_sha1sum_puppet_modules() -> str:
).strip()
sha1sum = hashlib.sha1()
sha1sum.update(json.dumps(data, sort_keys=True).encode("utf-8"))
sha1sum.update(json.dumps(data, sort_keys=True).encode())
return sha1sum.hexdigest()

View File

@ -451,7 +451,7 @@ def files_and_string_digest(filenames: Sequence[str], extra_strings: Sequence[st
sha1sum.update(file_to_hash.read())
for extra_string in extra_strings:
sha1sum.update(extra_string.encode("utf-8"))
sha1sum.update(extra_string.encode())
return sha1sum.hexdigest()

View File

@ -36,7 +36,7 @@ for tag in [t["name"] for t in resp.json()]:
f"https://raw.githubusercontent.com/zulip/zulip/{tag}/zproject/prod_settings_template.py",
)
if resp.status_code == 200:
templ[tag] = resp.content.decode("utf-8")
templ[tag] = resp.content.decode()
else:
print("Failure: ")
print(resp)

View File

@ -177,7 +177,7 @@ def send_bot_payload_message(
headers = get_requests_headers(integration.name, fixture_name)
headers.update(config.custom_headers)
if config.use_basic_auth:
credentials = base64.b64encode(f"{bot.email}:{bot.api_key}".encode("utf8")).decode("utf8")
credentials = base64.b64encode(f"{bot.email}:{bot.api_key}".encode()).decode()
auth = f"basic {credentials}"
headers.update(dict(Authorization=auth))

View File

@ -376,7 +376,7 @@ def main(options: argparse.Namespace) -> "NoReturn":
sha_sum = hashlib.sha1()
for apt_depedency in SYSTEM_DEPENDENCIES:
sha_sum.update(apt_depedency.encode("utf8"))
sha_sum.update(apt_depedency.encode())
if "debian" in os_families():
with open("scripts/lib/setup-apt-repo", "rb") as fb:
sha_sum.update(fb.read())

View File

@ -227,7 +227,7 @@ def generate_huddle_name(huddle_members: List[str]) -> str:
# lifetime of export tool run, as it doesn't appear in the output.
import hashlib
return hashlib.md5("".join(sorted(huddle_members)).encode("utf-8")).hexdigest()
return hashlib.md5("".join(sorted(huddle_members)).encode()).hexdigest()
def convert_huddle_data(

View File

@ -655,7 +655,7 @@ def authenticated_rest_api_view(
# case insensitive per RFC 1945
if auth_type.lower() != "basic":
raise JsonableError(_("This endpoint requires HTTP basic authentication."))
role, api_key = base64.b64decode(credentials).decode("utf-8").split(":")
role, api_key = base64.b64decode(credentials).decode().split(":")
except ValueError:
return json_unauthorized(_("Invalid authorization header for basic auth"))
except KeyError:

View File

@ -1286,7 +1286,7 @@ def do_deactivate_stream(
# Prepend a substring of the hashed stream ID to the new stream name
streamID = str(stream.id)
stream_id_hash_object = hashlib.sha512(streamID.encode("utf-8"))
stream_id_hash_object = hashlib.sha512(streamID.encode())
hashed_stream_id = stream_id_hash_object.hexdigest()[0:7]
new_name = (hashed_stream_id + "!DEACTIVATED:" + old_name)[: Stream.MAX_NAME_LENGTH]

View File

@ -120,7 +120,7 @@ def bounce_key_prefix_for_testing(test_name: str) -> None:
KEY_PREFIX = test_name + ":" + str(os.getpid()) + ":"
# We are taking the hash of the KEY_PREFIX to decrease the size of the key.
# Memcached keys should have a length of less than 250.
KEY_PREFIX = hashlib.sha1(KEY_PREFIX.encode("utf-8")).hexdigest() + ":"
KEY_PREFIX = hashlib.sha1(KEY_PREFIX.encode()).hexdigest() + ":"
def get_cache_backend(cache_name: Optional[str]) -> BaseCache:

View File

@ -5,8 +5,8 @@ from django.conf import settings
def generate_camo_url(url: str) -> str:
encoded_url = url.encode("utf-8")
encoded_camo_key = settings.CAMO_KEY.encode("utf-8")
encoded_url = url.encode()
encoded_camo_key = settings.CAMO_KEY.encode()
digest = hmac.new(encoded_camo_key, encoded_url, hashlib.sha1).hexdigest()
return "{}/{}".format(digest, encoded_url.hex())

View File

@ -91,7 +91,7 @@ def der_encode_uint32(val: int) -> bytes:
def der_encode_string(val: str) -> bytes:
if not isinstance(val, str):
raise TypeError("unicode")
return der_encode_tlv(0x1B, val.encode("utf-8"))
return der_encode_tlv(0x1B, val.encode())
def der_encode_octet_string(val: bytes) -> bytes:

View File

@ -10,9 +10,9 @@ def initial_password(email: str) -> Optional[str]:
created by populate_db."""
if settings.INITIAL_PASSWORD_SALT is not None:
encoded_key = (settings.INITIAL_PASSWORD_SALT + email).encode("utf-8")
encoded_key = (settings.INITIAL_PASSWORD_SALT + email).encode()
digest = hashlib.sha256(encoded_key).digest()
return base64.b64encode(digest)[:16].decode("utf-8")
return base64.b64encode(digest)[:16].decode()
else:
# None as a password for a user tells Django to set an unusable password
return None

View File

@ -487,7 +487,7 @@ def send_custom_email(users: List[UserProfile], options: Dict[str, Any]) -> None
with open(options["markdown_template_path"]) as f:
text = f.read()
parsed_email_template = Parser(policy=default).parsestr(text)
email_template_hash = hashlib.sha256(text.encode("utf-8")).hexdigest()[0:32]
email_template_hash = hashlib.sha256(text.encode()).hexdigest()[0:32]
email_filename = f"custom/custom_email_{email_template_hash}.source.html"
email_id = f"zerver/emails/custom/custom_email_{email_template_hash}"

View File

@ -718,7 +718,7 @@ Output:
identifier: Can be an email or a remote server uuid.
"""
credentials = f"{identifier}:{api_key}"
return "Basic " + base64.b64encode(credentials.encode("utf-8")).decode("utf-8")
return "Basic " + base64.b64encode(credentials.encode()).decode()
def uuid_get(self, identifier: str, *args: Any, **kwargs: Any) -> HttpResponse:
kwargs["HTTP_AUTHORIZATION"] = self.encode_uuid(identifier)
@ -904,17 +904,17 @@ Output:
self.assertIn(msg_substring, self.get_json_error(result, status_code=status_code))
def assert_in_response(self, substring: str, response: HttpResponse) -> None:
self.assertIn(substring, response.content.decode("utf-8"))
self.assertIn(substring, response.content.decode())
def assert_in_success_response(self, substrings: List[str], response: HttpResponse) -> None:
self.assertEqual(response.status_code, 200)
decoded = response.content.decode("utf-8")
decoded = response.content.decode()
for substring in substrings:
self.assertIn(substring, decoded)
def assert_not_in_success_response(self, substrings: List[str], response: HttpResponse) -> None:
self.assertEqual(response.status_code, 200)
decoded = response.content.decode("utf-8")
decoded = response.content.decode()
for substring in substrings:
self.assertNotIn(substring, decoded)

View File

@ -162,7 +162,7 @@ def queries_captured(
if include_savepoints or not isinstance(sql, str) or "SAVEPOINT" not in sql:
queries.append(
{
"sql": self.mogrify(sql, params).decode("utf-8"),
"sql": self.mogrify(sql, params).decode(),
"time": f"{duration:.3f}",
}
)

View File

@ -767,7 +767,7 @@ LOCAL_FILE_ACCESS_TOKEN_SALT = "local_file_"
def generate_unauthed_file_access_url(path_id: str) -> str:
signed_data = TimestampSigner(salt=LOCAL_FILE_ACCESS_TOKEN_SALT).sign(path_id)
token = base64.b16encode(signed_data.encode("utf-8")).decode("utf-8")
token = base64.b16encode(signed_data.encode()).decode()
filename = path_id.split("/")[-1]
return reverse("local_file_unauthed", args=[token, filename])
@ -776,7 +776,7 @@ def generate_unauthed_file_access_url(path_id: str) -> str:
def get_local_file_path_id_from_token(token: str) -> Optional[str]:
signer = TimestampSigner(salt=LOCAL_FILE_ACCESS_TOKEN_SALT)
try:
signed_data = base64.b16decode(token).decode("utf-8")
signed_data = base64.b16decode(token).decode()
path_id = signer.unsign(signed_data, max_age=timedelta(seconds=60))
except (BadSignature, binascii.Error):
return None

View File

@ -92,7 +92,7 @@ def make_safe_digest(string: str, hash_func: Callable[[bytes], Any] = hashlib.sh
"""
# hashlib.sha1, md5, etc. expect bytes, so non-ASCII strings must
# be encoded.
return hash_func(string.encode("utf-8")).hexdigest()
return hash_func(string.encode()).hexdigest()
def log_statsd_event(name: str) -> None:

View File

@ -606,8 +606,8 @@ def alter_content(request: HttpRequest, content: bytes) -> bytes:
).placeholder_open_graph_description
assert placeholder_open_graph_description is not None
return content.replace(
placeholder_open_graph_description.encode("utf-8"),
first_paragraph_text.encode("utf-8"),
placeholder_open_graph_description.encode(),
first_paragraph_text.encode(),
)

View File

@ -39,7 +39,7 @@ def move_missed_message_addresses_to_database(
redis_client.delete(key)
continue
topic_name = subject_b.decode("utf-8")
topic_name = subject_b.decode()
# The data model for missed-message emails has changed in two
# key ways: We're moving it from Redis to the database for

View File

@ -3995,7 +3995,7 @@ class GoogleAuthBackendTest(SocialAuthBase):
url = re.findall(
'action="(http://zulip.testserver/accounts/do_confirm[^"]*)"',
result.content.decode("utf-8"),
result.content.decode(),
)[0]
confirmation = Confirmation.objects.all().first()
assert confirmation is not None

View File

@ -538,11 +538,11 @@ class DecoratorLoggingTestCase(ZulipTestCase):
user_profile = self.example_user("hamlet")
api_key = get_api_key(user_profile)
credentials = f"{user_profile.email}:{api_key}"
api_auth = "Digest " + base64.b64encode(credentials.encode("utf-8")).decode("utf-8")
api_auth = "Digest " + base64.b64encode(credentials.encode()).decode()
result = self.client_post("/api/v1/external/zendesk", {}, HTTP_AUTHORIZATION=api_auth)
self.assert_json_error(result, "This endpoint requires HTTP basic authentication.")
api_auth = "Basic " + base64.b64encode(b"foo").decode("utf-8")
api_auth = "Basic " + base64.b64encode(b"foo").decode()
result = self.client_post("/api/v1/external/zendesk", {}, HTTP_AUTHORIZATION=api_auth)
self.assert_json_error(
result, "Invalid authorization header for basic auth", status_code=401

View File

@ -256,10 +256,10 @@ class DocPageTest(ZulipTestCase):
def test_electron_detection(self) -> None:
result = self.client_get("/accounts/password/reset/")
# TODO: Ideally, this Mozilla would be the specific browser.
self.assertTrue('data-platform="Mozilla"' in result.content.decode("utf-8"))
self.assertTrue('data-platform="Mozilla"' in result.content.decode())
result = self.client_get("/accounts/password/reset/", HTTP_USER_AGENT="ZulipElectron/1.0.0")
self.assertTrue('data-platform="ZulipElectron"' in result.content.decode("utf-8"))
self.assertTrue('data-platform="ZulipElectron"' in result.content.decode())
class HelpTest(ZulipTestCase):
@ -539,7 +539,7 @@ class AppsPageTest(ZulipTestCase):
with self.settings(ZILENCER_ENABLED=True):
result = self.client_get("/apps/")
self.assertEqual(result.status_code, 200)
html = result.content.decode("utf-8")
html = result.content.decode()
self.assertIn("Apps for every platform.", html)
def test_app_download_link_view(self) -> None:

View File

@ -240,7 +240,7 @@ class HomeTest(ZulipTestCase):
self.assert_length(queries, 43)
self.assert_length(cache_mock.call_args_list, 5)
html = result.content.decode("utf-8")
html = result.content.decode()
for html_bit in html_bits:
if html_bit not in html:
@ -351,7 +351,7 @@ class HomeTest(ZulipTestCase):
self.assert_length(queries2, 38)
# Do a sanity check that our new streams were in the payload.
html = result.content.decode("utf-8")
html = result.content.decode()
self.assertIn("test_stream_7", html)
def _get_home_page(self, **kwargs: Any) -> HttpResponse:
@ -366,7 +366,7 @@ class HomeTest(ZulipTestCase):
Use this for tests that are geared toward specific edge cases, but
which still want the home page to load properly.
"""
html = result.content.decode("utf-8")
html = result.content.decode()
if "start a conversation" not in html:
raise AssertionError("Home page probably did not load.")
@ -382,7 +382,7 @@ class HomeTest(ZulipTestCase):
result = self.client_get("/", dict(stream="Denmark"))
html = result.content.decode("utf-8")
html = result.content.decode()
self.assertIn("Accept the new Terms of Service", html)
def test_banned_desktop_app_versions(self) -> None:
@ -390,7 +390,7 @@ class HomeTest(ZulipTestCase):
self.login_user(user)
result = self.client_get("/", HTTP_USER_AGENT="ZulipElectron/2.3.82")
html = result.content.decode("utf-8")
html = result.content.decode()
self.assertIn("You are using old version of the Zulip desktop", html)
def test_unsupported_browser(self) -> None:
@ -405,7 +405,7 @@ class HomeTest(ZulipTestCase):
]
for user_agent in unsupported_user_agents:
result = self.client_get("/", HTTP_USER_AGENT=user_agent)
html = result.content.decode("utf-8")
html = result.content.decode()
self.assertIn("Internet Explorer is not supported by Zulip.", html)
def test_terms_of_service_first_time_template(self) -> None:
@ -445,7 +445,7 @@ class HomeTest(ZulipTestCase):
self.login("hamlet")
result = self._get_home_page(stream="Denmark", topic="lunch")
self._sanity_check(result)
html = result.content.decode("utf-8")
html = result.content.decode()
self.assertIn("lunch", html)
self.assertEqual(
set(result["Cache-Control"].split(", ")), {"must-revalidate", "no-store", "no-cache"}

View File

@ -241,7 +241,7 @@ class PushBouncerNotificationTest(BouncerTestCase):
del self.API_KEYS[self.server_uuid]
credentials = "{}:{}".format("5678-efgh", "invalid")
api_auth = "Basic " + base64.b64encode(credentials.encode("utf-8")).decode("utf-8")
api_auth = "Basic " + base64.b64encode(credentials.encode()).decode()
result = self.client_post(
endpoint,
{"user_id": user_id, "token_kind": token_kind, "token": token},

View File

@ -5114,7 +5114,7 @@ class TestLoginPage(ZulipTestCase):
class TestFindMyTeam(ZulipTestCase):
def test_template(self) -> None:
result = self.client_get("/accounts/find/")
self.assertIn("Find your Zulip accounts", result.content.decode("utf8"))
self.assertIn("Find your Zulip accounts", result.content.decode())
def test_result(self) -> None:
# We capitalize a letter in cordelia's email to test that the search is case-insensitive.
@ -5126,7 +5126,7 @@ class TestFindMyTeam(ZulipTestCase):
result.url, "/accounts/find/?emails=iago%40zulip.com%2CcordeliA%40zulip.com"
)
result = self.client_get(result.url)
content = result.content.decode("utf8")
content = result.content.decode()
self.assertIn("Emails sent! You will only receive emails", content)
self.assertIn("iago@zulip.com", content)
self.assertIn("cordeliA@zulip.com", content)
@ -5144,7 +5144,7 @@ class TestFindMyTeam(ZulipTestCase):
result.url, "/accounts/find/?emails=iago%40zulip.com%2Cinvalid_email%40zulip.com"
)
result = self.client_get(result.url)
content = result.content.decode("utf8")
content = result.content.decode()
self.assertIn("Emails sent! You will only receive emails", content)
self.assertIn(self.example_email("iago"), content)
self.assertIn("invalid_email@", content)
@ -5167,7 +5167,7 @@ class TestFindMyTeam(ZulipTestCase):
def test_find_team_zero_emails(self) -> None:
data = {"emails": ""}
result = self.client_post("/accounts/find/", data)
self.assertIn("This field is required", result.content.decode("utf8"))
self.assertIn("This field is required", result.content.decode())
self.assertEqual(result.status_code, 200)
from django.core.mail import outbox
@ -5215,7 +5215,7 @@ class TestFindMyTeam(ZulipTestCase):
data = {"emails": ",".join(f"hamlet-{i}@zulip.com" for i in range(11))}
result = self.client_post("/accounts/find/", data)
self.assertEqual(result.status_code, 200)
self.assertIn("Please enter at most 10", result.content.decode("utf8"))
self.assertIn("Please enter at most 10", result.content.decode())
from django.core.mail import outbox
self.assert_length(outbox, 0)

View File

@ -1395,7 +1395,7 @@ class StreamAdminTest(ZulipTestCase):
# A deleted stream's name is changed, is deactivated, is invite-only,
# and has no subscribers.
hashed_stream_id = hashlib.sha512(str(stream_id).encode("utf-8")).hexdigest()[0:7]
hashed_stream_id = hashlib.sha512(str(stream_id).encode()).hexdigest()[0:7]
deactivated_stream_name = hashed_stream_id + "!DEACTIVATED:" + active_name
deactivated_stream = get_stream(deactivated_stream_name, realm)
self.assertTrue(deactivated_stream.deactivated)

View File

@ -65,7 +65,7 @@ def webathena_kerberos_login(
"/home/zulip/python-zulip-api/zulip/integrations/zephyr/process_ccache",
user,
api_key,
base64.b64encode(ccache).decode("utf-8"),
base64.b64encode(ccache).decode(),
]
subprocess.check_call(
["ssh", settings.PERSONAL_ZMIRROR_SERVER, "--", " ".join(map(shlex.quote, command))]

View File

@ -62,10 +62,10 @@ def beanstalk_decoder(view_func: ViewFuncT) -> ViewFuncT:
encoded_value: str
auth_type, encoded_value = request.META["HTTP_AUTHORIZATION"].split()
if auth_type.lower() == "basic":
email, api_key = base64.b64decode(encoded_value).decode("utf-8").split(":")
email, api_key = base64.b64decode(encoded_value).decode().split(":")
email = email.replace("%40", "@")
credentials = f"{email}:{api_key}"
encoded_credentials: str = base64.b64encode(credentials.encode("utf-8")).decode("utf8")
encoded_credentials: str = base64.b64encode(credentials.encode()).decode()
request.META["HTTP_AUTHORIZATION"] = "Basic " + encoded_credentials
return view_func(request, *args, **kwargs)