From 58c213365f44647a0c844f34e970f439d873f1b2 Mon Sep 17 00:00:00 2001 From: zhenghuabin Date: Mon, 7 Sep 2015 12:07:29 +0800 Subject: [PATCH 1/2] add 'coding' option for python code generation thrift issues THRIFT-557 --- compiler/cpp/src/generate/t_py_generator.cc | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/compiler/cpp/src/generate/t_py_generator.cc b/compiler/cpp/src/generate/t_py_generator.cc index 2002c1e2884..74757538499 100644 --- a/compiler/cpp/src/generate/t_py_generator.cc +++ b/compiler/cpp/src/generate/t_py_generator.cc @@ -99,6 +99,11 @@ class t_py_generator : public t_generator { iter = parsed_options.find("utf8strings"); gen_utf8strings_ = (iter != parsed_options.end()); + iter = parsed_options.find("coding"); + if (iter != parsed_options.end()) { + coding_ = iter->second; + } + copy_options_ = option_string; if (gen_twisted_) { @@ -275,6 +280,12 @@ class t_py_generator : public t_generator { */ bool gen_utf8strings_; + /** + * specify generated file encoding + * eg. # coding: utf-8 + */ + string coding_; + /** * File streams */ @@ -382,7 +393,11 @@ string t_py_generator::render_fastbinary_includes() { * Autogen'd comment */ string t_py_generator::py_autogen_comment() { - return std::string("#\n") + "# Autogenerated by Thrift Compiler (" + THRIFT_VERSION + ")\n" + string coding; + if (!coding_.empty()) { + coding = "# coding: " + coding_ + "\n"; + } + return coding + std::string("#\n") + "# Autogenerated by Thrift Compiler (" + THRIFT_VERSION + ")\n" + "#\n" + "# DO NOT EDIT UNLESS YOU ARE SURE THAT YOU KNOW WHAT YOU ARE DOING\n" + "#\n" + "# options string: " + copy_options_ + "\n" + "#\n"; } @@ -2429,6 +2444,7 @@ THRIFT_REGISTER_GENERATOR( " twisted: Generate Twisted-friendly RPC services.\n" " tornado: Generate code for use with Tornado.\n" " utf8strings: Encode/decode strings using utf8 in the generated code.\n" + " coding=CODING: Add file encoding declare in generated file.\n" " slots: Generate code using slots for instance members.\n" " dynamic: Generate dynamic code, less code generated but slower.\n" " dynbase=CLS Derive generated classes from class CLS instead of TBase.\n" From e16e027c7d449361f536ab820c909e33152beb97 Mon Sep 17 00:00:00 2001 From: zhenghuabin Date: Mon, 21 Sep 2015 18:43:05 +0800 Subject: [PATCH 2/2] THRIFT-557 charset problem with file Autogenerated by Thrift --- compiler/cpp/src/generate/t_py_generator.cc | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/compiler/cpp/src/generate/t_py_generator.cc b/compiler/cpp/src/generate/t_py_generator.cc index 74757538499..10b1680ed71 100644 --- a/compiler/cpp/src/generate/t_py_generator.cc +++ b/compiler/cpp/src/generate/t_py_generator.cc @@ -282,7 +282,7 @@ class t_py_generator : public t_generator { /** * specify generated file encoding - * eg. # coding: utf-8 + * eg. # -*- coding: utf-8 -*- */ string coding_; @@ -395,7 +395,7 @@ string t_py_generator::render_fastbinary_includes() { string t_py_generator::py_autogen_comment() { string coding; if (!coding_.empty()) { - coding = "# coding: " + coding_ + "\n"; + coding = "# -*- coding: " + coding_ + " -*-\n"; } return coding + std::string("#\n") + "# Autogenerated by Thrift Compiler (" + THRIFT_VERSION + ")\n" + "#\n" + "# DO NOT EDIT UNLESS YOU ARE SURE THAT YOU KNOW WHAT YOU ARE DOING\n" + "#\n"