aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/google/protobuf/compiler/python/python_generator.cc
diff options
context:
space:
mode:
authorGravatar Feng Xiao <xfxyjwf@gmail.com>2014-11-14 11:50:31 -0800
committerGravatar Feng Xiao <xfxyjwf@gmail.com>2014-11-14 11:50:31 -0800
commitf157a5651c79a7a36e242af216a5d5b383ba8af2 (patch)
tree368bb0ca0e89ab7514302e4df00435a4ef461a28 /src/google/protobuf/compiler/python/python_generator.cc
parentfaf581d20866ad5e586b3e515f6c547d2dcec2c1 (diff)
Down-integrate from internal code base (C++ maps support).
Diffstat (limited to 'src/google/protobuf/compiler/python/python_generator.cc')
-rw-r--r--src/google/protobuf/compiler/python/python_generator.cc49
1 files changed, 7 insertions, 42 deletions
diff --git a/src/google/protobuf/compiler/python/python_generator.cc b/src/google/protobuf/compiler/python/python_generator.cc
index d72ca207..e6faf7ef 100644
--- a/src/google/protobuf/compiler/python/python_generator.cc
+++ b/src/google/protobuf/compiler/python/python_generator.cc
@@ -73,41 +73,6 @@ namespace python {
namespace {
-const char* const kKeywordList[] = {
- "and", "as", "break", "class", "continue", "def", "elif", "else", "except",
- "False", "for", "from", "if", "import", "not", "or", "raise", "return",
- "True", "try", "with", "while", "yield"
-};
-
-hash_set<string> MakeKeywordsMap() {
- hash_set<string> result;
- for (int i = 0; i < GOOGLE_ARRAYSIZE(kKeywordList); ++i) {
- result.insert(kKeywordList[i]);
- }
- return result;
-}
-
-hash_set<string>* keywords_ = NULL;
-GOOGLE_PROTOBUF_DECLARE_ONCE(keywords_once_);
-
-void InitKeywords() {
- keywords_ = new hash_set<string>();
- *keywords_ = MakeKeywordsMap();
-}
-
-hash_set<string>& GetKeywords() {
- ::google::protobuf::GoogleOnceInit(&keywords_once_, &InitKeywords);
- return *keywords_;
-}
-
-string FieldName(const FieldDescriptor& field) {
- string result = field.name();
- if (GetKeywords().count(result) > 0) {
- result.append("_");
- }
- return result;
-}
-
// Returns a copy of |filename| with any trailing ".protodevel" or ".proto
// suffix stripped.
// TODO(robinson): Unify with copy in compiler/cpp/internal/helpers.cc.
@@ -343,7 +308,7 @@ bool Generator::Generate(const FileDescriptor* file,
fdp.SerializeToString(&file_descriptor_serialized_);
- scoped_ptr<io::ZeroCopyOutputStream> output(context->Open(filename));
+ google::protobuf::scoped_ptr<io::ZeroCopyOutputStream> output(context->Open(filename));
GOOGLE_CHECK(output.get());
io::Printer printer(output.get(), '$');
printer_ = &printer;
@@ -531,7 +496,7 @@ void Generator::PrintTopLevelExtensions() const {
printer_->Print("$constant_name$ = $number$\n",
"constant_name", constant_name,
"number", SimpleItoa(extension_field.number()));
- printer_->Print("$name$ = ", "name", FieldName(extension_field));
+ printer_->Print("$name$ = ", "name", extension_field.name());
PrintFieldDescriptor(extension_field, is_extension);
printer_->Print("\n");
}
@@ -878,7 +843,7 @@ void Generator::AddExtensionToFileDescriptor(
const FieldDescriptor& descriptor) const {
map<string, string> m;
m["descriptor_name"] = kDescriptorKey;
- m["field_name"] = FieldName(descriptor);
+ m["field_name"] = descriptor.name();
const char file_descriptor_template[] =
"$descriptor_name$.extensions_by_name['$field_name$'] = "
"$field_name$\n";
@@ -931,12 +896,12 @@ string Generator::FieldReferencingExpression(
GOOGLE_CHECK_EQ(field.file(), file_) << field.file()->name() << " vs. "
<< file_->name();
if (!containing_type) {
- return FieldName(field);
+ return field.name();
}
return strings::Substitute(
"$0.$1['$2']",
ModuleLevelDescriptorName(*containing_type),
- python_dict_name, FieldName(field));
+ python_dict_name, field.name());
}
// Prints containing_type for nested descriptors or enum descriptors.
@@ -1065,7 +1030,7 @@ void Generator::PrintFieldDescriptor(
string options_string;
field.options().SerializeToString(&options_string);
map<string, string> m;
- m["name"] = FieldName(field);
+ m["name"] = field.name();
m["full_name"] = field.full_name();
m["index"] = SimpleItoa(field.index());
m["number"] = SimpleItoa(field.number());
@@ -1285,7 +1250,7 @@ void Generator::FixOptionsForField(
if (field.is_extension()) {
if (field.extension_scope() == NULL) {
// Top level extensions.
- field_name = FieldName(field);
+ field_name = field.name();
} else {
field_name = FieldReferencingExpression(
field.extension_scope(), field, "extensions_by_name");