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 = 33 
    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 
    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 ( 
    96      MAJOR < gen_major 
    97      or (MAJOR == gen_major and MINOR < gen_minor) 
    98      or (MAJOR == gen_major and MINOR == gen_minor and PATCH < gen_patch) 
    99  ): 
    100    _ReportVersionError( 
    101        'Detected incompatible Protobuf Gencode/Runtime versions when loading' 
    102        f' {location}: gencode {gen_version} runtime {version}. Runtime version' 
    103        f' cannot be older than the linked gencode version. {error_prompt}' 
    104    )