MCPcopy Index your code
hub / github.com/danielgtaylor/python-betterproto / dump

Method dump

src/betterproto/__init__.py:914–1019  ·  view source on GitHub ↗

Dumps the binary encoded Protobuf message to the stream. Parameters ----------- stream: :class:`BinaryIO` The stream to dump the message to. delimit: Whether to prefix the message with a varint declaring its size.

(self, stream: "SupportsWrite[bytes]", delimit: bool = False)

Source from the content-addressed store, hash-verified

912 return meta
913
914 def dump(self, stream: "SupportsWrite[bytes]", delimit: bool = False) -> None:
915 """
916 Dumps the binary encoded Protobuf message to the stream.
917
918 Parameters
919 -----------
920 stream: :class:`BinaryIO`
921 The stream to dump the message to.
922 delimit:
923 Whether to prefix the message with a varint declaring its size.
924 """
925 if delimit == SIZE_DELIMITED:
926 dump_varint(len(self), stream)
927
928 for field_name, meta in self._betterproto.meta_by_field_name.items():
929 try:
930 value = getattr(self, field_name)
931 except AttributeError:
932 continue
933
934 if value is None:
935 # Optional items should be skipped. This is used for the Google
936 # wrapper types and proto3 field presence/optional fields.
937 continue
938
939 # Being selected in a a group means this field is the one that is
940 # currently set in a `oneof` group, so it must be serialized even
941 # if the value is the default zero value.
942 #
943 # Note that proto3 field presence/optional fields are put in a
944 # synthetic single-item oneof by protoc, which helps us ensure we
945 # send the value even if the value is the default zero value.
946 selected_in_group = bool(meta.group) or meta.optional
947
948 # Empty messages can still be sent on the wire if they were
949 # set (or received empty).
950 serialize_empty = isinstance(value, Message) and value._serialized_on_wire
951
952 include_default_value_for_oneof = self._include_default_value_for_oneof(
953 field_name=field_name, meta=meta
954 )
955
956 if value == self._get_field_default(field_name) and not (
957 selected_in_group or serialize_empty or include_default_value_for_oneof
958 ):
959 # Default (zero) values are not serialized. Two exceptions are
960 # if this is the selected oneof item or if we know we have to
961 # serialize an empty message (i.e. zero value was explicitly
962 # set by the user).
963 continue
964
965 if isinstance(value, list):
966 if meta.proto_type in PACKED_TYPES:
967 # Packed lists look like a length-delimited field. First,
968 # preprocess/encode each value into a buffer and then
969 # treat it like a field of raw bytes.
970 buf = bytearray()
971 for item in value:

Callers 7

__bytes__Method · 0.95
test_single_messageFunction · 0.80
test_multiple_messagesFunction · 0.80
test_infinite_messagesFunction · 0.80

Calls 5

_get_field_defaultMethod · 0.95
dump_varintFunction · 0.85
_preprocess_singleFunction · 0.85
_serialize_singleFunction · 0.85

Tested by 6

test_single_messageFunction · 0.64
test_multiple_messagesFunction · 0.64
test_infinite_messagesFunction · 0.64