@@ -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|.
399409void 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 " );
0 commit comments