diff --git a/python/google/protobuf/runtime_version.py b/python/google/protobuf/runtime_version.py index edb2c6236a..a4d096a640 100644 --- a/python/google/protobuf/runtime_version.py +++ b/python/google/protobuf/runtime_version.py @@ -38,6 +38,9 @@ MINOR = OSS_MINOR PATCH = OSS_PATCH SUFFIX = OSS_SUFFIX +# Avoid flooding of warnings. +_MAX_WARNING_COUNT = 20 +_warning_count = 0 class VersionError(Exception): """Exception class for version violation.""" @@ -69,6 +72,8 @@ def ValidateProtobufRuntimeVersion( if disable_flag is not None and disable_flag.lower() == 'true': return + global _warning_count + version = f'{MAJOR}.{MINOR}.{PATCH}{SUFFIX}' gen_version = f'{gen_major}.{gen_minor}.{gen_patch}{gen_suffix}' @@ -89,12 +94,14 @@ def ValidateProtobufRuntimeVersion( if gen_major != MAJOR: if gen_major == MAJOR - 1: - warnings.warn( - 'Protobuf gencode version %s is exactly one major version older than' - ' the runtime version %s at %s. Please update the gencode to avoid' - ' compatibility violations in the next runtime release.' - % (gen_version, version, location) - ) + if _warning_count < _MAX_WARNING_COUNT: + warnings.warn( + 'Protobuf gencode version %s is exactly one major version older' + ' than the runtime version %s at %s. Please update the gencode to' + ' avoid compatibility violations in the next runtime release.' + % (gen_version, version, location) + ) + _warning_count += 1 else: _ReportVersionError( 'Detected mismatched Protobuf Gencode/Runtime major versions when' @@ -109,11 +116,13 @@ def ValidateProtobufRuntimeVersion( f' cannot be older than the linked gencode version. {error_prompt}' ) elif MINOR > gen_minor or PATCH > gen_patch: - warnings.warn( - 'Protobuf gencode version %s is older than the runtime version %s at' - ' %s. Please avoid checked-in Protobuf gencode that can be obsolete.' - % (gen_version, version, location) - ) + if _warning_count < _MAX_WARNING_COUNT: + warnings.warn( + 'Protobuf gencode version %s is older than the runtime version %s at' + ' %s. Please avoid checked-in Protobuf gencode that can be obsolete.' + % (gen_version, version, location) + ) + _warning_count += 1 if gen_suffix != SUFFIX: _ReportVersionError(