Repository: thrift
Updated Branches:
  refs/heads/master 378b727f8 -> 4733db4e1


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

This closes #769


Project: http://git-wip-us.apache.org/repos/asf/thrift/repo
Commit: http://git-wip-us.apache.org/repos/asf/thrift/commit/4733db4e
Tree: http://git-wip-us.apache.org/repos/asf/thrift/tree/4733db4e
Diff: http://git-wip-us.apache.org/repos/asf/thrift/diff/4733db4e

Branch: refs/heads/master
Commit: 4733db4e151794a7a6e1cbbb5396e5d1146f1f8e
Parents: 796667b
Author: Nobuaki Sukegawa <[email protected]>
Authored: Tue Jan 5 02:50:57 2016 +0900
Committer: Nobuaki Sukegawa <[email protected]>
Committed: Wed Jan 6 04:35:12 2016 +0900

----------------------------------------------------------------------
 compiler/cpp/src/generate/t_py_generator.cc |  3 +++
 lib/py/src/protocol/fastbinary.c            | 14 ++++++++++++--
 2 files changed, 15 insertions(+), 2 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/thrift/blob/4733db4e/compiler/cpp/src/generate/t_py_generator.cc
----------------------------------------------------------------------
diff --git a/compiler/cpp/src/generate/t_py_generator.cc 
b/compiler/cpp/src/generate/t_py_generator.cc
index 00be6e7..a90bfca 100644
--- a/compiler/cpp/src/generate/t_py_generator.cc
+++ b/compiler/cpp/src/generate/t_py_generator.cc
@@ -2555,6 +2555,9 @@ string t_py_generator::type_to_spec_args(t_type* ttype) {
 
   if (ttype->is_base_type() && 
reinterpret_cast<t_base_type*>(ttype)->is_binary()) {
     return  "'BINARY'";
+  } else if (gen_utf8strings_ && ttype->is_base_type()
+             && reinterpret_cast<t_base_type*>(ttype)->is_string()) {
+    return "'UTF8'";
   } else if (ttype->is_base_type() || ttype->is_enum()) {
     return  "None";
   } else if (ttype->is_struct() || ttype->is_xception()) {

http://git-wip-us.apache.org/repos/asf/thrift/blob/4733db4e/lib/py/src/protocol/fastbinary.c
----------------------------------------------------------------------
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 @@ parse_pyint(PyObject* o, int32_t* ret, int32_t min, 
int32_t max) {
   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 @@ output_val(PyObject* output, PyObject* value, TType type, 
PyObject* typeargs) {
   }
 
   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 @@ decode_val(DecodeBuffer* input, TType type, PyObject* 
typeargs) {
       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:

Reply via email to