THRIFT-557 charset problem with file Autogenerated by Thrift
Client: Python
Patch: zhenghuabin <zhenghuabin@bytedance.com>
This closes #601
diff --git a/compiler/cpp/src/generate/t_py_generator.cc b/compiler/cpp/src/generate/t_py_generator.cc
index 2002c1e..10b1680 100644
--- a/compiler/cpp/src/generate/t_py_generator.cc
+++ b/compiler/cpp/src/generate/t_py_generator.cc
@@ -99,6 +99,11 @@
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_) {
@@ -276,6 +281,12 @@
bool gen_utf8strings_;
/**
+ * specify generated file encoding
+ * eg. # -*- coding: utf-8 -*-
+ */
+ string coding_;
+
+ /**
* File streams
*/
@@ -382,7 +393,11 @@
* 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 @@
" 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"