THRIFT-3526 Code generated by py:utf8strings does not work for Python3
This closes #776
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 @@
* 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 @@
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 @@
} 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 @@
} 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: