[ 
https://issues.apache.org/jira/browse/BEAM-5270?focusedWorklogId=148686&page=com.atlassian.jira.plugin.system.issuetabpanels:worklog-tabpanel#worklog-148686
 ]

ASF GitHub Bot logged work on BEAM-5270:
----------------------------------------

                Author: ASF GitHub Bot
            Created on: 27/Sep/18 11:06
            Start Date: 27/Sep/18 11:06
    Worklog Time Spent: 10m 
      Work Description: robertwb closed pull request #6497: [BEAM-5270] Fix 
ToString coder to return bytes objects in Python 3.
URL: https://github.com/apache/beam/pull/6497
 
 
   

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/sdks/python/apache_beam/coders/coder_impl.py 
b/sdks/python/apache_beam/coders/coder_impl.py
index 6fd9b169ed6..eb6f9a1e510 100644
--- a/sdks/python/apache_beam/coders/coder_impl.py
+++ b/sdks/python/apache_beam/coders/coder_impl.py
@@ -197,6 +197,10 @@ def get_estimated_size_and_observables(self, value, 
nested=False):
 
     return self.estimate_size(value, nested), []
 
+  def __repr__(self):
+    return 'CallbackCoderImpl[encoder=%s, decoder=%s]' % (
+        self._encoder, self._decoder)
+
 
 class DeterministicFastPrimitivesCoderImpl(CoderImpl):
   """For internal use only; no backwards-compatibility guarantees."""
diff --git a/sdks/python/apache_beam/coders/coders.py 
b/sdks/python/apache_beam/coders/coders.py
index ad4edbbb374..f0ed6dcbeb9 100644
--- a/sdks/python/apache_beam/coders/coders.py
+++ b/sdks/python/apache_beam/coders/coders.py
@@ -22,6 +22,7 @@
 from __future__ import absolute_import
 
 import base64
+import sys
 from builtins import object
 
 import google.protobuf.wrappers_pb2
@@ -314,13 +315,17 @@ def is_deterministic(self):
 class ToStringCoder(Coder):
   """A default string coder used if no sink coder is specified."""
 
-  def encode(self, value):
-    try:               # Python 2
-      if isinstance(value, unicode):   # pylint: disable=unicode-builtin
-        return value.encode('utf-8')
-    except NameError:  # Python 3
-      pass
-    return str(value)
+  if sys.version_info.major == 2:
+
+    def encode(self, value):
+      # pylint: disable=unicode-builtin
+      return (value.encode('utf-8') if isinstance(value, unicode)  # noqa: F821
+              else str(value))
+
+  else:
+
+    def encode(self, value):
+      return value if isinstance(value, bytes) else str(value).encode('utf-8')
 
   def decode(self, _):
     raise NotImplementedError('ToStringCoder cannot be used for decoding.')


 

----------------------------------------------------------------
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:
[email protected]


Issue Time Tracking
-------------------

    Worklog Id:     (was: 148686)
    Time Spent: 3h 10m  (was: 3h)

> Finish Python 3 porting for coders module
> -----------------------------------------
>
>                 Key: BEAM-5270
>                 URL: https://issues.apache.org/jira/browse/BEAM-5270
>             Project: Beam
>          Issue Type: Sub-task
>          Components: sdk-py-core
>            Reporter: Robbe
>            Assignee: Robbe
>            Priority: Major
>             Fix For: Not applicable
>
>          Time Spent: 3h 10m
>  Remaining Estimate: 0h
>




--
This message was sent by Atlassian JIRA
(v7.6.3#76005)

Reply via email to