Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/google/protobuf/runtime_version.py: 80%

Shortcuts on this page

r m x   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

30 statements  

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 

18 

19 

20class Domain(Enum): 

21 GOOGLE_INTERNAL = 1 

22 PUBLIC = 2 

23 

24 

25class VersionError(Exception): 

26 """Exception class for version violation.""" 

27 

28 

29# The versions of this Python Protobuf runtime to be changed automatically by 

30# the Protobuf release process. Do not edit them manually. 

31DOMAIN = Domain.PUBLIC 

32MAJOR = 5 

33MINOR = 27 

34PATCH = 2 

35SUFFIX = '' 

36 

37 

38def ValidateProtobufRuntimeVersion( 

39 gen_domain, gen_major, gen_minor, gen_patch, gen_suffix, location 

40): 

41 """Function to validate versions. 

42 

43 Args: 

44 gen_domain: The domain where the code was generated from. 

45 gen_major: The major version number of the gencode. 

46 gen_minor: The minor version number of the gencode. 

47 gen_patch: The patch version number of the gencode. 

48 gen_suffix: The version suffix e.g. '-dev', '-rc1' of the gencode. 

49 location: The proto location that causes the version violation. 

50 

51 Raises: 

52 VersionError: if gencode version is invalid or incompatible with the 

53 runtime. 

54 """ 

55 

56 disable_flag = os.getenv('TEMORARILY_DISABLE_PROTOBUF_VERSION_CHECK') 

57 if disable_flag is not None and disable_flag.lower() == 'true': 

58 return 

59 

60 version = f'{MAJOR}.{MINOR}.{PATCH}{SUFFIX}' 

61 gen_version = f'{gen_major}.{gen_minor}.{gen_patch}{gen_suffix}' 

62 

63 if gen_major < 0 or gen_minor < 0 or gen_patch < 0: 

64 raise VersionError(f'Invalid gencode version: {gen_version}') 

65 

66 error_prompt = ( 

67 'See Protobuf version guarantees at' 

68 ' https://protobuf.dev/support/cross-version-runtime-guarantee.' 

69 ) 

70 

71 if gen_domain != DOMAIN: 

72 raise VersionError( 

73 'Detected mismatched Protobuf Gencode/Runtime domains when loading' 

74 f' {location}: gencode {gen_domain.name} runtime {DOMAIN.name}.' 

75 ' Cross-domain usage of Protobuf is not supported.' 

76 ) 

77 

78 if gen_major != MAJOR: 

79 raise VersionError( 

80 'Detected mismatched Protobuf Gencode/Runtime major versions when' 

81 f' loading {location}: gencode {gen_version} runtime {version}.' 

82 f' Same major version is required. {error_prompt}' 

83 ) 

84 

85 if MINOR < gen_minor or (MINOR == gen_minor and PATCH < gen_patch): 

86 raise VersionError( 

87 'Detected incompatible Protobuf Gencode/Runtime versions when loading' 

88 f' {location}: gencode {gen_version} runtime {version}. Runtime version' 

89 f' cannot be older than the linked gencode version. {error_prompt}' 

90 ) 

91 

92 if gen_suffix != SUFFIX: 

93 raise VersionError( 

94 'Detected mismatched Protobuf Gencode/Runtime version suffixes when' 

95 f' loading {location}: gencode {gen_version} runtime {version}.' 

96 f' Version suffixes must be the same. {error_prompt}' 

97 )