Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/google/protobuf/reflection.py: 62%
13 statements
« prev ^ index » next coverage.py v7.3.2, created at 2023-12-08 06:40 +0000
« prev ^ index » next coverage.py v7.3.2, created at 2023-12-08 06:40 +0000
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
8# This code is meant to work on Python 2.4 and above only.
10"""Contains a metaclass and helper functions used to create
11protocol message classes from Descriptor objects at runtime.
13Recall that a metaclass is the "type" of a class.
14(A class is to a metaclass what an instance is to a class.)
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.
20The upshot of all this is that the real implementation
21details for ALL pure-Python protocol buffers are *here in
22this file*.
23"""
25__author__ = 'robinson@google.com (Will Robinson)'
28from google.protobuf import message_factory
29from google.protobuf import symbol_database
31# The type of all Message classes.
32# Part of the public interface, but normally only used by message factories.
33GeneratedProtocolMessageType = message_factory._GENERATED_PROTOCOL_MESSAGE_TYPE
35MESSAGE_CLASS_CACHE = {}
38# Deprecated. Please NEVER use reflection.ParseMessage().
39def ParseMessage(descriptor, byte_str):
40 """Generate a new Message instance from this Descriptor and a byte string.
42 DEPRECATED: ParseMessage is deprecated because it is using MakeClass().
43 Please use MessageFactory.GetPrototype() instead.
45 Args:
46 descriptor: Protobuf Descriptor object
47 byte_str: Serialized protocol buffer byte string
49 Returns:
50 Newly created protobuf Message object.
51 """
52 result_class = MakeClass(descriptor)
53 new_msg = result_class()
54 new_msg.ParseFromString(byte_str)
55 return new_msg
58# Deprecated. Please NEVER use reflection.MakeClass().
59def MakeClass(descriptor):
60 """Construct a class object for a protobuf described by descriptor.
62 DEPRECATED: use MessageFactory.GetPrototype() instead.
64 Args:
65 descriptor: A descriptor.Descriptor object describing the protobuf.
66 Returns:
67 The Message class object described by the descriptor.
68 """
69 # Original implementation leads to duplicate message classes, which won't play
70 # well with extensions. Message factory info is also missing.
71 # Redirect to message_factory.
72 return message_factory.GetMessageClass(descriptor)