From 485f42a29e6eb2d8e5ce9ef93e434f969fed5ee1 Mon Sep 17 00:00:00 2001 From: leewz Date: Wed, 31 Oct 2018 10:52:19 -0400 Subject: [PATCH] Only encode as UTF-8 if necessary. Since a gostring is decoded as a pybytes, it should accept pybytes. --- pygob/types.py | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/pygob/types.py b/pygob/types.py index e875bd5..2c11513 100644 --- a/pygob/types.py +++ b/pygob/types.py @@ -287,14 +287,17 @@ def decode(buf): @staticmethod def encode(s): - """Encode a Python string as a Go string. The string will be UTF-8 - encoded before being turned into bytes since most Go programs - will expect that encoding: + """Encode a Python string or bytes as a Go string. + + A string will be UTF-8 encoded before being turned into + bytes since most Go programs will expect that encoding: >>> GoString.encode('alpha: α') b'\\talpha: \\xce\\xb1' """ - return GoByteSlice.encode(s.encode('utf-8')) + if hasattr(s, 'encode'): + s = s.encode('utf-8') + return GoByteSlice.encode(s) class GoComplex(GoType):