THRIFT-3519 fastbinary does not work with -gen py:utf8strings
Client:
Patch: Nobuaki Sukegawa

This closes #769
diff --git a/lib/py/src/protocol/fastbinary.c b/lib/py/src/protocol/fastbinary.c
index 091a617..714fb13 100644
--- a/lib/py/src/protocol/fastbinary.c
+++ b/lib/py/src/protocol/fastbinary.c
@@ -226,6 +226,10 @@
   return true;
 }
 
+static bool
+is_utf8(PyObject* typeargs) {
+  return PyString_Check(typeargs) && !strcmp(PyString_AS_STRING(typeargs), "UTF8");
+}
 
 /* --- FUNCTIONS TO PARSE STRUCT SPECIFICATOINS --- */
 
@@ -430,7 +434,10 @@
   }
 
   case T_STRING: {
-    Py_ssize_t len = PyString_Size(value);
+    Py_ssize_t len = 0;
+    if (is_utf8(typeargs) && PyUnicode_Check(value))
+      value = PyUnicode_AsUTF8String(value);
+    len = PyString_Size(value);
 
     if (!check_ssize_t_32(len)) {
       return false;
@@ -1053,7 +1060,10 @@
       return NULL;
     }
 
-    return PyString_FromStringAndSize(buf, len);
+    if (is_utf8(typeargs))
+      return PyUnicode_DecodeUTF8(buf, len, 0);
+    else
+      return PyString_FromStringAndSize(buf, len);
   }
 
   case T_LIST: