You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@thrift.apache.org by dr...@apache.org on 2010/07/01 07:36:25 UTC

svn commit: r959516 - /incubator/thrift/trunk/compiler/cpp/src/generate/t_py_generator.cc

Author: dreiss
Date: Thu Jul  1 05:36:25 2010
New Revision: 959516

URL: http://svn.apache.org/viewvc?rev=959516&view=rev
Log:
THRIFT-395. python: Add option to treat strings as UTF-8 unicode

Add the "utf8strings" option to the Python generator.  If set, all
Thrift strings (not binary) will be expected to be unicode objects, not
str.  They will be encoded as UTF-8 before serialization and decoded as
UTF-8 after deserialization.

The accelerator module for TBinaryProtocol is not affected.

Modified:
    incubator/thrift/trunk/compiler/cpp/src/generate/t_py_generator.cc

Modified: incubator/thrift/trunk/compiler/cpp/src/generate/t_py_generator.cc
URL: http://svn.apache.org/viewvc/incubator/thrift/trunk/compiler/cpp/src/generate/t_py_generator.cc?rev=959516&r1=959515&r2=959516&view=diff
==============================================================================
--- incubator/thrift/trunk/compiler/cpp/src/generate/t_py_generator.cc (original)
+++ incubator/thrift/trunk/compiler/cpp/src/generate/t_py_generator.cc Thu Jul  1 05:36:25 2010
@@ -52,6 +52,9 @@ class t_py_generator : public t_generato
     iter = parsed_options.find("twisted");
     gen_twisted_ = (iter != parsed_options.end());
 
+    iter = parsed_options.find("utf8strings");
+    gen_utf8strings_ = (iter != parsed_options.end());
+
     if (gen_twisted_){
       out_dir_base_ = "gen-py.twisted";
     } else {
@@ -206,6 +209,11 @@ class t_py_generator : public t_generato
   bool gen_twisted_;
 
   /**
+   * True iff strings should be encoded using utf-8.
+   */
+  bool gen_utf8strings_;
+
+  /**
    * File streams
    */
 
@@ -1752,7 +1760,11 @@ void t_py_generator::generate_deserializ
           name;
         break;
       case t_base_type::TYPE_STRING:
-        out << "readString();";
+        if (((t_base_type*)type)->is_binary() || !gen_utf8strings_) {
+          out << "readString();";
+        } else {
+          out << "readString().decode('utf-8')";
+        }
         break;
       case t_base_type::TYPE_BOOL:
         out << "readBool();";
@@ -1946,7 +1958,11 @@ void t_py_generator::generate_serialize_
           "compiler error: cannot serialize void field in a struct: " + name;
         break;
       case t_base_type::TYPE_STRING:
-        out << "writeString(" << name << ")";
+        if (((t_base_type*)type)->is_binary() || !gen_utf8strings_) {
+          out << "writeString(" << name << ")";
+        } else {
+          out << "writeString(" << name << ".encode('utf-8'))";
+        }
         break;
       case t_base_type::TYPE_BOOL:
         out << "writeBool(" << name << ")";