aboutsummaryrefslogtreecommitdiff
path: root/src/google/protobuf/compiler/python
diff options
context:
space:
mode:
Diffstat (limited to 'src/google/protobuf/compiler/python')
-rw-r--r--src/google/protobuf/compiler/python/python_generator.cc40
-rw-r--r--src/google/protobuf/compiler/python/python_generator.h42
-rw-r--r--src/google/protobuf/compiler/python/python_plugin_unittest.cc4
3 files changed, 42 insertions, 44 deletions
diff --git a/src/google/protobuf/compiler/python/python_generator.cc b/src/google/protobuf/compiler/python/python_generator.cc
index 01f28b37..6dc61ec1 100644
--- a/src/google/protobuf/compiler/python/python_generator.cc
+++ b/src/google/protobuf/compiler/python/python_generator.cc
@@ -45,7 +45,6 @@
// directly.
#include <algorithm>
-#include <google/protobuf/stubs/hash.h>
#include <limits>
#include <map>
#include <memory>
@@ -73,11 +72,6 @@ namespace python {
namespace {
-// Reimplemented here because we can't bring in
-// absl/strings/string_view_utils.h because it needs C++11.
-bool StrStartsWith(StringPiece sp, StringPiece x) {
- return sp.size() >= x.size() && sp.substr(0, x.size()) == x;
-}
bool StrEndsWith(StringPiece sp, StringPiece x) {
return sp.size() >= x.size() && sp.substr(sp.size() - x.size()) == x;
}
@@ -103,7 +97,7 @@ string ModuleName(const string& filename) {
// Returns the alias we assign to the module of the given .proto filename
// when importing. See testPackageInitializationImport in
-// google/protobuf/python/reflection_test.py
+// net/proto2/python/internal/reflection_test.py
// to see why we need the alias.
string ModuleAlias(const string& filename) {
string module_name = ModuleName(filename);
@@ -424,7 +418,7 @@ void Generator::PrintFileDescriptor() const {
printer_->Print(m, file_descriptor_template);
printer_->Indent();
printer_->Print(
-//##!PY25 "serialized_pb=b'$value$'\n",
+ //##!PY25 "serialized_pb=b'$value$'\n",
"serialized_pb=_b('$value$')\n", //##PY25
"value", strings::CHexEscape(file_descriptor_serialized_));
if (file_->dependency_count() != 0) {
@@ -476,9 +470,9 @@ void Generator::PrintTopLevelEnums() const {
}
for (int i = 0; i < top_level_enum_values.size(); ++i) {
- printer_->Print("$name$ = $value$\n",
- "name", top_level_enum_values[i].first,
- "value", SimpleItoa(top_level_enum_values[i].second));
+ printer_->Print("$name$ = $value$\n", "name",
+ top_level_enum_values[i].first, "value",
+ SimpleItoa(top_level_enum_values[i].second));
}
printer_->Print("\n");
}
@@ -550,9 +544,9 @@ void Generator::PrintTopLevelExtensions() const {
const FieldDescriptor& extension_field = *file_->extension(i);
string constant_name = extension_field.name() + "_FIELD_NUMBER";
UpperString(&constant_name);
- printer_->Print("$constant_name$ = $number$\n",
- "constant_name", constant_name,
- "number", SimpleItoa(extension_field.number()));
+ printer_->Print("$constant_name$ = $number$\n", "constant_name",
+ constant_name, "number",
+ SimpleItoa(extension_field.number()));
printer_->Print("$name$ = ", "name", extension_field.name());
PrintFieldDescriptor(extension_field, is_extension);
printer_->Print("\n");
@@ -745,9 +739,9 @@ void Generator::PrintDescriptor(const Descriptor& message_descriptor) const {
for (int i = 0; i < message_descriptor.extension_range_count(); ++i) {
const Descriptor::ExtensionRange* range =
message_descriptor.extension_range(i);
- printer_->Print("($start$, $end$), ",
- "start", SimpleItoa(range->start),
- "end", SimpleItoa(range->end));
+ printer_->Print("($start$, $end$), ", "start",
+ SimpleItoa(range->start), "end",
+ SimpleItoa(range->end));
}
printer_->Print("],\n");
printer_->Print("oneofs=[\n");
@@ -1184,7 +1178,8 @@ void Generator::PrintExtensionsInDescriptor(
}
bool Generator::GeneratingDescriptorProto() const {
- return file_->name() == "google/protobuf/descriptor.proto";
+ return file_->name() == "net/proto2/proto/descriptor.proto" ||
+ file_->name() == "google/protobuf/descriptor.proto";
}
// Returns the unique Python module-level identifier given to a descriptor.
@@ -1260,10 +1255,11 @@ void Generator::PrintSerializedPbInterval(
int offset = file_descriptor_serialized_.find(sp);
GOOGLE_CHECK_GE(offset, 0);
- printer_->Print("serialized_start=$serialized_start$,\n"
- "serialized_end=$serialized_end$,\n",
- "serialized_start", SimpleItoa(offset),
- "serialized_end", SimpleItoa(offset + sp.size()));
+ printer_->Print(
+ "serialized_start=$serialized_start$,\n"
+ "serialized_end=$serialized_end$,\n",
+ "serialized_start", SimpleItoa(offset), "serialized_end",
+ SimpleItoa(offset + sp.size()));
}
namespace {
diff --git a/src/google/protobuf/compiler/python/python_generator.h b/src/google/protobuf/compiler/python/python_generator.h
index 8e4050de..c21c36da 100644
--- a/src/google/protobuf/compiler/python/python_generator.h
+++ b/src/google/protobuf/compiler/python/python_generator.h
@@ -38,8 +38,8 @@
#include <string>
#include <google/protobuf/compiler/code_generator.h>
-#include <google/protobuf/stubs/mutex.h>
-#include <google/protobuf/stubs/common.h>
+
+#include <google/protobuf/port_def.inc>
namespace google {
namespace protobuf {
@@ -60,16 +60,16 @@ namespace python {
// If you create your own protocol compiler binary and you want it to support
// Python output, you can do so by registering an instance of this
// CodeGenerator with the CommandLineInterface in your main() function.
-class LIBPROTOC_EXPORT Generator : public CodeGenerator {
+class PROTOC_EXPORT Generator : public CodeGenerator {
public:
Generator();
virtual ~Generator();
// CodeGenerator methods.
virtual bool Generate(const FileDescriptor* file,
- const string& parameter,
+ const std::string& parameter,
GeneratorContext* generator_context,
- string* error) const;
+ std::string* error) const;
private:
void PrintImports() const;
@@ -86,7 +86,7 @@ class LIBPROTOC_EXPORT Generator : public CodeGenerator {
void PrintFieldDescriptorsInDescriptor(
const Descriptor& message_descriptor,
bool is_extension,
- const string& list_variable_name,
+ const std::string& list_variable_name,
int (Descriptor::*CountFn)() const,
const FieldDescriptor* (Descriptor::*GetterFn)(int) const) const;
void PrintFieldsInDescriptor(const Descriptor& message_descriptor) const;
@@ -96,11 +96,11 @@ class LIBPROTOC_EXPORT Generator : public CodeGenerator {
void PrintNestedDescriptors(const Descriptor& containing_descriptor) const;
void PrintMessages() const;
- void PrintMessage(const Descriptor& message_descriptor, const string& prefix,
- std::vector<string>* to_register) const;
+ void PrintMessage(const Descriptor& message_descriptor, const std::string& prefix,
+ std::vector<std::string>* to_register) const;
void PrintNestedMessages(const Descriptor& containing_descriptor,
- const string& prefix,
- std::vector<string>* to_register) const;
+ const std::string& prefix,
+ std::vector<std::string>* to_register) const;
void FixForeignFieldsInDescriptors() const;
void FixForeignFieldsInDescriptor(
@@ -108,14 +108,14 @@ class LIBPROTOC_EXPORT Generator : public CodeGenerator {
const Descriptor* containing_descriptor) const;
void FixForeignFieldsInField(const Descriptor* containing_type,
const FieldDescriptor& field,
- const string& python_dict_name) const;
+ const std::string& python_dict_name) const;
void AddMessageToFileDescriptor(const Descriptor& descriptor) const;
void AddEnumToFileDescriptor(const EnumDescriptor& descriptor) const;
void AddExtensionToFileDescriptor(const FieldDescriptor& descriptor) const;
void AddServiceToFileDescriptor(const ServiceDescriptor& descriptor) const;
- string FieldReferencingExpression(const Descriptor* containing_type,
+ std::string FieldReferencingExpression(const Descriptor* containing_type,
const FieldDescriptor& field,
- const string& python_dict_name) const;
+ const std::string& python_dict_name) const;
template <typename DescriptorT>
void FixContainingTypeInDescriptor(
const DescriptorT& descriptor,
@@ -135,13 +135,13 @@ class LIBPROTOC_EXPORT Generator : public CodeGenerator {
const ServiceDescriptor& descriptor) const;
void PrintEnumValueDescriptor(const EnumValueDescriptor& descriptor) const;
- string OptionsValue(const string& serialized_options) const;
+ std::string OptionsValue(const std::string& serialized_options) const;
bool GeneratingDescriptorProto() const;
template <typename DescriptorT>
- string ModuleLevelDescriptorName(const DescriptorT& descriptor) const;
- string ModuleLevelMessageName(const Descriptor& descriptor) const;
- string ModuleLevelServiceDescriptorName(
+ std::string ModuleLevelDescriptorName(const DescriptorT& descriptor) const;
+ std::string ModuleLevelMessageName(const Descriptor& descriptor) const;
+ std::string ModuleLevelServiceDescriptorName(
const ServiceDescriptor& descriptor) const;
template <typename DescriptorT, typename DescriptorProtoT>
@@ -155,13 +155,13 @@ class LIBPROTOC_EXPORT Generator : public CodeGenerator {
void FixOptionsForMessage(const Descriptor& descriptor) const;
void CopyPublicDependenciesAliases(
- const string& copy_from, const FileDescriptor* file) const;
+ const std::string& copy_from, const FileDescriptor* file) const;
// Very coarse-grained lock to ensure that Generate() is reentrant.
// Guards file_, printer_ and file_descriptor_serialized_.
mutable Mutex mutex_;
mutable const FileDescriptor* file_; // Set in Generate(). Under mutex_.
- mutable string file_descriptor_serialized_;
+ mutable std::string file_descriptor_serialized_;
mutable io::Printer* printer_; // Set in Generate(). Under mutex_.
GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(Generator);
@@ -170,6 +170,8 @@ class LIBPROTOC_EXPORT Generator : public CodeGenerator {
} // namespace python
} // namespace compiler
} // namespace protobuf
-
} // namespace google
+
+#include <google/protobuf/port_undef.inc>
+
#endif // GOOGLE_PROTOBUF_COMPILER_PYTHON_GENERATOR_H__
diff --git a/src/google/protobuf/compiler/python/python_plugin_unittest.cc b/src/google/protobuf/compiler/python/python_plugin_unittest.cc
index 2f096808..d19d11f0 100644
--- a/src/google/protobuf/compiler/python/python_plugin_unittest.cc
+++ b/src/google/protobuf/compiler/python/python_plugin_unittest.cc
@@ -90,7 +90,7 @@ TEST(PythonPluginTest, PluginTest) {
"}\n",
true));
- google::protobuf::compiler::CommandLineInterface cli;
+ compiler::CommandLineInterface cli;
cli.SetInputsAreProtoPathRelative(true);
python::Generator python_generator;
@@ -133,7 +133,7 @@ TEST(PythonPluginTest, ImportTest) {
"message Message2 {}\n",
true));
- google::protobuf::compiler::CommandLineInterface cli;
+ compiler::CommandLineInterface cli;
cli.SetInputsAreProtoPathRelative(true);
python::Generator python_generator;
cli.RegisterGenerator("--python_out", &python_generator, "");