Skip to content

Commit

Permalink
Enable Protobuf Python version check.
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 617626372
  • Loading branch information
protobuf-github-bot authored and copybara-github committed Mar 20, 2024
1 parent 7746272 commit 0442ed6
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 3 deletions.
2 changes: 1 addition & 1 deletion python/google/protobuf/runtime_version.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ def ValidateProtobufRuntimeVersion(
f' cannot be older than the linked gencode version. {error_prompt}'
)

if gen_suffix is not SUFFIX:
if gen_suffix != SUFFIX:
raise VersionError(
'Detected mismatched Protobuf Gencode/Runtime version suffixes when'
f' loading {location}: gencode {gen_version} runtime {version}.'
Expand Down
27 changes: 25 additions & 2 deletions src/google/protobuf/compiler/python/generator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -367,12 +367,35 @@ void Generator::PrintTopBoilerplate() const {
// instead uses aliases assigned when importing modules.
printer_->Print("import google3\n");
}
bool runtime_version_disabled = false;
printer_->Print(
"from google.protobuf import descriptor as _descriptor\n"
"from google.protobuf import descriptor_pool as _descriptor_pool\n"
"$runtime_version_import$"
"from google.protobuf import symbol_database as _symbol_database\n"
"from google.protobuf.internal import builder as _builder\n");

"from google.protobuf.internal import builder as _builder\n",
"runtime_version_import",
runtime_version_disabled ? ""
: "from google.protobuf import runtime_version "
"as _runtime_version\n");
if (!runtime_version_disabled) {
const auto& version = GetProtobufPythonVersion(opensource_runtime_);
printer_->Print(
"_runtime_version.ValidateProtobufRuntimeVersion(\n"
" $domain$,\n"
" $major$,\n"
" $minor$,\n"
" $patch$,\n"
" '$suffix$',\n"
" '$location$'\n"
")\n",
"domain",
opensource_runtime_ ? "_runtime_version.Domain.PUBLIC"
: "_runtime_version.Domain.GOOGLE_INTERNAL",
"major", absl::StrCat(version.major()), "minor",
absl::StrCat(version.minor()), "patch", absl::StrCat(version.patch()),
"suffix", version.suffix(), "location", file_->name());
}
printer_->Print("# @@protoc_insertion_point(imports)\n\n");
printer_->Print("_sym_db = _symbol_database.Default()\n");
printer_->Print("\n\n");
Expand Down

0 comments on commit 0442ed6

Please sign in to comment.