1# Protocol Buffers - Google's data interchange format
2# Copyright 2008 Google Inc. All rights reserved.
3#
4# Use of this source code is governed by a BSD-style
5# license that can be found in the LICENSE file or at
6# https://developers.google.com/open-source/licenses/bsd
7
8"""Protobuf Runtime versions and validators.
9
10It should only be accessed by Protobuf gencodes and tests. DO NOT USE it
11elsewhere.
12"""
13
14__author__ = 'shaod@google.com (Dennis Shao)'
15
16from enum import Enum
17import os
18import warnings
19
20
21class Domain(Enum):
22 GOOGLE_INTERNAL = 1
23 PUBLIC = 2
24
25
26# The versions of this Python Protobuf runtime to be changed automatically by
27# the Protobuf release process. Do not edit them manually.
28# These OSS versions are not stripped to avoid merging conflicts.
29OSS_DOMAIN = Domain.PUBLIC
30OSS_MAJOR = 6
31OSS_MINOR = 31
32OSS_PATCH = 1
33OSS_SUFFIX = ''
34
35DOMAIN = OSS_DOMAIN
36MAJOR = OSS_MAJOR
37MINOR = OSS_MINOR
38PATCH = OSS_PATCH
39SUFFIX = OSS_SUFFIX
40
41# Avoid flooding of warnings.
42_MAX_WARNING_COUNT = 20
43_warning_count = 0
44
45class VersionError(Exception):
46 """Exception class for version violation."""
47
48
49def _ReportVersionError(msg):
50 raise VersionError(msg)
51
52
53def ValidateProtobufRuntimeVersion(
54 gen_domain, gen_major, gen_minor, gen_patch, gen_suffix, location
55):
56 """Function to validate versions.
57
58 Args:
59 gen_domain: The domain where the code was generated from.
60 gen_major: The major version number of the gencode.
61 gen_minor: The minor version number of the gencode.
62 gen_patch: The patch version number of the gencode.
63 gen_suffix: The version suffix e.g. '-dev', '-rc1' of the gencode.
64 location: The proto location that causes the version violation.
65
66 Raises:
67 VersionError: if gencode version is invalid or incompatible with the
68 runtime.
69 """
70
71 disable_flag = os.getenv('TEMPORARILY_DISABLE_PROTOBUF_VERSION_CHECK')
72 if disable_flag is not None and disable_flag.lower() == 'true':
73 return
74
75 global _warning_count
76
77 version = f'{MAJOR}.{MINOR}.{PATCH}{SUFFIX}'
78 gen_version = f'{gen_major}.{gen_minor}.{gen_patch}{gen_suffix}'
79
80 if gen_major < 0 or gen_minor < 0 or gen_patch < 0:
81 raise VersionError(f'Invalid gencode version: {gen_version}')
82
83 error_prompt = (
84 'See Protobuf version guarantees at'
85 ' https://protobuf.dev/support/cross-version-runtime-guarantee.'
86 )
87
88 if gen_domain != DOMAIN:
89 _ReportVersionError(
90 'Detected mismatched Protobuf Gencode/Runtime domains when loading'
91 f' {location}: gencode {gen_domain.name} runtime {DOMAIN.name}.'
92 ' Cross-domain usage of Protobuf is not supported.'
93 )
94
95 if gen_major != MAJOR:
96 if gen_major == MAJOR - 1:
97 if _warning_count < _MAX_WARNING_COUNT:
98 warnings.warn(
99 'Protobuf gencode version %s is exactly one major version older'
100 ' than the runtime version %s at %s. Please update the gencode to'
101 ' avoid compatibility violations in the next runtime release.'
102 % (gen_version, version, location)
103 )
104 _warning_count += 1
105 else:
106 _ReportVersionError(
107 'Detected mismatched Protobuf Gencode/Runtime major versions when'
108 f' loading {location}: gencode {gen_version} runtime {version}.'
109 f' Same major version is required. {error_prompt}'
110 )
111
112 if MINOR < gen_minor or (MINOR == gen_minor and PATCH < gen_patch):
113 _ReportVersionError(
114 'Detected incompatible Protobuf Gencode/Runtime versions when loading'
115 f' {location}: gencode {gen_version} runtime {version}. Runtime version'
116 f' cannot be older than the linked gencode version. {error_prompt}'
117 )
118
119 if gen_suffix != SUFFIX:
120 _ReportVersionError(
121 'Detected mismatched Protobuf Gencode/Runtime version suffixes when'
122 f' loading {location}: gencode {gen_version} runtime {version}.'
123 f' Version suffixes must be the same. {error_prompt}'
124 )