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"""Protobuf Runtime versions and validators.
8
9It should only be accessed by Protobuf gencodes and tests. DO NOT USE it
10elsewhere.
11"""
12
13__author__ = 'shaod@google.com (Dennis Shao)'
14
15from enum import Enum
16
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 = 7
31OSS_MINOR = 36
32OSS_PATCH = 0
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
45
46class VersionError(Exception):
47 """Exception class for version violation."""
48
49
50def _ReportVersionError(msg):
51 raise VersionError(msg)
52
53
54def ValidateProtobufRuntimeVersion(
55 gen_domain, gen_major, gen_minor, gen_patch, gen_suffix, location
56):
57 """Function to validate versions.
58
59 Args:
60 gen_domain: The domain where the code was generated from.
61 gen_major: The major version number of the gencode.
62 gen_minor: The minor version number of the gencode.
63 gen_patch: The patch version number of the gencode.
64 gen_suffix: The version suffix e.g. '-dev', '-rc1' of the gencode.
65 location: The proto location that causes the version violation.
66
67 Raises:
68 VersionError: if gencode version is invalid or incompatible with the
69 runtime.
70 """
71
72 disable_flag = os.getenv('TEMPORARILY_DISABLE_PROTOBUF_VERSION_CHECK')
73 if disable_flag is not None and disable_flag.lower() == 'true':
74 return
75
76 global _warning_count
77
78 version = f'{MAJOR}.{MINOR}.{PATCH}{SUFFIX}'
79 gen_version = f'{gen_major}.{gen_minor}.{gen_patch}{gen_suffix}'
80
81 if gen_major < 0 or gen_minor < 0 or gen_patch < 0:
82 raise VersionError(f'Invalid gencode version: {gen_version}')
83
84 error_prompt = (
85 'See Protobuf version guarantees at'
86 ' https://protobuf.dev/support/cross-version-runtime-guarantee.'
87 )
88
89 if gen_domain != DOMAIN:
90 _ReportVersionError(
91 'Detected mismatched Protobuf Gencode/Runtime domains when loading'
92 f' {location}: gencode {gen_domain.name} runtime {DOMAIN.name}.'
93 ' Cross-domain usage of Protobuf is not supported.'
94 )
95
96 if (
97 MAJOR < gen_major
98 or (MAJOR == gen_major and MINOR < gen_minor)
99 or (MAJOR == gen_major and MINOR == gen_minor and PATCH < gen_patch)
100 ):
101 _ReportVersionError(
102 'Detected incompatible Protobuf Gencode/Runtime versions when loading'
103 f' {location}: gencode {gen_version} runtime {version}. Runtime version'
104 f' cannot be older than the linked gencode version. {error_prompt}'
105 )