Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 13 additions & 10 deletions src/google/protobuf/compiler/python/generator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,7 @@ bool Generator::Generate(const FileDescriptor* file,

printer.Print("# @@protoc_insertion_point(module_scope)\n");


return !printer.failed();
}

Expand Down Expand Up @@ -395,18 +396,23 @@ void Generator::PrintTopBoilerplate() const {
printer_->Print("\n\n");
}

std::string Generator::ImportModuleName(absl::string_view filename) const {
std::string module_name = ModuleName(filename);
if (!opensource_runtime_) {
module_name =
std::string(absl::StripPrefix(module_name, kThirdPartyPrefix));
}
return module_name;
}

// Prints Python imports for all modules imported by |file|.
void Generator::PrintImports() const {
bool has_importlib = false;
for (int i = 0; i < file_->dependency_count(); ++i) {
absl::string_view filename = file_->dependency(i)->name();

std::string module_name = ModuleName(filename);
std::string module_name = ImportModuleName(filename);
std::string module_alias = ModuleAlias(filename);
if (!opensource_runtime_) {
module_name =
std::string(absl::StripPrefix(module_name, kThirdPartyPrefix));
}
if (ContainsPythonKeyword(module_name)) {
// If the module path contains a Python keyword, we have to quote the
// module name and import it using importlib. Otherwise the usual kind of
Expand Down Expand Up @@ -440,11 +446,8 @@ void Generator::PrintImports() const {

// Print public imports.
for (int i = 0; i < file_->public_dependency_count(); ++i) {
std::string module_name = ModuleName(file_->public_dependency(i)->name());
if (!opensource_runtime_) {
module_name =
std::string(absl::StripPrefix(module_name, kThirdPartyPrefix));
}
std::string module_name =
ImportModuleName(file_->public_dependency(i)->name());
printer_->Print("from $module$ import *\n", "module", module_name);
}
printer_->Print("\n");
Expand Down
1 change: 1 addition & 0 deletions src/google/protobuf/compiler/python/generator.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ class PROTOC_EXPORT Generator : public CodeGenerator {
private:
GeneratorOptions ParseParameter(absl::string_view parameter,
std::string* error) const;
std::string ImportModuleName(absl::string_view filename) const;
void PrintImports() const;
template <typename DescriptorT>
std::string GetResolvedFeatures(const DescriptorT& descriptor) const;
Expand Down
1 change: 1 addition & 0 deletions src/google/protobuf/compiler/python/helpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ std::string GetFileName(const FileDescriptor* file_des,
absl::string_view suffix);
bool HasGenericServices(const FileDescriptor* file);
std::string GeneratedCodeToBase64(const GeneratedCodeInfo& annotations);
std::string PrintImport(const FileDescriptor* file, bool* has_importlib);

template <typename DescriptorT>
std::string NamePrefixedWithNestedTypes(const DescriptorT& descriptor,
Expand Down
Loading