Skip to content

Commit 0442ed6

Browse files
Enable Protobuf Python version check.
PiperOrigin-RevId: 617626372
1 parent 7746272 commit 0442ed6

File tree

2 files changed

+26
-3
lines changed

2 files changed

+26
-3
lines changed

python/google/protobuf/runtime_version.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ def ValidateProtobufRuntimeVersion(
8989
f' cannot be older than the linked gencode version. {error_prompt}'
9090
)
9191

92-
if gen_suffix is not SUFFIX:
92+
if gen_suffix != SUFFIX:
9393
raise VersionError(
9494
'Detected mismatched Protobuf Gencode/Runtime version suffixes when'
9595
f' loading {location}: gencode {gen_version} runtime {version}.'

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

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -367,12 +367,35 @@ void Generator::PrintTopBoilerplate() const {
367367
// instead uses aliases assigned when importing modules.
368368
printer_->Print("import google3\n");
369369
}
370+
bool runtime_version_disabled = false;
370371
printer_->Print(
371372
"from google.protobuf import descriptor as _descriptor\n"
372373
"from google.protobuf import descriptor_pool as _descriptor_pool\n"
374+
"$runtime_version_import$"
373375
"from google.protobuf import symbol_database as _symbol_database\n"
374-
"from google.protobuf.internal import builder as _builder\n");
375-
376+
"from google.protobuf.internal import builder as _builder\n",
377+
"runtime_version_import",
378+
runtime_version_disabled ? ""
379+
: "from google.protobuf import runtime_version "
380+
"as _runtime_version\n");
381+
if (!runtime_version_disabled) {
382+
const auto& version = GetProtobufPythonVersion(opensource_runtime_);
383+
printer_->Print(
384+
"_runtime_version.ValidateProtobufRuntimeVersion(\n"
385+
" $domain$,\n"
386+
" $major$,\n"
387+
" $minor$,\n"
388+
" $patch$,\n"
389+
" '$suffix$',\n"
390+
" '$location$'\n"
391+
")\n",
392+
"domain",
393+
opensource_runtime_ ? "_runtime_version.Domain.PUBLIC"
394+
: "_runtime_version.Domain.GOOGLE_INTERNAL",
395+
"major", absl::StrCat(version.major()), "minor",
396+
absl::StrCat(version.minor()), "patch", absl::StrCat(version.patch()),
397+
"suffix", version.suffix(), "location", file_->name());
398+
}
376399
printer_->Print("# @@protoc_insertion_point(imports)\n\n");
377400
printer_->Print("_sym_db = _symbol_database.Default()\n");
378401
printer_->Print("\n\n");

0 commit comments

Comments
 (0)