Skip to content

Commit

Permalink
Merge 1e8476b into 975e38a
Browse files Browse the repository at this point in the history
  • Loading branch information
catleeball committed Mar 18, 2019
2 parents 975e38a + 1e8476b commit 9b27027
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 4 deletions.
1 change: 1 addition & 0 deletions apitools/base/protorpclite/protojson.py
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,7 @@ def decode_message(self, message_type, encoded_message):
ValueError: If encoded_message is not valid JSON.
messages.ValidationError if merged message is not initialized.
"""
encoded_message = six.ensure_str(encoded_message)
if not encoded_message.strip():
return message_type()

Expand Down
4 changes: 2 additions & 2 deletions apitools/base/py/encoding_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -528,7 +528,7 @@ def _ProcessUnknownEnums(message, encoded_message):
"""
if not encoded_message:
return message
decoded_message = json.loads(encoded_message)
decoded_message = json.loads(six.ensure_str(encoded_message))
for field in message.all_fields():
if (isinstance(field, messages.EnumField) and
field.name in decoded_message and
Expand Down Expand Up @@ -556,7 +556,7 @@ def _ProcessUnknownMessages(message, encoded_message):
"""
if not encoded_message:
return message
decoded_message = json.loads(encoded_message)
decoded_message = json.loads(six.ensure_str(encoded_message))
message_fields = [x.name for x in message.all_fields()] + list(
message.all_unrecognized_fields())
missing_fields = [x for x in decoded_message.keys()
Expand Down
26 changes: 24 additions & 2 deletions apitools/base/py/transfer.py
Original file line number Diff line number Diff line change
Expand Up @@ -426,7 +426,10 @@ def __ProcessResponse(self, response):
raise exceptions.TransferRetryError(response.content)
if response.status_code in (http_client.OK,
http_client.PARTIAL_CONTENT):
self.stream.write(response.content)
try:
self.stream.write(six.ensure_binary(response.content))
except TypeError:
self.stream.write(six.ensure_text(response.content))
self.__progress += response.length
if response.info and 'content-encoding' in response.info:
# TODO(craigcitro): Handle the case where this changes over a
Expand Down Expand Up @@ -541,6 +544,25 @@ def StreamMedia(self, callback=None, finish_callback=None,
self._ExecuteCallback(finish_callback, response)


if six.PY3:
class MultipartBytesGenerator(email_generator.BytesGenerator):
"""Generates a bytes version of a Message object tree for multipart messages
This is a BytesGenerator that has been modified to not attempt line
termination character modification in the bytes payload. Known to work
with the compat32 policy only. It may work on others, but not tested.
The outfp object must accept bytes in its write method.
"""
def _handle_text(self, msg):
# If the string has surrogates the original source was bytes, so
# just write it back out.
if msg._payload is None:
return
self.write(msg._payload)

# Default body handler
_writeBody = _handle_text


class Upload(_Transfer):

"""Data for a single Upload.
Expand Down Expand Up @@ -793,7 +815,7 @@ def __ConfigureMultipartRequest(self, http_request):
# `> ` to `From ` lines.
fp = six.BytesIO()
if six.PY3:
generator_class = email_generator.BytesGenerator
generator_class = MultipartBytesGenerator
else:
generator_class = email_generator.Generator
g = generator_class(fp, mangle_from_=False)
Expand Down

0 comments on commit 9b27027

Please sign in to comment.