python: Skip unnecessary decode before JSON parsing.

Signed-off-by: Anders Kaseorg <anders@zulip.com>
This commit is contained in:
Anders Kaseorg 2020-10-29 17:18:43 -07:00 committed by Tim Abbott
parent 1802a50cc9
commit 86e8d81c7f
6 changed files with 13 additions and 17 deletions

View File

@ -81,7 +81,7 @@ MAX_ALLOWED_PAYLOAD = 25 * 1024 * 1024
def process_response_error(e: HTTPError) -> None:
if e.code == 400:
response_content = e.read()
response_data = json.loads(response_content.decode('utf8'))
response_data = json.loads(response_content)
print(response_data['msg'])
exit(posix.EX_NOUSER)
else:

View File

@ -1574,7 +1574,7 @@ class BotTest(ZulipTestCase, UploadSerializeMixin):
response = self.client_post("/json/bots", bot_metadata)
self.assertEqual(response.status_code, 400)
expected_error_message = 'Invalid stripe_api_key value _invalid_key (stripe_api_key starts with a "_" and is hence invalid.)'
self.assertEqual(orjson.loads(response.content.decode('utf-8'))["msg"], expected_error_message)
self.assertEqual(orjson.loads(response.content)["msg"], expected_error_message)
with self.assertRaises(UserProfile.DoesNotExist):
UserProfile.objects.get(full_name="My Stripe Bot")
@ -1590,7 +1590,7 @@ class BotTest(ZulipTestCase, UploadSerializeMixin):
response = self.client_post("/json/bots", bot_metadata)
self.assertEqual(response.status_code, 400)
expected_error_message = "Missing configuration parameters: {'stripe_api_key'}"
self.assertEqual(orjson.loads(response.content.decode('utf-8'))["msg"], expected_error_message)
self.assertEqual(orjson.loads(response.content)["msg"], expected_error_message)
with self.assertRaises(UserProfile.DoesNotExist):
UserProfile.objects.get(full_name="My Stripe Bot")
@ -1619,6 +1619,6 @@ class BotTest(ZulipTestCase, UploadSerializeMixin):
response = self.client_post("/json/bots", bot_metadata)
self.assertEqual(response.status_code, 400)
expected_error_message = "Invalid integration 'stripes'."
self.assertEqual(orjson.loads(response.content.decode('utf-8'))["msg"], expected_error_message)
self.assertEqual(orjson.loads(response.content)["msg"], expected_error_message)
with self.assertRaises(UserProfile.DoesNotExist):
UserProfile.objects.get(full_name="My Stripe Bot")

View File

@ -249,8 +249,7 @@ class EditMessageTest(ZulipTestCase):
messages_result = self.client_get("/json/messages",
{"anchor": msg_id_1, "num_before": 0, "num_after": 10})
self.assert_json_success(messages_result)
json_messages = orjson.loads(
messages_result.content.decode('utf-8'))
json_messages = orjson.loads(messages_result.content)
for msg in json_messages['messages']:
self.assertNotIn("edit_history", msg)
@ -271,8 +270,7 @@ class EditMessageTest(ZulipTestCase):
message_edit_history_1 = self.client_get(
"/json/messages/" + str(msg_id_1) + "/history")
json_response_1 = orjson.loads(
message_edit_history_1.content.decode('utf-8'))
json_response_1 = orjson.loads(message_edit_history_1.content)
message_history_1 = json_response_1['message_history']
# Check content of message after edit.
@ -307,8 +305,7 @@ class EditMessageTest(ZulipTestCase):
message_edit_history_2 = self.client_get(
"/json/messages/" + str(msg_id_2) + "/history")
json_response_2 = orjson.loads(
message_edit_history_2.content.decode('utf-8'))
json_response_2 = orjson.loads(message_edit_history_2.content)
message_history_2 = json_response_2['message_history']
self.assertEqual(message_history_2[0]['rendered_content'],
@ -342,8 +339,7 @@ class EditMessageTest(ZulipTestCase):
message_edit_history_1 = self.client_get(
"/json/messages/" + str(msg_id_1) + "/history")
json_response_1 = orjson.loads(
message_edit_history_1.content.decode('utf-8'))
json_response_1 = orjson.loads(message_edit_history_1.content)
message_history_1 = json_response_1['message_history']
# Check content of message after edit.
@ -469,7 +465,7 @@ class EditMessageTest(ZulipTestCase):
# correct filled-out fields
message_edit_history = self.client_get("/json/messages/" + str(msg_id) + "/history")
json_response = orjson.loads(message_edit_history.content.decode('utf-8'))
json_response = orjson.loads(message_edit_history.content)
# We reverse the message history view output so that the IDs line up with the above.
message_history = list(reversed(json_response['message_history']))

View File

@ -323,7 +323,7 @@ class MessagePOSTTest(ZulipTestCase):
"client": "test suite",
"to": othello.email})
self.assert_json_success(result)
message_id = orjson.loads(result.content.decode())['id']
message_id = orjson.loads(result.content)['id']
recent_conversations = get_recent_private_conversations(user_profile)
self.assertEqual(len(recent_conversations), 1)
@ -338,7 +338,7 @@ class MessagePOSTTest(ZulipTestCase):
"client": "test suite",
"to": user_profile.email})
self.assert_json_success(result)
self_message_id = orjson.loads(result.content.decode())['id']
self_message_id = orjson.loads(result.content)['id']
recent_conversations = get_recent_private_conversations(user_profile)
self.assertEqual(len(recent_conversations), 2)

View File

@ -306,7 +306,7 @@ class TestCreateStreams(ZulipTestCase):
"already_subscribed": {},
}
self.assertEqual(response.status_code, 200)
self.assertEqual(orjson.loads(response.content.decode()), expected_response)
self.assertEqual(orjson.loads(response.content), expected_response)
# 2 messages should be created, one in announce and one in the new stream itself.
self.assertEqual(final_message_count - initial_message_count, 2)

View File

@ -144,7 +144,7 @@ def make_zoom_video_call(request: HttpRequest, user: UserProfile) -> HttpRespons
@require_POST
@has_request_variables
def deauthorize_zoom_user(request: HttpRequest) -> HttpResponse:
data = json.loads(request.body.decode("utf-8"))
data = json.loads(request.body)
payload = data["payload"]
if payload["user_data_retention"] == "false":
requests.post(