Skip to content

Commit d39ea4d

Browse files
anandoleecopybara-github
authored andcommitted
Internal change
PiperOrigin-RevId: 873106638
1 parent 26569de commit d39ea4d

3 files changed

Lines changed: 15 additions & 10 deletions

File tree

src/google/protobuf/compiler/python/generator.cc

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,7 @@ bool Generator::Generate(const FileDescriptor* file,
342342

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

345+
345346
return !printer.failed();
346347
}
347348

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

399+
std::string Generator::ImportModuleName(absl::string_view filename) const {
400+
std::string module_name = ModuleName(filename);
401+
if (!opensource_runtime_) {
402+
module_name =
403+
std::string(absl::StripPrefix(module_name, kThirdPartyPrefix));
404+
}
405+
return module_name;
406+
}
407+
398408
// Prints Python imports for all modules imported by |file|.
399409
void Generator::PrintImports() const {
400410
bool has_importlib = false;
401411
for (int i = 0; i < file_->dependency_count(); ++i) {
402412
absl::string_view filename = file_->dependency(i)->name();
403413

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

441447
// Print public imports.
442448
for (int i = 0; i < file_->public_dependency_count(); ++i) {
443-
std::string module_name = ModuleName(file_->public_dependency(i)->name());
444-
if (!opensource_runtime_) {
445-
module_name =
446-
std::string(absl::StripPrefix(module_name, kThirdPartyPrefix));
447-
}
449+
std::string module_name =
450+
ImportModuleName(file_->public_dependency(i)->name());
448451
printer_->Print("from $module$ import *\n", "module", module_name);
449452
}
450453
printer_->Print("\n");

src/google/protobuf/compiler/python/generator.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ class PROTOC_EXPORT Generator : public CodeGenerator {
8282
private:
8383
GeneratorOptions ParseParameter(absl::string_view parameter,
8484
std::string* error) const;
85+
std::string ImportModuleName(absl::string_view filename) const;
8586
void PrintImports() const;
8687
template <typename DescriptorT>
8788
std::string GetResolvedFeatures(const DescriptorT& descriptor) const;

src/google/protobuf/compiler/python/helpers.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ std::string GetFileName(const FileDescriptor* file_des,
2929
absl::string_view suffix);
3030
bool HasGenericServices(const FileDescriptor* file);
3131
std::string GeneratedCodeToBase64(const GeneratedCodeInfo& annotations);
32+
std::string PrintImport(const FileDescriptor* file, bool* has_importlib);
3233

3334
template <typename DescriptorT>
3435
std::string NamePrefixedWithNestedTypes(const DescriptorT& descriptor,

0 commit comments

Comments
 (0)