THRIFT-3526 Code generated by py:utf8strings does not work for Python3

This closes #776


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

Branch: refs/heads/master
Commit: 03565bf467de14a8a229d419164c2b16c34c538e
Parents: 299255a
Author: Nobuaki Sukegawa <[email protected]>
Authored: Wed Jan 6 14:45:19 2016 +0900
Committer: Nobuaki Sukegawa <[email protected]>
Committed: Mon Jan 11 11:34:22 2016 +0900

----------------------------------------------------------------------
 compiler/cpp/src/generate/t_py_generator.cc | 10 ++++++----
 1 file changed, 6 insertions(+), 4 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/thrift/blob/03565bf4/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 928bc46..ebaa9c3 100644
--- a/compiler/cpp/src/generate/t_py_generator.cc
+++ b/compiler/cpp/src/generate/t_py_generator.cc
@@ -435,7 +435,9 @@ string t_py_generator::py_autogen_comment() {
  * Prints standard thrift imports
  */
 string t_py_generator::py_imports() {
-  return string("from thrift.Thrift import TType, TMessageType, TFrozenDict, 
TException, TApplicationException");
+  return gen_utf8strings_
+    ? string("from thrift.Thrift import TType, TMessageType, TFrozenDict, 
TException, TApplicationException\nimport sys")
+    : string("from thrift.Thrift import TType, TMessageType, TFrozenDict, 
TException, TApplicationException");
 }
 
 /**
@@ -1518,7 +1520,7 @@ void t_py_generator::generate_service_remote(t_service* 
tservice) {
     py_autogen_comment() << endl <<
     "import sys" << endl <<
     "import pprint" << endl <<
-    "if sys.version_info[0] == 3:" << endl <<
+    "if sys.version_info[0] > 2:" << endl <<
     "  from urllib.parse import urlparse" << endl <<
     "else:" << endl <<
     "  from urlparse import urlparse" << endl <<
@@ -2038,7 +2040,7 @@ void t_py_generator::generate_deserialize_field(ofstream& 
out,
         } else if(!gen_utf8strings_) {
           out << "readString()";
         } else {
-          out << "readString().decode('utf-8')";
+          out << "readString().decode('utf-8') if sys.version_info[0] == 2 
else iprot.readString()";
         }
         break;
       case t_base_type::TYPE_BOOL:
@@ -2226,7 +2228,7 @@ void t_py_generator::generate_serialize_field(ofstream& 
out, t_field* tfield, st
         } else if (!gen_utf8strings_) {
           out << "writeString(" << name << ")";
         } else {
-          out << "writeString(" << name << ".encode('utf-8'))";
+          out << "writeString(" << name << ".encode('utf-8') if 
sys.version_info[0] == 2 else " << name << ")";
         }
         break;
       case t_base_type::TYPE_BOOL:

Reply via email to