sijie closed pull request #2742: [functions][python] support both python 2 and 3 URL: https://github.com/apache/pulsar/pull/2742
This is a PR merged from a forked repository. As GitHub hides the original diff on merge, it is displayed below for the sake of provenance: As this is a foreign pull request (from a fork), the diff is supplied below (as it won't show otherwise due to GitHub magic): diff --git a/pulsar-functions/instance/src/main/python/python_instance.py b/pulsar-functions/instance/src/main/python/python_instance.py index 2aae5f64c6..54a7329265 100644 --- a/pulsar-functions/instance/src/main/python/python_instance.py +++ b/pulsar-functions/instance/src/main/python/python_instance.py @@ -52,6 +52,20 @@ InternalQuitMessage = namedtuple('InternalQuitMessage', 'quit') DEFAULT_SERIALIZER = "serde.IdentitySerDe" +PY3 = sys.version_info[0] >= 3 + +def base64ify(bytes_or_str): + if PY3 and isinstance(bytes_or_str, str): + input_bytes = bytes_or_str.encode('utf8') + else: + input_bytes = bytes_or_str + + output_bytes = base64.urlsafe_b64encode(input_bytes) + if PY3: + return output_bytes.decode('ascii') + else: + return output_bytes + # We keep track of the following metrics class Stats(object): def __init__(self): @@ -284,7 +298,7 @@ def process_result(self, output, msg): self.current_stats.nserialization_exceptions += 1 self.total_stats.nserialization_exceptions += 1 if output_bytes is not None: - props = {"__pfn_input_topic__" : str(msg.topic), "__pfn_input_msg_id__" : base64.b64encode(msg.message.message_id().serialize())} + props = {"__pfn_input_topic__" : str(msg.topic), "__pfn_input_msg_id__" : base64ify(msg.message.message_id().serialize())} try: self.producer.send_async(output_bytes, partial(self.done_producing, msg.consumer, msg.message), properties=props) except Exception as e: ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: us...@infra.apache.org With regards, Apache Git Services