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

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

16 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# This code is meant to work on Python 2.4 and above only. 

9 

10"""Contains a metaclass and helper functions used to create 

11protocol message classes from Descriptor objects at runtime. 

12 

13Recall that a metaclass is the "type" of a class. 

14(A class is to a metaclass what an instance is to a class.) 

15 

16In this case, we use the GeneratedProtocolMessageType metaclass 

17to inject all the useful functionality into the classes 

18output by the protocol compiler at compile-time. 

19 

20The upshot of all this is that the real implementation 

21details for ALL pure-Python protocol buffers are *here in 

22this file*. 

23""" 

24 

25__author__ = 'robinson@google.com (Will Robinson)' 

26 

27import warnings 

28 

29from google.protobuf import message_factory 

30from google.protobuf import symbol_database 

31 

32# The type of all Message classes. 

33# Part of the public interface, but normally only used by message factories. 

34GeneratedProtocolMessageType = message_factory._GENERATED_PROTOCOL_MESSAGE_TYPE 

35 

36MESSAGE_CLASS_CACHE = {} 

37 

38 

39# Deprecated. Please NEVER use reflection.ParseMessage(). 

40def ParseMessage(descriptor, byte_str): 

41 """Generate a new Message instance from this Descriptor and a byte string. 

42 

43 DEPRECATED: ParseMessage is deprecated because it is using MakeClass(). 

44 Please use MessageFactory.GetMessageClass() instead. 

45 

46 Args: 

47 descriptor: Protobuf Descriptor object 

48 byte_str: Serialized protocol buffer byte string 

49 

50 Returns: 

51 Newly created protobuf Message object. 

52 """ 

53 warnings.warn( 

54 'reflection.ParseMessage() is deprecated. Please use ' 

55 'MessageFactory.GetMessageClass() and message.ParseFromString() instead. ' 

56 'reflection.ParseMessage() will be removed in Jan 2025.', 

57 stacklevel=2, 

58 ) 

59 result_class = MakeClass(descriptor) 

60 new_msg = result_class() 

61 new_msg.ParseFromString(byte_str) 

62 return new_msg 

63 

64 

65# Deprecated. Please NEVER use reflection.MakeClass(). 

66def MakeClass(descriptor): 

67 """Construct a class object for a protobuf described by descriptor. 

68 

69 DEPRECATED: use MessageFactory.GetMessageClass() instead. 

70 

71 Args: 

72 descriptor: A descriptor.Descriptor object describing the protobuf. 

73 Returns: 

74 The Message class object described by the descriptor. 

75 """ 

76 warnings.warn( 

77 'reflection.MakeClass() is deprecated. Please use ' 

78 'MessageFactory.GetMessageClass() instead. ' 

79 'reflection.MakeClass() will be removed in Jan 2025.', 

80 stacklevel=2, 

81 ) 

82 # Original implementation leads to duplicate message classes, which won't play 

83 # well with extensions. Message factory info is also missing. 

84 # Redirect to message_factory. 

85 return message_factory.GetMessageClass(descriptor)