ortools.sat.cp_model_pb2
Generated protocol buffer code.
1# -*- coding: utf-8 -*- 2# Generated by the protocol buffer compiler. DO NOT EDIT! 3# source: ortools/sat/cp_model.proto 4# Protobuf Python Version: 4.25.3 5"""Generated protocol buffer code.""" 6from google.protobuf import descriptor as _descriptor 7from google.protobuf import descriptor_pool as _descriptor_pool 8from google.protobuf import symbol_database as _symbol_database 9from google.protobuf.internal import builder as _builder 10# @@protoc_insertion_point(imports) 11 12_sym_db = _symbol_database.Default() 13 14 15 16 17DESCRIPTOR = _descriptor_pool.Default().AddSerializedFile(b'\n\x1aortools/sat/cp_model.proto\x12\x17operations_research.sat\"4\n\x14IntegerVariableProto\x12\x0c\n\x04name\x18\x01 \x01(\t\x12\x0e\n\x06\x64omain\x18\x02 \x03(\x03\"%\n\x11\x42oolArgumentProto\x12\x10\n\x08literals\x18\x01 \x03(\x05\"E\n\x15LinearExpressionProto\x12\x0c\n\x04vars\x18\x01 \x03(\x05\x12\x0e\n\x06\x63oeffs\x18\x02 \x03(\x03\x12\x0e\n\x06offset\x18\x03 \x01(\x03\"\x94\x01\n\x13LinearArgumentProto\x12>\n\x06target\x18\x01 \x01(\x0b\x32..operations_research.sat.LinearExpressionProto\x12=\n\x05\x65xprs\x18\x02 \x03(\x0b\x32..operations_research.sat.LinearExpressionProto\"\\\n\x1b\x41llDifferentConstraintProto\x12=\n\x05\x65xprs\x18\x01 \x03(\x0b\x32..operations_research.sat.LinearExpressionProto\"E\n\x15LinearConstraintProto\x12\x0c\n\x04vars\x18\x01 \x03(\x05\x12\x0e\n\x06\x63oeffs\x18\x02 \x03(\x03\x12\x0e\n\x06\x64omain\x18\x03 \x03(\x03\"E\n\x16\x45lementConstraintProto\x12\r\n\x05index\x18\x01 \x01(\x05\x12\x0e\n\x06target\x18\x02 \x01(\x05\x12\x0c\n\x04vars\x18\x03 \x03(\x05\"\xd3\x01\n\x17IntervalConstraintProto\x12=\n\x05start\x18\x04 \x01(\x0b\x32..operations_research.sat.LinearExpressionProto\x12;\n\x03\x65nd\x18\x05 \x01(\x0b\x32..operations_research.sat.LinearExpressionProto\x12<\n\x04size\x18\x06 \x01(\x0b\x32..operations_research.sat.LinearExpressionProto\"-\n\x18NoOverlapConstraintProto\x12\x11\n\tintervals\x18\x01 \x03(\x05\"F\n\x1aNoOverlap2DConstraintProto\x12\x13\n\x0bx_intervals\x18\x01 \x03(\x05\x12\x13\n\x0by_intervals\x18\x02 \x03(\x05\"\xb1\x01\n\x19\x43umulativeConstraintProto\x12@\n\x08\x63\x61pacity\x18\x01 \x01(\x0b\x32..operations_research.sat.LinearExpressionProto\x12\x11\n\tintervals\x18\x02 \x03(\x05\x12?\n\x07\x64\x65mands\x18\x03 \x03(\x0b\x32..operations_research.sat.LinearExpressionProto\"\xea\x01\n\x18ReservoirConstraintProto\x12\x11\n\tmin_level\x18\x01 \x01(\x03\x12\x11\n\tmax_level\x18\x02 \x01(\x03\x12\x42\n\ntime_exprs\x18\x03 \x03(\x0b\x32..operations_research.sat.LinearExpressionProto\x12\x45\n\rlevel_changes\x18\x06 \x03(\x0b\x32..operations_research.sat.LinearExpressionProto\x12\x17\n\x0f\x61\x63tive_literals\x18\x05 \x03(\x05J\x04\x08\x04\x10\x05\"H\n\x16\x43ircuitConstraintProto\x12\r\n\x05tails\x18\x03 \x03(\x05\x12\r\n\x05heads\x18\x04 \x03(\x05\x12\x10\n\x08literals\x18\x05 \x03(\x05\"j\n\x15RoutesConstraintProto\x12\r\n\x05tails\x18\x01 \x03(\x05\x12\r\n\x05heads\x18\x02 \x03(\x05\x12\x10\n\x08literals\x18\x03 \x03(\x05\x12\x0f\n\x07\x64\x65mands\x18\x04 \x03(\x05\x12\x10\n\x08\x63\x61pacity\x18\x05 \x01(\x03\"E\n\x14TableConstraintProto\x12\x0c\n\x04vars\x18\x01 \x03(\x05\x12\x0e\n\x06values\x18\x02 \x03(\x03\x12\x0f\n\x07negated\x18\x03 \x01(\x08\"=\n\x16InverseConstraintProto\x12\x10\n\x08\x66_direct\x18\x01 \x03(\x05\x12\x11\n\tf_inverse\x18\x02 \x03(\x05\"\xa2\x01\n\x18\x41utomatonConstraintProto\x12\x16\n\x0estarting_state\x18\x02 \x01(\x03\x12\x14\n\x0c\x66inal_states\x18\x03 \x03(\x03\x12\x17\n\x0ftransition_tail\x18\x04 \x03(\x03\x12\x17\n\x0ftransition_head\x18\x05 \x03(\x03\x12\x18\n\x10transition_label\x18\x06 \x03(\x03\x12\x0c\n\x04vars\x18\x07 \x03(\x05\"$\n\x14ListOfVariablesProto\x12\x0c\n\x04vars\x18\x01 \x03(\x05\"\xf0\x0c\n\x0f\x43onstraintProto\x12\x0c\n\x04name\x18\x01 \x01(\t\x12\x1b\n\x13\x65nforcement_literal\x18\x02 \x03(\x05\x12=\n\x07\x62ool_or\x18\x03 \x01(\x0b\x32*.operations_research.sat.BoolArgumentProtoH\x00\x12>\n\x08\x62ool_and\x18\x04 \x01(\x0b\x32*.operations_research.sat.BoolArgumentProtoH\x00\x12\x41\n\x0b\x61t_most_one\x18\x1a \x01(\x0b\x32*.operations_research.sat.BoolArgumentProtoH\x00\x12\x41\n\x0b\x65xactly_one\x18\x1d \x01(\x0b\x32*.operations_research.sat.BoolArgumentProtoH\x00\x12>\n\x08\x62ool_xor\x18\x05 \x01(\x0b\x32*.operations_research.sat.BoolArgumentProtoH\x00\x12?\n\x07int_div\x18\x07 \x01(\x0b\x32,.operations_research.sat.LinearArgumentProtoH\x00\x12?\n\x07int_mod\x18\x08 \x01(\x0b\x32,.operations_research.sat.LinearArgumentProtoH\x00\x12@\n\x08int_prod\x18\x0b \x01(\x0b\x32,.operations_research.sat.LinearArgumentProtoH\x00\x12?\n\x07lin_max\x18\x1b \x01(\x0b\x32,.operations_research.sat.LinearArgumentProtoH\x00\x12@\n\x06linear\x18\x0c \x01(\x0b\x32..operations_research.sat.LinearConstraintProtoH\x00\x12H\n\x08\x61ll_diff\x18\r \x01(\x0b\x32\x34.operations_research.sat.AllDifferentConstraintProtoH\x00\x12\x42\n\x07\x65lement\x18\x0e \x01(\x0b\x32/.operations_research.sat.ElementConstraintProtoH\x00\x12\x42\n\x07\x63ircuit\x18\x0f \x01(\x0b\x32/.operations_research.sat.CircuitConstraintProtoH\x00\x12@\n\x06routes\x18\x17 \x01(\x0b\x32..operations_research.sat.RoutesConstraintProtoH\x00\x12>\n\x05table\x18\x10 \x01(\x0b\x32-.operations_research.sat.TableConstraintProtoH\x00\x12\x46\n\tautomaton\x18\x11 \x01(\x0b\x32\x31.operations_research.sat.AutomatonConstraintProtoH\x00\x12\x42\n\x07inverse\x18\x12 \x01(\x0b\x32/.operations_research.sat.InverseConstraintProtoH\x00\x12\x46\n\treservoir\x18\x18 \x01(\x0b\x32\x31.operations_research.sat.ReservoirConstraintProtoH\x00\x12\x44\n\x08interval\x18\x13 \x01(\x0b\x32\x30.operations_research.sat.IntervalConstraintProtoH\x00\x12G\n\nno_overlap\x18\x14 \x01(\x0b\x32\x31.operations_research.sat.NoOverlapConstraintProtoH\x00\x12L\n\rno_overlap_2d\x18\x15 \x01(\x0b\x32\x33.operations_research.sat.NoOverlap2DConstraintProtoH\x00\x12H\n\ncumulative\x18\x16 \x01(\x0b\x32\x32.operations_research.sat.CumulativeConstraintProtoH\x00\x12I\n\x10\x64ummy_constraint\x18\x1e \x01(\x0b\x32-.operations_research.sat.ListOfVariablesProtoH\x00\x42\x0c\n\nconstraint\"\xe0\x01\n\x10\x43pObjectiveProto\x12\x0c\n\x04vars\x18\x01 \x03(\x05\x12\x0e\n\x06\x63oeffs\x18\x04 \x03(\x03\x12\x0e\n\x06offset\x18\x02 \x01(\x01\x12\x16\n\x0escaling_factor\x18\x03 \x01(\x01\x12\x0e\n\x06\x64omain\x18\x05 \x03(\x03\x12\x19\n\x11scaling_was_exact\x18\x06 \x01(\x08\x12\x1d\n\x15integer_before_offset\x18\x07 \x01(\x03\x12\x1c\n\x14integer_after_offset\x18\t \x01(\x03\x12\x1e\n\x16integer_scaling_factor\x18\x08 \x01(\x03\"U\n\x13\x46loatObjectiveProto\x12\x0c\n\x04vars\x18\x01 \x03(\x05\x12\x0e\n\x06\x63oeffs\x18\x02 \x03(\x01\x12\x0e\n\x06offset\x18\x03 \x01(\x01\x12\x10\n\x08maximize\x18\x04 \x01(\x08\"\xe9\x04\n\x15\x44\x65\x63isionStrategyProto\x12\x11\n\tvariables\x18\x01 \x03(\x05\x12=\n\x05\x65xprs\x18\x05 \x03(\x0b\x32..operations_research.sat.LinearExpressionProto\x12m\n\x1bvariable_selection_strategy\x18\x02 \x01(\x0e\x32H.operations_research.sat.DecisionStrategyProto.VariableSelectionStrategy\x12i\n\x19\x64omain_reduction_strategy\x18\x03 \x01(\x0e\x32\x46.operations_research.sat.DecisionStrategyProto.DomainReductionStrategy\"\x94\x01\n\x19VariableSelectionStrategy\x12\x10\n\x0c\x43HOOSE_FIRST\x10\x00\x12\x15\n\x11\x43HOOSE_LOWEST_MIN\x10\x01\x12\x16\n\x12\x43HOOSE_HIGHEST_MAX\x10\x02\x12\x1a\n\x16\x43HOOSE_MIN_DOMAIN_SIZE\x10\x03\x12\x1a\n\x16\x43HOOSE_MAX_DOMAIN_SIZE\x10\x04\"\x8c\x01\n\x17\x44omainReductionStrategy\x12\x14\n\x10SELECT_MIN_VALUE\x10\x00\x12\x14\n\x10SELECT_MAX_VALUE\x10\x01\x12\x15\n\x11SELECT_LOWER_HALF\x10\x02\x12\x15\n\x11SELECT_UPPER_HALF\x10\x03\x12\x17\n\x13SELECT_MEDIAN_VALUE\x10\x04\"9\n\x19PartialVariableAssignment\x12\x0c\n\x04vars\x18\x01 \x03(\x05\x12\x0e\n\x06values\x18\x02 \x03(\x03\">\n\x16SparsePermutationProto\x12\x0f\n\x07support\x18\x01 \x03(\x05\x12\x13\n\x0b\x63ycle_sizes\x18\x02 \x03(\x05\"G\n\x10\x44\x65nseMatrixProto\x12\x10\n\x08num_rows\x18\x01 \x01(\x05\x12\x10\n\x08num_cols\x18\x02 \x01(\x05\x12\x0f\n\x07\x65ntries\x18\x03 \x03(\x05\"\x94\x01\n\rSymmetryProto\x12\x45\n\x0cpermutations\x18\x01 \x03(\x0b\x32/.operations_research.sat.SparsePermutationProto\x12<\n\torbitopes\x18\x02 \x03(\x0b\x32).operations_research.sat.DenseMatrixProto\"\x8e\x04\n\x0c\x43pModelProto\x12\x0c\n\x04name\x18\x01 \x01(\t\x12@\n\tvariables\x18\x02 \x03(\x0b\x32-.operations_research.sat.IntegerVariableProto\x12=\n\x0b\x63onstraints\x18\x03 \x03(\x0b\x32(.operations_research.sat.ConstraintProto\x12<\n\tobjective\x18\x04 \x01(\x0b\x32).operations_research.sat.CpObjectiveProto\x12N\n\x18\x66loating_point_objective\x18\t \x01(\x0b\x32,.operations_research.sat.FloatObjectiveProto\x12G\n\x0fsearch_strategy\x18\x05 \x03(\x0b\x32..operations_research.sat.DecisionStrategyProto\x12I\n\rsolution_hint\x18\x06 \x01(\x0b\x32\x32.operations_research.sat.PartialVariableAssignment\x12\x13\n\x0b\x61ssumptions\x18\x07 \x03(\x05\x12\x38\n\x08symmetry\x18\x08 \x01(\x0b\x32&.operations_research.sat.SymmetryProto\"\"\n\x10\x43pSolverSolution\x12\x0e\n\x06values\x18\x01 \x03(\x03\"\x95\x06\n\x10\x43pSolverResponse\x12\x37\n\x06status\x18\x01 \x01(\x0e\x32\'.operations_research.sat.CpSolverStatus\x12\x10\n\x08solution\x18\x02 \x03(\x03\x12\x17\n\x0fobjective_value\x18\x03 \x01(\x01\x12\x1c\n\x14\x62\x65st_objective_bound\x18\x04 \x01(\x01\x12G\n\x14\x61\x64\x64itional_solutions\x18\x1b \x03(\x0b\x32).operations_research.sat.CpSolverSolution\x12J\n\x13tightened_variables\x18\x15 \x03(\x0b\x32-.operations_research.sat.IntegerVariableProto\x12\x30\n(sufficient_assumptions_for_infeasibility\x18\x17 \x03(\x05\x12\x44\n\x11integer_objective\x18\x1c \x01(\x0b\x32).operations_research.sat.CpObjectiveProto\x12#\n\x1binner_objective_lower_bound\x18\x1d \x01(\x03\x12\x14\n\x0cnum_integers\x18\x1e \x01(\x03\x12\x14\n\x0cnum_booleans\x18\n \x01(\x03\x12\x15\n\rnum_conflicts\x18\x0b \x01(\x03\x12\x14\n\x0cnum_branches\x18\x0c \x01(\x03\x12\x1f\n\x17num_binary_propagations\x18\r \x01(\x03\x12 \n\x18num_integer_propagations\x18\x0e \x01(\x03\x12\x14\n\x0cnum_restarts\x18\x18 \x01(\x03\x12\x19\n\x11num_lp_iterations\x18\x19 \x01(\x03\x12\x11\n\twall_time\x18\x0f \x01(\x01\x12\x11\n\tuser_time\x18\x10 \x01(\x01\x12\x1a\n\x12\x64\x65terministic_time\x18\x11 \x01(\x01\x12\x14\n\x0cgap_integral\x18\x16 \x01(\x01\x12\x15\n\rsolution_info\x18\x14 \x01(\t\x12\x11\n\tsolve_log\x18\x1a \x01(\t*[\n\x0e\x43pSolverStatus\x12\x0b\n\x07UNKNOWN\x10\x00\x12\x11\n\rMODEL_INVALID\x10\x01\x12\x0c\n\x08\x46\x45\x41SIBLE\x10\x02\x12\x0e\n\nINFEASIBLE\x10\x03\x12\x0b\n\x07OPTIMAL\x10\x04\x42@\n\x16\x63om.google.ortools.satB\x0f\x43pModelProtobufP\x01\xaa\x02\x12Google.OrTools.Satb\x06proto3') 18 19_globals = globals() 20_builder.BuildMessageAndEnumDescriptors(DESCRIPTOR, _globals) 21_builder.BuildTopDescriptorsAndMessages(DESCRIPTOR, 'ortools.sat.cp_model_pb2', _globals) 22if _descriptor._USE_C_DESCRIPTORS == False: 23 _globals['DESCRIPTOR']._options = None 24 _globals['DESCRIPTOR']._serialized_options = b'\n\026com.google.ortools.satB\017CpModelProtobufP\001\252\002\022Google.OrTools.Sat' 25 _globals['_CPSOLVERSTATUS']._serialized_start=6164 26 _globals['_CPSOLVERSTATUS']._serialized_end=6255 27 _globals['_INTEGERVARIABLEPROTO']._serialized_start=55 28 _globals['_INTEGERVARIABLEPROTO']._serialized_end=107 29 _globals['_BOOLARGUMENTPROTO']._serialized_start=109 30 _globals['_BOOLARGUMENTPROTO']._serialized_end=146 31 _globals['_LINEAREXPRESSIONPROTO']._serialized_start=148 32 _globals['_LINEAREXPRESSIONPROTO']._serialized_end=217 33 _globals['_LINEARARGUMENTPROTO']._serialized_start=220 34 _globals['_LINEARARGUMENTPROTO']._serialized_end=368 35 _globals['_ALLDIFFERENTCONSTRAINTPROTO']._serialized_start=370 36 _globals['_ALLDIFFERENTCONSTRAINTPROTO']._serialized_end=462 37 _globals['_LINEARCONSTRAINTPROTO']._serialized_start=464 38 _globals['_LINEARCONSTRAINTPROTO']._serialized_end=533 39 _globals['_ELEMENTCONSTRAINTPROTO']._serialized_start=535 40 _globals['_ELEMENTCONSTRAINTPROTO']._serialized_end=604 41 _globals['_INTERVALCONSTRAINTPROTO']._serialized_start=607 42 _globals['_INTERVALCONSTRAINTPROTO']._serialized_end=818 43 _globals['_NOOVERLAPCONSTRAINTPROTO']._serialized_start=820 44 _globals['_NOOVERLAPCONSTRAINTPROTO']._serialized_end=865 45 _globals['_NOOVERLAP2DCONSTRAINTPROTO']._serialized_start=867 46 _globals['_NOOVERLAP2DCONSTRAINTPROTO']._serialized_end=937 47 _globals['_CUMULATIVECONSTRAINTPROTO']._serialized_start=940 48 _globals['_CUMULATIVECONSTRAINTPROTO']._serialized_end=1117 49 _globals['_RESERVOIRCONSTRAINTPROTO']._serialized_start=1120 50 _globals['_RESERVOIRCONSTRAINTPROTO']._serialized_end=1354 51 _globals['_CIRCUITCONSTRAINTPROTO']._serialized_start=1356 52 _globals['_CIRCUITCONSTRAINTPROTO']._serialized_end=1428 53 _globals['_ROUTESCONSTRAINTPROTO']._serialized_start=1430 54 _globals['_ROUTESCONSTRAINTPROTO']._serialized_end=1536 55 _globals['_TABLECONSTRAINTPROTO']._serialized_start=1538 56 _globals['_TABLECONSTRAINTPROTO']._serialized_end=1607 57 _globals['_INVERSECONSTRAINTPROTO']._serialized_start=1609 58 _globals['_INVERSECONSTRAINTPROTO']._serialized_end=1670 59 _globals['_AUTOMATONCONSTRAINTPROTO']._serialized_start=1673 60 _globals['_AUTOMATONCONSTRAINTPROTO']._serialized_end=1835 61 _globals['_LISTOFVARIABLESPROTO']._serialized_start=1837 62 _globals['_LISTOFVARIABLESPROTO']._serialized_end=1873 63 _globals['_CONSTRAINTPROTO']._serialized_start=1876 64 _globals['_CONSTRAINTPROTO']._serialized_end=3524 65 _globals['_CPOBJECTIVEPROTO']._serialized_start=3527 66 _globals['_CPOBJECTIVEPROTO']._serialized_end=3751 67 _globals['_FLOATOBJECTIVEPROTO']._serialized_start=3753 68 _globals['_FLOATOBJECTIVEPROTO']._serialized_end=3838 69 _globals['_DECISIONSTRATEGYPROTO']._serialized_start=3841 70 _globals['_DECISIONSTRATEGYPROTO']._serialized_end=4458 71 _globals['_DECISIONSTRATEGYPROTO_VARIABLESELECTIONSTRATEGY']._serialized_start=4167 72 _globals['_DECISIONSTRATEGYPROTO_VARIABLESELECTIONSTRATEGY']._serialized_end=4315 73 _globals['_DECISIONSTRATEGYPROTO_DOMAINREDUCTIONSTRATEGY']._serialized_start=4318 74 _globals['_DECISIONSTRATEGYPROTO_DOMAINREDUCTIONSTRATEGY']._serialized_end=4458 75 _globals['_PARTIALVARIABLEASSIGNMENT']._serialized_start=4460 76 _globals['_PARTIALVARIABLEASSIGNMENT']._serialized_end=4517 77 _globals['_SPARSEPERMUTATIONPROTO']._serialized_start=4519 78 _globals['_SPARSEPERMUTATIONPROTO']._serialized_end=4581 79 _globals['_DENSEMATRIXPROTO']._serialized_start=4583 80 _globals['_DENSEMATRIXPROTO']._serialized_end=4654 81 _globals['_SYMMETRYPROTO']._serialized_start=4657 82 _globals['_SYMMETRYPROTO']._serialized_end=4805 83 _globals['_CPMODELPROTO']._serialized_start=4808 84 _globals['_CPMODELPROTO']._serialized_end=5334 85 _globals['_CPSOLVERSOLUTION']._serialized_start=5336 86 _globals['_CPSOLVERSOLUTION']._serialized_end=5370 87 _globals['_CPSOLVERRESPONSE']._serialized_start=5373 88 _globals['_CPSOLVERRESPONSE']._serialized_end=6162 89# @@protoc_insertion_point(module_scope)
Abstract base class for protocol messages.
Protocol message classes are almost always generated by the protocol compiler. These generated types subclass Message and implement the methods shown below.
496 def init(self, **kwargs): 497 self._cached_byte_size = 0 498 self._cached_byte_size_dirty = len(kwargs) > 0 499 self._fields = {} 500 # Contains a mapping from oneof field descriptors to the descriptor 501 # of the currently set field in that oneof field. 502 self._oneofs = {} 503 504 # _unknown_fields is () when empty for efficiency, and will be turned into 505 # a list if fields are added. 506 self._unknown_fields = () 507 # _unknown_field_set is None when empty for efficiency, and will be 508 # turned into UnknownFieldSet struct if fields are added. 509 self._unknown_field_set = None # pylint: disable=protected-access 510 self._is_present_in_parent = False 511 self._listener = message_listener_mod.NullMessageListener() 512 self._listener_for_children = _Listener(self) 513 for field_name, field_value in kwargs.items(): 514 field = _GetFieldByName(message_descriptor, field_name) 515 if field is None: 516 raise TypeError('%s() got an unexpected keyword argument "%s"' % 517 (message_descriptor.name, field_name)) 518 if field_value is None: 519 # field=None is the same as no field at all. 520 continue 521 if field.label == _FieldDescriptor.LABEL_REPEATED: 522 copy = field._default_constructor(self) 523 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: # Composite 524 if _IsMapField(field): 525 if _IsMessageMapField(field): 526 for key in field_value: 527 copy[key].MergeFrom(field_value[key]) 528 else: 529 copy.update(field_value) 530 else: 531 for val in field_value: 532 if isinstance(val, dict): 533 copy.add(**val) 534 else: 535 copy.add().MergeFrom(val) 536 else: # Scalar 537 if field.cpp_type == _FieldDescriptor.CPPTYPE_ENUM: 538 field_value = [_GetIntegerEnumValue(field.enum_type, val) 539 for val in field_value] 540 copy.extend(field_value) 541 self._fields[field] = copy 542 elif field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 543 copy = field._default_constructor(self) 544 new_val = field_value 545 if isinstance(field_value, dict): 546 new_val = field.message_type._concrete_class(**field_value) 547 try: 548 copy.MergeFrom(new_val) 549 except TypeError: 550 _ReraiseTypeErrorWithFieldName(message_descriptor.name, field_name) 551 self._fields[field] = copy 552 else: 553 if field.cpp_type == _FieldDescriptor.CPPTYPE_ENUM: 554 field_value = _GetIntegerEnumValue(field.enum_type, field_value) 555 try: 556 setattr(self, field_name, field_value) 557 except TypeError: 558 _ReraiseTypeErrorWithFieldName(message_descriptor.name, field_name)
1312 def MergeFrom(self, msg): 1313 if not isinstance(msg, cls): 1314 raise TypeError( 1315 'Parameter to MergeFrom() must be instance of same class: ' 1316 'expected %s got %s.' % (_FullyQualifiedClassName(cls), 1317 _FullyQualifiedClassName(msg.__class__))) 1318 1319 assert msg is not self 1320 self._Modified() 1321 1322 fields = self._fields 1323 1324 for field, value in msg._fields.items(): 1325 if field.label == LABEL_REPEATED: 1326 field_value = fields.get(field) 1327 if field_value is None: 1328 # Construct a new object to represent this field. 1329 field_value = field._default_constructor(self) 1330 fields[field] = field_value 1331 field_value.MergeFrom(value) 1332 elif field.cpp_type == CPPTYPE_MESSAGE: 1333 if value._is_present_in_parent: 1334 field_value = fields.get(field) 1335 if field_value is None: 1336 # Construct a new object to represent this field. 1337 field_value = field._default_constructor(self) 1338 fields[field] = field_value 1339 field_value.MergeFrom(value) 1340 else: 1341 self._fields[field] = value 1342 if field.containing_oneof: 1343 self._UpdateOneofState(field) 1344 1345 if msg._unknown_fields: 1346 if not self._unknown_fields: 1347 self._unknown_fields = [] 1348 self._unknown_fields.extend(msg._unknown_fields) 1349 # pylint: disable=protected-access 1350 if self._unknown_field_set is None: 1351 self._unknown_field_set = containers.UnknownFieldSet() 1352 self._unknown_field_set._extend(msg._unknown_field_set)
Merges the contents of the specified message into current message.
This method merges the contents of the specified message into the current message. Singular fields that are set in the specified message overwrite the corresponding fields in the current message. Repeated fields are appended. Singular sub-messages and groups are recursively merged.
Arguments:
- other_msg (Message): A message to merge into the current message.
1375def _Clear(self): 1376 # Clear fields. 1377 self._fields = {} 1378 self._unknown_fields = () 1379 # pylint: disable=protected-access 1380 if self._unknown_field_set is not None: 1381 self._unknown_field_set._clear() 1382 self._unknown_field_set = None 1383 1384 self._oneofs = {} 1385 self._Modified()
Clears all data that was set in the message.
1452 def Modified(self): 1453 """Sets the _cached_byte_size_dirty bit to true, 1454 and propagates this to our listener iff this was a state change. 1455 """ 1456 1457 # Note: Some callers check _cached_byte_size_dirty before calling 1458 # _Modified() as an extra optimization. So, if this method is ever 1459 # changed such that it does stuff even when _cached_byte_size_dirty is 1460 # already true, the callers need to be updated. 1461 if not self._cached_byte_size_dirty: 1462 self._cached_byte_size_dirty = True 1463 self._listener_for_children.dirty = True 1464 self._is_present_in_parent = True 1465 self._listener.Modified()
Sets the _cached_byte_size_dirty bit to true, and propagates this to our listener iff this was a state change.
1213 def IsInitialized(self, errors=None): 1214 """Checks if all required fields of a message are set. 1215 1216 Args: 1217 errors: A list which, if provided, will be populated with the field 1218 paths of all missing required fields. 1219 1220 Returns: 1221 True iff the specified message has all required fields set. 1222 """ 1223 1224 # Performance is critical so we avoid HasField() and ListFields(). 1225 1226 for field in required_fields: 1227 if (field not in self._fields or 1228 (field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE and 1229 not self._fields[field]._is_present_in_parent)): 1230 if errors is not None: 1231 errors.extend(self.FindInitializationErrors()) 1232 return False 1233 1234 for field, value in list(self._fields.items()): # dict can change size! 1235 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 1236 if field.label == _FieldDescriptor.LABEL_REPEATED: 1237 if (field.message_type._is_map_entry): 1238 continue 1239 for element in value: 1240 if not element.IsInitialized(): 1241 if errors is not None: 1242 errors.extend(self.FindInitializationErrors()) 1243 return False 1244 elif value._is_present_in_parent and not value.IsInitialized(): 1245 if errors is not None: 1246 errors.extend(self.FindInitializationErrors()) 1247 return False 1248 1249 return True
Checks if all required fields of a message are set.
Arguments:
- errors: A list which, if provided, will be populated with the field paths of all missing required fields.
Returns:
True iff the specified message has all required fields set.
1119 def MergeFromString(self, serialized): 1120 serialized = memoryview(serialized) 1121 length = len(serialized) 1122 try: 1123 if self._InternalParse(serialized, 0, length) != length: 1124 # The only reason _InternalParse would return early is if it 1125 # encountered an end-group tag. 1126 raise message_mod.DecodeError('Unexpected end-group tag.') 1127 except (IndexError, TypeError): 1128 # Now ord(buf[p:p+1]) == ord('') gets TypeError. 1129 raise message_mod.DecodeError('Truncated message.') 1130 except struct.error as e: 1131 raise message_mod.DecodeError(e) 1132 return length # Return this for legacy reasons.
Merges serialized protocol buffer data into this message.
When we find a field in serialized
that is already present
in this message:
- If it's a "repeated" field, we append to the end of our list.
- Else, if it's a scalar, we overwrite our field.
- Else, (it's a nonrepeated composite), we recursively merge into the existing composite.
Arguments:
- serialized (bytes): Any object that allows us to call
memoryview(serialized)
to access a string of bytes using the buffer interface.
Returns:
int: The number of bytes read from
serialized
. For non-group messages, this will always belen(serialized)
, but for messages which are actually groups, this will generally be less thanlen(serialized)
, since we must stop when we reach anEND_GROUP
tag. Note that if we do stop because of anEND_GROUP
tag, the number of bytes returned does not include the bytes for theEND_GROUP
tag information.
Raises:
- DecodeError: if the input cannot be parsed.
1072 def SerializeToString(self, **kwargs): 1073 # Check if the message has all of its required fields set. 1074 if not self.IsInitialized(): 1075 raise message_mod.EncodeError( 1076 'Message %s is missing required fields: %s' % ( 1077 self.DESCRIPTOR.full_name, ','.join(self.FindInitializationErrors()))) 1078 return self.SerializePartialToString(**kwargs)
Serializes the protocol message to a binary string.
Keyword Args:
deterministic (bool): If true, requests deterministic serialization of the protobuf, with predictable ordering of map keys.
Returns:
A binary string representation of the message if all of the required fields in the message are set (i.e. the message is initialized).
Raises:
- EncodeError: if the message isn't initialized (see
IsInitialized()
).
1085 def SerializePartialToString(self, **kwargs): 1086 out = BytesIO() 1087 self._InternalSerialize(out.write, **kwargs) 1088 return out.getvalue()
Serializes the protocol message to a binary string.
This method is similar to SerializeToString but doesn't check if the message is initialized.
Keyword Args:
deterministic (bool): If true, requests deterministic serialization of the protobuf, with predictable ordering of map keys.
Returns:
bytes: A serialized representation of the partial message.
814 def ListFields(self): 815 all_fields = [item for item in self._fields.items() if _IsPresent(item)] 816 all_fields.sort(key = lambda item: item[0].number) 817 return all_fields
Returns a list of (FieldDescriptor, value) tuples for present fields.
A message field is non-empty if HasField() would return true. A singular primitive field is non-empty if HasField() would return true in proto2 or it is non zero in proto3. A repeated field is non-empty if it contains at least one element. The fields are ordered by field number.
Returns:
list[tuple(FieldDescriptor, value)]: field descriptors and values for all fields in the message which are not empty. The values vary by field type.
838 def HasField(self, field_name): 839 try: 840 field = hassable_fields[field_name] 841 except KeyError as exc: 842 raise ValueError('Protocol message %s has no non-repeated field "%s" ' 843 'nor has presence is not available for this field.' % ( 844 message_descriptor.full_name, field_name)) from exc 845 846 if isinstance(field, descriptor_mod.OneofDescriptor): 847 try: 848 return HasField(self, self._oneofs[field].name) 849 except KeyError: 850 return False 851 else: 852 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 853 value = self._fields.get(field) 854 return value is not None and value._is_present_in_parent 855 else: 856 return field in self._fields
Checks if a certain field is set for the message.
For a oneof group, checks if any field inside is set. Note that if the
field_name is not defined in the message descriptor, ValueError
will
be raised.
Arguments:
- field_name (str): The name of the field to check for presence.
Returns:
bool: Whether a value has been set for the named field.
Raises:
- ValueError: if the
field_name
is not a member of this message.
863 def ClearField(self, field_name): 864 try: 865 field = message_descriptor.fields_by_name[field_name] 866 except KeyError: 867 try: 868 field = message_descriptor.oneofs_by_name[field_name] 869 if field in self._oneofs: 870 field = self._oneofs[field] 871 else: 872 return 873 except KeyError: 874 raise ValueError('Protocol message %s has no "%s" field.' % 875 (message_descriptor.name, field_name)) 876 877 if field in self._fields: 878 # To match the C++ implementation, we need to invalidate iterators 879 # for map fields when ClearField() happens. 880 if hasattr(self._fields[field], 'InvalidateIterators'): 881 self._fields[field].InvalidateIterators() 882 883 # Note: If the field is a sub-message, its listener will still point 884 # at us. That's fine, because the worst than can happen is that it 885 # will call _Modified() and invalidate our byte size. Big deal. 886 del self._fields[field] 887 888 if self._oneofs.get(field.containing_oneof, None) is field: 889 del self._oneofs[field.containing_oneof] 890 891 # Always call _Modified() -- even if nothing was changed, this is 892 # a mutating method, and thus calling it should cause the field to become 893 # present in the parent message. 894 self._Modified()
Clears the contents of a given field.
Inside a oneof group, clears the field set. If the name neither refers to a
defined field or oneof group, ValueError
is raised.
Arguments:
- field_name (str): The name of the field to check for presence.
Raises:
- ValueError: if the
field_name
is not a member of this message.
1358 def WhichOneof(self, oneof_name): 1359 """Returns the name of the currently set field inside a oneof, or None.""" 1360 try: 1361 field = message_descriptor.oneofs_by_name[oneof_name] 1362 except KeyError: 1363 raise ValueError( 1364 'Protocol message has no oneof "%s" field.' % oneof_name) 1365 1366 nested_field = self._oneofs.get(field, None) 1367 if nested_field is not None and self.HasField(nested_field.name): 1368 return nested_field.name 1369 else: 1370 return None
Returns the name of the currently set field inside a oneof, or None.
1388def _UnknownFields(self): 1389 warnings.warn( 1390 'message.UnknownFields() is deprecated. Please use the add one ' 1391 'feature unknown_fields.UnknownFieldSet(message) in ' 1392 'unknown_fields.py instead.' 1393 ) 1394 if self._unknown_field_set is None: # pylint: disable=protected-access 1395 # pylint: disable=protected-access 1396 self._unknown_field_set = containers.UnknownFieldSet() 1397 return self._unknown_field_set # pylint: disable=protected-access
Returns the UnknownFieldSet.
Returns:
UnknownFieldSet: The unknown fields stored in this message.
1400def _DiscardUnknownFields(self): 1401 self._unknown_fields = [] 1402 self._unknown_field_set = None # pylint: disable=protected-access 1403 for field, value in self.ListFields(): 1404 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 1405 if _IsMapField(field): 1406 if _IsMessageMapField(field): 1407 for key in value: 1408 value[key].DiscardUnknownFields() 1409 elif field.label == _FieldDescriptor.LABEL_REPEATED: 1410 for sub_message in value: 1411 sub_message.DiscardUnknownFields() 1412 else: 1413 value.DiscardUnknownFields()
Clears all fields in the UnknownFieldSet
.
This operation is recursive for nested message.
1040 def ByteSize(self): 1041 if not self._cached_byte_size_dirty: 1042 return self._cached_byte_size 1043 1044 size = 0 1045 descriptor = self.DESCRIPTOR 1046 if descriptor._is_map_entry: 1047 # Fields of map entry should always be serialized. 1048 key_field = descriptor.fields_by_name['key'] 1049 _MaybeAddEncoder(cls, key_field) 1050 size = key_field._sizer(self.key) 1051 value_field = descriptor.fields_by_name['value'] 1052 _MaybeAddEncoder(cls, value_field) 1053 size += value_field._sizer(self.value) 1054 else: 1055 for field_descriptor, field_value in self.ListFields(): 1056 _MaybeAddEncoder(cls, field_descriptor) 1057 size += field_descriptor._sizer(field_value) 1058 for tag_bytes, value_bytes in self._unknown_fields: 1059 size += len(tag_bytes) + len(value_bytes) 1060 1061 self._cached_byte_size = size 1062 self._cached_byte_size_dirty = False 1063 self._listener_for_children.dirty = False 1064 return size
Returns the serialized size of this message.
Recursively calls ByteSize() on all contained messages.
Returns:
int: The number of bytes required to serialize this message.
784 def RegisterExtension(field_descriptor): 785 field_descriptor.containing_type = cls.DESCRIPTOR 786 # TODO: Use cls.MESSAGE_FACTORY.pool when available. 787 # pylint: disable=protected-access 788 cls.DESCRIPTOR.file.pool._AddExtensionDescriptor(field_descriptor) 789 _AttachFieldHelpers(cls, field_descriptor)
685 def getter(self): 686 # TODO: This may be broken since there may not be 687 # default_value. Combine with has_default_value somehow. 688 return self._fields.get(field, default_value)
Getter for name.
642 def getter(self): 643 field_value = self._fields.get(field) 644 if field_value is None: 645 # Construct a new object to represent this field. 646 field_value = field._default_constructor(self) 647 648 # Atomically check if another thread has preempted us and, if not, swap 649 # in the new object we just created. If someone has preempted us, we 650 # take that object and discard ours. 651 # WARNING: We are relying on setdefault() being atomic. This is true 652 # in CPython but we haven't investigated others. This warning appears 653 # in several other locations in this file. 654 field_value = self._fields.setdefault(field, field_value) 655 return field_value
Getter for domain.
1253 def FindInitializationErrors(self): 1254 """Finds required fields which are not initialized. 1255 1256 Returns: 1257 A list of strings. Each string is a path to an uninitialized field from 1258 the top-level message, e.g. "foo.bar[5].baz". 1259 """ 1260 1261 errors = [] # simplify things 1262 1263 for field in required_fields: 1264 if not self.HasField(field.name): 1265 errors.append(field.name) 1266 1267 for field, value in self.ListFields(): 1268 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 1269 if field.is_extension: 1270 name = '(%s)' % field.full_name 1271 else: 1272 name = field.name 1273 1274 if _IsMapField(field): 1275 if _IsMessageMapField(field): 1276 for key in value: 1277 element = value[key] 1278 prefix = '%s[%s].' % (name, key) 1279 sub_errors = element.FindInitializationErrors() 1280 errors += [prefix + error for error in sub_errors] 1281 else: 1282 # ScalarMaps can't have any initialization errors. 1283 pass 1284 elif field.label == _FieldDescriptor.LABEL_REPEATED: 1285 for i in range(len(value)): 1286 element = value[i] 1287 prefix = '%s[%d].' % (name, i) 1288 sub_errors = element.FindInitializationErrors() 1289 errors += [prefix + error for error in sub_errors] 1290 else: 1291 prefix = name + '.' 1292 sub_errors = value.FindInitializationErrors() 1293 errors += [prefix + error for error in sub_errors] 1294 1295 return errors
Finds required fields which are not initialized.
Returns:
A list of strings. Each string is a path to an uninitialized field from the top-level message, e.g. "foo.bar[5].baz".
Inherited Members
- google.protobuf.message.Message
- CopyFrom
- ParseFromString
- HasExtension
- ClearExtension
Abstract base class for protocol messages.
Protocol message classes are almost always generated by the protocol compiler. These generated types subclass Message and implement the methods shown below.
496 def init(self, **kwargs): 497 self._cached_byte_size = 0 498 self._cached_byte_size_dirty = len(kwargs) > 0 499 self._fields = {} 500 # Contains a mapping from oneof field descriptors to the descriptor 501 # of the currently set field in that oneof field. 502 self._oneofs = {} 503 504 # _unknown_fields is () when empty for efficiency, and will be turned into 505 # a list if fields are added. 506 self._unknown_fields = () 507 # _unknown_field_set is None when empty for efficiency, and will be 508 # turned into UnknownFieldSet struct if fields are added. 509 self._unknown_field_set = None # pylint: disable=protected-access 510 self._is_present_in_parent = False 511 self._listener = message_listener_mod.NullMessageListener() 512 self._listener_for_children = _Listener(self) 513 for field_name, field_value in kwargs.items(): 514 field = _GetFieldByName(message_descriptor, field_name) 515 if field is None: 516 raise TypeError('%s() got an unexpected keyword argument "%s"' % 517 (message_descriptor.name, field_name)) 518 if field_value is None: 519 # field=None is the same as no field at all. 520 continue 521 if field.label == _FieldDescriptor.LABEL_REPEATED: 522 copy = field._default_constructor(self) 523 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: # Composite 524 if _IsMapField(field): 525 if _IsMessageMapField(field): 526 for key in field_value: 527 copy[key].MergeFrom(field_value[key]) 528 else: 529 copy.update(field_value) 530 else: 531 for val in field_value: 532 if isinstance(val, dict): 533 copy.add(**val) 534 else: 535 copy.add().MergeFrom(val) 536 else: # Scalar 537 if field.cpp_type == _FieldDescriptor.CPPTYPE_ENUM: 538 field_value = [_GetIntegerEnumValue(field.enum_type, val) 539 for val in field_value] 540 copy.extend(field_value) 541 self._fields[field] = copy 542 elif field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 543 copy = field._default_constructor(self) 544 new_val = field_value 545 if isinstance(field_value, dict): 546 new_val = field.message_type._concrete_class(**field_value) 547 try: 548 copy.MergeFrom(new_val) 549 except TypeError: 550 _ReraiseTypeErrorWithFieldName(message_descriptor.name, field_name) 551 self._fields[field] = copy 552 else: 553 if field.cpp_type == _FieldDescriptor.CPPTYPE_ENUM: 554 field_value = _GetIntegerEnumValue(field.enum_type, field_value) 555 try: 556 setattr(self, field_name, field_value) 557 except TypeError: 558 _ReraiseTypeErrorWithFieldName(message_descriptor.name, field_name)
1312 def MergeFrom(self, msg): 1313 if not isinstance(msg, cls): 1314 raise TypeError( 1315 'Parameter to MergeFrom() must be instance of same class: ' 1316 'expected %s got %s.' % (_FullyQualifiedClassName(cls), 1317 _FullyQualifiedClassName(msg.__class__))) 1318 1319 assert msg is not self 1320 self._Modified() 1321 1322 fields = self._fields 1323 1324 for field, value in msg._fields.items(): 1325 if field.label == LABEL_REPEATED: 1326 field_value = fields.get(field) 1327 if field_value is None: 1328 # Construct a new object to represent this field. 1329 field_value = field._default_constructor(self) 1330 fields[field] = field_value 1331 field_value.MergeFrom(value) 1332 elif field.cpp_type == CPPTYPE_MESSAGE: 1333 if value._is_present_in_parent: 1334 field_value = fields.get(field) 1335 if field_value is None: 1336 # Construct a new object to represent this field. 1337 field_value = field._default_constructor(self) 1338 fields[field] = field_value 1339 field_value.MergeFrom(value) 1340 else: 1341 self._fields[field] = value 1342 if field.containing_oneof: 1343 self._UpdateOneofState(field) 1344 1345 if msg._unknown_fields: 1346 if not self._unknown_fields: 1347 self._unknown_fields = [] 1348 self._unknown_fields.extend(msg._unknown_fields) 1349 # pylint: disable=protected-access 1350 if self._unknown_field_set is None: 1351 self._unknown_field_set = containers.UnknownFieldSet() 1352 self._unknown_field_set._extend(msg._unknown_field_set)
Merges the contents of the specified message into current message.
This method merges the contents of the specified message into the current message. Singular fields that are set in the specified message overwrite the corresponding fields in the current message. Repeated fields are appended. Singular sub-messages and groups are recursively merged.
Arguments:
- other_msg (Message): A message to merge into the current message.
1375def _Clear(self): 1376 # Clear fields. 1377 self._fields = {} 1378 self._unknown_fields = () 1379 # pylint: disable=protected-access 1380 if self._unknown_field_set is not None: 1381 self._unknown_field_set._clear() 1382 self._unknown_field_set = None 1383 1384 self._oneofs = {} 1385 self._Modified()
Clears all data that was set in the message.
1452 def Modified(self): 1453 """Sets the _cached_byte_size_dirty bit to true, 1454 and propagates this to our listener iff this was a state change. 1455 """ 1456 1457 # Note: Some callers check _cached_byte_size_dirty before calling 1458 # _Modified() as an extra optimization. So, if this method is ever 1459 # changed such that it does stuff even when _cached_byte_size_dirty is 1460 # already true, the callers need to be updated. 1461 if not self._cached_byte_size_dirty: 1462 self._cached_byte_size_dirty = True 1463 self._listener_for_children.dirty = True 1464 self._is_present_in_parent = True 1465 self._listener.Modified()
Sets the _cached_byte_size_dirty bit to true, and propagates this to our listener iff this was a state change.
1213 def IsInitialized(self, errors=None): 1214 """Checks if all required fields of a message are set. 1215 1216 Args: 1217 errors: A list which, if provided, will be populated with the field 1218 paths of all missing required fields. 1219 1220 Returns: 1221 True iff the specified message has all required fields set. 1222 """ 1223 1224 # Performance is critical so we avoid HasField() and ListFields(). 1225 1226 for field in required_fields: 1227 if (field not in self._fields or 1228 (field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE and 1229 not self._fields[field]._is_present_in_parent)): 1230 if errors is not None: 1231 errors.extend(self.FindInitializationErrors()) 1232 return False 1233 1234 for field, value in list(self._fields.items()): # dict can change size! 1235 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 1236 if field.label == _FieldDescriptor.LABEL_REPEATED: 1237 if (field.message_type._is_map_entry): 1238 continue 1239 for element in value: 1240 if not element.IsInitialized(): 1241 if errors is not None: 1242 errors.extend(self.FindInitializationErrors()) 1243 return False 1244 elif value._is_present_in_parent and not value.IsInitialized(): 1245 if errors is not None: 1246 errors.extend(self.FindInitializationErrors()) 1247 return False 1248 1249 return True
Checks if all required fields of a message are set.
Arguments:
- errors: A list which, if provided, will be populated with the field paths of all missing required fields.
Returns:
True iff the specified message has all required fields set.
1119 def MergeFromString(self, serialized): 1120 serialized = memoryview(serialized) 1121 length = len(serialized) 1122 try: 1123 if self._InternalParse(serialized, 0, length) != length: 1124 # The only reason _InternalParse would return early is if it 1125 # encountered an end-group tag. 1126 raise message_mod.DecodeError('Unexpected end-group tag.') 1127 except (IndexError, TypeError): 1128 # Now ord(buf[p:p+1]) == ord('') gets TypeError. 1129 raise message_mod.DecodeError('Truncated message.') 1130 except struct.error as e: 1131 raise message_mod.DecodeError(e) 1132 return length # Return this for legacy reasons.
Merges serialized protocol buffer data into this message.
When we find a field in serialized
that is already present
in this message:
- If it's a "repeated" field, we append to the end of our list.
- Else, if it's a scalar, we overwrite our field.
- Else, (it's a nonrepeated composite), we recursively merge into the existing composite.
Arguments:
- serialized (bytes): Any object that allows us to call
memoryview(serialized)
to access a string of bytes using the buffer interface.
Returns:
int: The number of bytes read from
serialized
. For non-group messages, this will always belen(serialized)
, but for messages which are actually groups, this will generally be less thanlen(serialized)
, since we must stop when we reach anEND_GROUP
tag. Note that if we do stop because of anEND_GROUP
tag, the number of bytes returned does not include the bytes for theEND_GROUP
tag information.
Raises:
- DecodeError: if the input cannot be parsed.
1072 def SerializeToString(self, **kwargs): 1073 # Check if the message has all of its required fields set. 1074 if not self.IsInitialized(): 1075 raise message_mod.EncodeError( 1076 'Message %s is missing required fields: %s' % ( 1077 self.DESCRIPTOR.full_name, ','.join(self.FindInitializationErrors()))) 1078 return self.SerializePartialToString(**kwargs)
Serializes the protocol message to a binary string.
Keyword Args:
deterministic (bool): If true, requests deterministic serialization of the protobuf, with predictable ordering of map keys.
Returns:
A binary string representation of the message if all of the required fields in the message are set (i.e. the message is initialized).
Raises:
- EncodeError: if the message isn't initialized (see
IsInitialized()
).
1085 def SerializePartialToString(self, **kwargs): 1086 out = BytesIO() 1087 self._InternalSerialize(out.write, **kwargs) 1088 return out.getvalue()
Serializes the protocol message to a binary string.
This method is similar to SerializeToString but doesn't check if the message is initialized.
Keyword Args:
deterministic (bool): If true, requests deterministic serialization of the protobuf, with predictable ordering of map keys.
Returns:
bytes: A serialized representation of the partial message.
814 def ListFields(self): 815 all_fields = [item for item in self._fields.items() if _IsPresent(item)] 816 all_fields.sort(key = lambda item: item[0].number) 817 return all_fields
Returns a list of (FieldDescriptor, value) tuples for present fields.
A message field is non-empty if HasField() would return true. A singular primitive field is non-empty if HasField() would return true in proto2 or it is non zero in proto3. A repeated field is non-empty if it contains at least one element. The fields are ordered by field number.
Returns:
list[tuple(FieldDescriptor, value)]: field descriptors and values for all fields in the message which are not empty. The values vary by field type.
838 def HasField(self, field_name): 839 try: 840 field = hassable_fields[field_name] 841 except KeyError as exc: 842 raise ValueError('Protocol message %s has no non-repeated field "%s" ' 843 'nor has presence is not available for this field.' % ( 844 message_descriptor.full_name, field_name)) from exc 845 846 if isinstance(field, descriptor_mod.OneofDescriptor): 847 try: 848 return HasField(self, self._oneofs[field].name) 849 except KeyError: 850 return False 851 else: 852 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 853 value = self._fields.get(field) 854 return value is not None and value._is_present_in_parent 855 else: 856 return field in self._fields
Checks if a certain field is set for the message.
For a oneof group, checks if any field inside is set. Note that if the
field_name is not defined in the message descriptor, ValueError
will
be raised.
Arguments:
- field_name (str): The name of the field to check for presence.
Returns:
bool: Whether a value has been set for the named field.
Raises:
- ValueError: if the
field_name
is not a member of this message.
863 def ClearField(self, field_name): 864 try: 865 field = message_descriptor.fields_by_name[field_name] 866 except KeyError: 867 try: 868 field = message_descriptor.oneofs_by_name[field_name] 869 if field in self._oneofs: 870 field = self._oneofs[field] 871 else: 872 return 873 except KeyError: 874 raise ValueError('Protocol message %s has no "%s" field.' % 875 (message_descriptor.name, field_name)) 876 877 if field in self._fields: 878 # To match the C++ implementation, we need to invalidate iterators 879 # for map fields when ClearField() happens. 880 if hasattr(self._fields[field], 'InvalidateIterators'): 881 self._fields[field].InvalidateIterators() 882 883 # Note: If the field is a sub-message, its listener will still point 884 # at us. That's fine, because the worst than can happen is that it 885 # will call _Modified() and invalidate our byte size. Big deal. 886 del self._fields[field] 887 888 if self._oneofs.get(field.containing_oneof, None) is field: 889 del self._oneofs[field.containing_oneof] 890 891 # Always call _Modified() -- even if nothing was changed, this is 892 # a mutating method, and thus calling it should cause the field to become 893 # present in the parent message. 894 self._Modified()
Clears the contents of a given field.
Inside a oneof group, clears the field set. If the name neither refers to a
defined field or oneof group, ValueError
is raised.
Arguments:
- field_name (str): The name of the field to check for presence.
Raises:
- ValueError: if the
field_name
is not a member of this message.
1358 def WhichOneof(self, oneof_name): 1359 """Returns the name of the currently set field inside a oneof, or None.""" 1360 try: 1361 field = message_descriptor.oneofs_by_name[oneof_name] 1362 except KeyError: 1363 raise ValueError( 1364 'Protocol message has no oneof "%s" field.' % oneof_name) 1365 1366 nested_field = self._oneofs.get(field, None) 1367 if nested_field is not None and self.HasField(nested_field.name): 1368 return nested_field.name 1369 else: 1370 return None
Returns the name of the currently set field inside a oneof, or None.
1388def _UnknownFields(self): 1389 warnings.warn( 1390 'message.UnknownFields() is deprecated. Please use the add one ' 1391 'feature unknown_fields.UnknownFieldSet(message) in ' 1392 'unknown_fields.py instead.' 1393 ) 1394 if self._unknown_field_set is None: # pylint: disable=protected-access 1395 # pylint: disable=protected-access 1396 self._unknown_field_set = containers.UnknownFieldSet() 1397 return self._unknown_field_set # pylint: disable=protected-access
Returns the UnknownFieldSet.
Returns:
UnknownFieldSet: The unknown fields stored in this message.
1400def _DiscardUnknownFields(self): 1401 self._unknown_fields = [] 1402 self._unknown_field_set = None # pylint: disable=protected-access 1403 for field, value in self.ListFields(): 1404 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 1405 if _IsMapField(field): 1406 if _IsMessageMapField(field): 1407 for key in value: 1408 value[key].DiscardUnknownFields() 1409 elif field.label == _FieldDescriptor.LABEL_REPEATED: 1410 for sub_message in value: 1411 sub_message.DiscardUnknownFields() 1412 else: 1413 value.DiscardUnknownFields()
Clears all fields in the UnknownFieldSet
.
This operation is recursive for nested message.
1040 def ByteSize(self): 1041 if not self._cached_byte_size_dirty: 1042 return self._cached_byte_size 1043 1044 size = 0 1045 descriptor = self.DESCRIPTOR 1046 if descriptor._is_map_entry: 1047 # Fields of map entry should always be serialized. 1048 key_field = descriptor.fields_by_name['key'] 1049 _MaybeAddEncoder(cls, key_field) 1050 size = key_field._sizer(self.key) 1051 value_field = descriptor.fields_by_name['value'] 1052 _MaybeAddEncoder(cls, value_field) 1053 size += value_field._sizer(self.value) 1054 else: 1055 for field_descriptor, field_value in self.ListFields(): 1056 _MaybeAddEncoder(cls, field_descriptor) 1057 size += field_descriptor._sizer(field_value) 1058 for tag_bytes, value_bytes in self._unknown_fields: 1059 size += len(tag_bytes) + len(value_bytes) 1060 1061 self._cached_byte_size = size 1062 self._cached_byte_size_dirty = False 1063 self._listener_for_children.dirty = False 1064 return size
Returns the serialized size of this message.
Recursively calls ByteSize() on all contained messages.
Returns:
int: The number of bytes required to serialize this message.
784 def RegisterExtension(field_descriptor): 785 field_descriptor.containing_type = cls.DESCRIPTOR 786 # TODO: Use cls.MESSAGE_FACTORY.pool when available. 787 # pylint: disable=protected-access 788 cls.DESCRIPTOR.file.pool._AddExtensionDescriptor(field_descriptor) 789 _AttachFieldHelpers(cls, field_descriptor)
642 def getter(self): 643 field_value = self._fields.get(field) 644 if field_value is None: 645 # Construct a new object to represent this field. 646 field_value = field._default_constructor(self) 647 648 # Atomically check if another thread has preempted us and, if not, swap 649 # in the new object we just created. If someone has preempted us, we 650 # take that object and discard ours. 651 # WARNING: We are relying on setdefault() being atomic. This is true 652 # in CPython but we haven't investigated others. This warning appears 653 # in several other locations in this file. 654 field_value = self._fields.setdefault(field, field_value) 655 return field_value
Getter for literals.
1253 def FindInitializationErrors(self): 1254 """Finds required fields which are not initialized. 1255 1256 Returns: 1257 A list of strings. Each string is a path to an uninitialized field from 1258 the top-level message, e.g. "foo.bar[5].baz". 1259 """ 1260 1261 errors = [] # simplify things 1262 1263 for field in required_fields: 1264 if not self.HasField(field.name): 1265 errors.append(field.name) 1266 1267 for field, value in self.ListFields(): 1268 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 1269 if field.is_extension: 1270 name = '(%s)' % field.full_name 1271 else: 1272 name = field.name 1273 1274 if _IsMapField(field): 1275 if _IsMessageMapField(field): 1276 for key in value: 1277 element = value[key] 1278 prefix = '%s[%s].' % (name, key) 1279 sub_errors = element.FindInitializationErrors() 1280 errors += [prefix + error for error in sub_errors] 1281 else: 1282 # ScalarMaps can't have any initialization errors. 1283 pass 1284 elif field.label == _FieldDescriptor.LABEL_REPEATED: 1285 for i in range(len(value)): 1286 element = value[i] 1287 prefix = '%s[%d].' % (name, i) 1288 sub_errors = element.FindInitializationErrors() 1289 errors += [prefix + error for error in sub_errors] 1290 else: 1291 prefix = name + '.' 1292 sub_errors = value.FindInitializationErrors() 1293 errors += [prefix + error for error in sub_errors] 1294 1295 return errors
Finds required fields which are not initialized.
Returns:
A list of strings. Each string is a path to an uninitialized field from the top-level message, e.g. "foo.bar[5].baz".
Inherited Members
- google.protobuf.message.Message
- CopyFrom
- ParseFromString
- HasExtension
- ClearExtension
Abstract base class for protocol messages.
Protocol message classes are almost always generated by the protocol compiler. These generated types subclass Message and implement the methods shown below.
496 def init(self, **kwargs): 497 self._cached_byte_size = 0 498 self._cached_byte_size_dirty = len(kwargs) > 0 499 self._fields = {} 500 # Contains a mapping from oneof field descriptors to the descriptor 501 # of the currently set field in that oneof field. 502 self._oneofs = {} 503 504 # _unknown_fields is () when empty for efficiency, and will be turned into 505 # a list if fields are added. 506 self._unknown_fields = () 507 # _unknown_field_set is None when empty for efficiency, and will be 508 # turned into UnknownFieldSet struct if fields are added. 509 self._unknown_field_set = None # pylint: disable=protected-access 510 self._is_present_in_parent = False 511 self._listener = message_listener_mod.NullMessageListener() 512 self._listener_for_children = _Listener(self) 513 for field_name, field_value in kwargs.items(): 514 field = _GetFieldByName(message_descriptor, field_name) 515 if field is None: 516 raise TypeError('%s() got an unexpected keyword argument "%s"' % 517 (message_descriptor.name, field_name)) 518 if field_value is None: 519 # field=None is the same as no field at all. 520 continue 521 if field.label == _FieldDescriptor.LABEL_REPEATED: 522 copy = field._default_constructor(self) 523 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: # Composite 524 if _IsMapField(field): 525 if _IsMessageMapField(field): 526 for key in field_value: 527 copy[key].MergeFrom(field_value[key]) 528 else: 529 copy.update(field_value) 530 else: 531 for val in field_value: 532 if isinstance(val, dict): 533 copy.add(**val) 534 else: 535 copy.add().MergeFrom(val) 536 else: # Scalar 537 if field.cpp_type == _FieldDescriptor.CPPTYPE_ENUM: 538 field_value = [_GetIntegerEnumValue(field.enum_type, val) 539 for val in field_value] 540 copy.extend(field_value) 541 self._fields[field] = copy 542 elif field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 543 copy = field._default_constructor(self) 544 new_val = field_value 545 if isinstance(field_value, dict): 546 new_val = field.message_type._concrete_class(**field_value) 547 try: 548 copy.MergeFrom(new_val) 549 except TypeError: 550 _ReraiseTypeErrorWithFieldName(message_descriptor.name, field_name) 551 self._fields[field] = copy 552 else: 553 if field.cpp_type == _FieldDescriptor.CPPTYPE_ENUM: 554 field_value = _GetIntegerEnumValue(field.enum_type, field_value) 555 try: 556 setattr(self, field_name, field_value) 557 except TypeError: 558 _ReraiseTypeErrorWithFieldName(message_descriptor.name, field_name)
1312 def MergeFrom(self, msg): 1313 if not isinstance(msg, cls): 1314 raise TypeError( 1315 'Parameter to MergeFrom() must be instance of same class: ' 1316 'expected %s got %s.' % (_FullyQualifiedClassName(cls), 1317 _FullyQualifiedClassName(msg.__class__))) 1318 1319 assert msg is not self 1320 self._Modified() 1321 1322 fields = self._fields 1323 1324 for field, value in msg._fields.items(): 1325 if field.label == LABEL_REPEATED: 1326 field_value = fields.get(field) 1327 if field_value is None: 1328 # Construct a new object to represent this field. 1329 field_value = field._default_constructor(self) 1330 fields[field] = field_value 1331 field_value.MergeFrom(value) 1332 elif field.cpp_type == CPPTYPE_MESSAGE: 1333 if value._is_present_in_parent: 1334 field_value = fields.get(field) 1335 if field_value is None: 1336 # Construct a new object to represent this field. 1337 field_value = field._default_constructor(self) 1338 fields[field] = field_value 1339 field_value.MergeFrom(value) 1340 else: 1341 self._fields[field] = value 1342 if field.containing_oneof: 1343 self._UpdateOneofState(field) 1344 1345 if msg._unknown_fields: 1346 if not self._unknown_fields: 1347 self._unknown_fields = [] 1348 self._unknown_fields.extend(msg._unknown_fields) 1349 # pylint: disable=protected-access 1350 if self._unknown_field_set is None: 1351 self._unknown_field_set = containers.UnknownFieldSet() 1352 self._unknown_field_set._extend(msg._unknown_field_set)
Merges the contents of the specified message into current message.
This method merges the contents of the specified message into the current message. Singular fields that are set in the specified message overwrite the corresponding fields in the current message. Repeated fields are appended. Singular sub-messages and groups are recursively merged.
Arguments:
- other_msg (Message): A message to merge into the current message.
1375def _Clear(self): 1376 # Clear fields. 1377 self._fields = {} 1378 self._unknown_fields = () 1379 # pylint: disable=protected-access 1380 if self._unknown_field_set is not None: 1381 self._unknown_field_set._clear() 1382 self._unknown_field_set = None 1383 1384 self._oneofs = {} 1385 self._Modified()
Clears all data that was set in the message.
1452 def Modified(self): 1453 """Sets the _cached_byte_size_dirty bit to true, 1454 and propagates this to our listener iff this was a state change. 1455 """ 1456 1457 # Note: Some callers check _cached_byte_size_dirty before calling 1458 # _Modified() as an extra optimization. So, if this method is ever 1459 # changed such that it does stuff even when _cached_byte_size_dirty is 1460 # already true, the callers need to be updated. 1461 if not self._cached_byte_size_dirty: 1462 self._cached_byte_size_dirty = True 1463 self._listener_for_children.dirty = True 1464 self._is_present_in_parent = True 1465 self._listener.Modified()
Sets the _cached_byte_size_dirty bit to true, and propagates this to our listener iff this was a state change.
1213 def IsInitialized(self, errors=None): 1214 """Checks if all required fields of a message are set. 1215 1216 Args: 1217 errors: A list which, if provided, will be populated with the field 1218 paths of all missing required fields. 1219 1220 Returns: 1221 True iff the specified message has all required fields set. 1222 """ 1223 1224 # Performance is critical so we avoid HasField() and ListFields(). 1225 1226 for field in required_fields: 1227 if (field not in self._fields or 1228 (field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE and 1229 not self._fields[field]._is_present_in_parent)): 1230 if errors is not None: 1231 errors.extend(self.FindInitializationErrors()) 1232 return False 1233 1234 for field, value in list(self._fields.items()): # dict can change size! 1235 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 1236 if field.label == _FieldDescriptor.LABEL_REPEATED: 1237 if (field.message_type._is_map_entry): 1238 continue 1239 for element in value: 1240 if not element.IsInitialized(): 1241 if errors is not None: 1242 errors.extend(self.FindInitializationErrors()) 1243 return False 1244 elif value._is_present_in_parent and not value.IsInitialized(): 1245 if errors is not None: 1246 errors.extend(self.FindInitializationErrors()) 1247 return False 1248 1249 return True
Checks if all required fields of a message are set.
Arguments:
- errors: A list which, if provided, will be populated with the field paths of all missing required fields.
Returns:
True iff the specified message has all required fields set.
1119 def MergeFromString(self, serialized): 1120 serialized = memoryview(serialized) 1121 length = len(serialized) 1122 try: 1123 if self._InternalParse(serialized, 0, length) != length: 1124 # The only reason _InternalParse would return early is if it 1125 # encountered an end-group tag. 1126 raise message_mod.DecodeError('Unexpected end-group tag.') 1127 except (IndexError, TypeError): 1128 # Now ord(buf[p:p+1]) == ord('') gets TypeError. 1129 raise message_mod.DecodeError('Truncated message.') 1130 except struct.error as e: 1131 raise message_mod.DecodeError(e) 1132 return length # Return this for legacy reasons.
Merges serialized protocol buffer data into this message.
When we find a field in serialized
that is already present
in this message:
- If it's a "repeated" field, we append to the end of our list.
- Else, if it's a scalar, we overwrite our field.
- Else, (it's a nonrepeated composite), we recursively merge into the existing composite.
Arguments:
- serialized (bytes): Any object that allows us to call
memoryview(serialized)
to access a string of bytes using the buffer interface.
Returns:
int: The number of bytes read from
serialized
. For non-group messages, this will always belen(serialized)
, but for messages which are actually groups, this will generally be less thanlen(serialized)
, since we must stop when we reach anEND_GROUP
tag. Note that if we do stop because of anEND_GROUP
tag, the number of bytes returned does not include the bytes for theEND_GROUP
tag information.
Raises:
- DecodeError: if the input cannot be parsed.
1072 def SerializeToString(self, **kwargs): 1073 # Check if the message has all of its required fields set. 1074 if not self.IsInitialized(): 1075 raise message_mod.EncodeError( 1076 'Message %s is missing required fields: %s' % ( 1077 self.DESCRIPTOR.full_name, ','.join(self.FindInitializationErrors()))) 1078 return self.SerializePartialToString(**kwargs)
Serializes the protocol message to a binary string.
Keyword Args:
deterministic (bool): If true, requests deterministic serialization of the protobuf, with predictable ordering of map keys.
Returns:
A binary string representation of the message if all of the required fields in the message are set (i.e. the message is initialized).
Raises:
- EncodeError: if the message isn't initialized (see
IsInitialized()
).
1085 def SerializePartialToString(self, **kwargs): 1086 out = BytesIO() 1087 self._InternalSerialize(out.write, **kwargs) 1088 return out.getvalue()
Serializes the protocol message to a binary string.
This method is similar to SerializeToString but doesn't check if the message is initialized.
Keyword Args:
deterministic (bool): If true, requests deterministic serialization of the protobuf, with predictable ordering of map keys.
Returns:
bytes: A serialized representation of the partial message.
814 def ListFields(self): 815 all_fields = [item for item in self._fields.items() if _IsPresent(item)] 816 all_fields.sort(key = lambda item: item[0].number) 817 return all_fields
Returns a list of (FieldDescriptor, value) tuples for present fields.
A message field is non-empty if HasField() would return true. A singular primitive field is non-empty if HasField() would return true in proto2 or it is non zero in proto3. A repeated field is non-empty if it contains at least one element. The fields are ordered by field number.
Returns:
list[tuple(FieldDescriptor, value)]: field descriptors and values for all fields in the message which are not empty. The values vary by field type.
838 def HasField(self, field_name): 839 try: 840 field = hassable_fields[field_name] 841 except KeyError as exc: 842 raise ValueError('Protocol message %s has no non-repeated field "%s" ' 843 'nor has presence is not available for this field.' % ( 844 message_descriptor.full_name, field_name)) from exc 845 846 if isinstance(field, descriptor_mod.OneofDescriptor): 847 try: 848 return HasField(self, self._oneofs[field].name) 849 except KeyError: 850 return False 851 else: 852 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 853 value = self._fields.get(field) 854 return value is not None and value._is_present_in_parent 855 else: 856 return field in self._fields
Checks if a certain field is set for the message.
For a oneof group, checks if any field inside is set. Note that if the
field_name is not defined in the message descriptor, ValueError
will
be raised.
Arguments:
- field_name (str): The name of the field to check for presence.
Returns:
bool: Whether a value has been set for the named field.
Raises:
- ValueError: if the
field_name
is not a member of this message.
863 def ClearField(self, field_name): 864 try: 865 field = message_descriptor.fields_by_name[field_name] 866 except KeyError: 867 try: 868 field = message_descriptor.oneofs_by_name[field_name] 869 if field in self._oneofs: 870 field = self._oneofs[field] 871 else: 872 return 873 except KeyError: 874 raise ValueError('Protocol message %s has no "%s" field.' % 875 (message_descriptor.name, field_name)) 876 877 if field in self._fields: 878 # To match the C++ implementation, we need to invalidate iterators 879 # for map fields when ClearField() happens. 880 if hasattr(self._fields[field], 'InvalidateIterators'): 881 self._fields[field].InvalidateIterators() 882 883 # Note: If the field is a sub-message, its listener will still point 884 # at us. That's fine, because the worst than can happen is that it 885 # will call _Modified() and invalidate our byte size. Big deal. 886 del self._fields[field] 887 888 if self._oneofs.get(field.containing_oneof, None) is field: 889 del self._oneofs[field.containing_oneof] 890 891 # Always call _Modified() -- even if nothing was changed, this is 892 # a mutating method, and thus calling it should cause the field to become 893 # present in the parent message. 894 self._Modified()
Clears the contents of a given field.
Inside a oneof group, clears the field set. If the name neither refers to a
defined field or oneof group, ValueError
is raised.
Arguments:
- field_name (str): The name of the field to check for presence.
Raises:
- ValueError: if the
field_name
is not a member of this message.
1358 def WhichOneof(self, oneof_name): 1359 """Returns the name of the currently set field inside a oneof, or None.""" 1360 try: 1361 field = message_descriptor.oneofs_by_name[oneof_name] 1362 except KeyError: 1363 raise ValueError( 1364 'Protocol message has no oneof "%s" field.' % oneof_name) 1365 1366 nested_field = self._oneofs.get(field, None) 1367 if nested_field is not None and self.HasField(nested_field.name): 1368 return nested_field.name 1369 else: 1370 return None
Returns the name of the currently set field inside a oneof, or None.
1388def _UnknownFields(self): 1389 warnings.warn( 1390 'message.UnknownFields() is deprecated. Please use the add one ' 1391 'feature unknown_fields.UnknownFieldSet(message) in ' 1392 'unknown_fields.py instead.' 1393 ) 1394 if self._unknown_field_set is None: # pylint: disable=protected-access 1395 # pylint: disable=protected-access 1396 self._unknown_field_set = containers.UnknownFieldSet() 1397 return self._unknown_field_set # pylint: disable=protected-access
Returns the UnknownFieldSet.
Returns:
UnknownFieldSet: The unknown fields stored in this message.
1400def _DiscardUnknownFields(self): 1401 self._unknown_fields = [] 1402 self._unknown_field_set = None # pylint: disable=protected-access 1403 for field, value in self.ListFields(): 1404 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 1405 if _IsMapField(field): 1406 if _IsMessageMapField(field): 1407 for key in value: 1408 value[key].DiscardUnknownFields() 1409 elif field.label == _FieldDescriptor.LABEL_REPEATED: 1410 for sub_message in value: 1411 sub_message.DiscardUnknownFields() 1412 else: 1413 value.DiscardUnknownFields()
Clears all fields in the UnknownFieldSet
.
This operation is recursive for nested message.
1040 def ByteSize(self): 1041 if not self._cached_byte_size_dirty: 1042 return self._cached_byte_size 1043 1044 size = 0 1045 descriptor = self.DESCRIPTOR 1046 if descriptor._is_map_entry: 1047 # Fields of map entry should always be serialized. 1048 key_field = descriptor.fields_by_name['key'] 1049 _MaybeAddEncoder(cls, key_field) 1050 size = key_field._sizer(self.key) 1051 value_field = descriptor.fields_by_name['value'] 1052 _MaybeAddEncoder(cls, value_field) 1053 size += value_field._sizer(self.value) 1054 else: 1055 for field_descriptor, field_value in self.ListFields(): 1056 _MaybeAddEncoder(cls, field_descriptor) 1057 size += field_descriptor._sizer(field_value) 1058 for tag_bytes, value_bytes in self._unknown_fields: 1059 size += len(tag_bytes) + len(value_bytes) 1060 1061 self._cached_byte_size = size 1062 self._cached_byte_size_dirty = False 1063 self._listener_for_children.dirty = False 1064 return size
Returns the serialized size of this message.
Recursively calls ByteSize() on all contained messages.
Returns:
int: The number of bytes required to serialize this message.
784 def RegisterExtension(field_descriptor): 785 field_descriptor.containing_type = cls.DESCRIPTOR 786 # TODO: Use cls.MESSAGE_FACTORY.pool when available. 787 # pylint: disable=protected-access 788 cls.DESCRIPTOR.file.pool._AddExtensionDescriptor(field_descriptor) 789 _AttachFieldHelpers(cls, field_descriptor)
642 def getter(self): 643 field_value = self._fields.get(field) 644 if field_value is None: 645 # Construct a new object to represent this field. 646 field_value = field._default_constructor(self) 647 648 # Atomically check if another thread has preempted us and, if not, swap 649 # in the new object we just created. If someone has preempted us, we 650 # take that object and discard ours. 651 # WARNING: We are relying on setdefault() being atomic. This is true 652 # in CPython but we haven't investigated others. This warning appears 653 # in several other locations in this file. 654 field_value = self._fields.setdefault(field, field_value) 655 return field_value
Getter for vars.
642 def getter(self): 643 field_value = self._fields.get(field) 644 if field_value is None: 645 # Construct a new object to represent this field. 646 field_value = field._default_constructor(self) 647 648 # Atomically check if another thread has preempted us and, if not, swap 649 # in the new object we just created. If someone has preempted us, we 650 # take that object and discard ours. 651 # WARNING: We are relying on setdefault() being atomic. This is true 652 # in CPython but we haven't investigated others. This warning appears 653 # in several other locations in this file. 654 field_value = self._fields.setdefault(field, field_value) 655 return field_value
Getter for coeffs.
685 def getter(self): 686 # TODO: This may be broken since there may not be 687 # default_value. Combine with has_default_value somehow. 688 return self._fields.get(field, default_value)
Getter for offset.
1253 def FindInitializationErrors(self): 1254 """Finds required fields which are not initialized. 1255 1256 Returns: 1257 A list of strings. Each string is a path to an uninitialized field from 1258 the top-level message, e.g. "foo.bar[5].baz". 1259 """ 1260 1261 errors = [] # simplify things 1262 1263 for field in required_fields: 1264 if not self.HasField(field.name): 1265 errors.append(field.name) 1266 1267 for field, value in self.ListFields(): 1268 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 1269 if field.is_extension: 1270 name = '(%s)' % field.full_name 1271 else: 1272 name = field.name 1273 1274 if _IsMapField(field): 1275 if _IsMessageMapField(field): 1276 for key in value: 1277 element = value[key] 1278 prefix = '%s[%s].' % (name, key) 1279 sub_errors = element.FindInitializationErrors() 1280 errors += [prefix + error for error in sub_errors] 1281 else: 1282 # ScalarMaps can't have any initialization errors. 1283 pass 1284 elif field.label == _FieldDescriptor.LABEL_REPEATED: 1285 for i in range(len(value)): 1286 element = value[i] 1287 prefix = '%s[%d].' % (name, i) 1288 sub_errors = element.FindInitializationErrors() 1289 errors += [prefix + error for error in sub_errors] 1290 else: 1291 prefix = name + '.' 1292 sub_errors = value.FindInitializationErrors() 1293 errors += [prefix + error for error in sub_errors] 1294 1295 return errors
Finds required fields which are not initialized.
Returns:
A list of strings. Each string is a path to an uninitialized field from the top-level message, e.g. "foo.bar[5].baz".
Inherited Members
- google.protobuf.message.Message
- CopyFrom
- ParseFromString
- HasExtension
- ClearExtension
Abstract base class for protocol messages.
Protocol message classes are almost always generated by the protocol compiler. These generated types subclass Message and implement the methods shown below.
496 def init(self, **kwargs): 497 self._cached_byte_size = 0 498 self._cached_byte_size_dirty = len(kwargs) > 0 499 self._fields = {} 500 # Contains a mapping from oneof field descriptors to the descriptor 501 # of the currently set field in that oneof field. 502 self._oneofs = {} 503 504 # _unknown_fields is () when empty for efficiency, and will be turned into 505 # a list if fields are added. 506 self._unknown_fields = () 507 # _unknown_field_set is None when empty for efficiency, and will be 508 # turned into UnknownFieldSet struct if fields are added. 509 self._unknown_field_set = None # pylint: disable=protected-access 510 self._is_present_in_parent = False 511 self._listener = message_listener_mod.NullMessageListener() 512 self._listener_for_children = _Listener(self) 513 for field_name, field_value in kwargs.items(): 514 field = _GetFieldByName(message_descriptor, field_name) 515 if field is None: 516 raise TypeError('%s() got an unexpected keyword argument "%s"' % 517 (message_descriptor.name, field_name)) 518 if field_value is None: 519 # field=None is the same as no field at all. 520 continue 521 if field.label == _FieldDescriptor.LABEL_REPEATED: 522 copy = field._default_constructor(self) 523 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: # Composite 524 if _IsMapField(field): 525 if _IsMessageMapField(field): 526 for key in field_value: 527 copy[key].MergeFrom(field_value[key]) 528 else: 529 copy.update(field_value) 530 else: 531 for val in field_value: 532 if isinstance(val, dict): 533 copy.add(**val) 534 else: 535 copy.add().MergeFrom(val) 536 else: # Scalar 537 if field.cpp_type == _FieldDescriptor.CPPTYPE_ENUM: 538 field_value = [_GetIntegerEnumValue(field.enum_type, val) 539 for val in field_value] 540 copy.extend(field_value) 541 self._fields[field] = copy 542 elif field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 543 copy = field._default_constructor(self) 544 new_val = field_value 545 if isinstance(field_value, dict): 546 new_val = field.message_type._concrete_class(**field_value) 547 try: 548 copy.MergeFrom(new_val) 549 except TypeError: 550 _ReraiseTypeErrorWithFieldName(message_descriptor.name, field_name) 551 self._fields[field] = copy 552 else: 553 if field.cpp_type == _FieldDescriptor.CPPTYPE_ENUM: 554 field_value = _GetIntegerEnumValue(field.enum_type, field_value) 555 try: 556 setattr(self, field_name, field_value) 557 except TypeError: 558 _ReraiseTypeErrorWithFieldName(message_descriptor.name, field_name)
1312 def MergeFrom(self, msg): 1313 if not isinstance(msg, cls): 1314 raise TypeError( 1315 'Parameter to MergeFrom() must be instance of same class: ' 1316 'expected %s got %s.' % (_FullyQualifiedClassName(cls), 1317 _FullyQualifiedClassName(msg.__class__))) 1318 1319 assert msg is not self 1320 self._Modified() 1321 1322 fields = self._fields 1323 1324 for field, value in msg._fields.items(): 1325 if field.label == LABEL_REPEATED: 1326 field_value = fields.get(field) 1327 if field_value is None: 1328 # Construct a new object to represent this field. 1329 field_value = field._default_constructor(self) 1330 fields[field] = field_value 1331 field_value.MergeFrom(value) 1332 elif field.cpp_type == CPPTYPE_MESSAGE: 1333 if value._is_present_in_parent: 1334 field_value = fields.get(field) 1335 if field_value is None: 1336 # Construct a new object to represent this field. 1337 field_value = field._default_constructor(self) 1338 fields[field] = field_value 1339 field_value.MergeFrom(value) 1340 else: 1341 self._fields[field] = value 1342 if field.containing_oneof: 1343 self._UpdateOneofState(field) 1344 1345 if msg._unknown_fields: 1346 if not self._unknown_fields: 1347 self._unknown_fields = [] 1348 self._unknown_fields.extend(msg._unknown_fields) 1349 # pylint: disable=protected-access 1350 if self._unknown_field_set is None: 1351 self._unknown_field_set = containers.UnknownFieldSet() 1352 self._unknown_field_set._extend(msg._unknown_field_set)
Merges the contents of the specified message into current message.
This method merges the contents of the specified message into the current message. Singular fields that are set in the specified message overwrite the corresponding fields in the current message. Repeated fields are appended. Singular sub-messages and groups are recursively merged.
Arguments:
- other_msg (Message): A message to merge into the current message.
1375def _Clear(self): 1376 # Clear fields. 1377 self._fields = {} 1378 self._unknown_fields = () 1379 # pylint: disable=protected-access 1380 if self._unknown_field_set is not None: 1381 self._unknown_field_set._clear() 1382 self._unknown_field_set = None 1383 1384 self._oneofs = {} 1385 self._Modified()
Clears all data that was set in the message.
1452 def Modified(self): 1453 """Sets the _cached_byte_size_dirty bit to true, 1454 and propagates this to our listener iff this was a state change. 1455 """ 1456 1457 # Note: Some callers check _cached_byte_size_dirty before calling 1458 # _Modified() as an extra optimization. So, if this method is ever 1459 # changed such that it does stuff even when _cached_byte_size_dirty is 1460 # already true, the callers need to be updated. 1461 if not self._cached_byte_size_dirty: 1462 self._cached_byte_size_dirty = True 1463 self._listener_for_children.dirty = True 1464 self._is_present_in_parent = True 1465 self._listener.Modified()
Sets the _cached_byte_size_dirty bit to true, and propagates this to our listener iff this was a state change.
1213 def IsInitialized(self, errors=None): 1214 """Checks if all required fields of a message are set. 1215 1216 Args: 1217 errors: A list which, if provided, will be populated with the field 1218 paths of all missing required fields. 1219 1220 Returns: 1221 True iff the specified message has all required fields set. 1222 """ 1223 1224 # Performance is critical so we avoid HasField() and ListFields(). 1225 1226 for field in required_fields: 1227 if (field not in self._fields or 1228 (field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE and 1229 not self._fields[field]._is_present_in_parent)): 1230 if errors is not None: 1231 errors.extend(self.FindInitializationErrors()) 1232 return False 1233 1234 for field, value in list(self._fields.items()): # dict can change size! 1235 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 1236 if field.label == _FieldDescriptor.LABEL_REPEATED: 1237 if (field.message_type._is_map_entry): 1238 continue 1239 for element in value: 1240 if not element.IsInitialized(): 1241 if errors is not None: 1242 errors.extend(self.FindInitializationErrors()) 1243 return False 1244 elif value._is_present_in_parent and not value.IsInitialized(): 1245 if errors is not None: 1246 errors.extend(self.FindInitializationErrors()) 1247 return False 1248 1249 return True
Checks if all required fields of a message are set.
Arguments:
- errors: A list which, if provided, will be populated with the field paths of all missing required fields.
Returns:
True iff the specified message has all required fields set.
1119 def MergeFromString(self, serialized): 1120 serialized = memoryview(serialized) 1121 length = len(serialized) 1122 try: 1123 if self._InternalParse(serialized, 0, length) != length: 1124 # The only reason _InternalParse would return early is if it 1125 # encountered an end-group tag. 1126 raise message_mod.DecodeError('Unexpected end-group tag.') 1127 except (IndexError, TypeError): 1128 # Now ord(buf[p:p+1]) == ord('') gets TypeError. 1129 raise message_mod.DecodeError('Truncated message.') 1130 except struct.error as e: 1131 raise message_mod.DecodeError(e) 1132 return length # Return this for legacy reasons.
Merges serialized protocol buffer data into this message.
When we find a field in serialized
that is already present
in this message:
- If it's a "repeated" field, we append to the end of our list.
- Else, if it's a scalar, we overwrite our field.
- Else, (it's a nonrepeated composite), we recursively merge into the existing composite.
Arguments:
- serialized (bytes): Any object that allows us to call
memoryview(serialized)
to access a string of bytes using the buffer interface.
Returns:
int: The number of bytes read from
serialized
. For non-group messages, this will always belen(serialized)
, but for messages which are actually groups, this will generally be less thanlen(serialized)
, since we must stop when we reach anEND_GROUP
tag. Note that if we do stop because of anEND_GROUP
tag, the number of bytes returned does not include the bytes for theEND_GROUP
tag information.
Raises:
- DecodeError: if the input cannot be parsed.
1072 def SerializeToString(self, **kwargs): 1073 # Check if the message has all of its required fields set. 1074 if not self.IsInitialized(): 1075 raise message_mod.EncodeError( 1076 'Message %s is missing required fields: %s' % ( 1077 self.DESCRIPTOR.full_name, ','.join(self.FindInitializationErrors()))) 1078 return self.SerializePartialToString(**kwargs)
Serializes the protocol message to a binary string.
Keyword Args:
deterministic (bool): If true, requests deterministic serialization of the protobuf, with predictable ordering of map keys.
Returns:
A binary string representation of the message if all of the required fields in the message are set (i.e. the message is initialized).
Raises:
- EncodeError: if the message isn't initialized (see
IsInitialized()
).
1085 def SerializePartialToString(self, **kwargs): 1086 out = BytesIO() 1087 self._InternalSerialize(out.write, **kwargs) 1088 return out.getvalue()
Serializes the protocol message to a binary string.
This method is similar to SerializeToString but doesn't check if the message is initialized.
Keyword Args:
deterministic (bool): If true, requests deterministic serialization of the protobuf, with predictable ordering of map keys.
Returns:
bytes: A serialized representation of the partial message.
814 def ListFields(self): 815 all_fields = [item for item in self._fields.items() if _IsPresent(item)] 816 all_fields.sort(key = lambda item: item[0].number) 817 return all_fields
Returns a list of (FieldDescriptor, value) tuples for present fields.
A message field is non-empty if HasField() would return true. A singular primitive field is non-empty if HasField() would return true in proto2 or it is non zero in proto3. A repeated field is non-empty if it contains at least one element. The fields are ordered by field number.
Returns:
list[tuple(FieldDescriptor, value)]: field descriptors and values for all fields in the message which are not empty. The values vary by field type.
838 def HasField(self, field_name): 839 try: 840 field = hassable_fields[field_name] 841 except KeyError as exc: 842 raise ValueError('Protocol message %s has no non-repeated field "%s" ' 843 'nor has presence is not available for this field.' % ( 844 message_descriptor.full_name, field_name)) from exc 845 846 if isinstance(field, descriptor_mod.OneofDescriptor): 847 try: 848 return HasField(self, self._oneofs[field].name) 849 except KeyError: 850 return False 851 else: 852 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 853 value = self._fields.get(field) 854 return value is not None and value._is_present_in_parent 855 else: 856 return field in self._fields
Checks if a certain field is set for the message.
For a oneof group, checks if any field inside is set. Note that if the
field_name is not defined in the message descriptor, ValueError
will
be raised.
Arguments:
- field_name (str): The name of the field to check for presence.
Returns:
bool: Whether a value has been set for the named field.
Raises:
- ValueError: if the
field_name
is not a member of this message.
863 def ClearField(self, field_name): 864 try: 865 field = message_descriptor.fields_by_name[field_name] 866 except KeyError: 867 try: 868 field = message_descriptor.oneofs_by_name[field_name] 869 if field in self._oneofs: 870 field = self._oneofs[field] 871 else: 872 return 873 except KeyError: 874 raise ValueError('Protocol message %s has no "%s" field.' % 875 (message_descriptor.name, field_name)) 876 877 if field in self._fields: 878 # To match the C++ implementation, we need to invalidate iterators 879 # for map fields when ClearField() happens. 880 if hasattr(self._fields[field], 'InvalidateIterators'): 881 self._fields[field].InvalidateIterators() 882 883 # Note: If the field is a sub-message, its listener will still point 884 # at us. That's fine, because the worst than can happen is that it 885 # will call _Modified() and invalidate our byte size. Big deal. 886 del self._fields[field] 887 888 if self._oneofs.get(field.containing_oneof, None) is field: 889 del self._oneofs[field.containing_oneof] 890 891 # Always call _Modified() -- even if nothing was changed, this is 892 # a mutating method, and thus calling it should cause the field to become 893 # present in the parent message. 894 self._Modified()
Clears the contents of a given field.
Inside a oneof group, clears the field set. If the name neither refers to a
defined field or oneof group, ValueError
is raised.
Arguments:
- field_name (str): The name of the field to check for presence.
Raises:
- ValueError: if the
field_name
is not a member of this message.
1358 def WhichOneof(self, oneof_name): 1359 """Returns the name of the currently set field inside a oneof, or None.""" 1360 try: 1361 field = message_descriptor.oneofs_by_name[oneof_name] 1362 except KeyError: 1363 raise ValueError( 1364 'Protocol message has no oneof "%s" field.' % oneof_name) 1365 1366 nested_field = self._oneofs.get(field, None) 1367 if nested_field is not None and self.HasField(nested_field.name): 1368 return nested_field.name 1369 else: 1370 return None
Returns the name of the currently set field inside a oneof, or None.
1388def _UnknownFields(self): 1389 warnings.warn( 1390 'message.UnknownFields() is deprecated. Please use the add one ' 1391 'feature unknown_fields.UnknownFieldSet(message) in ' 1392 'unknown_fields.py instead.' 1393 ) 1394 if self._unknown_field_set is None: # pylint: disable=protected-access 1395 # pylint: disable=protected-access 1396 self._unknown_field_set = containers.UnknownFieldSet() 1397 return self._unknown_field_set # pylint: disable=protected-access
Returns the UnknownFieldSet.
Returns:
UnknownFieldSet: The unknown fields stored in this message.
1400def _DiscardUnknownFields(self): 1401 self._unknown_fields = [] 1402 self._unknown_field_set = None # pylint: disable=protected-access 1403 for field, value in self.ListFields(): 1404 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 1405 if _IsMapField(field): 1406 if _IsMessageMapField(field): 1407 for key in value: 1408 value[key].DiscardUnknownFields() 1409 elif field.label == _FieldDescriptor.LABEL_REPEATED: 1410 for sub_message in value: 1411 sub_message.DiscardUnknownFields() 1412 else: 1413 value.DiscardUnknownFields()
Clears all fields in the UnknownFieldSet
.
This operation is recursive for nested message.
1040 def ByteSize(self): 1041 if not self._cached_byte_size_dirty: 1042 return self._cached_byte_size 1043 1044 size = 0 1045 descriptor = self.DESCRIPTOR 1046 if descriptor._is_map_entry: 1047 # Fields of map entry should always be serialized. 1048 key_field = descriptor.fields_by_name['key'] 1049 _MaybeAddEncoder(cls, key_field) 1050 size = key_field._sizer(self.key) 1051 value_field = descriptor.fields_by_name['value'] 1052 _MaybeAddEncoder(cls, value_field) 1053 size += value_field._sizer(self.value) 1054 else: 1055 for field_descriptor, field_value in self.ListFields(): 1056 _MaybeAddEncoder(cls, field_descriptor) 1057 size += field_descriptor._sizer(field_value) 1058 for tag_bytes, value_bytes in self._unknown_fields: 1059 size += len(tag_bytes) + len(value_bytes) 1060 1061 self._cached_byte_size = size 1062 self._cached_byte_size_dirty = False 1063 self._listener_for_children.dirty = False 1064 return size
Returns the serialized size of this message.
Recursively calls ByteSize() on all contained messages.
Returns:
int: The number of bytes required to serialize this message.
784 def RegisterExtension(field_descriptor): 785 field_descriptor.containing_type = cls.DESCRIPTOR 786 # TODO: Use cls.MESSAGE_FACTORY.pool when available. 787 # pylint: disable=protected-access 788 cls.DESCRIPTOR.file.pool._AddExtensionDescriptor(field_descriptor) 789 _AttachFieldHelpers(cls, field_descriptor)
741 def getter(self): 742 field_value = self._fields.get(field) 743 if field_value is None: 744 # Construct a new object to represent this field. 745 field_value = field._default_constructor(self) 746 747 # Atomically check if another thread has preempted us and, if not, swap 748 # in the new object we just created. If someone has preempted us, we 749 # take that object and discard ours. 750 # WARNING: We are relying on setdefault() being atomic. This is true 751 # in CPython but we haven't investigated others. This warning appears 752 # in several other locations in this file. 753 field_value = self._fields.setdefault(field, field_value) 754 return field_value
Getter for target.
642 def getter(self): 643 field_value = self._fields.get(field) 644 if field_value is None: 645 # Construct a new object to represent this field. 646 field_value = field._default_constructor(self) 647 648 # Atomically check if another thread has preempted us and, if not, swap 649 # in the new object we just created. If someone has preempted us, we 650 # take that object and discard ours. 651 # WARNING: We are relying on setdefault() being atomic. This is true 652 # in CPython but we haven't investigated others. This warning appears 653 # in several other locations in this file. 654 field_value = self._fields.setdefault(field, field_value) 655 return field_value
Getter for exprs.
1253 def FindInitializationErrors(self): 1254 """Finds required fields which are not initialized. 1255 1256 Returns: 1257 A list of strings. Each string is a path to an uninitialized field from 1258 the top-level message, e.g. "foo.bar[5].baz". 1259 """ 1260 1261 errors = [] # simplify things 1262 1263 for field in required_fields: 1264 if not self.HasField(field.name): 1265 errors.append(field.name) 1266 1267 for field, value in self.ListFields(): 1268 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 1269 if field.is_extension: 1270 name = '(%s)' % field.full_name 1271 else: 1272 name = field.name 1273 1274 if _IsMapField(field): 1275 if _IsMessageMapField(field): 1276 for key in value: 1277 element = value[key] 1278 prefix = '%s[%s].' % (name, key) 1279 sub_errors = element.FindInitializationErrors() 1280 errors += [prefix + error for error in sub_errors] 1281 else: 1282 # ScalarMaps can't have any initialization errors. 1283 pass 1284 elif field.label == _FieldDescriptor.LABEL_REPEATED: 1285 for i in range(len(value)): 1286 element = value[i] 1287 prefix = '%s[%d].' % (name, i) 1288 sub_errors = element.FindInitializationErrors() 1289 errors += [prefix + error for error in sub_errors] 1290 else: 1291 prefix = name + '.' 1292 sub_errors = value.FindInitializationErrors() 1293 errors += [prefix + error for error in sub_errors] 1294 1295 return errors
Finds required fields which are not initialized.
Returns:
A list of strings. Each string is a path to an uninitialized field from the top-level message, e.g. "foo.bar[5].baz".
Inherited Members
- google.protobuf.message.Message
- CopyFrom
- ParseFromString
- HasExtension
- ClearExtension
Abstract base class for protocol messages.
Protocol message classes are almost always generated by the protocol compiler. These generated types subclass Message and implement the methods shown below.
496 def init(self, **kwargs): 497 self._cached_byte_size = 0 498 self._cached_byte_size_dirty = len(kwargs) > 0 499 self._fields = {} 500 # Contains a mapping from oneof field descriptors to the descriptor 501 # of the currently set field in that oneof field. 502 self._oneofs = {} 503 504 # _unknown_fields is () when empty for efficiency, and will be turned into 505 # a list if fields are added. 506 self._unknown_fields = () 507 # _unknown_field_set is None when empty for efficiency, and will be 508 # turned into UnknownFieldSet struct if fields are added. 509 self._unknown_field_set = None # pylint: disable=protected-access 510 self._is_present_in_parent = False 511 self._listener = message_listener_mod.NullMessageListener() 512 self._listener_for_children = _Listener(self) 513 for field_name, field_value in kwargs.items(): 514 field = _GetFieldByName(message_descriptor, field_name) 515 if field is None: 516 raise TypeError('%s() got an unexpected keyword argument "%s"' % 517 (message_descriptor.name, field_name)) 518 if field_value is None: 519 # field=None is the same as no field at all. 520 continue 521 if field.label == _FieldDescriptor.LABEL_REPEATED: 522 copy = field._default_constructor(self) 523 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: # Composite 524 if _IsMapField(field): 525 if _IsMessageMapField(field): 526 for key in field_value: 527 copy[key].MergeFrom(field_value[key]) 528 else: 529 copy.update(field_value) 530 else: 531 for val in field_value: 532 if isinstance(val, dict): 533 copy.add(**val) 534 else: 535 copy.add().MergeFrom(val) 536 else: # Scalar 537 if field.cpp_type == _FieldDescriptor.CPPTYPE_ENUM: 538 field_value = [_GetIntegerEnumValue(field.enum_type, val) 539 for val in field_value] 540 copy.extend(field_value) 541 self._fields[field] = copy 542 elif field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 543 copy = field._default_constructor(self) 544 new_val = field_value 545 if isinstance(field_value, dict): 546 new_val = field.message_type._concrete_class(**field_value) 547 try: 548 copy.MergeFrom(new_val) 549 except TypeError: 550 _ReraiseTypeErrorWithFieldName(message_descriptor.name, field_name) 551 self._fields[field] = copy 552 else: 553 if field.cpp_type == _FieldDescriptor.CPPTYPE_ENUM: 554 field_value = _GetIntegerEnumValue(field.enum_type, field_value) 555 try: 556 setattr(self, field_name, field_value) 557 except TypeError: 558 _ReraiseTypeErrorWithFieldName(message_descriptor.name, field_name)
1312 def MergeFrom(self, msg): 1313 if not isinstance(msg, cls): 1314 raise TypeError( 1315 'Parameter to MergeFrom() must be instance of same class: ' 1316 'expected %s got %s.' % (_FullyQualifiedClassName(cls), 1317 _FullyQualifiedClassName(msg.__class__))) 1318 1319 assert msg is not self 1320 self._Modified() 1321 1322 fields = self._fields 1323 1324 for field, value in msg._fields.items(): 1325 if field.label == LABEL_REPEATED: 1326 field_value = fields.get(field) 1327 if field_value is None: 1328 # Construct a new object to represent this field. 1329 field_value = field._default_constructor(self) 1330 fields[field] = field_value 1331 field_value.MergeFrom(value) 1332 elif field.cpp_type == CPPTYPE_MESSAGE: 1333 if value._is_present_in_parent: 1334 field_value = fields.get(field) 1335 if field_value is None: 1336 # Construct a new object to represent this field. 1337 field_value = field._default_constructor(self) 1338 fields[field] = field_value 1339 field_value.MergeFrom(value) 1340 else: 1341 self._fields[field] = value 1342 if field.containing_oneof: 1343 self._UpdateOneofState(field) 1344 1345 if msg._unknown_fields: 1346 if not self._unknown_fields: 1347 self._unknown_fields = [] 1348 self._unknown_fields.extend(msg._unknown_fields) 1349 # pylint: disable=protected-access 1350 if self._unknown_field_set is None: 1351 self._unknown_field_set = containers.UnknownFieldSet() 1352 self._unknown_field_set._extend(msg._unknown_field_set)
Merges the contents of the specified message into current message.
This method merges the contents of the specified message into the current message. Singular fields that are set in the specified message overwrite the corresponding fields in the current message. Repeated fields are appended. Singular sub-messages and groups are recursively merged.
Arguments:
- other_msg (Message): A message to merge into the current message.
1375def _Clear(self): 1376 # Clear fields. 1377 self._fields = {} 1378 self._unknown_fields = () 1379 # pylint: disable=protected-access 1380 if self._unknown_field_set is not None: 1381 self._unknown_field_set._clear() 1382 self._unknown_field_set = None 1383 1384 self._oneofs = {} 1385 self._Modified()
Clears all data that was set in the message.
1452 def Modified(self): 1453 """Sets the _cached_byte_size_dirty bit to true, 1454 and propagates this to our listener iff this was a state change. 1455 """ 1456 1457 # Note: Some callers check _cached_byte_size_dirty before calling 1458 # _Modified() as an extra optimization. So, if this method is ever 1459 # changed such that it does stuff even when _cached_byte_size_dirty is 1460 # already true, the callers need to be updated. 1461 if not self._cached_byte_size_dirty: 1462 self._cached_byte_size_dirty = True 1463 self._listener_for_children.dirty = True 1464 self._is_present_in_parent = True 1465 self._listener.Modified()
Sets the _cached_byte_size_dirty bit to true, and propagates this to our listener iff this was a state change.
1213 def IsInitialized(self, errors=None): 1214 """Checks if all required fields of a message are set. 1215 1216 Args: 1217 errors: A list which, if provided, will be populated with the field 1218 paths of all missing required fields. 1219 1220 Returns: 1221 True iff the specified message has all required fields set. 1222 """ 1223 1224 # Performance is critical so we avoid HasField() and ListFields(). 1225 1226 for field in required_fields: 1227 if (field not in self._fields or 1228 (field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE and 1229 not self._fields[field]._is_present_in_parent)): 1230 if errors is not None: 1231 errors.extend(self.FindInitializationErrors()) 1232 return False 1233 1234 for field, value in list(self._fields.items()): # dict can change size! 1235 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 1236 if field.label == _FieldDescriptor.LABEL_REPEATED: 1237 if (field.message_type._is_map_entry): 1238 continue 1239 for element in value: 1240 if not element.IsInitialized(): 1241 if errors is not None: 1242 errors.extend(self.FindInitializationErrors()) 1243 return False 1244 elif value._is_present_in_parent and not value.IsInitialized(): 1245 if errors is not None: 1246 errors.extend(self.FindInitializationErrors()) 1247 return False 1248 1249 return True
Checks if all required fields of a message are set.
Arguments:
- errors: A list which, if provided, will be populated with the field paths of all missing required fields.
Returns:
True iff the specified message has all required fields set.
1119 def MergeFromString(self, serialized): 1120 serialized = memoryview(serialized) 1121 length = len(serialized) 1122 try: 1123 if self._InternalParse(serialized, 0, length) != length: 1124 # The only reason _InternalParse would return early is if it 1125 # encountered an end-group tag. 1126 raise message_mod.DecodeError('Unexpected end-group tag.') 1127 except (IndexError, TypeError): 1128 # Now ord(buf[p:p+1]) == ord('') gets TypeError. 1129 raise message_mod.DecodeError('Truncated message.') 1130 except struct.error as e: 1131 raise message_mod.DecodeError(e) 1132 return length # Return this for legacy reasons.
Merges serialized protocol buffer data into this message.
When we find a field in serialized
that is already present
in this message:
- If it's a "repeated" field, we append to the end of our list.
- Else, if it's a scalar, we overwrite our field.
- Else, (it's a nonrepeated composite), we recursively merge into the existing composite.
Arguments:
- serialized (bytes): Any object that allows us to call
memoryview(serialized)
to access a string of bytes using the buffer interface.
Returns:
int: The number of bytes read from
serialized
. For non-group messages, this will always belen(serialized)
, but for messages which are actually groups, this will generally be less thanlen(serialized)
, since we must stop when we reach anEND_GROUP
tag. Note that if we do stop because of anEND_GROUP
tag, the number of bytes returned does not include the bytes for theEND_GROUP
tag information.
Raises:
- DecodeError: if the input cannot be parsed.
1072 def SerializeToString(self, **kwargs): 1073 # Check if the message has all of its required fields set. 1074 if not self.IsInitialized(): 1075 raise message_mod.EncodeError( 1076 'Message %s is missing required fields: %s' % ( 1077 self.DESCRIPTOR.full_name, ','.join(self.FindInitializationErrors()))) 1078 return self.SerializePartialToString(**kwargs)
Serializes the protocol message to a binary string.
Keyword Args:
deterministic (bool): If true, requests deterministic serialization of the protobuf, with predictable ordering of map keys.
Returns:
A binary string representation of the message if all of the required fields in the message are set (i.e. the message is initialized).
Raises:
- EncodeError: if the message isn't initialized (see
IsInitialized()
).
1085 def SerializePartialToString(self, **kwargs): 1086 out = BytesIO() 1087 self._InternalSerialize(out.write, **kwargs) 1088 return out.getvalue()
Serializes the protocol message to a binary string.
This method is similar to SerializeToString but doesn't check if the message is initialized.
Keyword Args:
deterministic (bool): If true, requests deterministic serialization of the protobuf, with predictable ordering of map keys.
Returns:
bytes: A serialized representation of the partial message.
814 def ListFields(self): 815 all_fields = [item for item in self._fields.items() if _IsPresent(item)] 816 all_fields.sort(key = lambda item: item[0].number) 817 return all_fields
Returns a list of (FieldDescriptor, value) tuples for present fields.
A message field is non-empty if HasField() would return true. A singular primitive field is non-empty if HasField() would return true in proto2 or it is non zero in proto3. A repeated field is non-empty if it contains at least one element. The fields are ordered by field number.
Returns:
list[tuple(FieldDescriptor, value)]: field descriptors and values for all fields in the message which are not empty. The values vary by field type.
838 def HasField(self, field_name): 839 try: 840 field = hassable_fields[field_name] 841 except KeyError as exc: 842 raise ValueError('Protocol message %s has no non-repeated field "%s" ' 843 'nor has presence is not available for this field.' % ( 844 message_descriptor.full_name, field_name)) from exc 845 846 if isinstance(field, descriptor_mod.OneofDescriptor): 847 try: 848 return HasField(self, self._oneofs[field].name) 849 except KeyError: 850 return False 851 else: 852 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 853 value = self._fields.get(field) 854 return value is not None and value._is_present_in_parent 855 else: 856 return field in self._fields
Checks if a certain field is set for the message.
For a oneof group, checks if any field inside is set. Note that if the
field_name is not defined in the message descriptor, ValueError
will
be raised.
Arguments:
- field_name (str): The name of the field to check for presence.
Returns:
bool: Whether a value has been set for the named field.
Raises:
- ValueError: if the
field_name
is not a member of this message.
863 def ClearField(self, field_name): 864 try: 865 field = message_descriptor.fields_by_name[field_name] 866 except KeyError: 867 try: 868 field = message_descriptor.oneofs_by_name[field_name] 869 if field in self._oneofs: 870 field = self._oneofs[field] 871 else: 872 return 873 except KeyError: 874 raise ValueError('Protocol message %s has no "%s" field.' % 875 (message_descriptor.name, field_name)) 876 877 if field in self._fields: 878 # To match the C++ implementation, we need to invalidate iterators 879 # for map fields when ClearField() happens. 880 if hasattr(self._fields[field], 'InvalidateIterators'): 881 self._fields[field].InvalidateIterators() 882 883 # Note: If the field is a sub-message, its listener will still point 884 # at us. That's fine, because the worst than can happen is that it 885 # will call _Modified() and invalidate our byte size. Big deal. 886 del self._fields[field] 887 888 if self._oneofs.get(field.containing_oneof, None) is field: 889 del self._oneofs[field.containing_oneof] 890 891 # Always call _Modified() -- even if nothing was changed, this is 892 # a mutating method, and thus calling it should cause the field to become 893 # present in the parent message. 894 self._Modified()
Clears the contents of a given field.
Inside a oneof group, clears the field set. If the name neither refers to a
defined field or oneof group, ValueError
is raised.
Arguments:
- field_name (str): The name of the field to check for presence.
Raises:
- ValueError: if the
field_name
is not a member of this message.
1358 def WhichOneof(self, oneof_name): 1359 """Returns the name of the currently set field inside a oneof, or None.""" 1360 try: 1361 field = message_descriptor.oneofs_by_name[oneof_name] 1362 except KeyError: 1363 raise ValueError( 1364 'Protocol message has no oneof "%s" field.' % oneof_name) 1365 1366 nested_field = self._oneofs.get(field, None) 1367 if nested_field is not None and self.HasField(nested_field.name): 1368 return nested_field.name 1369 else: 1370 return None
Returns the name of the currently set field inside a oneof, or None.
1388def _UnknownFields(self): 1389 warnings.warn( 1390 'message.UnknownFields() is deprecated. Please use the add one ' 1391 'feature unknown_fields.UnknownFieldSet(message) in ' 1392 'unknown_fields.py instead.' 1393 ) 1394 if self._unknown_field_set is None: # pylint: disable=protected-access 1395 # pylint: disable=protected-access 1396 self._unknown_field_set = containers.UnknownFieldSet() 1397 return self._unknown_field_set # pylint: disable=protected-access
Returns the UnknownFieldSet.
Returns:
UnknownFieldSet: The unknown fields stored in this message.
1400def _DiscardUnknownFields(self): 1401 self._unknown_fields = [] 1402 self._unknown_field_set = None # pylint: disable=protected-access 1403 for field, value in self.ListFields(): 1404 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 1405 if _IsMapField(field): 1406 if _IsMessageMapField(field): 1407 for key in value: 1408 value[key].DiscardUnknownFields() 1409 elif field.label == _FieldDescriptor.LABEL_REPEATED: 1410 for sub_message in value: 1411 sub_message.DiscardUnknownFields() 1412 else: 1413 value.DiscardUnknownFields()
Clears all fields in the UnknownFieldSet
.
This operation is recursive for nested message.
1040 def ByteSize(self): 1041 if not self._cached_byte_size_dirty: 1042 return self._cached_byte_size 1043 1044 size = 0 1045 descriptor = self.DESCRIPTOR 1046 if descriptor._is_map_entry: 1047 # Fields of map entry should always be serialized. 1048 key_field = descriptor.fields_by_name['key'] 1049 _MaybeAddEncoder(cls, key_field) 1050 size = key_field._sizer(self.key) 1051 value_field = descriptor.fields_by_name['value'] 1052 _MaybeAddEncoder(cls, value_field) 1053 size += value_field._sizer(self.value) 1054 else: 1055 for field_descriptor, field_value in self.ListFields(): 1056 _MaybeAddEncoder(cls, field_descriptor) 1057 size += field_descriptor._sizer(field_value) 1058 for tag_bytes, value_bytes in self._unknown_fields: 1059 size += len(tag_bytes) + len(value_bytes) 1060 1061 self._cached_byte_size = size 1062 self._cached_byte_size_dirty = False 1063 self._listener_for_children.dirty = False 1064 return size
Returns the serialized size of this message.
Recursively calls ByteSize() on all contained messages.
Returns:
int: The number of bytes required to serialize this message.
784 def RegisterExtension(field_descriptor): 785 field_descriptor.containing_type = cls.DESCRIPTOR 786 # TODO: Use cls.MESSAGE_FACTORY.pool when available. 787 # pylint: disable=protected-access 788 cls.DESCRIPTOR.file.pool._AddExtensionDescriptor(field_descriptor) 789 _AttachFieldHelpers(cls, field_descriptor)
642 def getter(self): 643 field_value = self._fields.get(field) 644 if field_value is None: 645 # Construct a new object to represent this field. 646 field_value = field._default_constructor(self) 647 648 # Atomically check if another thread has preempted us and, if not, swap 649 # in the new object we just created. If someone has preempted us, we 650 # take that object and discard ours. 651 # WARNING: We are relying on setdefault() being atomic. This is true 652 # in CPython but we haven't investigated others. This warning appears 653 # in several other locations in this file. 654 field_value = self._fields.setdefault(field, field_value) 655 return field_value
Getter for exprs.
1253 def FindInitializationErrors(self): 1254 """Finds required fields which are not initialized. 1255 1256 Returns: 1257 A list of strings. Each string is a path to an uninitialized field from 1258 the top-level message, e.g. "foo.bar[5].baz". 1259 """ 1260 1261 errors = [] # simplify things 1262 1263 for field in required_fields: 1264 if not self.HasField(field.name): 1265 errors.append(field.name) 1266 1267 for field, value in self.ListFields(): 1268 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 1269 if field.is_extension: 1270 name = '(%s)' % field.full_name 1271 else: 1272 name = field.name 1273 1274 if _IsMapField(field): 1275 if _IsMessageMapField(field): 1276 for key in value: 1277 element = value[key] 1278 prefix = '%s[%s].' % (name, key) 1279 sub_errors = element.FindInitializationErrors() 1280 errors += [prefix + error for error in sub_errors] 1281 else: 1282 # ScalarMaps can't have any initialization errors. 1283 pass 1284 elif field.label == _FieldDescriptor.LABEL_REPEATED: 1285 for i in range(len(value)): 1286 element = value[i] 1287 prefix = '%s[%d].' % (name, i) 1288 sub_errors = element.FindInitializationErrors() 1289 errors += [prefix + error for error in sub_errors] 1290 else: 1291 prefix = name + '.' 1292 sub_errors = value.FindInitializationErrors() 1293 errors += [prefix + error for error in sub_errors] 1294 1295 return errors
Finds required fields which are not initialized.
Returns:
A list of strings. Each string is a path to an uninitialized field from the top-level message, e.g. "foo.bar[5].baz".
Inherited Members
- google.protobuf.message.Message
- CopyFrom
- ParseFromString
- HasExtension
- ClearExtension
Abstract base class for protocol messages.
Protocol message classes are almost always generated by the protocol compiler. These generated types subclass Message and implement the methods shown below.
496 def init(self, **kwargs): 497 self._cached_byte_size = 0 498 self._cached_byte_size_dirty = len(kwargs) > 0 499 self._fields = {} 500 # Contains a mapping from oneof field descriptors to the descriptor 501 # of the currently set field in that oneof field. 502 self._oneofs = {} 503 504 # _unknown_fields is () when empty for efficiency, and will be turned into 505 # a list if fields are added. 506 self._unknown_fields = () 507 # _unknown_field_set is None when empty for efficiency, and will be 508 # turned into UnknownFieldSet struct if fields are added. 509 self._unknown_field_set = None # pylint: disable=protected-access 510 self._is_present_in_parent = False 511 self._listener = message_listener_mod.NullMessageListener() 512 self._listener_for_children = _Listener(self) 513 for field_name, field_value in kwargs.items(): 514 field = _GetFieldByName(message_descriptor, field_name) 515 if field is None: 516 raise TypeError('%s() got an unexpected keyword argument "%s"' % 517 (message_descriptor.name, field_name)) 518 if field_value is None: 519 # field=None is the same as no field at all. 520 continue 521 if field.label == _FieldDescriptor.LABEL_REPEATED: 522 copy = field._default_constructor(self) 523 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: # Composite 524 if _IsMapField(field): 525 if _IsMessageMapField(field): 526 for key in field_value: 527 copy[key].MergeFrom(field_value[key]) 528 else: 529 copy.update(field_value) 530 else: 531 for val in field_value: 532 if isinstance(val, dict): 533 copy.add(**val) 534 else: 535 copy.add().MergeFrom(val) 536 else: # Scalar 537 if field.cpp_type == _FieldDescriptor.CPPTYPE_ENUM: 538 field_value = [_GetIntegerEnumValue(field.enum_type, val) 539 for val in field_value] 540 copy.extend(field_value) 541 self._fields[field] = copy 542 elif field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 543 copy = field._default_constructor(self) 544 new_val = field_value 545 if isinstance(field_value, dict): 546 new_val = field.message_type._concrete_class(**field_value) 547 try: 548 copy.MergeFrom(new_val) 549 except TypeError: 550 _ReraiseTypeErrorWithFieldName(message_descriptor.name, field_name) 551 self._fields[field] = copy 552 else: 553 if field.cpp_type == _FieldDescriptor.CPPTYPE_ENUM: 554 field_value = _GetIntegerEnumValue(field.enum_type, field_value) 555 try: 556 setattr(self, field_name, field_value) 557 except TypeError: 558 _ReraiseTypeErrorWithFieldName(message_descriptor.name, field_name)
1312 def MergeFrom(self, msg): 1313 if not isinstance(msg, cls): 1314 raise TypeError( 1315 'Parameter to MergeFrom() must be instance of same class: ' 1316 'expected %s got %s.' % (_FullyQualifiedClassName(cls), 1317 _FullyQualifiedClassName(msg.__class__))) 1318 1319 assert msg is not self 1320 self._Modified() 1321 1322 fields = self._fields 1323 1324 for field, value in msg._fields.items(): 1325 if field.label == LABEL_REPEATED: 1326 field_value = fields.get(field) 1327 if field_value is None: 1328 # Construct a new object to represent this field. 1329 field_value = field._default_constructor(self) 1330 fields[field] = field_value 1331 field_value.MergeFrom(value) 1332 elif field.cpp_type == CPPTYPE_MESSAGE: 1333 if value._is_present_in_parent: 1334 field_value = fields.get(field) 1335 if field_value is None: 1336 # Construct a new object to represent this field. 1337 field_value = field._default_constructor(self) 1338 fields[field] = field_value 1339 field_value.MergeFrom(value) 1340 else: 1341 self._fields[field] = value 1342 if field.containing_oneof: 1343 self._UpdateOneofState(field) 1344 1345 if msg._unknown_fields: 1346 if not self._unknown_fields: 1347 self._unknown_fields = [] 1348 self._unknown_fields.extend(msg._unknown_fields) 1349 # pylint: disable=protected-access 1350 if self._unknown_field_set is None: 1351 self._unknown_field_set = containers.UnknownFieldSet() 1352 self._unknown_field_set._extend(msg._unknown_field_set)
Merges the contents of the specified message into current message.
This method merges the contents of the specified message into the current message. Singular fields that are set in the specified message overwrite the corresponding fields in the current message. Repeated fields are appended. Singular sub-messages and groups are recursively merged.
Arguments:
- other_msg (Message): A message to merge into the current message.
1375def _Clear(self): 1376 # Clear fields. 1377 self._fields = {} 1378 self._unknown_fields = () 1379 # pylint: disable=protected-access 1380 if self._unknown_field_set is not None: 1381 self._unknown_field_set._clear() 1382 self._unknown_field_set = None 1383 1384 self._oneofs = {} 1385 self._Modified()
Clears all data that was set in the message.
1452 def Modified(self): 1453 """Sets the _cached_byte_size_dirty bit to true, 1454 and propagates this to our listener iff this was a state change. 1455 """ 1456 1457 # Note: Some callers check _cached_byte_size_dirty before calling 1458 # _Modified() as an extra optimization. So, if this method is ever 1459 # changed such that it does stuff even when _cached_byte_size_dirty is 1460 # already true, the callers need to be updated. 1461 if not self._cached_byte_size_dirty: 1462 self._cached_byte_size_dirty = True 1463 self._listener_for_children.dirty = True 1464 self._is_present_in_parent = True 1465 self._listener.Modified()
Sets the _cached_byte_size_dirty bit to true, and propagates this to our listener iff this was a state change.
1213 def IsInitialized(self, errors=None): 1214 """Checks if all required fields of a message are set. 1215 1216 Args: 1217 errors: A list which, if provided, will be populated with the field 1218 paths of all missing required fields. 1219 1220 Returns: 1221 True iff the specified message has all required fields set. 1222 """ 1223 1224 # Performance is critical so we avoid HasField() and ListFields(). 1225 1226 for field in required_fields: 1227 if (field not in self._fields or 1228 (field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE and 1229 not self._fields[field]._is_present_in_parent)): 1230 if errors is not None: 1231 errors.extend(self.FindInitializationErrors()) 1232 return False 1233 1234 for field, value in list(self._fields.items()): # dict can change size! 1235 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 1236 if field.label == _FieldDescriptor.LABEL_REPEATED: 1237 if (field.message_type._is_map_entry): 1238 continue 1239 for element in value: 1240 if not element.IsInitialized(): 1241 if errors is not None: 1242 errors.extend(self.FindInitializationErrors()) 1243 return False 1244 elif value._is_present_in_parent and not value.IsInitialized(): 1245 if errors is not None: 1246 errors.extend(self.FindInitializationErrors()) 1247 return False 1248 1249 return True
Checks if all required fields of a message are set.
Arguments:
- errors: A list which, if provided, will be populated with the field paths of all missing required fields.
Returns:
True iff the specified message has all required fields set.
1119 def MergeFromString(self, serialized): 1120 serialized = memoryview(serialized) 1121 length = len(serialized) 1122 try: 1123 if self._InternalParse(serialized, 0, length) != length: 1124 # The only reason _InternalParse would return early is if it 1125 # encountered an end-group tag. 1126 raise message_mod.DecodeError('Unexpected end-group tag.') 1127 except (IndexError, TypeError): 1128 # Now ord(buf[p:p+1]) == ord('') gets TypeError. 1129 raise message_mod.DecodeError('Truncated message.') 1130 except struct.error as e: 1131 raise message_mod.DecodeError(e) 1132 return length # Return this for legacy reasons.
Merges serialized protocol buffer data into this message.
When we find a field in serialized
that is already present
in this message:
- If it's a "repeated" field, we append to the end of our list.
- Else, if it's a scalar, we overwrite our field.
- Else, (it's a nonrepeated composite), we recursively merge into the existing composite.
Arguments:
- serialized (bytes): Any object that allows us to call
memoryview(serialized)
to access a string of bytes using the buffer interface.
Returns:
int: The number of bytes read from
serialized
. For non-group messages, this will always belen(serialized)
, but for messages which are actually groups, this will generally be less thanlen(serialized)
, since we must stop when we reach anEND_GROUP
tag. Note that if we do stop because of anEND_GROUP
tag, the number of bytes returned does not include the bytes for theEND_GROUP
tag information.
Raises:
- DecodeError: if the input cannot be parsed.
1072 def SerializeToString(self, **kwargs): 1073 # Check if the message has all of its required fields set. 1074 if not self.IsInitialized(): 1075 raise message_mod.EncodeError( 1076 'Message %s is missing required fields: %s' % ( 1077 self.DESCRIPTOR.full_name, ','.join(self.FindInitializationErrors()))) 1078 return self.SerializePartialToString(**kwargs)
Serializes the protocol message to a binary string.
Keyword Args:
deterministic (bool): If true, requests deterministic serialization of the protobuf, with predictable ordering of map keys.
Returns:
A binary string representation of the message if all of the required fields in the message are set (i.e. the message is initialized).
Raises:
- EncodeError: if the message isn't initialized (see
IsInitialized()
).
1085 def SerializePartialToString(self, **kwargs): 1086 out = BytesIO() 1087 self._InternalSerialize(out.write, **kwargs) 1088 return out.getvalue()
Serializes the protocol message to a binary string.
This method is similar to SerializeToString but doesn't check if the message is initialized.
Keyword Args:
deterministic (bool): If true, requests deterministic serialization of the protobuf, with predictable ordering of map keys.
Returns:
bytes: A serialized representation of the partial message.
814 def ListFields(self): 815 all_fields = [item for item in self._fields.items() if _IsPresent(item)] 816 all_fields.sort(key = lambda item: item[0].number) 817 return all_fields
Returns a list of (FieldDescriptor, value) tuples for present fields.
A message field is non-empty if HasField() would return true. A singular primitive field is non-empty if HasField() would return true in proto2 or it is non zero in proto3. A repeated field is non-empty if it contains at least one element. The fields are ordered by field number.
Returns:
list[tuple(FieldDescriptor, value)]: field descriptors and values for all fields in the message which are not empty. The values vary by field type.
838 def HasField(self, field_name): 839 try: 840 field = hassable_fields[field_name] 841 except KeyError as exc: 842 raise ValueError('Protocol message %s has no non-repeated field "%s" ' 843 'nor has presence is not available for this field.' % ( 844 message_descriptor.full_name, field_name)) from exc 845 846 if isinstance(field, descriptor_mod.OneofDescriptor): 847 try: 848 return HasField(self, self._oneofs[field].name) 849 except KeyError: 850 return False 851 else: 852 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 853 value = self._fields.get(field) 854 return value is not None and value._is_present_in_parent 855 else: 856 return field in self._fields
Checks if a certain field is set for the message.
For a oneof group, checks if any field inside is set. Note that if the
field_name is not defined in the message descriptor, ValueError
will
be raised.
Arguments:
- field_name (str): The name of the field to check for presence.
Returns:
bool: Whether a value has been set for the named field.
Raises:
- ValueError: if the
field_name
is not a member of this message.
863 def ClearField(self, field_name): 864 try: 865 field = message_descriptor.fields_by_name[field_name] 866 except KeyError: 867 try: 868 field = message_descriptor.oneofs_by_name[field_name] 869 if field in self._oneofs: 870 field = self._oneofs[field] 871 else: 872 return 873 except KeyError: 874 raise ValueError('Protocol message %s has no "%s" field.' % 875 (message_descriptor.name, field_name)) 876 877 if field in self._fields: 878 # To match the C++ implementation, we need to invalidate iterators 879 # for map fields when ClearField() happens. 880 if hasattr(self._fields[field], 'InvalidateIterators'): 881 self._fields[field].InvalidateIterators() 882 883 # Note: If the field is a sub-message, its listener will still point 884 # at us. That's fine, because the worst than can happen is that it 885 # will call _Modified() and invalidate our byte size. Big deal. 886 del self._fields[field] 887 888 if self._oneofs.get(field.containing_oneof, None) is field: 889 del self._oneofs[field.containing_oneof] 890 891 # Always call _Modified() -- even if nothing was changed, this is 892 # a mutating method, and thus calling it should cause the field to become 893 # present in the parent message. 894 self._Modified()
Clears the contents of a given field.
Inside a oneof group, clears the field set. If the name neither refers to a
defined field or oneof group, ValueError
is raised.
Arguments:
- field_name (str): The name of the field to check for presence.
Raises:
- ValueError: if the
field_name
is not a member of this message.
1358 def WhichOneof(self, oneof_name): 1359 """Returns the name of the currently set field inside a oneof, or None.""" 1360 try: 1361 field = message_descriptor.oneofs_by_name[oneof_name] 1362 except KeyError: 1363 raise ValueError( 1364 'Protocol message has no oneof "%s" field.' % oneof_name) 1365 1366 nested_field = self._oneofs.get(field, None) 1367 if nested_field is not None and self.HasField(nested_field.name): 1368 return nested_field.name 1369 else: 1370 return None
Returns the name of the currently set field inside a oneof, or None.
1388def _UnknownFields(self): 1389 warnings.warn( 1390 'message.UnknownFields() is deprecated. Please use the add one ' 1391 'feature unknown_fields.UnknownFieldSet(message) in ' 1392 'unknown_fields.py instead.' 1393 ) 1394 if self._unknown_field_set is None: # pylint: disable=protected-access 1395 # pylint: disable=protected-access 1396 self._unknown_field_set = containers.UnknownFieldSet() 1397 return self._unknown_field_set # pylint: disable=protected-access
Returns the UnknownFieldSet.
Returns:
UnknownFieldSet: The unknown fields stored in this message.
1400def _DiscardUnknownFields(self): 1401 self._unknown_fields = [] 1402 self._unknown_field_set = None # pylint: disable=protected-access 1403 for field, value in self.ListFields(): 1404 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 1405 if _IsMapField(field): 1406 if _IsMessageMapField(field): 1407 for key in value: 1408 value[key].DiscardUnknownFields() 1409 elif field.label == _FieldDescriptor.LABEL_REPEATED: 1410 for sub_message in value: 1411 sub_message.DiscardUnknownFields() 1412 else: 1413 value.DiscardUnknownFields()
Clears all fields in the UnknownFieldSet
.
This operation is recursive for nested message.
1040 def ByteSize(self): 1041 if not self._cached_byte_size_dirty: 1042 return self._cached_byte_size 1043 1044 size = 0 1045 descriptor = self.DESCRIPTOR 1046 if descriptor._is_map_entry: 1047 # Fields of map entry should always be serialized. 1048 key_field = descriptor.fields_by_name['key'] 1049 _MaybeAddEncoder(cls, key_field) 1050 size = key_field._sizer(self.key) 1051 value_field = descriptor.fields_by_name['value'] 1052 _MaybeAddEncoder(cls, value_field) 1053 size += value_field._sizer(self.value) 1054 else: 1055 for field_descriptor, field_value in self.ListFields(): 1056 _MaybeAddEncoder(cls, field_descriptor) 1057 size += field_descriptor._sizer(field_value) 1058 for tag_bytes, value_bytes in self._unknown_fields: 1059 size += len(tag_bytes) + len(value_bytes) 1060 1061 self._cached_byte_size = size 1062 self._cached_byte_size_dirty = False 1063 self._listener_for_children.dirty = False 1064 return size
Returns the serialized size of this message.
Recursively calls ByteSize() on all contained messages.
Returns:
int: The number of bytes required to serialize this message.
784 def RegisterExtension(field_descriptor): 785 field_descriptor.containing_type = cls.DESCRIPTOR 786 # TODO: Use cls.MESSAGE_FACTORY.pool when available. 787 # pylint: disable=protected-access 788 cls.DESCRIPTOR.file.pool._AddExtensionDescriptor(field_descriptor) 789 _AttachFieldHelpers(cls, field_descriptor)
642 def getter(self): 643 field_value = self._fields.get(field) 644 if field_value is None: 645 # Construct a new object to represent this field. 646 field_value = field._default_constructor(self) 647 648 # Atomically check if another thread has preempted us and, if not, swap 649 # in the new object we just created. If someone has preempted us, we 650 # take that object and discard ours. 651 # WARNING: We are relying on setdefault() being atomic. This is true 652 # in CPython but we haven't investigated others. This warning appears 653 # in several other locations in this file. 654 field_value = self._fields.setdefault(field, field_value) 655 return field_value
Getter for vars.
642 def getter(self): 643 field_value = self._fields.get(field) 644 if field_value is None: 645 # Construct a new object to represent this field. 646 field_value = field._default_constructor(self) 647 648 # Atomically check if another thread has preempted us and, if not, swap 649 # in the new object we just created. If someone has preempted us, we 650 # take that object and discard ours. 651 # WARNING: We are relying on setdefault() being atomic. This is true 652 # in CPython but we haven't investigated others. This warning appears 653 # in several other locations in this file. 654 field_value = self._fields.setdefault(field, field_value) 655 return field_value
Getter for coeffs.
642 def getter(self): 643 field_value = self._fields.get(field) 644 if field_value is None: 645 # Construct a new object to represent this field. 646 field_value = field._default_constructor(self) 647 648 # Atomically check if another thread has preempted us and, if not, swap 649 # in the new object we just created. If someone has preempted us, we 650 # take that object and discard ours. 651 # WARNING: We are relying on setdefault() being atomic. This is true 652 # in CPython but we haven't investigated others. This warning appears 653 # in several other locations in this file. 654 field_value = self._fields.setdefault(field, field_value) 655 return field_value
Getter for domain.
1253 def FindInitializationErrors(self): 1254 """Finds required fields which are not initialized. 1255 1256 Returns: 1257 A list of strings. Each string is a path to an uninitialized field from 1258 the top-level message, e.g. "foo.bar[5].baz". 1259 """ 1260 1261 errors = [] # simplify things 1262 1263 for field in required_fields: 1264 if not self.HasField(field.name): 1265 errors.append(field.name) 1266 1267 for field, value in self.ListFields(): 1268 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 1269 if field.is_extension: 1270 name = '(%s)' % field.full_name 1271 else: 1272 name = field.name 1273 1274 if _IsMapField(field): 1275 if _IsMessageMapField(field): 1276 for key in value: 1277 element = value[key] 1278 prefix = '%s[%s].' % (name, key) 1279 sub_errors = element.FindInitializationErrors() 1280 errors += [prefix + error for error in sub_errors] 1281 else: 1282 # ScalarMaps can't have any initialization errors. 1283 pass 1284 elif field.label == _FieldDescriptor.LABEL_REPEATED: 1285 for i in range(len(value)): 1286 element = value[i] 1287 prefix = '%s[%d].' % (name, i) 1288 sub_errors = element.FindInitializationErrors() 1289 errors += [prefix + error for error in sub_errors] 1290 else: 1291 prefix = name + '.' 1292 sub_errors = value.FindInitializationErrors() 1293 errors += [prefix + error for error in sub_errors] 1294 1295 return errors
Finds required fields which are not initialized.
Returns:
A list of strings. Each string is a path to an uninitialized field from the top-level message, e.g. "foo.bar[5].baz".
Inherited Members
- google.protobuf.message.Message
- CopyFrom
- ParseFromString
- HasExtension
- ClearExtension
Abstract base class for protocol messages.
Protocol message classes are almost always generated by the protocol compiler. These generated types subclass Message and implement the methods shown below.
496 def init(self, **kwargs): 497 self._cached_byte_size = 0 498 self._cached_byte_size_dirty = len(kwargs) > 0 499 self._fields = {} 500 # Contains a mapping from oneof field descriptors to the descriptor 501 # of the currently set field in that oneof field. 502 self._oneofs = {} 503 504 # _unknown_fields is () when empty for efficiency, and will be turned into 505 # a list if fields are added. 506 self._unknown_fields = () 507 # _unknown_field_set is None when empty for efficiency, and will be 508 # turned into UnknownFieldSet struct if fields are added. 509 self._unknown_field_set = None # pylint: disable=protected-access 510 self._is_present_in_parent = False 511 self._listener = message_listener_mod.NullMessageListener() 512 self._listener_for_children = _Listener(self) 513 for field_name, field_value in kwargs.items(): 514 field = _GetFieldByName(message_descriptor, field_name) 515 if field is None: 516 raise TypeError('%s() got an unexpected keyword argument "%s"' % 517 (message_descriptor.name, field_name)) 518 if field_value is None: 519 # field=None is the same as no field at all. 520 continue 521 if field.label == _FieldDescriptor.LABEL_REPEATED: 522 copy = field._default_constructor(self) 523 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: # Composite 524 if _IsMapField(field): 525 if _IsMessageMapField(field): 526 for key in field_value: 527 copy[key].MergeFrom(field_value[key]) 528 else: 529 copy.update(field_value) 530 else: 531 for val in field_value: 532 if isinstance(val, dict): 533 copy.add(**val) 534 else: 535 copy.add().MergeFrom(val) 536 else: # Scalar 537 if field.cpp_type == _FieldDescriptor.CPPTYPE_ENUM: 538 field_value = [_GetIntegerEnumValue(field.enum_type, val) 539 for val in field_value] 540 copy.extend(field_value) 541 self._fields[field] = copy 542 elif field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 543 copy = field._default_constructor(self) 544 new_val = field_value 545 if isinstance(field_value, dict): 546 new_val = field.message_type._concrete_class(**field_value) 547 try: 548 copy.MergeFrom(new_val) 549 except TypeError: 550 _ReraiseTypeErrorWithFieldName(message_descriptor.name, field_name) 551 self._fields[field] = copy 552 else: 553 if field.cpp_type == _FieldDescriptor.CPPTYPE_ENUM: 554 field_value = _GetIntegerEnumValue(field.enum_type, field_value) 555 try: 556 setattr(self, field_name, field_value) 557 except TypeError: 558 _ReraiseTypeErrorWithFieldName(message_descriptor.name, field_name)
1312 def MergeFrom(self, msg): 1313 if not isinstance(msg, cls): 1314 raise TypeError( 1315 'Parameter to MergeFrom() must be instance of same class: ' 1316 'expected %s got %s.' % (_FullyQualifiedClassName(cls), 1317 _FullyQualifiedClassName(msg.__class__))) 1318 1319 assert msg is not self 1320 self._Modified() 1321 1322 fields = self._fields 1323 1324 for field, value in msg._fields.items(): 1325 if field.label == LABEL_REPEATED: 1326 field_value = fields.get(field) 1327 if field_value is None: 1328 # Construct a new object to represent this field. 1329 field_value = field._default_constructor(self) 1330 fields[field] = field_value 1331 field_value.MergeFrom(value) 1332 elif field.cpp_type == CPPTYPE_MESSAGE: 1333 if value._is_present_in_parent: 1334 field_value = fields.get(field) 1335 if field_value is None: 1336 # Construct a new object to represent this field. 1337 field_value = field._default_constructor(self) 1338 fields[field] = field_value 1339 field_value.MergeFrom(value) 1340 else: 1341 self._fields[field] = value 1342 if field.containing_oneof: 1343 self._UpdateOneofState(field) 1344 1345 if msg._unknown_fields: 1346 if not self._unknown_fields: 1347 self._unknown_fields = [] 1348 self._unknown_fields.extend(msg._unknown_fields) 1349 # pylint: disable=protected-access 1350 if self._unknown_field_set is None: 1351 self._unknown_field_set = containers.UnknownFieldSet() 1352 self._unknown_field_set._extend(msg._unknown_field_set)
Merges the contents of the specified message into current message.
This method merges the contents of the specified message into the current message. Singular fields that are set in the specified message overwrite the corresponding fields in the current message. Repeated fields are appended. Singular sub-messages and groups are recursively merged.
Arguments:
- other_msg (Message): A message to merge into the current message.
1375def _Clear(self): 1376 # Clear fields. 1377 self._fields = {} 1378 self._unknown_fields = () 1379 # pylint: disable=protected-access 1380 if self._unknown_field_set is not None: 1381 self._unknown_field_set._clear() 1382 self._unknown_field_set = None 1383 1384 self._oneofs = {} 1385 self._Modified()
Clears all data that was set in the message.
1452 def Modified(self): 1453 """Sets the _cached_byte_size_dirty bit to true, 1454 and propagates this to our listener iff this was a state change. 1455 """ 1456 1457 # Note: Some callers check _cached_byte_size_dirty before calling 1458 # _Modified() as an extra optimization. So, if this method is ever 1459 # changed such that it does stuff even when _cached_byte_size_dirty is 1460 # already true, the callers need to be updated. 1461 if not self._cached_byte_size_dirty: 1462 self._cached_byte_size_dirty = True 1463 self._listener_for_children.dirty = True 1464 self._is_present_in_parent = True 1465 self._listener.Modified()
Sets the _cached_byte_size_dirty bit to true, and propagates this to our listener iff this was a state change.
1213 def IsInitialized(self, errors=None): 1214 """Checks if all required fields of a message are set. 1215 1216 Args: 1217 errors: A list which, if provided, will be populated with the field 1218 paths of all missing required fields. 1219 1220 Returns: 1221 True iff the specified message has all required fields set. 1222 """ 1223 1224 # Performance is critical so we avoid HasField() and ListFields(). 1225 1226 for field in required_fields: 1227 if (field not in self._fields or 1228 (field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE and 1229 not self._fields[field]._is_present_in_parent)): 1230 if errors is not None: 1231 errors.extend(self.FindInitializationErrors()) 1232 return False 1233 1234 for field, value in list(self._fields.items()): # dict can change size! 1235 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 1236 if field.label == _FieldDescriptor.LABEL_REPEATED: 1237 if (field.message_type._is_map_entry): 1238 continue 1239 for element in value: 1240 if not element.IsInitialized(): 1241 if errors is not None: 1242 errors.extend(self.FindInitializationErrors()) 1243 return False 1244 elif value._is_present_in_parent and not value.IsInitialized(): 1245 if errors is not None: 1246 errors.extend(self.FindInitializationErrors()) 1247 return False 1248 1249 return True
Checks if all required fields of a message are set.
Arguments:
- errors: A list which, if provided, will be populated with the field paths of all missing required fields.
Returns:
True iff the specified message has all required fields set.
1119 def MergeFromString(self, serialized): 1120 serialized = memoryview(serialized) 1121 length = len(serialized) 1122 try: 1123 if self._InternalParse(serialized, 0, length) != length: 1124 # The only reason _InternalParse would return early is if it 1125 # encountered an end-group tag. 1126 raise message_mod.DecodeError('Unexpected end-group tag.') 1127 except (IndexError, TypeError): 1128 # Now ord(buf[p:p+1]) == ord('') gets TypeError. 1129 raise message_mod.DecodeError('Truncated message.') 1130 except struct.error as e: 1131 raise message_mod.DecodeError(e) 1132 return length # Return this for legacy reasons.
Merges serialized protocol buffer data into this message.
When we find a field in serialized
that is already present
in this message:
- If it's a "repeated" field, we append to the end of our list.
- Else, if it's a scalar, we overwrite our field.
- Else, (it's a nonrepeated composite), we recursively merge into the existing composite.
Arguments:
- serialized (bytes): Any object that allows us to call
memoryview(serialized)
to access a string of bytes using the buffer interface.
Returns:
int: The number of bytes read from
serialized
. For non-group messages, this will always belen(serialized)
, but for messages which are actually groups, this will generally be less thanlen(serialized)
, since we must stop when we reach anEND_GROUP
tag. Note that if we do stop because of anEND_GROUP
tag, the number of bytes returned does not include the bytes for theEND_GROUP
tag information.
Raises:
- DecodeError: if the input cannot be parsed.
1072 def SerializeToString(self, **kwargs): 1073 # Check if the message has all of its required fields set. 1074 if not self.IsInitialized(): 1075 raise message_mod.EncodeError( 1076 'Message %s is missing required fields: %s' % ( 1077 self.DESCRIPTOR.full_name, ','.join(self.FindInitializationErrors()))) 1078 return self.SerializePartialToString(**kwargs)
Serializes the protocol message to a binary string.
Keyword Args:
deterministic (bool): If true, requests deterministic serialization of the protobuf, with predictable ordering of map keys.
Returns:
A binary string representation of the message if all of the required fields in the message are set (i.e. the message is initialized).
Raises:
- EncodeError: if the message isn't initialized (see
IsInitialized()
).
1085 def SerializePartialToString(self, **kwargs): 1086 out = BytesIO() 1087 self._InternalSerialize(out.write, **kwargs) 1088 return out.getvalue()
Serializes the protocol message to a binary string.
This method is similar to SerializeToString but doesn't check if the message is initialized.
Keyword Args:
deterministic (bool): If true, requests deterministic serialization of the protobuf, with predictable ordering of map keys.
Returns:
bytes: A serialized representation of the partial message.
814 def ListFields(self): 815 all_fields = [item for item in self._fields.items() if _IsPresent(item)] 816 all_fields.sort(key = lambda item: item[0].number) 817 return all_fields
Returns a list of (FieldDescriptor, value) tuples for present fields.
A message field is non-empty if HasField() would return true. A singular primitive field is non-empty if HasField() would return true in proto2 or it is non zero in proto3. A repeated field is non-empty if it contains at least one element. The fields are ordered by field number.
Returns:
list[tuple(FieldDescriptor, value)]: field descriptors and values for all fields in the message which are not empty. The values vary by field type.
838 def HasField(self, field_name): 839 try: 840 field = hassable_fields[field_name] 841 except KeyError as exc: 842 raise ValueError('Protocol message %s has no non-repeated field "%s" ' 843 'nor has presence is not available for this field.' % ( 844 message_descriptor.full_name, field_name)) from exc 845 846 if isinstance(field, descriptor_mod.OneofDescriptor): 847 try: 848 return HasField(self, self._oneofs[field].name) 849 except KeyError: 850 return False 851 else: 852 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 853 value = self._fields.get(field) 854 return value is not None and value._is_present_in_parent 855 else: 856 return field in self._fields
Checks if a certain field is set for the message.
For a oneof group, checks if any field inside is set. Note that if the
field_name is not defined in the message descriptor, ValueError
will
be raised.
Arguments:
- field_name (str): The name of the field to check for presence.
Returns:
bool: Whether a value has been set for the named field.
Raises:
- ValueError: if the
field_name
is not a member of this message.
863 def ClearField(self, field_name): 864 try: 865 field = message_descriptor.fields_by_name[field_name] 866 except KeyError: 867 try: 868 field = message_descriptor.oneofs_by_name[field_name] 869 if field in self._oneofs: 870 field = self._oneofs[field] 871 else: 872 return 873 except KeyError: 874 raise ValueError('Protocol message %s has no "%s" field.' % 875 (message_descriptor.name, field_name)) 876 877 if field in self._fields: 878 # To match the C++ implementation, we need to invalidate iterators 879 # for map fields when ClearField() happens. 880 if hasattr(self._fields[field], 'InvalidateIterators'): 881 self._fields[field].InvalidateIterators() 882 883 # Note: If the field is a sub-message, its listener will still point 884 # at us. That's fine, because the worst than can happen is that it 885 # will call _Modified() and invalidate our byte size. Big deal. 886 del self._fields[field] 887 888 if self._oneofs.get(field.containing_oneof, None) is field: 889 del self._oneofs[field.containing_oneof] 890 891 # Always call _Modified() -- even if nothing was changed, this is 892 # a mutating method, and thus calling it should cause the field to become 893 # present in the parent message. 894 self._Modified()
Clears the contents of a given field.
Inside a oneof group, clears the field set. If the name neither refers to a
defined field or oneof group, ValueError
is raised.
Arguments:
- field_name (str): The name of the field to check for presence.
Raises:
- ValueError: if the
field_name
is not a member of this message.
1358 def WhichOneof(self, oneof_name): 1359 """Returns the name of the currently set field inside a oneof, or None.""" 1360 try: 1361 field = message_descriptor.oneofs_by_name[oneof_name] 1362 except KeyError: 1363 raise ValueError( 1364 'Protocol message has no oneof "%s" field.' % oneof_name) 1365 1366 nested_field = self._oneofs.get(field, None) 1367 if nested_field is not None and self.HasField(nested_field.name): 1368 return nested_field.name 1369 else: 1370 return None
Returns the name of the currently set field inside a oneof, or None.
1388def _UnknownFields(self): 1389 warnings.warn( 1390 'message.UnknownFields() is deprecated. Please use the add one ' 1391 'feature unknown_fields.UnknownFieldSet(message) in ' 1392 'unknown_fields.py instead.' 1393 ) 1394 if self._unknown_field_set is None: # pylint: disable=protected-access 1395 # pylint: disable=protected-access 1396 self._unknown_field_set = containers.UnknownFieldSet() 1397 return self._unknown_field_set # pylint: disable=protected-access
Returns the UnknownFieldSet.
Returns:
UnknownFieldSet: The unknown fields stored in this message.
1400def _DiscardUnknownFields(self): 1401 self._unknown_fields = [] 1402 self._unknown_field_set = None # pylint: disable=protected-access 1403 for field, value in self.ListFields(): 1404 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 1405 if _IsMapField(field): 1406 if _IsMessageMapField(field): 1407 for key in value: 1408 value[key].DiscardUnknownFields() 1409 elif field.label == _FieldDescriptor.LABEL_REPEATED: 1410 for sub_message in value: 1411 sub_message.DiscardUnknownFields() 1412 else: 1413 value.DiscardUnknownFields()
Clears all fields in the UnknownFieldSet
.
This operation is recursive for nested message.
1040 def ByteSize(self): 1041 if not self._cached_byte_size_dirty: 1042 return self._cached_byte_size 1043 1044 size = 0 1045 descriptor = self.DESCRIPTOR 1046 if descriptor._is_map_entry: 1047 # Fields of map entry should always be serialized. 1048 key_field = descriptor.fields_by_name['key'] 1049 _MaybeAddEncoder(cls, key_field) 1050 size = key_field._sizer(self.key) 1051 value_field = descriptor.fields_by_name['value'] 1052 _MaybeAddEncoder(cls, value_field) 1053 size += value_field._sizer(self.value) 1054 else: 1055 for field_descriptor, field_value in self.ListFields(): 1056 _MaybeAddEncoder(cls, field_descriptor) 1057 size += field_descriptor._sizer(field_value) 1058 for tag_bytes, value_bytes in self._unknown_fields: 1059 size += len(tag_bytes) + len(value_bytes) 1060 1061 self._cached_byte_size = size 1062 self._cached_byte_size_dirty = False 1063 self._listener_for_children.dirty = False 1064 return size
Returns the serialized size of this message.
Recursively calls ByteSize() on all contained messages.
Returns:
int: The number of bytes required to serialize this message.
784 def RegisterExtension(field_descriptor): 785 field_descriptor.containing_type = cls.DESCRIPTOR 786 # TODO: Use cls.MESSAGE_FACTORY.pool when available. 787 # pylint: disable=protected-access 788 cls.DESCRIPTOR.file.pool._AddExtensionDescriptor(field_descriptor) 789 _AttachFieldHelpers(cls, field_descriptor)
685 def getter(self): 686 # TODO: This may be broken since there may not be 687 # default_value. Combine with has_default_value somehow. 688 return self._fields.get(field, default_value)
Getter for index.
685 def getter(self): 686 # TODO: This may be broken since there may not be 687 # default_value. Combine with has_default_value somehow. 688 return self._fields.get(field, default_value)
Getter for target.
642 def getter(self): 643 field_value = self._fields.get(field) 644 if field_value is None: 645 # Construct a new object to represent this field. 646 field_value = field._default_constructor(self) 647 648 # Atomically check if another thread has preempted us and, if not, swap 649 # in the new object we just created. If someone has preempted us, we 650 # take that object and discard ours. 651 # WARNING: We are relying on setdefault() being atomic. This is true 652 # in CPython but we haven't investigated others. This warning appears 653 # in several other locations in this file. 654 field_value = self._fields.setdefault(field, field_value) 655 return field_value
Getter for vars.
1253 def FindInitializationErrors(self): 1254 """Finds required fields which are not initialized. 1255 1256 Returns: 1257 A list of strings. Each string is a path to an uninitialized field from 1258 the top-level message, e.g. "foo.bar[5].baz". 1259 """ 1260 1261 errors = [] # simplify things 1262 1263 for field in required_fields: 1264 if not self.HasField(field.name): 1265 errors.append(field.name) 1266 1267 for field, value in self.ListFields(): 1268 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 1269 if field.is_extension: 1270 name = '(%s)' % field.full_name 1271 else: 1272 name = field.name 1273 1274 if _IsMapField(field): 1275 if _IsMessageMapField(field): 1276 for key in value: 1277 element = value[key] 1278 prefix = '%s[%s].' % (name, key) 1279 sub_errors = element.FindInitializationErrors() 1280 errors += [prefix + error for error in sub_errors] 1281 else: 1282 # ScalarMaps can't have any initialization errors. 1283 pass 1284 elif field.label == _FieldDescriptor.LABEL_REPEATED: 1285 for i in range(len(value)): 1286 element = value[i] 1287 prefix = '%s[%d].' % (name, i) 1288 sub_errors = element.FindInitializationErrors() 1289 errors += [prefix + error for error in sub_errors] 1290 else: 1291 prefix = name + '.' 1292 sub_errors = value.FindInitializationErrors() 1293 errors += [prefix + error for error in sub_errors] 1294 1295 return errors
Finds required fields which are not initialized.
Returns:
A list of strings. Each string is a path to an uninitialized field from the top-level message, e.g. "foo.bar[5].baz".
Inherited Members
- google.protobuf.message.Message
- CopyFrom
- ParseFromString
- HasExtension
- ClearExtension
Abstract base class for protocol messages.
Protocol message classes are almost always generated by the protocol compiler. These generated types subclass Message and implement the methods shown below.
496 def init(self, **kwargs): 497 self._cached_byte_size = 0 498 self._cached_byte_size_dirty = len(kwargs) > 0 499 self._fields = {} 500 # Contains a mapping from oneof field descriptors to the descriptor 501 # of the currently set field in that oneof field. 502 self._oneofs = {} 503 504 # _unknown_fields is () when empty for efficiency, and will be turned into 505 # a list if fields are added. 506 self._unknown_fields = () 507 # _unknown_field_set is None when empty for efficiency, and will be 508 # turned into UnknownFieldSet struct if fields are added. 509 self._unknown_field_set = None # pylint: disable=protected-access 510 self._is_present_in_parent = False 511 self._listener = message_listener_mod.NullMessageListener() 512 self._listener_for_children = _Listener(self) 513 for field_name, field_value in kwargs.items(): 514 field = _GetFieldByName(message_descriptor, field_name) 515 if field is None: 516 raise TypeError('%s() got an unexpected keyword argument "%s"' % 517 (message_descriptor.name, field_name)) 518 if field_value is None: 519 # field=None is the same as no field at all. 520 continue 521 if field.label == _FieldDescriptor.LABEL_REPEATED: 522 copy = field._default_constructor(self) 523 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: # Composite 524 if _IsMapField(field): 525 if _IsMessageMapField(field): 526 for key in field_value: 527 copy[key].MergeFrom(field_value[key]) 528 else: 529 copy.update(field_value) 530 else: 531 for val in field_value: 532 if isinstance(val, dict): 533 copy.add(**val) 534 else: 535 copy.add().MergeFrom(val) 536 else: # Scalar 537 if field.cpp_type == _FieldDescriptor.CPPTYPE_ENUM: 538 field_value = [_GetIntegerEnumValue(field.enum_type, val) 539 for val in field_value] 540 copy.extend(field_value) 541 self._fields[field] = copy 542 elif field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 543 copy = field._default_constructor(self) 544 new_val = field_value 545 if isinstance(field_value, dict): 546 new_val = field.message_type._concrete_class(**field_value) 547 try: 548 copy.MergeFrom(new_val) 549 except TypeError: 550 _ReraiseTypeErrorWithFieldName(message_descriptor.name, field_name) 551 self._fields[field] = copy 552 else: 553 if field.cpp_type == _FieldDescriptor.CPPTYPE_ENUM: 554 field_value = _GetIntegerEnumValue(field.enum_type, field_value) 555 try: 556 setattr(self, field_name, field_value) 557 except TypeError: 558 _ReraiseTypeErrorWithFieldName(message_descriptor.name, field_name)
1312 def MergeFrom(self, msg): 1313 if not isinstance(msg, cls): 1314 raise TypeError( 1315 'Parameter to MergeFrom() must be instance of same class: ' 1316 'expected %s got %s.' % (_FullyQualifiedClassName(cls), 1317 _FullyQualifiedClassName(msg.__class__))) 1318 1319 assert msg is not self 1320 self._Modified() 1321 1322 fields = self._fields 1323 1324 for field, value in msg._fields.items(): 1325 if field.label == LABEL_REPEATED: 1326 field_value = fields.get(field) 1327 if field_value is None: 1328 # Construct a new object to represent this field. 1329 field_value = field._default_constructor(self) 1330 fields[field] = field_value 1331 field_value.MergeFrom(value) 1332 elif field.cpp_type == CPPTYPE_MESSAGE: 1333 if value._is_present_in_parent: 1334 field_value = fields.get(field) 1335 if field_value is None: 1336 # Construct a new object to represent this field. 1337 field_value = field._default_constructor(self) 1338 fields[field] = field_value 1339 field_value.MergeFrom(value) 1340 else: 1341 self._fields[field] = value 1342 if field.containing_oneof: 1343 self._UpdateOneofState(field) 1344 1345 if msg._unknown_fields: 1346 if not self._unknown_fields: 1347 self._unknown_fields = [] 1348 self._unknown_fields.extend(msg._unknown_fields) 1349 # pylint: disable=protected-access 1350 if self._unknown_field_set is None: 1351 self._unknown_field_set = containers.UnknownFieldSet() 1352 self._unknown_field_set._extend(msg._unknown_field_set)
Merges the contents of the specified message into current message.
This method merges the contents of the specified message into the current message. Singular fields that are set in the specified message overwrite the corresponding fields in the current message. Repeated fields are appended. Singular sub-messages and groups are recursively merged.
Arguments:
- other_msg (Message): A message to merge into the current message.
1375def _Clear(self): 1376 # Clear fields. 1377 self._fields = {} 1378 self._unknown_fields = () 1379 # pylint: disable=protected-access 1380 if self._unknown_field_set is not None: 1381 self._unknown_field_set._clear() 1382 self._unknown_field_set = None 1383 1384 self._oneofs = {} 1385 self._Modified()
Clears all data that was set in the message.
1452 def Modified(self): 1453 """Sets the _cached_byte_size_dirty bit to true, 1454 and propagates this to our listener iff this was a state change. 1455 """ 1456 1457 # Note: Some callers check _cached_byte_size_dirty before calling 1458 # _Modified() as an extra optimization. So, if this method is ever 1459 # changed such that it does stuff even when _cached_byte_size_dirty is 1460 # already true, the callers need to be updated. 1461 if not self._cached_byte_size_dirty: 1462 self._cached_byte_size_dirty = True 1463 self._listener_for_children.dirty = True 1464 self._is_present_in_parent = True 1465 self._listener.Modified()
Sets the _cached_byte_size_dirty bit to true, and propagates this to our listener iff this was a state change.
1213 def IsInitialized(self, errors=None): 1214 """Checks if all required fields of a message are set. 1215 1216 Args: 1217 errors: A list which, if provided, will be populated with the field 1218 paths of all missing required fields. 1219 1220 Returns: 1221 True iff the specified message has all required fields set. 1222 """ 1223 1224 # Performance is critical so we avoid HasField() and ListFields(). 1225 1226 for field in required_fields: 1227 if (field not in self._fields or 1228 (field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE and 1229 not self._fields[field]._is_present_in_parent)): 1230 if errors is not None: 1231 errors.extend(self.FindInitializationErrors()) 1232 return False 1233 1234 for field, value in list(self._fields.items()): # dict can change size! 1235 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 1236 if field.label == _FieldDescriptor.LABEL_REPEATED: 1237 if (field.message_type._is_map_entry): 1238 continue 1239 for element in value: 1240 if not element.IsInitialized(): 1241 if errors is not None: 1242 errors.extend(self.FindInitializationErrors()) 1243 return False 1244 elif value._is_present_in_parent and not value.IsInitialized(): 1245 if errors is not None: 1246 errors.extend(self.FindInitializationErrors()) 1247 return False 1248 1249 return True
Checks if all required fields of a message are set.
Arguments:
- errors: A list which, if provided, will be populated with the field paths of all missing required fields.
Returns:
True iff the specified message has all required fields set.
1119 def MergeFromString(self, serialized): 1120 serialized = memoryview(serialized) 1121 length = len(serialized) 1122 try: 1123 if self._InternalParse(serialized, 0, length) != length: 1124 # The only reason _InternalParse would return early is if it 1125 # encountered an end-group tag. 1126 raise message_mod.DecodeError('Unexpected end-group tag.') 1127 except (IndexError, TypeError): 1128 # Now ord(buf[p:p+1]) == ord('') gets TypeError. 1129 raise message_mod.DecodeError('Truncated message.') 1130 except struct.error as e: 1131 raise message_mod.DecodeError(e) 1132 return length # Return this for legacy reasons.
Merges serialized protocol buffer data into this message.
When we find a field in serialized
that is already present
in this message:
- If it's a "repeated" field, we append to the end of our list.
- Else, if it's a scalar, we overwrite our field.
- Else, (it's a nonrepeated composite), we recursively merge into the existing composite.
Arguments:
- serialized (bytes): Any object that allows us to call
memoryview(serialized)
to access a string of bytes using the buffer interface.
Returns:
int: The number of bytes read from
serialized
. For non-group messages, this will always belen(serialized)
, but for messages which are actually groups, this will generally be less thanlen(serialized)
, since we must stop when we reach anEND_GROUP
tag. Note that if we do stop because of anEND_GROUP
tag, the number of bytes returned does not include the bytes for theEND_GROUP
tag information.
Raises:
- DecodeError: if the input cannot be parsed.
1072 def SerializeToString(self, **kwargs): 1073 # Check if the message has all of its required fields set. 1074 if not self.IsInitialized(): 1075 raise message_mod.EncodeError( 1076 'Message %s is missing required fields: %s' % ( 1077 self.DESCRIPTOR.full_name, ','.join(self.FindInitializationErrors()))) 1078 return self.SerializePartialToString(**kwargs)
Serializes the protocol message to a binary string.
Keyword Args:
deterministic (bool): If true, requests deterministic serialization of the protobuf, with predictable ordering of map keys.
Returns:
A binary string representation of the message if all of the required fields in the message are set (i.e. the message is initialized).
Raises:
- EncodeError: if the message isn't initialized (see
IsInitialized()
).
1085 def SerializePartialToString(self, **kwargs): 1086 out = BytesIO() 1087 self._InternalSerialize(out.write, **kwargs) 1088 return out.getvalue()
Serializes the protocol message to a binary string.
This method is similar to SerializeToString but doesn't check if the message is initialized.
Keyword Args:
deterministic (bool): If true, requests deterministic serialization of the protobuf, with predictable ordering of map keys.
Returns:
bytes: A serialized representation of the partial message.
814 def ListFields(self): 815 all_fields = [item for item in self._fields.items() if _IsPresent(item)] 816 all_fields.sort(key = lambda item: item[0].number) 817 return all_fields
Returns a list of (FieldDescriptor, value) tuples for present fields.
A message field is non-empty if HasField() would return true. A singular primitive field is non-empty if HasField() would return true in proto2 or it is non zero in proto3. A repeated field is non-empty if it contains at least one element. The fields are ordered by field number.
Returns:
list[tuple(FieldDescriptor, value)]: field descriptors and values for all fields in the message which are not empty. The values vary by field type.
838 def HasField(self, field_name): 839 try: 840 field = hassable_fields[field_name] 841 except KeyError as exc: 842 raise ValueError('Protocol message %s has no non-repeated field "%s" ' 843 'nor has presence is not available for this field.' % ( 844 message_descriptor.full_name, field_name)) from exc 845 846 if isinstance(field, descriptor_mod.OneofDescriptor): 847 try: 848 return HasField(self, self._oneofs[field].name) 849 except KeyError: 850 return False 851 else: 852 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 853 value = self._fields.get(field) 854 return value is not None and value._is_present_in_parent 855 else: 856 return field in self._fields
Checks if a certain field is set for the message.
For a oneof group, checks if any field inside is set. Note that if the
field_name is not defined in the message descriptor, ValueError
will
be raised.
Arguments:
- field_name (str): The name of the field to check for presence.
Returns:
bool: Whether a value has been set for the named field.
Raises:
- ValueError: if the
field_name
is not a member of this message.
863 def ClearField(self, field_name): 864 try: 865 field = message_descriptor.fields_by_name[field_name] 866 except KeyError: 867 try: 868 field = message_descriptor.oneofs_by_name[field_name] 869 if field in self._oneofs: 870 field = self._oneofs[field] 871 else: 872 return 873 except KeyError: 874 raise ValueError('Protocol message %s has no "%s" field.' % 875 (message_descriptor.name, field_name)) 876 877 if field in self._fields: 878 # To match the C++ implementation, we need to invalidate iterators 879 # for map fields when ClearField() happens. 880 if hasattr(self._fields[field], 'InvalidateIterators'): 881 self._fields[field].InvalidateIterators() 882 883 # Note: If the field is a sub-message, its listener will still point 884 # at us. That's fine, because the worst than can happen is that it 885 # will call _Modified() and invalidate our byte size. Big deal. 886 del self._fields[field] 887 888 if self._oneofs.get(field.containing_oneof, None) is field: 889 del self._oneofs[field.containing_oneof] 890 891 # Always call _Modified() -- even if nothing was changed, this is 892 # a mutating method, and thus calling it should cause the field to become 893 # present in the parent message. 894 self._Modified()
Clears the contents of a given field.
Inside a oneof group, clears the field set. If the name neither refers to a
defined field or oneof group, ValueError
is raised.
Arguments:
- field_name (str): The name of the field to check for presence.
Raises:
- ValueError: if the
field_name
is not a member of this message.
1358 def WhichOneof(self, oneof_name): 1359 """Returns the name of the currently set field inside a oneof, or None.""" 1360 try: 1361 field = message_descriptor.oneofs_by_name[oneof_name] 1362 except KeyError: 1363 raise ValueError( 1364 'Protocol message has no oneof "%s" field.' % oneof_name) 1365 1366 nested_field = self._oneofs.get(field, None) 1367 if nested_field is not None and self.HasField(nested_field.name): 1368 return nested_field.name 1369 else: 1370 return None
Returns the name of the currently set field inside a oneof, or None.
1388def _UnknownFields(self): 1389 warnings.warn( 1390 'message.UnknownFields() is deprecated. Please use the add one ' 1391 'feature unknown_fields.UnknownFieldSet(message) in ' 1392 'unknown_fields.py instead.' 1393 ) 1394 if self._unknown_field_set is None: # pylint: disable=protected-access 1395 # pylint: disable=protected-access 1396 self._unknown_field_set = containers.UnknownFieldSet() 1397 return self._unknown_field_set # pylint: disable=protected-access
Returns the UnknownFieldSet.
Returns:
UnknownFieldSet: The unknown fields stored in this message.
1400def _DiscardUnknownFields(self): 1401 self._unknown_fields = [] 1402 self._unknown_field_set = None # pylint: disable=protected-access 1403 for field, value in self.ListFields(): 1404 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 1405 if _IsMapField(field): 1406 if _IsMessageMapField(field): 1407 for key in value: 1408 value[key].DiscardUnknownFields() 1409 elif field.label == _FieldDescriptor.LABEL_REPEATED: 1410 for sub_message in value: 1411 sub_message.DiscardUnknownFields() 1412 else: 1413 value.DiscardUnknownFields()
Clears all fields in the UnknownFieldSet
.
This operation is recursive for nested message.
1040 def ByteSize(self): 1041 if not self._cached_byte_size_dirty: 1042 return self._cached_byte_size 1043 1044 size = 0 1045 descriptor = self.DESCRIPTOR 1046 if descriptor._is_map_entry: 1047 # Fields of map entry should always be serialized. 1048 key_field = descriptor.fields_by_name['key'] 1049 _MaybeAddEncoder(cls, key_field) 1050 size = key_field._sizer(self.key) 1051 value_field = descriptor.fields_by_name['value'] 1052 _MaybeAddEncoder(cls, value_field) 1053 size += value_field._sizer(self.value) 1054 else: 1055 for field_descriptor, field_value in self.ListFields(): 1056 _MaybeAddEncoder(cls, field_descriptor) 1057 size += field_descriptor._sizer(field_value) 1058 for tag_bytes, value_bytes in self._unknown_fields: 1059 size += len(tag_bytes) + len(value_bytes) 1060 1061 self._cached_byte_size = size 1062 self._cached_byte_size_dirty = False 1063 self._listener_for_children.dirty = False 1064 return size
Returns the serialized size of this message.
Recursively calls ByteSize() on all contained messages.
Returns:
int: The number of bytes required to serialize this message.
784 def RegisterExtension(field_descriptor): 785 field_descriptor.containing_type = cls.DESCRIPTOR 786 # TODO: Use cls.MESSAGE_FACTORY.pool when available. 787 # pylint: disable=protected-access 788 cls.DESCRIPTOR.file.pool._AddExtensionDescriptor(field_descriptor) 789 _AttachFieldHelpers(cls, field_descriptor)
741 def getter(self): 742 field_value = self._fields.get(field) 743 if field_value is None: 744 # Construct a new object to represent this field. 745 field_value = field._default_constructor(self) 746 747 # Atomically check if another thread has preempted us and, if not, swap 748 # in the new object we just created. If someone has preempted us, we 749 # take that object and discard ours. 750 # WARNING: We are relying on setdefault() being atomic. This is true 751 # in CPython but we haven't investigated others. This warning appears 752 # in several other locations in this file. 753 field_value = self._fields.setdefault(field, field_value) 754 return field_value
Getter for start.
741 def getter(self): 742 field_value = self._fields.get(field) 743 if field_value is None: 744 # Construct a new object to represent this field. 745 field_value = field._default_constructor(self) 746 747 # Atomically check if another thread has preempted us and, if not, swap 748 # in the new object we just created. If someone has preempted us, we 749 # take that object and discard ours. 750 # WARNING: We are relying on setdefault() being atomic. This is true 751 # in CPython but we haven't investigated others. This warning appears 752 # in several other locations in this file. 753 field_value = self._fields.setdefault(field, field_value) 754 return field_value
Getter for end.
741 def getter(self): 742 field_value = self._fields.get(field) 743 if field_value is None: 744 # Construct a new object to represent this field. 745 field_value = field._default_constructor(self) 746 747 # Atomically check if another thread has preempted us and, if not, swap 748 # in the new object we just created. If someone has preempted us, we 749 # take that object and discard ours. 750 # WARNING: We are relying on setdefault() being atomic. This is true 751 # in CPython but we haven't investigated others. This warning appears 752 # in several other locations in this file. 753 field_value = self._fields.setdefault(field, field_value) 754 return field_value
Getter for size.
1253 def FindInitializationErrors(self): 1254 """Finds required fields which are not initialized. 1255 1256 Returns: 1257 A list of strings. Each string is a path to an uninitialized field from 1258 the top-level message, e.g. "foo.bar[5].baz". 1259 """ 1260 1261 errors = [] # simplify things 1262 1263 for field in required_fields: 1264 if not self.HasField(field.name): 1265 errors.append(field.name) 1266 1267 for field, value in self.ListFields(): 1268 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 1269 if field.is_extension: 1270 name = '(%s)' % field.full_name 1271 else: 1272 name = field.name 1273 1274 if _IsMapField(field): 1275 if _IsMessageMapField(field): 1276 for key in value: 1277 element = value[key] 1278 prefix = '%s[%s].' % (name, key) 1279 sub_errors = element.FindInitializationErrors() 1280 errors += [prefix + error for error in sub_errors] 1281 else: 1282 # ScalarMaps can't have any initialization errors. 1283 pass 1284 elif field.label == _FieldDescriptor.LABEL_REPEATED: 1285 for i in range(len(value)): 1286 element = value[i] 1287 prefix = '%s[%d].' % (name, i) 1288 sub_errors = element.FindInitializationErrors() 1289 errors += [prefix + error for error in sub_errors] 1290 else: 1291 prefix = name + '.' 1292 sub_errors = value.FindInitializationErrors() 1293 errors += [prefix + error for error in sub_errors] 1294 1295 return errors
Finds required fields which are not initialized.
Returns:
A list of strings. Each string is a path to an uninitialized field from the top-level message, e.g. "foo.bar[5].baz".
Inherited Members
- google.protobuf.message.Message
- CopyFrom
- ParseFromString
- HasExtension
- ClearExtension
Abstract base class for protocol messages.
Protocol message classes are almost always generated by the protocol compiler. These generated types subclass Message and implement the methods shown below.
496 def init(self, **kwargs): 497 self._cached_byte_size = 0 498 self._cached_byte_size_dirty = len(kwargs) > 0 499 self._fields = {} 500 # Contains a mapping from oneof field descriptors to the descriptor 501 # of the currently set field in that oneof field. 502 self._oneofs = {} 503 504 # _unknown_fields is () when empty for efficiency, and will be turned into 505 # a list if fields are added. 506 self._unknown_fields = () 507 # _unknown_field_set is None when empty for efficiency, and will be 508 # turned into UnknownFieldSet struct if fields are added. 509 self._unknown_field_set = None # pylint: disable=protected-access 510 self._is_present_in_parent = False 511 self._listener = message_listener_mod.NullMessageListener() 512 self._listener_for_children = _Listener(self) 513 for field_name, field_value in kwargs.items(): 514 field = _GetFieldByName(message_descriptor, field_name) 515 if field is None: 516 raise TypeError('%s() got an unexpected keyword argument "%s"' % 517 (message_descriptor.name, field_name)) 518 if field_value is None: 519 # field=None is the same as no field at all. 520 continue 521 if field.label == _FieldDescriptor.LABEL_REPEATED: 522 copy = field._default_constructor(self) 523 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: # Composite 524 if _IsMapField(field): 525 if _IsMessageMapField(field): 526 for key in field_value: 527 copy[key].MergeFrom(field_value[key]) 528 else: 529 copy.update(field_value) 530 else: 531 for val in field_value: 532 if isinstance(val, dict): 533 copy.add(**val) 534 else: 535 copy.add().MergeFrom(val) 536 else: # Scalar 537 if field.cpp_type == _FieldDescriptor.CPPTYPE_ENUM: 538 field_value = [_GetIntegerEnumValue(field.enum_type, val) 539 for val in field_value] 540 copy.extend(field_value) 541 self._fields[field] = copy 542 elif field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 543 copy = field._default_constructor(self) 544 new_val = field_value 545 if isinstance(field_value, dict): 546 new_val = field.message_type._concrete_class(**field_value) 547 try: 548 copy.MergeFrom(new_val) 549 except TypeError: 550 _ReraiseTypeErrorWithFieldName(message_descriptor.name, field_name) 551 self._fields[field] = copy 552 else: 553 if field.cpp_type == _FieldDescriptor.CPPTYPE_ENUM: 554 field_value = _GetIntegerEnumValue(field.enum_type, field_value) 555 try: 556 setattr(self, field_name, field_value) 557 except TypeError: 558 _ReraiseTypeErrorWithFieldName(message_descriptor.name, field_name)
1312 def MergeFrom(self, msg): 1313 if not isinstance(msg, cls): 1314 raise TypeError( 1315 'Parameter to MergeFrom() must be instance of same class: ' 1316 'expected %s got %s.' % (_FullyQualifiedClassName(cls), 1317 _FullyQualifiedClassName(msg.__class__))) 1318 1319 assert msg is not self 1320 self._Modified() 1321 1322 fields = self._fields 1323 1324 for field, value in msg._fields.items(): 1325 if field.label == LABEL_REPEATED: 1326 field_value = fields.get(field) 1327 if field_value is None: 1328 # Construct a new object to represent this field. 1329 field_value = field._default_constructor(self) 1330 fields[field] = field_value 1331 field_value.MergeFrom(value) 1332 elif field.cpp_type == CPPTYPE_MESSAGE: 1333 if value._is_present_in_parent: 1334 field_value = fields.get(field) 1335 if field_value is None: 1336 # Construct a new object to represent this field. 1337 field_value = field._default_constructor(self) 1338 fields[field] = field_value 1339 field_value.MergeFrom(value) 1340 else: 1341 self._fields[field] = value 1342 if field.containing_oneof: 1343 self._UpdateOneofState(field) 1344 1345 if msg._unknown_fields: 1346 if not self._unknown_fields: 1347 self._unknown_fields = [] 1348 self._unknown_fields.extend(msg._unknown_fields) 1349 # pylint: disable=protected-access 1350 if self._unknown_field_set is None: 1351 self._unknown_field_set = containers.UnknownFieldSet() 1352 self._unknown_field_set._extend(msg._unknown_field_set)
Merges the contents of the specified message into current message.
This method merges the contents of the specified message into the current message. Singular fields that are set in the specified message overwrite the corresponding fields in the current message. Repeated fields are appended. Singular sub-messages and groups are recursively merged.
Arguments:
- other_msg (Message): A message to merge into the current message.
1375def _Clear(self): 1376 # Clear fields. 1377 self._fields = {} 1378 self._unknown_fields = () 1379 # pylint: disable=protected-access 1380 if self._unknown_field_set is not None: 1381 self._unknown_field_set._clear() 1382 self._unknown_field_set = None 1383 1384 self._oneofs = {} 1385 self._Modified()
Clears all data that was set in the message.
1452 def Modified(self): 1453 """Sets the _cached_byte_size_dirty bit to true, 1454 and propagates this to our listener iff this was a state change. 1455 """ 1456 1457 # Note: Some callers check _cached_byte_size_dirty before calling 1458 # _Modified() as an extra optimization. So, if this method is ever 1459 # changed such that it does stuff even when _cached_byte_size_dirty is 1460 # already true, the callers need to be updated. 1461 if not self._cached_byte_size_dirty: 1462 self._cached_byte_size_dirty = True 1463 self._listener_for_children.dirty = True 1464 self._is_present_in_parent = True 1465 self._listener.Modified()
Sets the _cached_byte_size_dirty bit to true, and propagates this to our listener iff this was a state change.
1213 def IsInitialized(self, errors=None): 1214 """Checks if all required fields of a message are set. 1215 1216 Args: 1217 errors: A list which, if provided, will be populated with the field 1218 paths of all missing required fields. 1219 1220 Returns: 1221 True iff the specified message has all required fields set. 1222 """ 1223 1224 # Performance is critical so we avoid HasField() and ListFields(). 1225 1226 for field in required_fields: 1227 if (field not in self._fields or 1228 (field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE and 1229 not self._fields[field]._is_present_in_parent)): 1230 if errors is not None: 1231 errors.extend(self.FindInitializationErrors()) 1232 return False 1233 1234 for field, value in list(self._fields.items()): # dict can change size! 1235 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 1236 if field.label == _FieldDescriptor.LABEL_REPEATED: 1237 if (field.message_type._is_map_entry): 1238 continue 1239 for element in value: 1240 if not element.IsInitialized(): 1241 if errors is not None: 1242 errors.extend(self.FindInitializationErrors()) 1243 return False 1244 elif value._is_present_in_parent and not value.IsInitialized(): 1245 if errors is not None: 1246 errors.extend(self.FindInitializationErrors()) 1247 return False 1248 1249 return True
Checks if all required fields of a message are set.
Arguments:
- errors: A list which, if provided, will be populated with the field paths of all missing required fields.
Returns:
True iff the specified message has all required fields set.
1119 def MergeFromString(self, serialized): 1120 serialized = memoryview(serialized) 1121 length = len(serialized) 1122 try: 1123 if self._InternalParse(serialized, 0, length) != length: 1124 # The only reason _InternalParse would return early is if it 1125 # encountered an end-group tag. 1126 raise message_mod.DecodeError('Unexpected end-group tag.') 1127 except (IndexError, TypeError): 1128 # Now ord(buf[p:p+1]) == ord('') gets TypeError. 1129 raise message_mod.DecodeError('Truncated message.') 1130 except struct.error as e: 1131 raise message_mod.DecodeError(e) 1132 return length # Return this for legacy reasons.
Merges serialized protocol buffer data into this message.
When we find a field in serialized
that is already present
in this message:
- If it's a "repeated" field, we append to the end of our list.
- Else, if it's a scalar, we overwrite our field.
- Else, (it's a nonrepeated composite), we recursively merge into the existing composite.
Arguments:
- serialized (bytes): Any object that allows us to call
memoryview(serialized)
to access a string of bytes using the buffer interface.
Returns:
int: The number of bytes read from
serialized
. For non-group messages, this will always belen(serialized)
, but for messages which are actually groups, this will generally be less thanlen(serialized)
, since we must stop when we reach anEND_GROUP
tag. Note that if we do stop because of anEND_GROUP
tag, the number of bytes returned does not include the bytes for theEND_GROUP
tag information.
Raises:
- DecodeError: if the input cannot be parsed.
1072 def SerializeToString(self, **kwargs): 1073 # Check if the message has all of its required fields set. 1074 if not self.IsInitialized(): 1075 raise message_mod.EncodeError( 1076 'Message %s is missing required fields: %s' % ( 1077 self.DESCRIPTOR.full_name, ','.join(self.FindInitializationErrors()))) 1078 return self.SerializePartialToString(**kwargs)
Serializes the protocol message to a binary string.
Keyword Args:
deterministic (bool): If true, requests deterministic serialization of the protobuf, with predictable ordering of map keys.
Returns:
A binary string representation of the message if all of the required fields in the message are set (i.e. the message is initialized).
Raises:
- EncodeError: if the message isn't initialized (see
IsInitialized()
).
1085 def SerializePartialToString(self, **kwargs): 1086 out = BytesIO() 1087 self._InternalSerialize(out.write, **kwargs) 1088 return out.getvalue()
Serializes the protocol message to a binary string.
This method is similar to SerializeToString but doesn't check if the message is initialized.
Keyword Args:
deterministic (bool): If true, requests deterministic serialization of the protobuf, with predictable ordering of map keys.
Returns:
bytes: A serialized representation of the partial message.
814 def ListFields(self): 815 all_fields = [item for item in self._fields.items() if _IsPresent(item)] 816 all_fields.sort(key = lambda item: item[0].number) 817 return all_fields
Returns a list of (FieldDescriptor, value) tuples for present fields.
A message field is non-empty if HasField() would return true. A singular primitive field is non-empty if HasField() would return true in proto2 or it is non zero in proto3. A repeated field is non-empty if it contains at least one element. The fields are ordered by field number.
Returns:
list[tuple(FieldDescriptor, value)]: field descriptors and values for all fields in the message which are not empty. The values vary by field type.
838 def HasField(self, field_name): 839 try: 840 field = hassable_fields[field_name] 841 except KeyError as exc: 842 raise ValueError('Protocol message %s has no non-repeated field "%s" ' 843 'nor has presence is not available for this field.' % ( 844 message_descriptor.full_name, field_name)) from exc 845 846 if isinstance(field, descriptor_mod.OneofDescriptor): 847 try: 848 return HasField(self, self._oneofs[field].name) 849 except KeyError: 850 return False 851 else: 852 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 853 value = self._fields.get(field) 854 return value is not None and value._is_present_in_parent 855 else: 856 return field in self._fields
Checks if a certain field is set for the message.
For a oneof group, checks if any field inside is set. Note that if the
field_name is not defined in the message descriptor, ValueError
will
be raised.
Arguments:
- field_name (str): The name of the field to check for presence.
Returns:
bool: Whether a value has been set for the named field.
Raises:
- ValueError: if the
field_name
is not a member of this message.
863 def ClearField(self, field_name): 864 try: 865 field = message_descriptor.fields_by_name[field_name] 866 except KeyError: 867 try: 868 field = message_descriptor.oneofs_by_name[field_name] 869 if field in self._oneofs: 870 field = self._oneofs[field] 871 else: 872 return 873 except KeyError: 874 raise ValueError('Protocol message %s has no "%s" field.' % 875 (message_descriptor.name, field_name)) 876 877 if field in self._fields: 878 # To match the C++ implementation, we need to invalidate iterators 879 # for map fields when ClearField() happens. 880 if hasattr(self._fields[field], 'InvalidateIterators'): 881 self._fields[field].InvalidateIterators() 882 883 # Note: If the field is a sub-message, its listener will still point 884 # at us. That's fine, because the worst than can happen is that it 885 # will call _Modified() and invalidate our byte size. Big deal. 886 del self._fields[field] 887 888 if self._oneofs.get(field.containing_oneof, None) is field: 889 del self._oneofs[field.containing_oneof] 890 891 # Always call _Modified() -- even if nothing was changed, this is 892 # a mutating method, and thus calling it should cause the field to become 893 # present in the parent message. 894 self._Modified()
Clears the contents of a given field.
Inside a oneof group, clears the field set. If the name neither refers to a
defined field or oneof group, ValueError
is raised.
Arguments:
- field_name (str): The name of the field to check for presence.
Raises:
- ValueError: if the
field_name
is not a member of this message.
1358 def WhichOneof(self, oneof_name): 1359 """Returns the name of the currently set field inside a oneof, or None.""" 1360 try: 1361 field = message_descriptor.oneofs_by_name[oneof_name] 1362 except KeyError: 1363 raise ValueError( 1364 'Protocol message has no oneof "%s" field.' % oneof_name) 1365 1366 nested_field = self._oneofs.get(field, None) 1367 if nested_field is not None and self.HasField(nested_field.name): 1368 return nested_field.name 1369 else: 1370 return None
Returns the name of the currently set field inside a oneof, or None.
1388def _UnknownFields(self): 1389 warnings.warn( 1390 'message.UnknownFields() is deprecated. Please use the add one ' 1391 'feature unknown_fields.UnknownFieldSet(message) in ' 1392 'unknown_fields.py instead.' 1393 ) 1394 if self._unknown_field_set is None: # pylint: disable=protected-access 1395 # pylint: disable=protected-access 1396 self._unknown_field_set = containers.UnknownFieldSet() 1397 return self._unknown_field_set # pylint: disable=protected-access
Returns the UnknownFieldSet.
Returns:
UnknownFieldSet: The unknown fields stored in this message.
1400def _DiscardUnknownFields(self): 1401 self._unknown_fields = [] 1402 self._unknown_field_set = None # pylint: disable=protected-access 1403 for field, value in self.ListFields(): 1404 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 1405 if _IsMapField(field): 1406 if _IsMessageMapField(field): 1407 for key in value: 1408 value[key].DiscardUnknownFields() 1409 elif field.label == _FieldDescriptor.LABEL_REPEATED: 1410 for sub_message in value: 1411 sub_message.DiscardUnknownFields() 1412 else: 1413 value.DiscardUnknownFields()
Clears all fields in the UnknownFieldSet
.
This operation is recursive for nested message.
1040 def ByteSize(self): 1041 if not self._cached_byte_size_dirty: 1042 return self._cached_byte_size 1043 1044 size = 0 1045 descriptor = self.DESCRIPTOR 1046 if descriptor._is_map_entry: 1047 # Fields of map entry should always be serialized. 1048 key_field = descriptor.fields_by_name['key'] 1049 _MaybeAddEncoder(cls, key_field) 1050 size = key_field._sizer(self.key) 1051 value_field = descriptor.fields_by_name['value'] 1052 _MaybeAddEncoder(cls, value_field) 1053 size += value_field._sizer(self.value) 1054 else: 1055 for field_descriptor, field_value in self.ListFields(): 1056 _MaybeAddEncoder(cls, field_descriptor) 1057 size += field_descriptor._sizer(field_value) 1058 for tag_bytes, value_bytes in self._unknown_fields: 1059 size += len(tag_bytes) + len(value_bytes) 1060 1061 self._cached_byte_size = size 1062 self._cached_byte_size_dirty = False 1063 self._listener_for_children.dirty = False 1064 return size
Returns the serialized size of this message.
Recursively calls ByteSize() on all contained messages.
Returns:
int: The number of bytes required to serialize this message.
784 def RegisterExtension(field_descriptor): 785 field_descriptor.containing_type = cls.DESCRIPTOR 786 # TODO: Use cls.MESSAGE_FACTORY.pool when available. 787 # pylint: disable=protected-access 788 cls.DESCRIPTOR.file.pool._AddExtensionDescriptor(field_descriptor) 789 _AttachFieldHelpers(cls, field_descriptor)
642 def getter(self): 643 field_value = self._fields.get(field) 644 if field_value is None: 645 # Construct a new object to represent this field. 646 field_value = field._default_constructor(self) 647 648 # Atomically check if another thread has preempted us and, if not, swap 649 # in the new object we just created. If someone has preempted us, we 650 # take that object and discard ours. 651 # WARNING: We are relying on setdefault() being atomic. This is true 652 # in CPython but we haven't investigated others. This warning appears 653 # in several other locations in this file. 654 field_value = self._fields.setdefault(field, field_value) 655 return field_value
Getter for intervals.
1253 def FindInitializationErrors(self): 1254 """Finds required fields which are not initialized. 1255 1256 Returns: 1257 A list of strings. Each string is a path to an uninitialized field from 1258 the top-level message, e.g. "foo.bar[5].baz". 1259 """ 1260 1261 errors = [] # simplify things 1262 1263 for field in required_fields: 1264 if not self.HasField(field.name): 1265 errors.append(field.name) 1266 1267 for field, value in self.ListFields(): 1268 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 1269 if field.is_extension: 1270 name = '(%s)' % field.full_name 1271 else: 1272 name = field.name 1273 1274 if _IsMapField(field): 1275 if _IsMessageMapField(field): 1276 for key in value: 1277 element = value[key] 1278 prefix = '%s[%s].' % (name, key) 1279 sub_errors = element.FindInitializationErrors() 1280 errors += [prefix + error for error in sub_errors] 1281 else: 1282 # ScalarMaps can't have any initialization errors. 1283 pass 1284 elif field.label == _FieldDescriptor.LABEL_REPEATED: 1285 for i in range(len(value)): 1286 element = value[i] 1287 prefix = '%s[%d].' % (name, i) 1288 sub_errors = element.FindInitializationErrors() 1289 errors += [prefix + error for error in sub_errors] 1290 else: 1291 prefix = name + '.' 1292 sub_errors = value.FindInitializationErrors() 1293 errors += [prefix + error for error in sub_errors] 1294 1295 return errors
Finds required fields which are not initialized.
Returns:
A list of strings. Each string is a path to an uninitialized field from the top-level message, e.g. "foo.bar[5].baz".
Inherited Members
- google.protobuf.message.Message
- CopyFrom
- ParseFromString
- HasExtension
- ClearExtension
Abstract base class for protocol messages.
Protocol message classes are almost always generated by the protocol compiler. These generated types subclass Message and implement the methods shown below.
496 def init(self, **kwargs): 497 self._cached_byte_size = 0 498 self._cached_byte_size_dirty = len(kwargs) > 0 499 self._fields = {} 500 # Contains a mapping from oneof field descriptors to the descriptor 501 # of the currently set field in that oneof field. 502 self._oneofs = {} 503 504 # _unknown_fields is () when empty for efficiency, and will be turned into 505 # a list if fields are added. 506 self._unknown_fields = () 507 # _unknown_field_set is None when empty for efficiency, and will be 508 # turned into UnknownFieldSet struct if fields are added. 509 self._unknown_field_set = None # pylint: disable=protected-access 510 self._is_present_in_parent = False 511 self._listener = message_listener_mod.NullMessageListener() 512 self._listener_for_children = _Listener(self) 513 for field_name, field_value in kwargs.items(): 514 field = _GetFieldByName(message_descriptor, field_name) 515 if field is None: 516 raise TypeError('%s() got an unexpected keyword argument "%s"' % 517 (message_descriptor.name, field_name)) 518 if field_value is None: 519 # field=None is the same as no field at all. 520 continue 521 if field.label == _FieldDescriptor.LABEL_REPEATED: 522 copy = field._default_constructor(self) 523 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: # Composite 524 if _IsMapField(field): 525 if _IsMessageMapField(field): 526 for key in field_value: 527 copy[key].MergeFrom(field_value[key]) 528 else: 529 copy.update(field_value) 530 else: 531 for val in field_value: 532 if isinstance(val, dict): 533 copy.add(**val) 534 else: 535 copy.add().MergeFrom(val) 536 else: # Scalar 537 if field.cpp_type == _FieldDescriptor.CPPTYPE_ENUM: 538 field_value = [_GetIntegerEnumValue(field.enum_type, val) 539 for val in field_value] 540 copy.extend(field_value) 541 self._fields[field] = copy 542 elif field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 543 copy = field._default_constructor(self) 544 new_val = field_value 545 if isinstance(field_value, dict): 546 new_val = field.message_type._concrete_class(**field_value) 547 try: 548 copy.MergeFrom(new_val) 549 except TypeError: 550 _ReraiseTypeErrorWithFieldName(message_descriptor.name, field_name) 551 self._fields[field] = copy 552 else: 553 if field.cpp_type == _FieldDescriptor.CPPTYPE_ENUM: 554 field_value = _GetIntegerEnumValue(field.enum_type, field_value) 555 try: 556 setattr(self, field_name, field_value) 557 except TypeError: 558 _ReraiseTypeErrorWithFieldName(message_descriptor.name, field_name)
1312 def MergeFrom(self, msg): 1313 if not isinstance(msg, cls): 1314 raise TypeError( 1315 'Parameter to MergeFrom() must be instance of same class: ' 1316 'expected %s got %s.' % (_FullyQualifiedClassName(cls), 1317 _FullyQualifiedClassName(msg.__class__))) 1318 1319 assert msg is not self 1320 self._Modified() 1321 1322 fields = self._fields 1323 1324 for field, value in msg._fields.items(): 1325 if field.label == LABEL_REPEATED: 1326 field_value = fields.get(field) 1327 if field_value is None: 1328 # Construct a new object to represent this field. 1329 field_value = field._default_constructor(self) 1330 fields[field] = field_value 1331 field_value.MergeFrom(value) 1332 elif field.cpp_type == CPPTYPE_MESSAGE: 1333 if value._is_present_in_parent: 1334 field_value = fields.get(field) 1335 if field_value is None: 1336 # Construct a new object to represent this field. 1337 field_value = field._default_constructor(self) 1338 fields[field] = field_value 1339 field_value.MergeFrom(value) 1340 else: 1341 self._fields[field] = value 1342 if field.containing_oneof: 1343 self._UpdateOneofState(field) 1344 1345 if msg._unknown_fields: 1346 if not self._unknown_fields: 1347 self._unknown_fields = [] 1348 self._unknown_fields.extend(msg._unknown_fields) 1349 # pylint: disable=protected-access 1350 if self._unknown_field_set is None: 1351 self._unknown_field_set = containers.UnknownFieldSet() 1352 self._unknown_field_set._extend(msg._unknown_field_set)
Merges the contents of the specified message into current message.
This method merges the contents of the specified message into the current message. Singular fields that are set in the specified message overwrite the corresponding fields in the current message. Repeated fields are appended. Singular sub-messages and groups are recursively merged.
Arguments:
- other_msg (Message): A message to merge into the current message.
1375def _Clear(self): 1376 # Clear fields. 1377 self._fields = {} 1378 self._unknown_fields = () 1379 # pylint: disable=protected-access 1380 if self._unknown_field_set is not None: 1381 self._unknown_field_set._clear() 1382 self._unknown_field_set = None 1383 1384 self._oneofs = {} 1385 self._Modified()
Clears all data that was set in the message.
1452 def Modified(self): 1453 """Sets the _cached_byte_size_dirty bit to true, 1454 and propagates this to our listener iff this was a state change. 1455 """ 1456 1457 # Note: Some callers check _cached_byte_size_dirty before calling 1458 # _Modified() as an extra optimization. So, if this method is ever 1459 # changed such that it does stuff even when _cached_byte_size_dirty is 1460 # already true, the callers need to be updated. 1461 if not self._cached_byte_size_dirty: 1462 self._cached_byte_size_dirty = True 1463 self._listener_for_children.dirty = True 1464 self._is_present_in_parent = True 1465 self._listener.Modified()
Sets the _cached_byte_size_dirty bit to true, and propagates this to our listener iff this was a state change.
1213 def IsInitialized(self, errors=None): 1214 """Checks if all required fields of a message are set. 1215 1216 Args: 1217 errors: A list which, if provided, will be populated with the field 1218 paths of all missing required fields. 1219 1220 Returns: 1221 True iff the specified message has all required fields set. 1222 """ 1223 1224 # Performance is critical so we avoid HasField() and ListFields(). 1225 1226 for field in required_fields: 1227 if (field not in self._fields or 1228 (field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE and 1229 not self._fields[field]._is_present_in_parent)): 1230 if errors is not None: 1231 errors.extend(self.FindInitializationErrors()) 1232 return False 1233 1234 for field, value in list(self._fields.items()): # dict can change size! 1235 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 1236 if field.label == _FieldDescriptor.LABEL_REPEATED: 1237 if (field.message_type._is_map_entry): 1238 continue 1239 for element in value: 1240 if not element.IsInitialized(): 1241 if errors is not None: 1242 errors.extend(self.FindInitializationErrors()) 1243 return False 1244 elif value._is_present_in_parent and not value.IsInitialized(): 1245 if errors is not None: 1246 errors.extend(self.FindInitializationErrors()) 1247 return False 1248 1249 return True
Checks if all required fields of a message are set.
Arguments:
- errors: A list which, if provided, will be populated with the field paths of all missing required fields.
Returns:
True iff the specified message has all required fields set.
1119 def MergeFromString(self, serialized): 1120 serialized = memoryview(serialized) 1121 length = len(serialized) 1122 try: 1123 if self._InternalParse(serialized, 0, length) != length: 1124 # The only reason _InternalParse would return early is if it 1125 # encountered an end-group tag. 1126 raise message_mod.DecodeError('Unexpected end-group tag.') 1127 except (IndexError, TypeError): 1128 # Now ord(buf[p:p+1]) == ord('') gets TypeError. 1129 raise message_mod.DecodeError('Truncated message.') 1130 except struct.error as e: 1131 raise message_mod.DecodeError(e) 1132 return length # Return this for legacy reasons.
Merges serialized protocol buffer data into this message.
When we find a field in serialized
that is already present
in this message:
- If it's a "repeated" field, we append to the end of our list.
- Else, if it's a scalar, we overwrite our field.
- Else, (it's a nonrepeated composite), we recursively merge into the existing composite.
Arguments:
- serialized (bytes): Any object that allows us to call
memoryview(serialized)
to access a string of bytes using the buffer interface.
Returns:
int: The number of bytes read from
serialized
. For non-group messages, this will always belen(serialized)
, but for messages which are actually groups, this will generally be less thanlen(serialized)
, since we must stop when we reach anEND_GROUP
tag. Note that if we do stop because of anEND_GROUP
tag, the number of bytes returned does not include the bytes for theEND_GROUP
tag information.
Raises:
- DecodeError: if the input cannot be parsed.
1072 def SerializeToString(self, **kwargs): 1073 # Check if the message has all of its required fields set. 1074 if not self.IsInitialized(): 1075 raise message_mod.EncodeError( 1076 'Message %s is missing required fields: %s' % ( 1077 self.DESCRIPTOR.full_name, ','.join(self.FindInitializationErrors()))) 1078 return self.SerializePartialToString(**kwargs)
Serializes the protocol message to a binary string.
Keyword Args:
deterministic (bool): If true, requests deterministic serialization of the protobuf, with predictable ordering of map keys.
Returns:
A binary string representation of the message if all of the required fields in the message are set (i.e. the message is initialized).
Raises:
- EncodeError: if the message isn't initialized (see
IsInitialized()
).
1085 def SerializePartialToString(self, **kwargs): 1086 out = BytesIO() 1087 self._InternalSerialize(out.write, **kwargs) 1088 return out.getvalue()
Serializes the protocol message to a binary string.
This method is similar to SerializeToString but doesn't check if the message is initialized.
Keyword Args:
deterministic (bool): If true, requests deterministic serialization of the protobuf, with predictable ordering of map keys.
Returns:
bytes: A serialized representation of the partial message.
814 def ListFields(self): 815 all_fields = [item for item in self._fields.items() if _IsPresent(item)] 816 all_fields.sort(key = lambda item: item[0].number) 817 return all_fields
Returns a list of (FieldDescriptor, value) tuples for present fields.
A message field is non-empty if HasField() would return true. A singular primitive field is non-empty if HasField() would return true in proto2 or it is non zero in proto3. A repeated field is non-empty if it contains at least one element. The fields are ordered by field number.
Returns:
list[tuple(FieldDescriptor, value)]: field descriptors and values for all fields in the message which are not empty. The values vary by field type.
838 def HasField(self, field_name): 839 try: 840 field = hassable_fields[field_name] 841 except KeyError as exc: 842 raise ValueError('Protocol message %s has no non-repeated field "%s" ' 843 'nor has presence is not available for this field.' % ( 844 message_descriptor.full_name, field_name)) from exc 845 846 if isinstance(field, descriptor_mod.OneofDescriptor): 847 try: 848 return HasField(self, self._oneofs[field].name) 849 except KeyError: 850 return False 851 else: 852 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 853 value = self._fields.get(field) 854 return value is not None and value._is_present_in_parent 855 else: 856 return field in self._fields
Checks if a certain field is set for the message.
For a oneof group, checks if any field inside is set. Note that if the
field_name is not defined in the message descriptor, ValueError
will
be raised.
Arguments:
- field_name (str): The name of the field to check for presence.
Returns:
bool: Whether a value has been set for the named field.
Raises:
- ValueError: if the
field_name
is not a member of this message.
863 def ClearField(self, field_name): 864 try: 865 field = message_descriptor.fields_by_name[field_name] 866 except KeyError: 867 try: 868 field = message_descriptor.oneofs_by_name[field_name] 869 if field in self._oneofs: 870 field = self._oneofs[field] 871 else: 872 return 873 except KeyError: 874 raise ValueError('Protocol message %s has no "%s" field.' % 875 (message_descriptor.name, field_name)) 876 877 if field in self._fields: 878 # To match the C++ implementation, we need to invalidate iterators 879 # for map fields when ClearField() happens. 880 if hasattr(self._fields[field], 'InvalidateIterators'): 881 self._fields[field].InvalidateIterators() 882 883 # Note: If the field is a sub-message, its listener will still point 884 # at us. That's fine, because the worst than can happen is that it 885 # will call _Modified() and invalidate our byte size. Big deal. 886 del self._fields[field] 887 888 if self._oneofs.get(field.containing_oneof, None) is field: 889 del self._oneofs[field.containing_oneof] 890 891 # Always call _Modified() -- even if nothing was changed, this is 892 # a mutating method, and thus calling it should cause the field to become 893 # present in the parent message. 894 self._Modified()
Clears the contents of a given field.
Inside a oneof group, clears the field set. If the name neither refers to a
defined field or oneof group, ValueError
is raised.
Arguments:
- field_name (str): The name of the field to check for presence.
Raises:
- ValueError: if the
field_name
is not a member of this message.
1358 def WhichOneof(self, oneof_name): 1359 """Returns the name of the currently set field inside a oneof, or None.""" 1360 try: 1361 field = message_descriptor.oneofs_by_name[oneof_name] 1362 except KeyError: 1363 raise ValueError( 1364 'Protocol message has no oneof "%s" field.' % oneof_name) 1365 1366 nested_field = self._oneofs.get(field, None) 1367 if nested_field is not None and self.HasField(nested_field.name): 1368 return nested_field.name 1369 else: 1370 return None
Returns the name of the currently set field inside a oneof, or None.
1388def _UnknownFields(self): 1389 warnings.warn( 1390 'message.UnknownFields() is deprecated. Please use the add one ' 1391 'feature unknown_fields.UnknownFieldSet(message) in ' 1392 'unknown_fields.py instead.' 1393 ) 1394 if self._unknown_field_set is None: # pylint: disable=protected-access 1395 # pylint: disable=protected-access 1396 self._unknown_field_set = containers.UnknownFieldSet() 1397 return self._unknown_field_set # pylint: disable=protected-access
Returns the UnknownFieldSet.
Returns:
UnknownFieldSet: The unknown fields stored in this message.
1400def _DiscardUnknownFields(self): 1401 self._unknown_fields = [] 1402 self._unknown_field_set = None # pylint: disable=protected-access 1403 for field, value in self.ListFields(): 1404 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 1405 if _IsMapField(field): 1406 if _IsMessageMapField(field): 1407 for key in value: 1408 value[key].DiscardUnknownFields() 1409 elif field.label == _FieldDescriptor.LABEL_REPEATED: 1410 for sub_message in value: 1411 sub_message.DiscardUnknownFields() 1412 else: 1413 value.DiscardUnknownFields()
Clears all fields in the UnknownFieldSet
.
This operation is recursive for nested message.
1040 def ByteSize(self): 1041 if not self._cached_byte_size_dirty: 1042 return self._cached_byte_size 1043 1044 size = 0 1045 descriptor = self.DESCRIPTOR 1046 if descriptor._is_map_entry: 1047 # Fields of map entry should always be serialized. 1048 key_field = descriptor.fields_by_name['key'] 1049 _MaybeAddEncoder(cls, key_field) 1050 size = key_field._sizer(self.key) 1051 value_field = descriptor.fields_by_name['value'] 1052 _MaybeAddEncoder(cls, value_field) 1053 size += value_field._sizer(self.value) 1054 else: 1055 for field_descriptor, field_value in self.ListFields(): 1056 _MaybeAddEncoder(cls, field_descriptor) 1057 size += field_descriptor._sizer(field_value) 1058 for tag_bytes, value_bytes in self._unknown_fields: 1059 size += len(tag_bytes) + len(value_bytes) 1060 1061 self._cached_byte_size = size 1062 self._cached_byte_size_dirty = False 1063 self._listener_for_children.dirty = False 1064 return size
Returns the serialized size of this message.
Recursively calls ByteSize() on all contained messages.
Returns:
int: The number of bytes required to serialize this message.
784 def RegisterExtension(field_descriptor): 785 field_descriptor.containing_type = cls.DESCRIPTOR 786 # TODO: Use cls.MESSAGE_FACTORY.pool when available. 787 # pylint: disable=protected-access 788 cls.DESCRIPTOR.file.pool._AddExtensionDescriptor(field_descriptor) 789 _AttachFieldHelpers(cls, field_descriptor)
642 def getter(self): 643 field_value = self._fields.get(field) 644 if field_value is None: 645 # Construct a new object to represent this field. 646 field_value = field._default_constructor(self) 647 648 # Atomically check if another thread has preempted us and, if not, swap 649 # in the new object we just created. If someone has preempted us, we 650 # take that object and discard ours. 651 # WARNING: We are relying on setdefault() being atomic. This is true 652 # in CPython but we haven't investigated others. This warning appears 653 # in several other locations in this file. 654 field_value = self._fields.setdefault(field, field_value) 655 return field_value
Getter for x_intervals.
642 def getter(self): 643 field_value = self._fields.get(field) 644 if field_value is None: 645 # Construct a new object to represent this field. 646 field_value = field._default_constructor(self) 647 648 # Atomically check if another thread has preempted us and, if not, swap 649 # in the new object we just created. If someone has preempted us, we 650 # take that object and discard ours. 651 # WARNING: We are relying on setdefault() being atomic. This is true 652 # in CPython but we haven't investigated others. This warning appears 653 # in several other locations in this file. 654 field_value = self._fields.setdefault(field, field_value) 655 return field_value
Getter for y_intervals.
1253 def FindInitializationErrors(self): 1254 """Finds required fields which are not initialized. 1255 1256 Returns: 1257 A list of strings. Each string is a path to an uninitialized field from 1258 the top-level message, e.g. "foo.bar[5].baz". 1259 """ 1260 1261 errors = [] # simplify things 1262 1263 for field in required_fields: 1264 if not self.HasField(field.name): 1265 errors.append(field.name) 1266 1267 for field, value in self.ListFields(): 1268 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 1269 if field.is_extension: 1270 name = '(%s)' % field.full_name 1271 else: 1272 name = field.name 1273 1274 if _IsMapField(field): 1275 if _IsMessageMapField(field): 1276 for key in value: 1277 element = value[key] 1278 prefix = '%s[%s].' % (name, key) 1279 sub_errors = element.FindInitializationErrors() 1280 errors += [prefix + error for error in sub_errors] 1281 else: 1282 # ScalarMaps can't have any initialization errors. 1283 pass 1284 elif field.label == _FieldDescriptor.LABEL_REPEATED: 1285 for i in range(len(value)): 1286 element = value[i] 1287 prefix = '%s[%d].' % (name, i) 1288 sub_errors = element.FindInitializationErrors() 1289 errors += [prefix + error for error in sub_errors] 1290 else: 1291 prefix = name + '.' 1292 sub_errors = value.FindInitializationErrors() 1293 errors += [prefix + error for error in sub_errors] 1294 1295 return errors
Finds required fields which are not initialized.
Returns:
A list of strings. Each string is a path to an uninitialized field from the top-level message, e.g. "foo.bar[5].baz".
Inherited Members
- google.protobuf.message.Message
- CopyFrom
- ParseFromString
- HasExtension
- ClearExtension
Abstract base class for protocol messages.
Protocol message classes are almost always generated by the protocol compiler. These generated types subclass Message and implement the methods shown below.
496 def init(self, **kwargs): 497 self._cached_byte_size = 0 498 self._cached_byte_size_dirty = len(kwargs) > 0 499 self._fields = {} 500 # Contains a mapping from oneof field descriptors to the descriptor 501 # of the currently set field in that oneof field. 502 self._oneofs = {} 503 504 # _unknown_fields is () when empty for efficiency, and will be turned into 505 # a list if fields are added. 506 self._unknown_fields = () 507 # _unknown_field_set is None when empty for efficiency, and will be 508 # turned into UnknownFieldSet struct if fields are added. 509 self._unknown_field_set = None # pylint: disable=protected-access 510 self._is_present_in_parent = False 511 self._listener = message_listener_mod.NullMessageListener() 512 self._listener_for_children = _Listener(self) 513 for field_name, field_value in kwargs.items(): 514 field = _GetFieldByName(message_descriptor, field_name) 515 if field is None: 516 raise TypeError('%s() got an unexpected keyword argument "%s"' % 517 (message_descriptor.name, field_name)) 518 if field_value is None: 519 # field=None is the same as no field at all. 520 continue 521 if field.label == _FieldDescriptor.LABEL_REPEATED: 522 copy = field._default_constructor(self) 523 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: # Composite 524 if _IsMapField(field): 525 if _IsMessageMapField(field): 526 for key in field_value: 527 copy[key].MergeFrom(field_value[key]) 528 else: 529 copy.update(field_value) 530 else: 531 for val in field_value: 532 if isinstance(val, dict): 533 copy.add(**val) 534 else: 535 copy.add().MergeFrom(val) 536 else: # Scalar 537 if field.cpp_type == _FieldDescriptor.CPPTYPE_ENUM: 538 field_value = [_GetIntegerEnumValue(field.enum_type, val) 539 for val in field_value] 540 copy.extend(field_value) 541 self._fields[field] = copy 542 elif field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 543 copy = field._default_constructor(self) 544 new_val = field_value 545 if isinstance(field_value, dict): 546 new_val = field.message_type._concrete_class(**field_value) 547 try: 548 copy.MergeFrom(new_val) 549 except TypeError: 550 _ReraiseTypeErrorWithFieldName(message_descriptor.name, field_name) 551 self._fields[field] = copy 552 else: 553 if field.cpp_type == _FieldDescriptor.CPPTYPE_ENUM: 554 field_value = _GetIntegerEnumValue(field.enum_type, field_value) 555 try: 556 setattr(self, field_name, field_value) 557 except TypeError: 558 _ReraiseTypeErrorWithFieldName(message_descriptor.name, field_name)
1312 def MergeFrom(self, msg): 1313 if not isinstance(msg, cls): 1314 raise TypeError( 1315 'Parameter to MergeFrom() must be instance of same class: ' 1316 'expected %s got %s.' % (_FullyQualifiedClassName(cls), 1317 _FullyQualifiedClassName(msg.__class__))) 1318 1319 assert msg is not self 1320 self._Modified() 1321 1322 fields = self._fields 1323 1324 for field, value in msg._fields.items(): 1325 if field.label == LABEL_REPEATED: 1326 field_value = fields.get(field) 1327 if field_value is None: 1328 # Construct a new object to represent this field. 1329 field_value = field._default_constructor(self) 1330 fields[field] = field_value 1331 field_value.MergeFrom(value) 1332 elif field.cpp_type == CPPTYPE_MESSAGE: 1333 if value._is_present_in_parent: 1334 field_value = fields.get(field) 1335 if field_value is None: 1336 # Construct a new object to represent this field. 1337 field_value = field._default_constructor(self) 1338 fields[field] = field_value 1339 field_value.MergeFrom(value) 1340 else: 1341 self._fields[field] = value 1342 if field.containing_oneof: 1343 self._UpdateOneofState(field) 1344 1345 if msg._unknown_fields: 1346 if not self._unknown_fields: 1347 self._unknown_fields = [] 1348 self._unknown_fields.extend(msg._unknown_fields) 1349 # pylint: disable=protected-access 1350 if self._unknown_field_set is None: 1351 self._unknown_field_set = containers.UnknownFieldSet() 1352 self._unknown_field_set._extend(msg._unknown_field_set)
Merges the contents of the specified message into current message.
This method merges the contents of the specified message into the current message. Singular fields that are set in the specified message overwrite the corresponding fields in the current message. Repeated fields are appended. Singular sub-messages and groups are recursively merged.
Arguments:
- other_msg (Message): A message to merge into the current message.
1375def _Clear(self): 1376 # Clear fields. 1377 self._fields = {} 1378 self._unknown_fields = () 1379 # pylint: disable=protected-access 1380 if self._unknown_field_set is not None: 1381 self._unknown_field_set._clear() 1382 self._unknown_field_set = None 1383 1384 self._oneofs = {} 1385 self._Modified()
Clears all data that was set in the message.
1452 def Modified(self): 1453 """Sets the _cached_byte_size_dirty bit to true, 1454 and propagates this to our listener iff this was a state change. 1455 """ 1456 1457 # Note: Some callers check _cached_byte_size_dirty before calling 1458 # _Modified() as an extra optimization. So, if this method is ever 1459 # changed such that it does stuff even when _cached_byte_size_dirty is 1460 # already true, the callers need to be updated. 1461 if not self._cached_byte_size_dirty: 1462 self._cached_byte_size_dirty = True 1463 self._listener_for_children.dirty = True 1464 self._is_present_in_parent = True 1465 self._listener.Modified()
Sets the _cached_byte_size_dirty bit to true, and propagates this to our listener iff this was a state change.
1213 def IsInitialized(self, errors=None): 1214 """Checks if all required fields of a message are set. 1215 1216 Args: 1217 errors: A list which, if provided, will be populated with the field 1218 paths of all missing required fields. 1219 1220 Returns: 1221 True iff the specified message has all required fields set. 1222 """ 1223 1224 # Performance is critical so we avoid HasField() and ListFields(). 1225 1226 for field in required_fields: 1227 if (field not in self._fields or 1228 (field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE and 1229 not self._fields[field]._is_present_in_parent)): 1230 if errors is not None: 1231 errors.extend(self.FindInitializationErrors()) 1232 return False 1233 1234 for field, value in list(self._fields.items()): # dict can change size! 1235 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 1236 if field.label == _FieldDescriptor.LABEL_REPEATED: 1237 if (field.message_type._is_map_entry): 1238 continue 1239 for element in value: 1240 if not element.IsInitialized(): 1241 if errors is not None: 1242 errors.extend(self.FindInitializationErrors()) 1243 return False 1244 elif value._is_present_in_parent and not value.IsInitialized(): 1245 if errors is not None: 1246 errors.extend(self.FindInitializationErrors()) 1247 return False 1248 1249 return True
Checks if all required fields of a message are set.
Arguments:
- errors: A list which, if provided, will be populated with the field paths of all missing required fields.
Returns:
True iff the specified message has all required fields set.
1119 def MergeFromString(self, serialized): 1120 serialized = memoryview(serialized) 1121 length = len(serialized) 1122 try: 1123 if self._InternalParse(serialized, 0, length) != length: 1124 # The only reason _InternalParse would return early is if it 1125 # encountered an end-group tag. 1126 raise message_mod.DecodeError('Unexpected end-group tag.') 1127 except (IndexError, TypeError): 1128 # Now ord(buf[p:p+1]) == ord('') gets TypeError. 1129 raise message_mod.DecodeError('Truncated message.') 1130 except struct.error as e: 1131 raise message_mod.DecodeError(e) 1132 return length # Return this for legacy reasons.
Merges serialized protocol buffer data into this message.
When we find a field in serialized
that is already present
in this message:
- If it's a "repeated" field, we append to the end of our list.
- Else, if it's a scalar, we overwrite our field.
- Else, (it's a nonrepeated composite), we recursively merge into the existing composite.
Arguments:
- serialized (bytes): Any object that allows us to call
memoryview(serialized)
to access a string of bytes using the buffer interface.
Returns:
int: The number of bytes read from
serialized
. For non-group messages, this will always belen(serialized)
, but for messages which are actually groups, this will generally be less thanlen(serialized)
, since we must stop when we reach anEND_GROUP
tag. Note that if we do stop because of anEND_GROUP
tag, the number of bytes returned does not include the bytes for theEND_GROUP
tag information.
Raises:
- DecodeError: if the input cannot be parsed.
1072 def SerializeToString(self, **kwargs): 1073 # Check if the message has all of its required fields set. 1074 if not self.IsInitialized(): 1075 raise message_mod.EncodeError( 1076 'Message %s is missing required fields: %s' % ( 1077 self.DESCRIPTOR.full_name, ','.join(self.FindInitializationErrors()))) 1078 return self.SerializePartialToString(**kwargs)
Serializes the protocol message to a binary string.
Keyword Args:
deterministic (bool): If true, requests deterministic serialization of the protobuf, with predictable ordering of map keys.
Returns:
A binary string representation of the message if all of the required fields in the message are set (i.e. the message is initialized).
Raises:
- EncodeError: if the message isn't initialized (see
IsInitialized()
).
1085 def SerializePartialToString(self, **kwargs): 1086 out = BytesIO() 1087 self._InternalSerialize(out.write, **kwargs) 1088 return out.getvalue()
Serializes the protocol message to a binary string.
This method is similar to SerializeToString but doesn't check if the message is initialized.
Keyword Args:
deterministic (bool): If true, requests deterministic serialization of the protobuf, with predictable ordering of map keys.
Returns:
bytes: A serialized representation of the partial message.
814 def ListFields(self): 815 all_fields = [item for item in self._fields.items() if _IsPresent(item)] 816 all_fields.sort(key = lambda item: item[0].number) 817 return all_fields
Returns a list of (FieldDescriptor, value) tuples for present fields.
A message field is non-empty if HasField() would return true. A singular primitive field is non-empty if HasField() would return true in proto2 or it is non zero in proto3. A repeated field is non-empty if it contains at least one element. The fields are ordered by field number.
Returns:
list[tuple(FieldDescriptor, value)]: field descriptors and values for all fields in the message which are not empty. The values vary by field type.
838 def HasField(self, field_name): 839 try: 840 field = hassable_fields[field_name] 841 except KeyError as exc: 842 raise ValueError('Protocol message %s has no non-repeated field "%s" ' 843 'nor has presence is not available for this field.' % ( 844 message_descriptor.full_name, field_name)) from exc 845 846 if isinstance(field, descriptor_mod.OneofDescriptor): 847 try: 848 return HasField(self, self._oneofs[field].name) 849 except KeyError: 850 return False 851 else: 852 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 853 value = self._fields.get(field) 854 return value is not None and value._is_present_in_parent 855 else: 856 return field in self._fields
Checks if a certain field is set for the message.
For a oneof group, checks if any field inside is set. Note that if the
field_name is not defined in the message descriptor, ValueError
will
be raised.
Arguments:
- field_name (str): The name of the field to check for presence.
Returns:
bool: Whether a value has been set for the named field.
Raises:
- ValueError: if the
field_name
is not a member of this message.
863 def ClearField(self, field_name): 864 try: 865 field = message_descriptor.fields_by_name[field_name] 866 except KeyError: 867 try: 868 field = message_descriptor.oneofs_by_name[field_name] 869 if field in self._oneofs: 870 field = self._oneofs[field] 871 else: 872 return 873 except KeyError: 874 raise ValueError('Protocol message %s has no "%s" field.' % 875 (message_descriptor.name, field_name)) 876 877 if field in self._fields: 878 # To match the C++ implementation, we need to invalidate iterators 879 # for map fields when ClearField() happens. 880 if hasattr(self._fields[field], 'InvalidateIterators'): 881 self._fields[field].InvalidateIterators() 882 883 # Note: If the field is a sub-message, its listener will still point 884 # at us. That's fine, because the worst than can happen is that it 885 # will call _Modified() and invalidate our byte size. Big deal. 886 del self._fields[field] 887 888 if self._oneofs.get(field.containing_oneof, None) is field: 889 del self._oneofs[field.containing_oneof] 890 891 # Always call _Modified() -- even if nothing was changed, this is 892 # a mutating method, and thus calling it should cause the field to become 893 # present in the parent message. 894 self._Modified()
Clears the contents of a given field.
Inside a oneof group, clears the field set. If the name neither refers to a
defined field or oneof group, ValueError
is raised.
Arguments:
- field_name (str): The name of the field to check for presence.
Raises:
- ValueError: if the
field_name
is not a member of this message.
1358 def WhichOneof(self, oneof_name): 1359 """Returns the name of the currently set field inside a oneof, or None.""" 1360 try: 1361 field = message_descriptor.oneofs_by_name[oneof_name] 1362 except KeyError: 1363 raise ValueError( 1364 'Protocol message has no oneof "%s" field.' % oneof_name) 1365 1366 nested_field = self._oneofs.get(field, None) 1367 if nested_field is not None and self.HasField(nested_field.name): 1368 return nested_field.name 1369 else: 1370 return None
Returns the name of the currently set field inside a oneof, or None.
1388def _UnknownFields(self): 1389 warnings.warn( 1390 'message.UnknownFields() is deprecated. Please use the add one ' 1391 'feature unknown_fields.UnknownFieldSet(message) in ' 1392 'unknown_fields.py instead.' 1393 ) 1394 if self._unknown_field_set is None: # pylint: disable=protected-access 1395 # pylint: disable=protected-access 1396 self._unknown_field_set = containers.UnknownFieldSet() 1397 return self._unknown_field_set # pylint: disable=protected-access
Returns the UnknownFieldSet.
Returns:
UnknownFieldSet: The unknown fields stored in this message.
1400def _DiscardUnknownFields(self): 1401 self._unknown_fields = [] 1402 self._unknown_field_set = None # pylint: disable=protected-access 1403 for field, value in self.ListFields(): 1404 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 1405 if _IsMapField(field): 1406 if _IsMessageMapField(field): 1407 for key in value: 1408 value[key].DiscardUnknownFields() 1409 elif field.label == _FieldDescriptor.LABEL_REPEATED: 1410 for sub_message in value: 1411 sub_message.DiscardUnknownFields() 1412 else: 1413 value.DiscardUnknownFields()
Clears all fields in the UnknownFieldSet
.
This operation is recursive for nested message.
1040 def ByteSize(self): 1041 if not self._cached_byte_size_dirty: 1042 return self._cached_byte_size 1043 1044 size = 0 1045 descriptor = self.DESCRIPTOR 1046 if descriptor._is_map_entry: 1047 # Fields of map entry should always be serialized. 1048 key_field = descriptor.fields_by_name['key'] 1049 _MaybeAddEncoder(cls, key_field) 1050 size = key_field._sizer(self.key) 1051 value_field = descriptor.fields_by_name['value'] 1052 _MaybeAddEncoder(cls, value_field) 1053 size += value_field._sizer(self.value) 1054 else: 1055 for field_descriptor, field_value in self.ListFields(): 1056 _MaybeAddEncoder(cls, field_descriptor) 1057 size += field_descriptor._sizer(field_value) 1058 for tag_bytes, value_bytes in self._unknown_fields: 1059 size += len(tag_bytes) + len(value_bytes) 1060 1061 self._cached_byte_size = size 1062 self._cached_byte_size_dirty = False 1063 self._listener_for_children.dirty = False 1064 return size
Returns the serialized size of this message.
Recursively calls ByteSize() on all contained messages.
Returns:
int: The number of bytes required to serialize this message.
784 def RegisterExtension(field_descriptor): 785 field_descriptor.containing_type = cls.DESCRIPTOR 786 # TODO: Use cls.MESSAGE_FACTORY.pool when available. 787 # pylint: disable=protected-access 788 cls.DESCRIPTOR.file.pool._AddExtensionDescriptor(field_descriptor) 789 _AttachFieldHelpers(cls, field_descriptor)
741 def getter(self): 742 field_value = self._fields.get(field) 743 if field_value is None: 744 # Construct a new object to represent this field. 745 field_value = field._default_constructor(self) 746 747 # Atomically check if another thread has preempted us and, if not, swap 748 # in the new object we just created. If someone has preempted us, we 749 # take that object and discard ours. 750 # WARNING: We are relying on setdefault() being atomic. This is true 751 # in CPython but we haven't investigated others. This warning appears 752 # in several other locations in this file. 753 field_value = self._fields.setdefault(field, field_value) 754 return field_value
Getter for capacity.
642 def getter(self): 643 field_value = self._fields.get(field) 644 if field_value is None: 645 # Construct a new object to represent this field. 646 field_value = field._default_constructor(self) 647 648 # Atomically check if another thread has preempted us and, if not, swap 649 # in the new object we just created. If someone has preempted us, we 650 # take that object and discard ours. 651 # WARNING: We are relying on setdefault() being atomic. This is true 652 # in CPython but we haven't investigated others. This warning appears 653 # in several other locations in this file. 654 field_value = self._fields.setdefault(field, field_value) 655 return field_value
Getter for intervals.
642 def getter(self): 643 field_value = self._fields.get(field) 644 if field_value is None: 645 # Construct a new object to represent this field. 646 field_value = field._default_constructor(self) 647 648 # Atomically check if another thread has preempted us and, if not, swap 649 # in the new object we just created. If someone has preempted us, we 650 # take that object and discard ours. 651 # WARNING: We are relying on setdefault() being atomic. This is true 652 # in CPython but we haven't investigated others. This warning appears 653 # in several other locations in this file. 654 field_value = self._fields.setdefault(field, field_value) 655 return field_value
Getter for demands.
1253 def FindInitializationErrors(self): 1254 """Finds required fields which are not initialized. 1255 1256 Returns: 1257 A list of strings. Each string is a path to an uninitialized field from 1258 the top-level message, e.g. "foo.bar[5].baz". 1259 """ 1260 1261 errors = [] # simplify things 1262 1263 for field in required_fields: 1264 if not self.HasField(field.name): 1265 errors.append(field.name) 1266 1267 for field, value in self.ListFields(): 1268 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 1269 if field.is_extension: 1270 name = '(%s)' % field.full_name 1271 else: 1272 name = field.name 1273 1274 if _IsMapField(field): 1275 if _IsMessageMapField(field): 1276 for key in value: 1277 element = value[key] 1278 prefix = '%s[%s].' % (name, key) 1279 sub_errors = element.FindInitializationErrors() 1280 errors += [prefix + error for error in sub_errors] 1281 else: 1282 # ScalarMaps can't have any initialization errors. 1283 pass 1284 elif field.label == _FieldDescriptor.LABEL_REPEATED: 1285 for i in range(len(value)): 1286 element = value[i] 1287 prefix = '%s[%d].' % (name, i) 1288 sub_errors = element.FindInitializationErrors() 1289 errors += [prefix + error for error in sub_errors] 1290 else: 1291 prefix = name + '.' 1292 sub_errors = value.FindInitializationErrors() 1293 errors += [prefix + error for error in sub_errors] 1294 1295 return errors
Finds required fields which are not initialized.
Returns:
A list of strings. Each string is a path to an uninitialized field from the top-level message, e.g. "foo.bar[5].baz".
Inherited Members
- google.protobuf.message.Message
- CopyFrom
- ParseFromString
- HasExtension
- ClearExtension
Abstract base class for protocol messages.
Protocol message classes are almost always generated by the protocol compiler. These generated types subclass Message and implement the methods shown below.
496 def init(self, **kwargs): 497 self._cached_byte_size = 0 498 self._cached_byte_size_dirty = len(kwargs) > 0 499 self._fields = {} 500 # Contains a mapping from oneof field descriptors to the descriptor 501 # of the currently set field in that oneof field. 502 self._oneofs = {} 503 504 # _unknown_fields is () when empty for efficiency, and will be turned into 505 # a list if fields are added. 506 self._unknown_fields = () 507 # _unknown_field_set is None when empty for efficiency, and will be 508 # turned into UnknownFieldSet struct if fields are added. 509 self._unknown_field_set = None # pylint: disable=protected-access 510 self._is_present_in_parent = False 511 self._listener = message_listener_mod.NullMessageListener() 512 self._listener_for_children = _Listener(self) 513 for field_name, field_value in kwargs.items(): 514 field = _GetFieldByName(message_descriptor, field_name) 515 if field is None: 516 raise TypeError('%s() got an unexpected keyword argument "%s"' % 517 (message_descriptor.name, field_name)) 518 if field_value is None: 519 # field=None is the same as no field at all. 520 continue 521 if field.label == _FieldDescriptor.LABEL_REPEATED: 522 copy = field._default_constructor(self) 523 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: # Composite 524 if _IsMapField(field): 525 if _IsMessageMapField(field): 526 for key in field_value: 527 copy[key].MergeFrom(field_value[key]) 528 else: 529 copy.update(field_value) 530 else: 531 for val in field_value: 532 if isinstance(val, dict): 533 copy.add(**val) 534 else: 535 copy.add().MergeFrom(val) 536 else: # Scalar 537 if field.cpp_type == _FieldDescriptor.CPPTYPE_ENUM: 538 field_value = [_GetIntegerEnumValue(field.enum_type, val) 539 for val in field_value] 540 copy.extend(field_value) 541 self._fields[field] = copy 542 elif field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 543 copy = field._default_constructor(self) 544 new_val = field_value 545 if isinstance(field_value, dict): 546 new_val = field.message_type._concrete_class(**field_value) 547 try: 548 copy.MergeFrom(new_val) 549 except TypeError: 550 _ReraiseTypeErrorWithFieldName(message_descriptor.name, field_name) 551 self._fields[field] = copy 552 else: 553 if field.cpp_type == _FieldDescriptor.CPPTYPE_ENUM: 554 field_value = _GetIntegerEnumValue(field.enum_type, field_value) 555 try: 556 setattr(self, field_name, field_value) 557 except TypeError: 558 _ReraiseTypeErrorWithFieldName(message_descriptor.name, field_name)
1312 def MergeFrom(self, msg): 1313 if not isinstance(msg, cls): 1314 raise TypeError( 1315 'Parameter to MergeFrom() must be instance of same class: ' 1316 'expected %s got %s.' % (_FullyQualifiedClassName(cls), 1317 _FullyQualifiedClassName(msg.__class__))) 1318 1319 assert msg is not self 1320 self._Modified() 1321 1322 fields = self._fields 1323 1324 for field, value in msg._fields.items(): 1325 if field.label == LABEL_REPEATED: 1326 field_value = fields.get(field) 1327 if field_value is None: 1328 # Construct a new object to represent this field. 1329 field_value = field._default_constructor(self) 1330 fields[field] = field_value 1331 field_value.MergeFrom(value) 1332 elif field.cpp_type == CPPTYPE_MESSAGE: 1333 if value._is_present_in_parent: 1334 field_value = fields.get(field) 1335 if field_value is None: 1336 # Construct a new object to represent this field. 1337 field_value = field._default_constructor(self) 1338 fields[field] = field_value 1339 field_value.MergeFrom(value) 1340 else: 1341 self._fields[field] = value 1342 if field.containing_oneof: 1343 self._UpdateOneofState(field) 1344 1345 if msg._unknown_fields: 1346 if not self._unknown_fields: 1347 self._unknown_fields = [] 1348 self._unknown_fields.extend(msg._unknown_fields) 1349 # pylint: disable=protected-access 1350 if self._unknown_field_set is None: 1351 self._unknown_field_set = containers.UnknownFieldSet() 1352 self._unknown_field_set._extend(msg._unknown_field_set)
Merges the contents of the specified message into current message.
This method merges the contents of the specified message into the current message. Singular fields that are set in the specified message overwrite the corresponding fields in the current message. Repeated fields are appended. Singular sub-messages and groups are recursively merged.
Arguments:
- other_msg (Message): A message to merge into the current message.
1375def _Clear(self): 1376 # Clear fields. 1377 self._fields = {} 1378 self._unknown_fields = () 1379 # pylint: disable=protected-access 1380 if self._unknown_field_set is not None: 1381 self._unknown_field_set._clear() 1382 self._unknown_field_set = None 1383 1384 self._oneofs = {} 1385 self._Modified()
Clears all data that was set in the message.
1452 def Modified(self): 1453 """Sets the _cached_byte_size_dirty bit to true, 1454 and propagates this to our listener iff this was a state change. 1455 """ 1456 1457 # Note: Some callers check _cached_byte_size_dirty before calling 1458 # _Modified() as an extra optimization. So, if this method is ever 1459 # changed such that it does stuff even when _cached_byte_size_dirty is 1460 # already true, the callers need to be updated. 1461 if not self._cached_byte_size_dirty: 1462 self._cached_byte_size_dirty = True 1463 self._listener_for_children.dirty = True 1464 self._is_present_in_parent = True 1465 self._listener.Modified()
Sets the _cached_byte_size_dirty bit to true, and propagates this to our listener iff this was a state change.
1213 def IsInitialized(self, errors=None): 1214 """Checks if all required fields of a message are set. 1215 1216 Args: 1217 errors: A list which, if provided, will be populated with the field 1218 paths of all missing required fields. 1219 1220 Returns: 1221 True iff the specified message has all required fields set. 1222 """ 1223 1224 # Performance is critical so we avoid HasField() and ListFields(). 1225 1226 for field in required_fields: 1227 if (field not in self._fields or 1228 (field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE and 1229 not self._fields[field]._is_present_in_parent)): 1230 if errors is not None: 1231 errors.extend(self.FindInitializationErrors()) 1232 return False 1233 1234 for field, value in list(self._fields.items()): # dict can change size! 1235 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 1236 if field.label == _FieldDescriptor.LABEL_REPEATED: 1237 if (field.message_type._is_map_entry): 1238 continue 1239 for element in value: 1240 if not element.IsInitialized(): 1241 if errors is not None: 1242 errors.extend(self.FindInitializationErrors()) 1243 return False 1244 elif value._is_present_in_parent and not value.IsInitialized(): 1245 if errors is not None: 1246 errors.extend(self.FindInitializationErrors()) 1247 return False 1248 1249 return True
Checks if all required fields of a message are set.
Arguments:
- errors: A list which, if provided, will be populated with the field paths of all missing required fields.
Returns:
True iff the specified message has all required fields set.
1119 def MergeFromString(self, serialized): 1120 serialized = memoryview(serialized) 1121 length = len(serialized) 1122 try: 1123 if self._InternalParse(serialized, 0, length) != length: 1124 # The only reason _InternalParse would return early is if it 1125 # encountered an end-group tag. 1126 raise message_mod.DecodeError('Unexpected end-group tag.') 1127 except (IndexError, TypeError): 1128 # Now ord(buf[p:p+1]) == ord('') gets TypeError. 1129 raise message_mod.DecodeError('Truncated message.') 1130 except struct.error as e: 1131 raise message_mod.DecodeError(e) 1132 return length # Return this for legacy reasons.
Merges serialized protocol buffer data into this message.
When we find a field in serialized
that is already present
in this message:
- If it's a "repeated" field, we append to the end of our list.
- Else, if it's a scalar, we overwrite our field.
- Else, (it's a nonrepeated composite), we recursively merge into the existing composite.
Arguments:
- serialized (bytes): Any object that allows us to call
memoryview(serialized)
to access a string of bytes using the buffer interface.
Returns:
int: The number of bytes read from
serialized
. For non-group messages, this will always belen(serialized)
, but for messages which are actually groups, this will generally be less thanlen(serialized)
, since we must stop when we reach anEND_GROUP
tag. Note that if we do stop because of anEND_GROUP
tag, the number of bytes returned does not include the bytes for theEND_GROUP
tag information.
Raises:
- DecodeError: if the input cannot be parsed.
1072 def SerializeToString(self, **kwargs): 1073 # Check if the message has all of its required fields set. 1074 if not self.IsInitialized(): 1075 raise message_mod.EncodeError( 1076 'Message %s is missing required fields: %s' % ( 1077 self.DESCRIPTOR.full_name, ','.join(self.FindInitializationErrors()))) 1078 return self.SerializePartialToString(**kwargs)
Serializes the protocol message to a binary string.
Keyword Args:
deterministic (bool): If true, requests deterministic serialization of the protobuf, with predictable ordering of map keys.
Returns:
A binary string representation of the message if all of the required fields in the message are set (i.e. the message is initialized).
Raises:
- EncodeError: if the message isn't initialized (see
IsInitialized()
).
1085 def SerializePartialToString(self, **kwargs): 1086 out = BytesIO() 1087 self._InternalSerialize(out.write, **kwargs) 1088 return out.getvalue()
Serializes the protocol message to a binary string.
This method is similar to SerializeToString but doesn't check if the message is initialized.
Keyword Args:
deterministic (bool): If true, requests deterministic serialization of the protobuf, with predictable ordering of map keys.
Returns:
bytes: A serialized representation of the partial message.
814 def ListFields(self): 815 all_fields = [item for item in self._fields.items() if _IsPresent(item)] 816 all_fields.sort(key = lambda item: item[0].number) 817 return all_fields
Returns a list of (FieldDescriptor, value) tuples for present fields.
A message field is non-empty if HasField() would return true. A singular primitive field is non-empty if HasField() would return true in proto2 or it is non zero in proto3. A repeated field is non-empty if it contains at least one element. The fields are ordered by field number.
Returns:
list[tuple(FieldDescriptor, value)]: field descriptors and values for all fields in the message which are not empty. The values vary by field type.
838 def HasField(self, field_name): 839 try: 840 field = hassable_fields[field_name] 841 except KeyError as exc: 842 raise ValueError('Protocol message %s has no non-repeated field "%s" ' 843 'nor has presence is not available for this field.' % ( 844 message_descriptor.full_name, field_name)) from exc 845 846 if isinstance(field, descriptor_mod.OneofDescriptor): 847 try: 848 return HasField(self, self._oneofs[field].name) 849 except KeyError: 850 return False 851 else: 852 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 853 value = self._fields.get(field) 854 return value is not None and value._is_present_in_parent 855 else: 856 return field in self._fields
Checks if a certain field is set for the message.
For a oneof group, checks if any field inside is set. Note that if the
field_name is not defined in the message descriptor, ValueError
will
be raised.
Arguments:
- field_name (str): The name of the field to check for presence.
Returns:
bool: Whether a value has been set for the named field.
Raises:
- ValueError: if the
field_name
is not a member of this message.
863 def ClearField(self, field_name): 864 try: 865 field = message_descriptor.fields_by_name[field_name] 866 except KeyError: 867 try: 868 field = message_descriptor.oneofs_by_name[field_name] 869 if field in self._oneofs: 870 field = self._oneofs[field] 871 else: 872 return 873 except KeyError: 874 raise ValueError('Protocol message %s has no "%s" field.' % 875 (message_descriptor.name, field_name)) 876 877 if field in self._fields: 878 # To match the C++ implementation, we need to invalidate iterators 879 # for map fields when ClearField() happens. 880 if hasattr(self._fields[field], 'InvalidateIterators'): 881 self._fields[field].InvalidateIterators() 882 883 # Note: If the field is a sub-message, its listener will still point 884 # at us. That's fine, because the worst than can happen is that it 885 # will call _Modified() and invalidate our byte size. Big deal. 886 del self._fields[field] 887 888 if self._oneofs.get(field.containing_oneof, None) is field: 889 del self._oneofs[field.containing_oneof] 890 891 # Always call _Modified() -- even if nothing was changed, this is 892 # a mutating method, and thus calling it should cause the field to become 893 # present in the parent message. 894 self._Modified()
Clears the contents of a given field.
Inside a oneof group, clears the field set. If the name neither refers to a
defined field or oneof group, ValueError
is raised.
Arguments:
- field_name (str): The name of the field to check for presence.
Raises:
- ValueError: if the
field_name
is not a member of this message.
1358 def WhichOneof(self, oneof_name): 1359 """Returns the name of the currently set field inside a oneof, or None.""" 1360 try: 1361 field = message_descriptor.oneofs_by_name[oneof_name] 1362 except KeyError: 1363 raise ValueError( 1364 'Protocol message has no oneof "%s" field.' % oneof_name) 1365 1366 nested_field = self._oneofs.get(field, None) 1367 if nested_field is not None and self.HasField(nested_field.name): 1368 return nested_field.name 1369 else: 1370 return None
Returns the name of the currently set field inside a oneof, or None.
1388def _UnknownFields(self): 1389 warnings.warn( 1390 'message.UnknownFields() is deprecated. Please use the add one ' 1391 'feature unknown_fields.UnknownFieldSet(message) in ' 1392 'unknown_fields.py instead.' 1393 ) 1394 if self._unknown_field_set is None: # pylint: disable=protected-access 1395 # pylint: disable=protected-access 1396 self._unknown_field_set = containers.UnknownFieldSet() 1397 return self._unknown_field_set # pylint: disable=protected-access
Returns the UnknownFieldSet.
Returns:
UnknownFieldSet: The unknown fields stored in this message.
1400def _DiscardUnknownFields(self): 1401 self._unknown_fields = [] 1402 self._unknown_field_set = None # pylint: disable=protected-access 1403 for field, value in self.ListFields(): 1404 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 1405 if _IsMapField(field): 1406 if _IsMessageMapField(field): 1407 for key in value: 1408 value[key].DiscardUnknownFields() 1409 elif field.label == _FieldDescriptor.LABEL_REPEATED: 1410 for sub_message in value: 1411 sub_message.DiscardUnknownFields() 1412 else: 1413 value.DiscardUnknownFields()
Clears all fields in the UnknownFieldSet
.
This operation is recursive for nested message.
1040 def ByteSize(self): 1041 if not self._cached_byte_size_dirty: 1042 return self._cached_byte_size 1043 1044 size = 0 1045 descriptor = self.DESCRIPTOR 1046 if descriptor._is_map_entry: 1047 # Fields of map entry should always be serialized. 1048 key_field = descriptor.fields_by_name['key'] 1049 _MaybeAddEncoder(cls, key_field) 1050 size = key_field._sizer(self.key) 1051 value_field = descriptor.fields_by_name['value'] 1052 _MaybeAddEncoder(cls, value_field) 1053 size += value_field._sizer(self.value) 1054 else: 1055 for field_descriptor, field_value in self.ListFields(): 1056 _MaybeAddEncoder(cls, field_descriptor) 1057 size += field_descriptor._sizer(field_value) 1058 for tag_bytes, value_bytes in self._unknown_fields: 1059 size += len(tag_bytes) + len(value_bytes) 1060 1061 self._cached_byte_size = size 1062 self._cached_byte_size_dirty = False 1063 self._listener_for_children.dirty = False 1064 return size
Returns the serialized size of this message.
Recursively calls ByteSize() on all contained messages.
Returns:
int: The number of bytes required to serialize this message.
784 def RegisterExtension(field_descriptor): 785 field_descriptor.containing_type = cls.DESCRIPTOR 786 # TODO: Use cls.MESSAGE_FACTORY.pool when available. 787 # pylint: disable=protected-access 788 cls.DESCRIPTOR.file.pool._AddExtensionDescriptor(field_descriptor) 789 _AttachFieldHelpers(cls, field_descriptor)
685 def getter(self): 686 # TODO: This may be broken since there may not be 687 # default_value. Combine with has_default_value somehow. 688 return self._fields.get(field, default_value)
Getter for min_level.
685 def getter(self): 686 # TODO: This may be broken since there may not be 687 # default_value. Combine with has_default_value somehow. 688 return self._fields.get(field, default_value)
Getter for max_level.
642 def getter(self): 643 field_value = self._fields.get(field) 644 if field_value is None: 645 # Construct a new object to represent this field. 646 field_value = field._default_constructor(self) 647 648 # Atomically check if another thread has preempted us and, if not, swap 649 # in the new object we just created. If someone has preempted us, we 650 # take that object and discard ours. 651 # WARNING: We are relying on setdefault() being atomic. This is true 652 # in CPython but we haven't investigated others. This warning appears 653 # in several other locations in this file. 654 field_value = self._fields.setdefault(field, field_value) 655 return field_value
Getter for time_exprs.
642 def getter(self): 643 field_value = self._fields.get(field) 644 if field_value is None: 645 # Construct a new object to represent this field. 646 field_value = field._default_constructor(self) 647 648 # Atomically check if another thread has preempted us and, if not, swap 649 # in the new object we just created. If someone has preempted us, we 650 # take that object and discard ours. 651 # WARNING: We are relying on setdefault() being atomic. This is true 652 # in CPython but we haven't investigated others. This warning appears 653 # in several other locations in this file. 654 field_value = self._fields.setdefault(field, field_value) 655 return field_value
Getter for level_changes.
642 def getter(self): 643 field_value = self._fields.get(field) 644 if field_value is None: 645 # Construct a new object to represent this field. 646 field_value = field._default_constructor(self) 647 648 # Atomically check if another thread has preempted us and, if not, swap 649 # in the new object we just created. If someone has preempted us, we 650 # take that object and discard ours. 651 # WARNING: We are relying on setdefault() being atomic. This is true 652 # in CPython but we haven't investigated others. This warning appears 653 # in several other locations in this file. 654 field_value = self._fields.setdefault(field, field_value) 655 return field_value
Getter for active_literals.
1253 def FindInitializationErrors(self): 1254 """Finds required fields which are not initialized. 1255 1256 Returns: 1257 A list of strings. Each string is a path to an uninitialized field from 1258 the top-level message, e.g. "foo.bar[5].baz". 1259 """ 1260 1261 errors = [] # simplify things 1262 1263 for field in required_fields: 1264 if not self.HasField(field.name): 1265 errors.append(field.name) 1266 1267 for field, value in self.ListFields(): 1268 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 1269 if field.is_extension: 1270 name = '(%s)' % field.full_name 1271 else: 1272 name = field.name 1273 1274 if _IsMapField(field): 1275 if _IsMessageMapField(field): 1276 for key in value: 1277 element = value[key] 1278 prefix = '%s[%s].' % (name, key) 1279 sub_errors = element.FindInitializationErrors() 1280 errors += [prefix + error for error in sub_errors] 1281 else: 1282 # ScalarMaps can't have any initialization errors. 1283 pass 1284 elif field.label == _FieldDescriptor.LABEL_REPEATED: 1285 for i in range(len(value)): 1286 element = value[i] 1287 prefix = '%s[%d].' % (name, i) 1288 sub_errors = element.FindInitializationErrors() 1289 errors += [prefix + error for error in sub_errors] 1290 else: 1291 prefix = name + '.' 1292 sub_errors = value.FindInitializationErrors() 1293 errors += [prefix + error for error in sub_errors] 1294 1295 return errors
Finds required fields which are not initialized.
Returns:
A list of strings. Each string is a path to an uninitialized field from the top-level message, e.g. "foo.bar[5].baz".
Inherited Members
- google.protobuf.message.Message
- CopyFrom
- ParseFromString
- HasExtension
- ClearExtension
Abstract base class for protocol messages.
Protocol message classes are almost always generated by the protocol compiler. These generated types subclass Message and implement the methods shown below.
496 def init(self, **kwargs): 497 self._cached_byte_size = 0 498 self._cached_byte_size_dirty = len(kwargs) > 0 499 self._fields = {} 500 # Contains a mapping from oneof field descriptors to the descriptor 501 # of the currently set field in that oneof field. 502 self._oneofs = {} 503 504 # _unknown_fields is () when empty for efficiency, and will be turned into 505 # a list if fields are added. 506 self._unknown_fields = () 507 # _unknown_field_set is None when empty for efficiency, and will be 508 # turned into UnknownFieldSet struct if fields are added. 509 self._unknown_field_set = None # pylint: disable=protected-access 510 self._is_present_in_parent = False 511 self._listener = message_listener_mod.NullMessageListener() 512 self._listener_for_children = _Listener(self) 513 for field_name, field_value in kwargs.items(): 514 field = _GetFieldByName(message_descriptor, field_name) 515 if field is None: 516 raise TypeError('%s() got an unexpected keyword argument "%s"' % 517 (message_descriptor.name, field_name)) 518 if field_value is None: 519 # field=None is the same as no field at all. 520 continue 521 if field.label == _FieldDescriptor.LABEL_REPEATED: 522 copy = field._default_constructor(self) 523 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: # Composite 524 if _IsMapField(field): 525 if _IsMessageMapField(field): 526 for key in field_value: 527 copy[key].MergeFrom(field_value[key]) 528 else: 529 copy.update(field_value) 530 else: 531 for val in field_value: 532 if isinstance(val, dict): 533 copy.add(**val) 534 else: 535 copy.add().MergeFrom(val) 536 else: # Scalar 537 if field.cpp_type == _FieldDescriptor.CPPTYPE_ENUM: 538 field_value = [_GetIntegerEnumValue(field.enum_type, val) 539 for val in field_value] 540 copy.extend(field_value) 541 self._fields[field] = copy 542 elif field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 543 copy = field._default_constructor(self) 544 new_val = field_value 545 if isinstance(field_value, dict): 546 new_val = field.message_type._concrete_class(**field_value) 547 try: 548 copy.MergeFrom(new_val) 549 except TypeError: 550 _ReraiseTypeErrorWithFieldName(message_descriptor.name, field_name) 551 self._fields[field] = copy 552 else: 553 if field.cpp_type == _FieldDescriptor.CPPTYPE_ENUM: 554 field_value = _GetIntegerEnumValue(field.enum_type, field_value) 555 try: 556 setattr(self, field_name, field_value) 557 except TypeError: 558 _ReraiseTypeErrorWithFieldName(message_descriptor.name, field_name)
1312 def MergeFrom(self, msg): 1313 if not isinstance(msg, cls): 1314 raise TypeError( 1315 'Parameter to MergeFrom() must be instance of same class: ' 1316 'expected %s got %s.' % (_FullyQualifiedClassName(cls), 1317 _FullyQualifiedClassName(msg.__class__))) 1318 1319 assert msg is not self 1320 self._Modified() 1321 1322 fields = self._fields 1323 1324 for field, value in msg._fields.items(): 1325 if field.label == LABEL_REPEATED: 1326 field_value = fields.get(field) 1327 if field_value is None: 1328 # Construct a new object to represent this field. 1329 field_value = field._default_constructor(self) 1330 fields[field] = field_value 1331 field_value.MergeFrom(value) 1332 elif field.cpp_type == CPPTYPE_MESSAGE: 1333 if value._is_present_in_parent: 1334 field_value = fields.get(field) 1335 if field_value is None: 1336 # Construct a new object to represent this field. 1337 field_value = field._default_constructor(self) 1338 fields[field] = field_value 1339 field_value.MergeFrom(value) 1340 else: 1341 self._fields[field] = value 1342 if field.containing_oneof: 1343 self._UpdateOneofState(field) 1344 1345 if msg._unknown_fields: 1346 if not self._unknown_fields: 1347 self._unknown_fields = [] 1348 self._unknown_fields.extend(msg._unknown_fields) 1349 # pylint: disable=protected-access 1350 if self._unknown_field_set is None: 1351 self._unknown_field_set = containers.UnknownFieldSet() 1352 self._unknown_field_set._extend(msg._unknown_field_set)
Merges the contents of the specified message into current message.
This method merges the contents of the specified message into the current message. Singular fields that are set in the specified message overwrite the corresponding fields in the current message. Repeated fields are appended. Singular sub-messages and groups are recursively merged.
Arguments:
- other_msg (Message): A message to merge into the current message.
1375def _Clear(self): 1376 # Clear fields. 1377 self._fields = {} 1378 self._unknown_fields = () 1379 # pylint: disable=protected-access 1380 if self._unknown_field_set is not None: 1381 self._unknown_field_set._clear() 1382 self._unknown_field_set = None 1383 1384 self._oneofs = {} 1385 self._Modified()
Clears all data that was set in the message.
1452 def Modified(self): 1453 """Sets the _cached_byte_size_dirty bit to true, 1454 and propagates this to our listener iff this was a state change. 1455 """ 1456 1457 # Note: Some callers check _cached_byte_size_dirty before calling 1458 # _Modified() as an extra optimization. So, if this method is ever 1459 # changed such that it does stuff even when _cached_byte_size_dirty is 1460 # already true, the callers need to be updated. 1461 if not self._cached_byte_size_dirty: 1462 self._cached_byte_size_dirty = True 1463 self._listener_for_children.dirty = True 1464 self._is_present_in_parent = True 1465 self._listener.Modified()
Sets the _cached_byte_size_dirty bit to true, and propagates this to our listener iff this was a state change.
1213 def IsInitialized(self, errors=None): 1214 """Checks if all required fields of a message are set. 1215 1216 Args: 1217 errors: A list which, if provided, will be populated with the field 1218 paths of all missing required fields. 1219 1220 Returns: 1221 True iff the specified message has all required fields set. 1222 """ 1223 1224 # Performance is critical so we avoid HasField() and ListFields(). 1225 1226 for field in required_fields: 1227 if (field not in self._fields or 1228 (field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE and 1229 not self._fields[field]._is_present_in_parent)): 1230 if errors is not None: 1231 errors.extend(self.FindInitializationErrors()) 1232 return False 1233 1234 for field, value in list(self._fields.items()): # dict can change size! 1235 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 1236 if field.label == _FieldDescriptor.LABEL_REPEATED: 1237 if (field.message_type._is_map_entry): 1238 continue 1239 for element in value: 1240 if not element.IsInitialized(): 1241 if errors is not None: 1242 errors.extend(self.FindInitializationErrors()) 1243 return False 1244 elif value._is_present_in_parent and not value.IsInitialized(): 1245 if errors is not None: 1246 errors.extend(self.FindInitializationErrors()) 1247 return False 1248 1249 return True
Checks if all required fields of a message are set.
Arguments:
- errors: A list which, if provided, will be populated with the field paths of all missing required fields.
Returns:
True iff the specified message has all required fields set.
1119 def MergeFromString(self, serialized): 1120 serialized = memoryview(serialized) 1121 length = len(serialized) 1122 try: 1123 if self._InternalParse(serialized, 0, length) != length: 1124 # The only reason _InternalParse would return early is if it 1125 # encountered an end-group tag. 1126 raise message_mod.DecodeError('Unexpected end-group tag.') 1127 except (IndexError, TypeError): 1128 # Now ord(buf[p:p+1]) == ord('') gets TypeError. 1129 raise message_mod.DecodeError('Truncated message.') 1130 except struct.error as e: 1131 raise message_mod.DecodeError(e) 1132 return length # Return this for legacy reasons.
Merges serialized protocol buffer data into this message.
When we find a field in serialized
that is already present
in this message:
- If it's a "repeated" field, we append to the end of our list.
- Else, if it's a scalar, we overwrite our field.
- Else, (it's a nonrepeated composite), we recursively merge into the existing composite.
Arguments:
- serialized (bytes): Any object that allows us to call
memoryview(serialized)
to access a string of bytes using the buffer interface.
Returns:
int: The number of bytes read from
serialized
. For non-group messages, this will always belen(serialized)
, but for messages which are actually groups, this will generally be less thanlen(serialized)
, since we must stop when we reach anEND_GROUP
tag. Note that if we do stop because of anEND_GROUP
tag, the number of bytes returned does not include the bytes for theEND_GROUP
tag information.
Raises:
- DecodeError: if the input cannot be parsed.
1072 def SerializeToString(self, **kwargs): 1073 # Check if the message has all of its required fields set. 1074 if not self.IsInitialized(): 1075 raise message_mod.EncodeError( 1076 'Message %s is missing required fields: %s' % ( 1077 self.DESCRIPTOR.full_name, ','.join(self.FindInitializationErrors()))) 1078 return self.SerializePartialToString(**kwargs)
Serializes the protocol message to a binary string.
Keyword Args:
deterministic (bool): If true, requests deterministic serialization of the protobuf, with predictable ordering of map keys.
Returns:
A binary string representation of the message if all of the required fields in the message are set (i.e. the message is initialized).
Raises:
- EncodeError: if the message isn't initialized (see
IsInitialized()
).
1085 def SerializePartialToString(self, **kwargs): 1086 out = BytesIO() 1087 self._InternalSerialize(out.write, **kwargs) 1088 return out.getvalue()
Serializes the protocol message to a binary string.
This method is similar to SerializeToString but doesn't check if the message is initialized.
Keyword Args:
deterministic (bool): If true, requests deterministic serialization of the protobuf, with predictable ordering of map keys.
Returns:
bytes: A serialized representation of the partial message.
814 def ListFields(self): 815 all_fields = [item for item in self._fields.items() if _IsPresent(item)] 816 all_fields.sort(key = lambda item: item[0].number) 817 return all_fields
Returns a list of (FieldDescriptor, value) tuples for present fields.
A message field is non-empty if HasField() would return true. A singular primitive field is non-empty if HasField() would return true in proto2 or it is non zero in proto3. A repeated field is non-empty if it contains at least one element. The fields are ordered by field number.
Returns:
list[tuple(FieldDescriptor, value)]: field descriptors and values for all fields in the message which are not empty. The values vary by field type.
838 def HasField(self, field_name): 839 try: 840 field = hassable_fields[field_name] 841 except KeyError as exc: 842 raise ValueError('Protocol message %s has no non-repeated field "%s" ' 843 'nor has presence is not available for this field.' % ( 844 message_descriptor.full_name, field_name)) from exc 845 846 if isinstance(field, descriptor_mod.OneofDescriptor): 847 try: 848 return HasField(self, self._oneofs[field].name) 849 except KeyError: 850 return False 851 else: 852 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 853 value = self._fields.get(field) 854 return value is not None and value._is_present_in_parent 855 else: 856 return field in self._fields
Checks if a certain field is set for the message.
For a oneof group, checks if any field inside is set. Note that if the
field_name is not defined in the message descriptor, ValueError
will
be raised.
Arguments:
- field_name (str): The name of the field to check for presence.
Returns:
bool: Whether a value has been set for the named field.
Raises:
- ValueError: if the
field_name
is not a member of this message.
863 def ClearField(self, field_name): 864 try: 865 field = message_descriptor.fields_by_name[field_name] 866 except KeyError: 867 try: 868 field = message_descriptor.oneofs_by_name[field_name] 869 if field in self._oneofs: 870 field = self._oneofs[field] 871 else: 872 return 873 except KeyError: 874 raise ValueError('Protocol message %s has no "%s" field.' % 875 (message_descriptor.name, field_name)) 876 877 if field in self._fields: 878 # To match the C++ implementation, we need to invalidate iterators 879 # for map fields when ClearField() happens. 880 if hasattr(self._fields[field], 'InvalidateIterators'): 881 self._fields[field].InvalidateIterators() 882 883 # Note: If the field is a sub-message, its listener will still point 884 # at us. That's fine, because the worst than can happen is that it 885 # will call _Modified() and invalidate our byte size. Big deal. 886 del self._fields[field] 887 888 if self._oneofs.get(field.containing_oneof, None) is field: 889 del self._oneofs[field.containing_oneof] 890 891 # Always call _Modified() -- even if nothing was changed, this is 892 # a mutating method, and thus calling it should cause the field to become 893 # present in the parent message. 894 self._Modified()
Clears the contents of a given field.
Inside a oneof group, clears the field set. If the name neither refers to a
defined field or oneof group, ValueError
is raised.
Arguments:
- field_name (str): The name of the field to check for presence.
Raises:
- ValueError: if the
field_name
is not a member of this message.
1358 def WhichOneof(self, oneof_name): 1359 """Returns the name of the currently set field inside a oneof, or None.""" 1360 try: 1361 field = message_descriptor.oneofs_by_name[oneof_name] 1362 except KeyError: 1363 raise ValueError( 1364 'Protocol message has no oneof "%s" field.' % oneof_name) 1365 1366 nested_field = self._oneofs.get(field, None) 1367 if nested_field is not None and self.HasField(nested_field.name): 1368 return nested_field.name 1369 else: 1370 return None
Returns the name of the currently set field inside a oneof, or None.
1388def _UnknownFields(self): 1389 warnings.warn( 1390 'message.UnknownFields() is deprecated. Please use the add one ' 1391 'feature unknown_fields.UnknownFieldSet(message) in ' 1392 'unknown_fields.py instead.' 1393 ) 1394 if self._unknown_field_set is None: # pylint: disable=protected-access 1395 # pylint: disable=protected-access 1396 self._unknown_field_set = containers.UnknownFieldSet() 1397 return self._unknown_field_set # pylint: disable=protected-access
Returns the UnknownFieldSet.
Returns:
UnknownFieldSet: The unknown fields stored in this message.
1400def _DiscardUnknownFields(self): 1401 self._unknown_fields = [] 1402 self._unknown_field_set = None # pylint: disable=protected-access 1403 for field, value in self.ListFields(): 1404 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 1405 if _IsMapField(field): 1406 if _IsMessageMapField(field): 1407 for key in value: 1408 value[key].DiscardUnknownFields() 1409 elif field.label == _FieldDescriptor.LABEL_REPEATED: 1410 for sub_message in value: 1411 sub_message.DiscardUnknownFields() 1412 else: 1413 value.DiscardUnknownFields()
Clears all fields in the UnknownFieldSet
.
This operation is recursive for nested message.
1040 def ByteSize(self): 1041 if not self._cached_byte_size_dirty: 1042 return self._cached_byte_size 1043 1044 size = 0 1045 descriptor = self.DESCRIPTOR 1046 if descriptor._is_map_entry: 1047 # Fields of map entry should always be serialized. 1048 key_field = descriptor.fields_by_name['key'] 1049 _MaybeAddEncoder(cls, key_field) 1050 size = key_field._sizer(self.key) 1051 value_field = descriptor.fields_by_name['value'] 1052 _MaybeAddEncoder(cls, value_field) 1053 size += value_field._sizer(self.value) 1054 else: 1055 for field_descriptor, field_value in self.ListFields(): 1056 _MaybeAddEncoder(cls, field_descriptor) 1057 size += field_descriptor._sizer(field_value) 1058 for tag_bytes, value_bytes in self._unknown_fields: 1059 size += len(tag_bytes) + len(value_bytes) 1060 1061 self._cached_byte_size = size 1062 self._cached_byte_size_dirty = False 1063 self._listener_for_children.dirty = False 1064 return size
Returns the serialized size of this message.
Recursively calls ByteSize() on all contained messages.
Returns:
int: The number of bytes required to serialize this message.
784 def RegisterExtension(field_descriptor): 785 field_descriptor.containing_type = cls.DESCRIPTOR 786 # TODO: Use cls.MESSAGE_FACTORY.pool when available. 787 # pylint: disable=protected-access 788 cls.DESCRIPTOR.file.pool._AddExtensionDescriptor(field_descriptor) 789 _AttachFieldHelpers(cls, field_descriptor)
642 def getter(self): 643 field_value = self._fields.get(field) 644 if field_value is None: 645 # Construct a new object to represent this field. 646 field_value = field._default_constructor(self) 647 648 # Atomically check if another thread has preempted us and, if not, swap 649 # in the new object we just created. If someone has preempted us, we 650 # take that object and discard ours. 651 # WARNING: We are relying on setdefault() being atomic. This is true 652 # in CPython but we haven't investigated others. This warning appears 653 # in several other locations in this file. 654 field_value = self._fields.setdefault(field, field_value) 655 return field_value
Getter for tails.
642 def getter(self): 643 field_value = self._fields.get(field) 644 if field_value is None: 645 # Construct a new object to represent this field. 646 field_value = field._default_constructor(self) 647 648 # Atomically check if another thread has preempted us and, if not, swap 649 # in the new object we just created. If someone has preempted us, we 650 # take that object and discard ours. 651 # WARNING: We are relying on setdefault() being atomic. This is true 652 # in CPython but we haven't investigated others. This warning appears 653 # in several other locations in this file. 654 field_value = self._fields.setdefault(field, field_value) 655 return field_value
Getter for heads.
642 def getter(self): 643 field_value = self._fields.get(field) 644 if field_value is None: 645 # Construct a new object to represent this field. 646 field_value = field._default_constructor(self) 647 648 # Atomically check if another thread has preempted us and, if not, swap 649 # in the new object we just created. If someone has preempted us, we 650 # take that object and discard ours. 651 # WARNING: We are relying on setdefault() being atomic. This is true 652 # in CPython but we haven't investigated others. This warning appears 653 # in several other locations in this file. 654 field_value = self._fields.setdefault(field, field_value) 655 return field_value
Getter for literals.
1253 def FindInitializationErrors(self): 1254 """Finds required fields which are not initialized. 1255 1256 Returns: 1257 A list of strings. Each string is a path to an uninitialized field from 1258 the top-level message, e.g. "foo.bar[5].baz". 1259 """ 1260 1261 errors = [] # simplify things 1262 1263 for field in required_fields: 1264 if not self.HasField(field.name): 1265 errors.append(field.name) 1266 1267 for field, value in self.ListFields(): 1268 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 1269 if field.is_extension: 1270 name = '(%s)' % field.full_name 1271 else: 1272 name = field.name 1273 1274 if _IsMapField(field): 1275 if _IsMessageMapField(field): 1276 for key in value: 1277 element = value[key] 1278 prefix = '%s[%s].' % (name, key) 1279 sub_errors = element.FindInitializationErrors() 1280 errors += [prefix + error for error in sub_errors] 1281 else: 1282 # ScalarMaps can't have any initialization errors. 1283 pass 1284 elif field.label == _FieldDescriptor.LABEL_REPEATED: 1285 for i in range(len(value)): 1286 element = value[i] 1287 prefix = '%s[%d].' % (name, i) 1288 sub_errors = element.FindInitializationErrors() 1289 errors += [prefix + error for error in sub_errors] 1290 else: 1291 prefix = name + '.' 1292 sub_errors = value.FindInitializationErrors() 1293 errors += [prefix + error for error in sub_errors] 1294 1295 return errors
Finds required fields which are not initialized.
Returns:
A list of strings. Each string is a path to an uninitialized field from the top-level message, e.g. "foo.bar[5].baz".
Inherited Members
- google.protobuf.message.Message
- CopyFrom
- ParseFromString
- HasExtension
- ClearExtension
Abstract base class for protocol messages.
Protocol message classes are almost always generated by the protocol compiler. These generated types subclass Message and implement the methods shown below.
496 def init(self, **kwargs): 497 self._cached_byte_size = 0 498 self._cached_byte_size_dirty = len(kwargs) > 0 499 self._fields = {} 500 # Contains a mapping from oneof field descriptors to the descriptor 501 # of the currently set field in that oneof field. 502 self._oneofs = {} 503 504 # _unknown_fields is () when empty for efficiency, and will be turned into 505 # a list if fields are added. 506 self._unknown_fields = () 507 # _unknown_field_set is None when empty for efficiency, and will be 508 # turned into UnknownFieldSet struct if fields are added. 509 self._unknown_field_set = None # pylint: disable=protected-access 510 self._is_present_in_parent = False 511 self._listener = message_listener_mod.NullMessageListener() 512 self._listener_for_children = _Listener(self) 513 for field_name, field_value in kwargs.items(): 514 field = _GetFieldByName(message_descriptor, field_name) 515 if field is None: 516 raise TypeError('%s() got an unexpected keyword argument "%s"' % 517 (message_descriptor.name, field_name)) 518 if field_value is None: 519 # field=None is the same as no field at all. 520 continue 521 if field.label == _FieldDescriptor.LABEL_REPEATED: 522 copy = field._default_constructor(self) 523 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: # Composite 524 if _IsMapField(field): 525 if _IsMessageMapField(field): 526 for key in field_value: 527 copy[key].MergeFrom(field_value[key]) 528 else: 529 copy.update(field_value) 530 else: 531 for val in field_value: 532 if isinstance(val, dict): 533 copy.add(**val) 534 else: 535 copy.add().MergeFrom(val) 536 else: # Scalar 537 if field.cpp_type == _FieldDescriptor.CPPTYPE_ENUM: 538 field_value = [_GetIntegerEnumValue(field.enum_type, val) 539 for val in field_value] 540 copy.extend(field_value) 541 self._fields[field] = copy 542 elif field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 543 copy = field._default_constructor(self) 544 new_val = field_value 545 if isinstance(field_value, dict): 546 new_val = field.message_type._concrete_class(**field_value) 547 try: 548 copy.MergeFrom(new_val) 549 except TypeError: 550 _ReraiseTypeErrorWithFieldName(message_descriptor.name, field_name) 551 self._fields[field] = copy 552 else: 553 if field.cpp_type == _FieldDescriptor.CPPTYPE_ENUM: 554 field_value = _GetIntegerEnumValue(field.enum_type, field_value) 555 try: 556 setattr(self, field_name, field_value) 557 except TypeError: 558 _ReraiseTypeErrorWithFieldName(message_descriptor.name, field_name)
1312 def MergeFrom(self, msg): 1313 if not isinstance(msg, cls): 1314 raise TypeError( 1315 'Parameter to MergeFrom() must be instance of same class: ' 1316 'expected %s got %s.' % (_FullyQualifiedClassName(cls), 1317 _FullyQualifiedClassName(msg.__class__))) 1318 1319 assert msg is not self 1320 self._Modified() 1321 1322 fields = self._fields 1323 1324 for field, value in msg._fields.items(): 1325 if field.label == LABEL_REPEATED: 1326 field_value = fields.get(field) 1327 if field_value is None: 1328 # Construct a new object to represent this field. 1329 field_value = field._default_constructor(self) 1330 fields[field] = field_value 1331 field_value.MergeFrom(value) 1332 elif field.cpp_type == CPPTYPE_MESSAGE: 1333 if value._is_present_in_parent: 1334 field_value = fields.get(field) 1335 if field_value is None: 1336 # Construct a new object to represent this field. 1337 field_value = field._default_constructor(self) 1338 fields[field] = field_value 1339 field_value.MergeFrom(value) 1340 else: 1341 self._fields[field] = value 1342 if field.containing_oneof: 1343 self._UpdateOneofState(field) 1344 1345 if msg._unknown_fields: 1346 if not self._unknown_fields: 1347 self._unknown_fields = [] 1348 self._unknown_fields.extend(msg._unknown_fields) 1349 # pylint: disable=protected-access 1350 if self._unknown_field_set is None: 1351 self._unknown_field_set = containers.UnknownFieldSet() 1352 self._unknown_field_set._extend(msg._unknown_field_set)
Merges the contents of the specified message into current message.
This method merges the contents of the specified message into the current message. Singular fields that are set in the specified message overwrite the corresponding fields in the current message. Repeated fields are appended. Singular sub-messages and groups are recursively merged.
Arguments:
- other_msg (Message): A message to merge into the current message.
1375def _Clear(self): 1376 # Clear fields. 1377 self._fields = {} 1378 self._unknown_fields = () 1379 # pylint: disable=protected-access 1380 if self._unknown_field_set is not None: 1381 self._unknown_field_set._clear() 1382 self._unknown_field_set = None 1383 1384 self._oneofs = {} 1385 self._Modified()
Clears all data that was set in the message.
1452 def Modified(self): 1453 """Sets the _cached_byte_size_dirty bit to true, 1454 and propagates this to our listener iff this was a state change. 1455 """ 1456 1457 # Note: Some callers check _cached_byte_size_dirty before calling 1458 # _Modified() as an extra optimization. So, if this method is ever 1459 # changed such that it does stuff even when _cached_byte_size_dirty is 1460 # already true, the callers need to be updated. 1461 if not self._cached_byte_size_dirty: 1462 self._cached_byte_size_dirty = True 1463 self._listener_for_children.dirty = True 1464 self._is_present_in_parent = True 1465 self._listener.Modified()
Sets the _cached_byte_size_dirty bit to true, and propagates this to our listener iff this was a state change.
1213 def IsInitialized(self, errors=None): 1214 """Checks if all required fields of a message are set. 1215 1216 Args: 1217 errors: A list which, if provided, will be populated with the field 1218 paths of all missing required fields. 1219 1220 Returns: 1221 True iff the specified message has all required fields set. 1222 """ 1223 1224 # Performance is critical so we avoid HasField() and ListFields(). 1225 1226 for field in required_fields: 1227 if (field not in self._fields or 1228 (field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE and 1229 not self._fields[field]._is_present_in_parent)): 1230 if errors is not None: 1231 errors.extend(self.FindInitializationErrors()) 1232 return False 1233 1234 for field, value in list(self._fields.items()): # dict can change size! 1235 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 1236 if field.label == _FieldDescriptor.LABEL_REPEATED: 1237 if (field.message_type._is_map_entry): 1238 continue 1239 for element in value: 1240 if not element.IsInitialized(): 1241 if errors is not None: 1242 errors.extend(self.FindInitializationErrors()) 1243 return False 1244 elif value._is_present_in_parent and not value.IsInitialized(): 1245 if errors is not None: 1246 errors.extend(self.FindInitializationErrors()) 1247 return False 1248 1249 return True
Checks if all required fields of a message are set.
Arguments:
- errors: A list which, if provided, will be populated with the field paths of all missing required fields.
Returns:
True iff the specified message has all required fields set.
1119 def MergeFromString(self, serialized): 1120 serialized = memoryview(serialized) 1121 length = len(serialized) 1122 try: 1123 if self._InternalParse(serialized, 0, length) != length: 1124 # The only reason _InternalParse would return early is if it 1125 # encountered an end-group tag. 1126 raise message_mod.DecodeError('Unexpected end-group tag.') 1127 except (IndexError, TypeError): 1128 # Now ord(buf[p:p+1]) == ord('') gets TypeError. 1129 raise message_mod.DecodeError('Truncated message.') 1130 except struct.error as e: 1131 raise message_mod.DecodeError(e) 1132 return length # Return this for legacy reasons.
Merges serialized protocol buffer data into this message.
When we find a field in serialized
that is already present
in this message:
- If it's a "repeated" field, we append to the end of our list.
- Else, if it's a scalar, we overwrite our field.
- Else, (it's a nonrepeated composite), we recursively merge into the existing composite.
Arguments:
- serialized (bytes): Any object that allows us to call
memoryview(serialized)
to access a string of bytes using the buffer interface.
Returns:
int: The number of bytes read from
serialized
. For non-group messages, this will always belen(serialized)
, but for messages which are actually groups, this will generally be less thanlen(serialized)
, since we must stop when we reach anEND_GROUP
tag. Note that if we do stop because of anEND_GROUP
tag, the number of bytes returned does not include the bytes for theEND_GROUP
tag information.
Raises:
- DecodeError: if the input cannot be parsed.
1072 def SerializeToString(self, **kwargs): 1073 # Check if the message has all of its required fields set. 1074 if not self.IsInitialized(): 1075 raise message_mod.EncodeError( 1076 'Message %s is missing required fields: %s' % ( 1077 self.DESCRIPTOR.full_name, ','.join(self.FindInitializationErrors()))) 1078 return self.SerializePartialToString(**kwargs)
Serializes the protocol message to a binary string.
Keyword Args:
deterministic (bool): If true, requests deterministic serialization of the protobuf, with predictable ordering of map keys.
Returns:
A binary string representation of the message if all of the required fields in the message are set (i.e. the message is initialized).
Raises:
- EncodeError: if the message isn't initialized (see
IsInitialized()
).
1085 def SerializePartialToString(self, **kwargs): 1086 out = BytesIO() 1087 self._InternalSerialize(out.write, **kwargs) 1088 return out.getvalue()
Serializes the protocol message to a binary string.
This method is similar to SerializeToString but doesn't check if the message is initialized.
Keyword Args:
deterministic (bool): If true, requests deterministic serialization of the protobuf, with predictable ordering of map keys.
Returns:
bytes: A serialized representation of the partial message.
814 def ListFields(self): 815 all_fields = [item for item in self._fields.items() if _IsPresent(item)] 816 all_fields.sort(key = lambda item: item[0].number) 817 return all_fields
Returns a list of (FieldDescriptor, value) tuples for present fields.
A message field is non-empty if HasField() would return true. A singular primitive field is non-empty if HasField() would return true in proto2 or it is non zero in proto3. A repeated field is non-empty if it contains at least one element. The fields are ordered by field number.
Returns:
list[tuple(FieldDescriptor, value)]: field descriptors and values for all fields in the message which are not empty. The values vary by field type.
838 def HasField(self, field_name): 839 try: 840 field = hassable_fields[field_name] 841 except KeyError as exc: 842 raise ValueError('Protocol message %s has no non-repeated field "%s" ' 843 'nor has presence is not available for this field.' % ( 844 message_descriptor.full_name, field_name)) from exc 845 846 if isinstance(field, descriptor_mod.OneofDescriptor): 847 try: 848 return HasField(self, self._oneofs[field].name) 849 except KeyError: 850 return False 851 else: 852 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 853 value = self._fields.get(field) 854 return value is not None and value._is_present_in_parent 855 else: 856 return field in self._fields
Checks if a certain field is set for the message.
For a oneof group, checks if any field inside is set. Note that if the
field_name is not defined in the message descriptor, ValueError
will
be raised.
Arguments:
- field_name (str): The name of the field to check for presence.
Returns:
bool: Whether a value has been set for the named field.
Raises:
- ValueError: if the
field_name
is not a member of this message.
863 def ClearField(self, field_name): 864 try: 865 field = message_descriptor.fields_by_name[field_name] 866 except KeyError: 867 try: 868 field = message_descriptor.oneofs_by_name[field_name] 869 if field in self._oneofs: 870 field = self._oneofs[field] 871 else: 872 return 873 except KeyError: 874 raise ValueError('Protocol message %s has no "%s" field.' % 875 (message_descriptor.name, field_name)) 876 877 if field in self._fields: 878 # To match the C++ implementation, we need to invalidate iterators 879 # for map fields when ClearField() happens. 880 if hasattr(self._fields[field], 'InvalidateIterators'): 881 self._fields[field].InvalidateIterators() 882 883 # Note: If the field is a sub-message, its listener will still point 884 # at us. That's fine, because the worst than can happen is that it 885 # will call _Modified() and invalidate our byte size. Big deal. 886 del self._fields[field] 887 888 if self._oneofs.get(field.containing_oneof, None) is field: 889 del self._oneofs[field.containing_oneof] 890 891 # Always call _Modified() -- even if nothing was changed, this is 892 # a mutating method, and thus calling it should cause the field to become 893 # present in the parent message. 894 self._Modified()
Clears the contents of a given field.
Inside a oneof group, clears the field set. If the name neither refers to a
defined field or oneof group, ValueError
is raised.
Arguments:
- field_name (str): The name of the field to check for presence.
Raises:
- ValueError: if the
field_name
is not a member of this message.
1358 def WhichOneof(self, oneof_name): 1359 """Returns the name of the currently set field inside a oneof, or None.""" 1360 try: 1361 field = message_descriptor.oneofs_by_name[oneof_name] 1362 except KeyError: 1363 raise ValueError( 1364 'Protocol message has no oneof "%s" field.' % oneof_name) 1365 1366 nested_field = self._oneofs.get(field, None) 1367 if nested_field is not None and self.HasField(nested_field.name): 1368 return nested_field.name 1369 else: 1370 return None
Returns the name of the currently set field inside a oneof, or None.
1388def _UnknownFields(self): 1389 warnings.warn( 1390 'message.UnknownFields() is deprecated. Please use the add one ' 1391 'feature unknown_fields.UnknownFieldSet(message) in ' 1392 'unknown_fields.py instead.' 1393 ) 1394 if self._unknown_field_set is None: # pylint: disable=protected-access 1395 # pylint: disable=protected-access 1396 self._unknown_field_set = containers.UnknownFieldSet() 1397 return self._unknown_field_set # pylint: disable=protected-access
Returns the UnknownFieldSet.
Returns:
UnknownFieldSet: The unknown fields stored in this message.
1400def _DiscardUnknownFields(self): 1401 self._unknown_fields = [] 1402 self._unknown_field_set = None # pylint: disable=protected-access 1403 for field, value in self.ListFields(): 1404 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 1405 if _IsMapField(field): 1406 if _IsMessageMapField(field): 1407 for key in value: 1408 value[key].DiscardUnknownFields() 1409 elif field.label == _FieldDescriptor.LABEL_REPEATED: 1410 for sub_message in value: 1411 sub_message.DiscardUnknownFields() 1412 else: 1413 value.DiscardUnknownFields()
Clears all fields in the UnknownFieldSet
.
This operation is recursive for nested message.
1040 def ByteSize(self): 1041 if not self._cached_byte_size_dirty: 1042 return self._cached_byte_size 1043 1044 size = 0 1045 descriptor = self.DESCRIPTOR 1046 if descriptor._is_map_entry: 1047 # Fields of map entry should always be serialized. 1048 key_field = descriptor.fields_by_name['key'] 1049 _MaybeAddEncoder(cls, key_field) 1050 size = key_field._sizer(self.key) 1051 value_field = descriptor.fields_by_name['value'] 1052 _MaybeAddEncoder(cls, value_field) 1053 size += value_field._sizer(self.value) 1054 else: 1055 for field_descriptor, field_value in self.ListFields(): 1056 _MaybeAddEncoder(cls, field_descriptor) 1057 size += field_descriptor._sizer(field_value) 1058 for tag_bytes, value_bytes in self._unknown_fields: 1059 size += len(tag_bytes) + len(value_bytes) 1060 1061 self._cached_byte_size = size 1062 self._cached_byte_size_dirty = False 1063 self._listener_for_children.dirty = False 1064 return size
Returns the serialized size of this message.
Recursively calls ByteSize() on all contained messages.
Returns:
int: The number of bytes required to serialize this message.
784 def RegisterExtension(field_descriptor): 785 field_descriptor.containing_type = cls.DESCRIPTOR 786 # TODO: Use cls.MESSAGE_FACTORY.pool when available. 787 # pylint: disable=protected-access 788 cls.DESCRIPTOR.file.pool._AddExtensionDescriptor(field_descriptor) 789 _AttachFieldHelpers(cls, field_descriptor)
642 def getter(self): 643 field_value = self._fields.get(field) 644 if field_value is None: 645 # Construct a new object to represent this field. 646 field_value = field._default_constructor(self) 647 648 # Atomically check if another thread has preempted us and, if not, swap 649 # in the new object we just created. If someone has preempted us, we 650 # take that object and discard ours. 651 # WARNING: We are relying on setdefault() being atomic. This is true 652 # in CPython but we haven't investigated others. This warning appears 653 # in several other locations in this file. 654 field_value = self._fields.setdefault(field, field_value) 655 return field_value
Getter for tails.
642 def getter(self): 643 field_value = self._fields.get(field) 644 if field_value is None: 645 # Construct a new object to represent this field. 646 field_value = field._default_constructor(self) 647 648 # Atomically check if another thread has preempted us and, if not, swap 649 # in the new object we just created. If someone has preempted us, we 650 # take that object and discard ours. 651 # WARNING: We are relying on setdefault() being atomic. This is true 652 # in CPython but we haven't investigated others. This warning appears 653 # in several other locations in this file. 654 field_value = self._fields.setdefault(field, field_value) 655 return field_value
Getter for heads.
642 def getter(self): 643 field_value = self._fields.get(field) 644 if field_value is None: 645 # Construct a new object to represent this field. 646 field_value = field._default_constructor(self) 647 648 # Atomically check if another thread has preempted us and, if not, swap 649 # in the new object we just created. If someone has preempted us, we 650 # take that object and discard ours. 651 # WARNING: We are relying on setdefault() being atomic. This is true 652 # in CPython but we haven't investigated others. This warning appears 653 # in several other locations in this file. 654 field_value = self._fields.setdefault(field, field_value) 655 return field_value
Getter for literals.
642 def getter(self): 643 field_value = self._fields.get(field) 644 if field_value is None: 645 # Construct a new object to represent this field. 646 field_value = field._default_constructor(self) 647 648 # Atomically check if another thread has preempted us and, if not, swap 649 # in the new object we just created. If someone has preempted us, we 650 # take that object and discard ours. 651 # WARNING: We are relying on setdefault() being atomic. This is true 652 # in CPython but we haven't investigated others. This warning appears 653 # in several other locations in this file. 654 field_value = self._fields.setdefault(field, field_value) 655 return field_value
Getter for demands.
685 def getter(self): 686 # TODO: This may be broken since there may not be 687 # default_value. Combine with has_default_value somehow. 688 return self._fields.get(field, default_value)
Getter for capacity.
1253 def FindInitializationErrors(self): 1254 """Finds required fields which are not initialized. 1255 1256 Returns: 1257 A list of strings. Each string is a path to an uninitialized field from 1258 the top-level message, e.g. "foo.bar[5].baz". 1259 """ 1260 1261 errors = [] # simplify things 1262 1263 for field in required_fields: 1264 if not self.HasField(field.name): 1265 errors.append(field.name) 1266 1267 for field, value in self.ListFields(): 1268 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 1269 if field.is_extension: 1270 name = '(%s)' % field.full_name 1271 else: 1272 name = field.name 1273 1274 if _IsMapField(field): 1275 if _IsMessageMapField(field): 1276 for key in value: 1277 element = value[key] 1278 prefix = '%s[%s].' % (name, key) 1279 sub_errors = element.FindInitializationErrors() 1280 errors += [prefix + error for error in sub_errors] 1281 else: 1282 # ScalarMaps can't have any initialization errors. 1283 pass 1284 elif field.label == _FieldDescriptor.LABEL_REPEATED: 1285 for i in range(len(value)): 1286 element = value[i] 1287 prefix = '%s[%d].' % (name, i) 1288 sub_errors = element.FindInitializationErrors() 1289 errors += [prefix + error for error in sub_errors] 1290 else: 1291 prefix = name + '.' 1292 sub_errors = value.FindInitializationErrors() 1293 errors += [prefix + error for error in sub_errors] 1294 1295 return errors
Finds required fields which are not initialized.
Returns:
A list of strings. Each string is a path to an uninitialized field from the top-level message, e.g. "foo.bar[5].baz".
Inherited Members
- google.protobuf.message.Message
- CopyFrom
- ParseFromString
- HasExtension
- ClearExtension
Abstract base class for protocol messages.
Protocol message classes are almost always generated by the protocol compiler. These generated types subclass Message and implement the methods shown below.
496 def init(self, **kwargs): 497 self._cached_byte_size = 0 498 self._cached_byte_size_dirty = len(kwargs) > 0 499 self._fields = {} 500 # Contains a mapping from oneof field descriptors to the descriptor 501 # of the currently set field in that oneof field. 502 self._oneofs = {} 503 504 # _unknown_fields is () when empty for efficiency, and will be turned into 505 # a list if fields are added. 506 self._unknown_fields = () 507 # _unknown_field_set is None when empty for efficiency, and will be 508 # turned into UnknownFieldSet struct if fields are added. 509 self._unknown_field_set = None # pylint: disable=protected-access 510 self._is_present_in_parent = False 511 self._listener = message_listener_mod.NullMessageListener() 512 self._listener_for_children = _Listener(self) 513 for field_name, field_value in kwargs.items(): 514 field = _GetFieldByName(message_descriptor, field_name) 515 if field is None: 516 raise TypeError('%s() got an unexpected keyword argument "%s"' % 517 (message_descriptor.name, field_name)) 518 if field_value is None: 519 # field=None is the same as no field at all. 520 continue 521 if field.label == _FieldDescriptor.LABEL_REPEATED: 522 copy = field._default_constructor(self) 523 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: # Composite 524 if _IsMapField(field): 525 if _IsMessageMapField(field): 526 for key in field_value: 527 copy[key].MergeFrom(field_value[key]) 528 else: 529 copy.update(field_value) 530 else: 531 for val in field_value: 532 if isinstance(val, dict): 533 copy.add(**val) 534 else: 535 copy.add().MergeFrom(val) 536 else: # Scalar 537 if field.cpp_type == _FieldDescriptor.CPPTYPE_ENUM: 538 field_value = [_GetIntegerEnumValue(field.enum_type, val) 539 for val in field_value] 540 copy.extend(field_value) 541 self._fields[field] = copy 542 elif field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 543 copy = field._default_constructor(self) 544 new_val = field_value 545 if isinstance(field_value, dict): 546 new_val = field.message_type._concrete_class(**field_value) 547 try: 548 copy.MergeFrom(new_val) 549 except TypeError: 550 _ReraiseTypeErrorWithFieldName(message_descriptor.name, field_name) 551 self._fields[field] = copy 552 else: 553 if field.cpp_type == _FieldDescriptor.CPPTYPE_ENUM: 554 field_value = _GetIntegerEnumValue(field.enum_type, field_value) 555 try: 556 setattr(self, field_name, field_value) 557 except TypeError: 558 _ReraiseTypeErrorWithFieldName(message_descriptor.name, field_name)
1312 def MergeFrom(self, msg): 1313 if not isinstance(msg, cls): 1314 raise TypeError( 1315 'Parameter to MergeFrom() must be instance of same class: ' 1316 'expected %s got %s.' % (_FullyQualifiedClassName(cls), 1317 _FullyQualifiedClassName(msg.__class__))) 1318 1319 assert msg is not self 1320 self._Modified() 1321 1322 fields = self._fields 1323 1324 for field, value in msg._fields.items(): 1325 if field.label == LABEL_REPEATED: 1326 field_value = fields.get(field) 1327 if field_value is None: 1328 # Construct a new object to represent this field. 1329 field_value = field._default_constructor(self) 1330 fields[field] = field_value 1331 field_value.MergeFrom(value) 1332 elif field.cpp_type == CPPTYPE_MESSAGE: 1333 if value._is_present_in_parent: 1334 field_value = fields.get(field) 1335 if field_value is None: 1336 # Construct a new object to represent this field. 1337 field_value = field._default_constructor(self) 1338 fields[field] = field_value 1339 field_value.MergeFrom(value) 1340 else: 1341 self._fields[field] = value 1342 if field.containing_oneof: 1343 self._UpdateOneofState(field) 1344 1345 if msg._unknown_fields: 1346 if not self._unknown_fields: 1347 self._unknown_fields = [] 1348 self._unknown_fields.extend(msg._unknown_fields) 1349 # pylint: disable=protected-access 1350 if self._unknown_field_set is None: 1351 self._unknown_field_set = containers.UnknownFieldSet() 1352 self._unknown_field_set._extend(msg._unknown_field_set)
Merges the contents of the specified message into current message.
This method merges the contents of the specified message into the current message. Singular fields that are set in the specified message overwrite the corresponding fields in the current message. Repeated fields are appended. Singular sub-messages and groups are recursively merged.
Arguments:
- other_msg (Message): A message to merge into the current message.
1375def _Clear(self): 1376 # Clear fields. 1377 self._fields = {} 1378 self._unknown_fields = () 1379 # pylint: disable=protected-access 1380 if self._unknown_field_set is not None: 1381 self._unknown_field_set._clear() 1382 self._unknown_field_set = None 1383 1384 self._oneofs = {} 1385 self._Modified()
Clears all data that was set in the message.
1452 def Modified(self): 1453 """Sets the _cached_byte_size_dirty bit to true, 1454 and propagates this to our listener iff this was a state change. 1455 """ 1456 1457 # Note: Some callers check _cached_byte_size_dirty before calling 1458 # _Modified() as an extra optimization. So, if this method is ever 1459 # changed such that it does stuff even when _cached_byte_size_dirty is 1460 # already true, the callers need to be updated. 1461 if not self._cached_byte_size_dirty: 1462 self._cached_byte_size_dirty = True 1463 self._listener_for_children.dirty = True 1464 self._is_present_in_parent = True 1465 self._listener.Modified()
Sets the _cached_byte_size_dirty bit to true, and propagates this to our listener iff this was a state change.
1213 def IsInitialized(self, errors=None): 1214 """Checks if all required fields of a message are set. 1215 1216 Args: 1217 errors: A list which, if provided, will be populated with the field 1218 paths of all missing required fields. 1219 1220 Returns: 1221 True iff the specified message has all required fields set. 1222 """ 1223 1224 # Performance is critical so we avoid HasField() and ListFields(). 1225 1226 for field in required_fields: 1227 if (field not in self._fields or 1228 (field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE and 1229 not self._fields[field]._is_present_in_parent)): 1230 if errors is not None: 1231 errors.extend(self.FindInitializationErrors()) 1232 return False 1233 1234 for field, value in list(self._fields.items()): # dict can change size! 1235 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 1236 if field.label == _FieldDescriptor.LABEL_REPEATED: 1237 if (field.message_type._is_map_entry): 1238 continue 1239 for element in value: 1240 if not element.IsInitialized(): 1241 if errors is not None: 1242 errors.extend(self.FindInitializationErrors()) 1243 return False 1244 elif value._is_present_in_parent and not value.IsInitialized(): 1245 if errors is not None: 1246 errors.extend(self.FindInitializationErrors()) 1247 return False 1248 1249 return True
Checks if all required fields of a message are set.
Arguments:
- errors: A list which, if provided, will be populated with the field paths of all missing required fields.
Returns:
True iff the specified message has all required fields set.
1119 def MergeFromString(self, serialized): 1120 serialized = memoryview(serialized) 1121 length = len(serialized) 1122 try: 1123 if self._InternalParse(serialized, 0, length) != length: 1124 # The only reason _InternalParse would return early is if it 1125 # encountered an end-group tag. 1126 raise message_mod.DecodeError('Unexpected end-group tag.') 1127 except (IndexError, TypeError): 1128 # Now ord(buf[p:p+1]) == ord('') gets TypeError. 1129 raise message_mod.DecodeError('Truncated message.') 1130 except struct.error as e: 1131 raise message_mod.DecodeError(e) 1132 return length # Return this for legacy reasons.
Merges serialized protocol buffer data into this message.
When we find a field in serialized
that is already present
in this message:
- If it's a "repeated" field, we append to the end of our list.
- Else, if it's a scalar, we overwrite our field.
- Else, (it's a nonrepeated composite), we recursively merge into the existing composite.
Arguments:
- serialized (bytes): Any object that allows us to call
memoryview(serialized)
to access a string of bytes using the buffer interface.
Returns:
int: The number of bytes read from
serialized
. For non-group messages, this will always belen(serialized)
, but for messages which are actually groups, this will generally be less thanlen(serialized)
, since we must stop when we reach anEND_GROUP
tag. Note that if we do stop because of anEND_GROUP
tag, the number of bytes returned does not include the bytes for theEND_GROUP
tag information.
Raises:
- DecodeError: if the input cannot be parsed.
1072 def SerializeToString(self, **kwargs): 1073 # Check if the message has all of its required fields set. 1074 if not self.IsInitialized(): 1075 raise message_mod.EncodeError( 1076 'Message %s is missing required fields: %s' % ( 1077 self.DESCRIPTOR.full_name, ','.join(self.FindInitializationErrors()))) 1078 return self.SerializePartialToString(**kwargs)
Serializes the protocol message to a binary string.
Keyword Args:
deterministic (bool): If true, requests deterministic serialization of the protobuf, with predictable ordering of map keys.
Returns:
A binary string representation of the message if all of the required fields in the message are set (i.e. the message is initialized).
Raises:
- EncodeError: if the message isn't initialized (see
IsInitialized()
).
1085 def SerializePartialToString(self, **kwargs): 1086 out = BytesIO() 1087 self._InternalSerialize(out.write, **kwargs) 1088 return out.getvalue()
Serializes the protocol message to a binary string.
This method is similar to SerializeToString but doesn't check if the message is initialized.
Keyword Args:
deterministic (bool): If true, requests deterministic serialization of the protobuf, with predictable ordering of map keys.
Returns:
bytes: A serialized representation of the partial message.
814 def ListFields(self): 815 all_fields = [item for item in self._fields.items() if _IsPresent(item)] 816 all_fields.sort(key = lambda item: item[0].number) 817 return all_fields
Returns a list of (FieldDescriptor, value) tuples for present fields.
A message field is non-empty if HasField() would return true. A singular primitive field is non-empty if HasField() would return true in proto2 or it is non zero in proto3. A repeated field is non-empty if it contains at least one element. The fields are ordered by field number.
Returns:
list[tuple(FieldDescriptor, value)]: field descriptors and values for all fields in the message which are not empty. The values vary by field type.
838 def HasField(self, field_name): 839 try: 840 field = hassable_fields[field_name] 841 except KeyError as exc: 842 raise ValueError('Protocol message %s has no non-repeated field "%s" ' 843 'nor has presence is not available for this field.' % ( 844 message_descriptor.full_name, field_name)) from exc 845 846 if isinstance(field, descriptor_mod.OneofDescriptor): 847 try: 848 return HasField(self, self._oneofs[field].name) 849 except KeyError: 850 return False 851 else: 852 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 853 value = self._fields.get(field) 854 return value is not None and value._is_present_in_parent 855 else: 856 return field in self._fields
Checks if a certain field is set for the message.
For a oneof group, checks if any field inside is set. Note that if the
field_name is not defined in the message descriptor, ValueError
will
be raised.
Arguments:
- field_name (str): The name of the field to check for presence.
Returns:
bool: Whether a value has been set for the named field.
Raises:
- ValueError: if the
field_name
is not a member of this message.
863 def ClearField(self, field_name): 864 try: 865 field = message_descriptor.fields_by_name[field_name] 866 except KeyError: 867 try: 868 field = message_descriptor.oneofs_by_name[field_name] 869 if field in self._oneofs: 870 field = self._oneofs[field] 871 else: 872 return 873 except KeyError: 874 raise ValueError('Protocol message %s has no "%s" field.' % 875 (message_descriptor.name, field_name)) 876 877 if field in self._fields: 878 # To match the C++ implementation, we need to invalidate iterators 879 # for map fields when ClearField() happens. 880 if hasattr(self._fields[field], 'InvalidateIterators'): 881 self._fields[field].InvalidateIterators() 882 883 # Note: If the field is a sub-message, its listener will still point 884 # at us. That's fine, because the worst than can happen is that it 885 # will call _Modified() and invalidate our byte size. Big deal. 886 del self._fields[field] 887 888 if self._oneofs.get(field.containing_oneof, None) is field: 889 del self._oneofs[field.containing_oneof] 890 891 # Always call _Modified() -- even if nothing was changed, this is 892 # a mutating method, and thus calling it should cause the field to become 893 # present in the parent message. 894 self._Modified()
Clears the contents of a given field.
Inside a oneof group, clears the field set. If the name neither refers to a
defined field or oneof group, ValueError
is raised.
Arguments:
- field_name (str): The name of the field to check for presence.
Raises:
- ValueError: if the
field_name
is not a member of this message.
1358 def WhichOneof(self, oneof_name): 1359 """Returns the name of the currently set field inside a oneof, or None.""" 1360 try: 1361 field = message_descriptor.oneofs_by_name[oneof_name] 1362 except KeyError: 1363 raise ValueError( 1364 'Protocol message has no oneof "%s" field.' % oneof_name) 1365 1366 nested_field = self._oneofs.get(field, None) 1367 if nested_field is not None and self.HasField(nested_field.name): 1368 return nested_field.name 1369 else: 1370 return None
Returns the name of the currently set field inside a oneof, or None.
1388def _UnknownFields(self): 1389 warnings.warn( 1390 'message.UnknownFields() is deprecated. Please use the add one ' 1391 'feature unknown_fields.UnknownFieldSet(message) in ' 1392 'unknown_fields.py instead.' 1393 ) 1394 if self._unknown_field_set is None: # pylint: disable=protected-access 1395 # pylint: disable=protected-access 1396 self._unknown_field_set = containers.UnknownFieldSet() 1397 return self._unknown_field_set # pylint: disable=protected-access
Returns the UnknownFieldSet.
Returns:
UnknownFieldSet: The unknown fields stored in this message.
1400def _DiscardUnknownFields(self): 1401 self._unknown_fields = [] 1402 self._unknown_field_set = None # pylint: disable=protected-access 1403 for field, value in self.ListFields(): 1404 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 1405 if _IsMapField(field): 1406 if _IsMessageMapField(field): 1407 for key in value: 1408 value[key].DiscardUnknownFields() 1409 elif field.label == _FieldDescriptor.LABEL_REPEATED: 1410 for sub_message in value: 1411 sub_message.DiscardUnknownFields() 1412 else: 1413 value.DiscardUnknownFields()
Clears all fields in the UnknownFieldSet
.
This operation is recursive for nested message.
1040 def ByteSize(self): 1041 if not self._cached_byte_size_dirty: 1042 return self._cached_byte_size 1043 1044 size = 0 1045 descriptor = self.DESCRIPTOR 1046 if descriptor._is_map_entry: 1047 # Fields of map entry should always be serialized. 1048 key_field = descriptor.fields_by_name['key'] 1049 _MaybeAddEncoder(cls, key_field) 1050 size = key_field._sizer(self.key) 1051 value_field = descriptor.fields_by_name['value'] 1052 _MaybeAddEncoder(cls, value_field) 1053 size += value_field._sizer(self.value) 1054 else: 1055 for field_descriptor, field_value in self.ListFields(): 1056 _MaybeAddEncoder(cls, field_descriptor) 1057 size += field_descriptor._sizer(field_value) 1058 for tag_bytes, value_bytes in self._unknown_fields: 1059 size += len(tag_bytes) + len(value_bytes) 1060 1061 self._cached_byte_size = size 1062 self._cached_byte_size_dirty = False 1063 self._listener_for_children.dirty = False 1064 return size
Returns the serialized size of this message.
Recursively calls ByteSize() on all contained messages.
Returns:
int: The number of bytes required to serialize this message.
784 def RegisterExtension(field_descriptor): 785 field_descriptor.containing_type = cls.DESCRIPTOR 786 # TODO: Use cls.MESSAGE_FACTORY.pool when available. 787 # pylint: disable=protected-access 788 cls.DESCRIPTOR.file.pool._AddExtensionDescriptor(field_descriptor) 789 _AttachFieldHelpers(cls, field_descriptor)
642 def getter(self): 643 field_value = self._fields.get(field) 644 if field_value is None: 645 # Construct a new object to represent this field. 646 field_value = field._default_constructor(self) 647 648 # Atomically check if another thread has preempted us and, if not, swap 649 # in the new object we just created. If someone has preempted us, we 650 # take that object and discard ours. 651 # WARNING: We are relying on setdefault() being atomic. This is true 652 # in CPython but we haven't investigated others. This warning appears 653 # in several other locations in this file. 654 field_value = self._fields.setdefault(field, field_value) 655 return field_value
Getter for vars.
642 def getter(self): 643 field_value = self._fields.get(field) 644 if field_value is None: 645 # Construct a new object to represent this field. 646 field_value = field._default_constructor(self) 647 648 # Atomically check if another thread has preempted us and, if not, swap 649 # in the new object we just created. If someone has preempted us, we 650 # take that object and discard ours. 651 # WARNING: We are relying on setdefault() being atomic. This is true 652 # in CPython but we haven't investigated others. This warning appears 653 # in several other locations in this file. 654 field_value = self._fields.setdefault(field, field_value) 655 return field_value
Getter for values.
685 def getter(self): 686 # TODO: This may be broken since there may not be 687 # default_value. Combine with has_default_value somehow. 688 return self._fields.get(field, default_value)
Getter for negated.
1253 def FindInitializationErrors(self): 1254 """Finds required fields which are not initialized. 1255 1256 Returns: 1257 A list of strings. Each string is a path to an uninitialized field from 1258 the top-level message, e.g. "foo.bar[5].baz". 1259 """ 1260 1261 errors = [] # simplify things 1262 1263 for field in required_fields: 1264 if not self.HasField(field.name): 1265 errors.append(field.name) 1266 1267 for field, value in self.ListFields(): 1268 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 1269 if field.is_extension: 1270 name = '(%s)' % field.full_name 1271 else: 1272 name = field.name 1273 1274 if _IsMapField(field): 1275 if _IsMessageMapField(field): 1276 for key in value: 1277 element = value[key] 1278 prefix = '%s[%s].' % (name, key) 1279 sub_errors = element.FindInitializationErrors() 1280 errors += [prefix + error for error in sub_errors] 1281 else: 1282 # ScalarMaps can't have any initialization errors. 1283 pass 1284 elif field.label == _FieldDescriptor.LABEL_REPEATED: 1285 for i in range(len(value)): 1286 element = value[i] 1287 prefix = '%s[%d].' % (name, i) 1288 sub_errors = element.FindInitializationErrors() 1289 errors += [prefix + error for error in sub_errors] 1290 else: 1291 prefix = name + '.' 1292 sub_errors = value.FindInitializationErrors() 1293 errors += [prefix + error for error in sub_errors] 1294 1295 return errors
Finds required fields which are not initialized.
Returns:
A list of strings. Each string is a path to an uninitialized field from the top-level message, e.g. "foo.bar[5].baz".
Inherited Members
- google.protobuf.message.Message
- CopyFrom
- ParseFromString
- HasExtension
- ClearExtension
Abstract base class for protocol messages.
Protocol message classes are almost always generated by the protocol compiler. These generated types subclass Message and implement the methods shown below.
496 def init(self, **kwargs): 497 self._cached_byte_size = 0 498 self._cached_byte_size_dirty = len(kwargs) > 0 499 self._fields = {} 500 # Contains a mapping from oneof field descriptors to the descriptor 501 # of the currently set field in that oneof field. 502 self._oneofs = {} 503 504 # _unknown_fields is () when empty for efficiency, and will be turned into 505 # a list if fields are added. 506 self._unknown_fields = () 507 # _unknown_field_set is None when empty for efficiency, and will be 508 # turned into UnknownFieldSet struct if fields are added. 509 self._unknown_field_set = None # pylint: disable=protected-access 510 self._is_present_in_parent = False 511 self._listener = message_listener_mod.NullMessageListener() 512 self._listener_for_children = _Listener(self) 513 for field_name, field_value in kwargs.items(): 514 field = _GetFieldByName(message_descriptor, field_name) 515 if field is None: 516 raise TypeError('%s() got an unexpected keyword argument "%s"' % 517 (message_descriptor.name, field_name)) 518 if field_value is None: 519 # field=None is the same as no field at all. 520 continue 521 if field.label == _FieldDescriptor.LABEL_REPEATED: 522 copy = field._default_constructor(self) 523 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: # Composite 524 if _IsMapField(field): 525 if _IsMessageMapField(field): 526 for key in field_value: 527 copy[key].MergeFrom(field_value[key]) 528 else: 529 copy.update(field_value) 530 else: 531 for val in field_value: 532 if isinstance(val, dict): 533 copy.add(**val) 534 else: 535 copy.add().MergeFrom(val) 536 else: # Scalar 537 if field.cpp_type == _FieldDescriptor.CPPTYPE_ENUM: 538 field_value = [_GetIntegerEnumValue(field.enum_type, val) 539 for val in field_value] 540 copy.extend(field_value) 541 self._fields[field] = copy 542 elif field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 543 copy = field._default_constructor(self) 544 new_val = field_value 545 if isinstance(field_value, dict): 546 new_val = field.message_type._concrete_class(**field_value) 547 try: 548 copy.MergeFrom(new_val) 549 except TypeError: 550 _ReraiseTypeErrorWithFieldName(message_descriptor.name, field_name) 551 self._fields[field] = copy 552 else: 553 if field.cpp_type == _FieldDescriptor.CPPTYPE_ENUM: 554 field_value = _GetIntegerEnumValue(field.enum_type, field_value) 555 try: 556 setattr(self, field_name, field_value) 557 except TypeError: 558 _ReraiseTypeErrorWithFieldName(message_descriptor.name, field_name)
1312 def MergeFrom(self, msg): 1313 if not isinstance(msg, cls): 1314 raise TypeError( 1315 'Parameter to MergeFrom() must be instance of same class: ' 1316 'expected %s got %s.' % (_FullyQualifiedClassName(cls), 1317 _FullyQualifiedClassName(msg.__class__))) 1318 1319 assert msg is not self 1320 self._Modified() 1321 1322 fields = self._fields 1323 1324 for field, value in msg._fields.items(): 1325 if field.label == LABEL_REPEATED: 1326 field_value = fields.get(field) 1327 if field_value is None: 1328 # Construct a new object to represent this field. 1329 field_value = field._default_constructor(self) 1330 fields[field] = field_value 1331 field_value.MergeFrom(value) 1332 elif field.cpp_type == CPPTYPE_MESSAGE: 1333 if value._is_present_in_parent: 1334 field_value = fields.get(field) 1335 if field_value is None: 1336 # Construct a new object to represent this field. 1337 field_value = field._default_constructor(self) 1338 fields[field] = field_value 1339 field_value.MergeFrom(value) 1340 else: 1341 self._fields[field] = value 1342 if field.containing_oneof: 1343 self._UpdateOneofState(field) 1344 1345 if msg._unknown_fields: 1346 if not self._unknown_fields: 1347 self._unknown_fields = [] 1348 self._unknown_fields.extend(msg._unknown_fields) 1349 # pylint: disable=protected-access 1350 if self._unknown_field_set is None: 1351 self._unknown_field_set = containers.UnknownFieldSet() 1352 self._unknown_field_set._extend(msg._unknown_field_set)
Merges the contents of the specified message into current message.
This method merges the contents of the specified message into the current message. Singular fields that are set in the specified message overwrite the corresponding fields in the current message. Repeated fields are appended. Singular sub-messages and groups are recursively merged.
Arguments:
- other_msg (Message): A message to merge into the current message.
1375def _Clear(self): 1376 # Clear fields. 1377 self._fields = {} 1378 self._unknown_fields = () 1379 # pylint: disable=protected-access 1380 if self._unknown_field_set is not None: 1381 self._unknown_field_set._clear() 1382 self._unknown_field_set = None 1383 1384 self._oneofs = {} 1385 self._Modified()
Clears all data that was set in the message.
1452 def Modified(self): 1453 """Sets the _cached_byte_size_dirty bit to true, 1454 and propagates this to our listener iff this was a state change. 1455 """ 1456 1457 # Note: Some callers check _cached_byte_size_dirty before calling 1458 # _Modified() as an extra optimization. So, if this method is ever 1459 # changed such that it does stuff even when _cached_byte_size_dirty is 1460 # already true, the callers need to be updated. 1461 if not self._cached_byte_size_dirty: 1462 self._cached_byte_size_dirty = True 1463 self._listener_for_children.dirty = True 1464 self._is_present_in_parent = True 1465 self._listener.Modified()
Sets the _cached_byte_size_dirty bit to true, and propagates this to our listener iff this was a state change.
1213 def IsInitialized(self, errors=None): 1214 """Checks if all required fields of a message are set. 1215 1216 Args: 1217 errors: A list which, if provided, will be populated with the field 1218 paths of all missing required fields. 1219 1220 Returns: 1221 True iff the specified message has all required fields set. 1222 """ 1223 1224 # Performance is critical so we avoid HasField() and ListFields(). 1225 1226 for field in required_fields: 1227 if (field not in self._fields or 1228 (field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE and 1229 not self._fields[field]._is_present_in_parent)): 1230 if errors is not None: 1231 errors.extend(self.FindInitializationErrors()) 1232 return False 1233 1234 for field, value in list(self._fields.items()): # dict can change size! 1235 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 1236 if field.label == _FieldDescriptor.LABEL_REPEATED: 1237 if (field.message_type._is_map_entry): 1238 continue 1239 for element in value: 1240 if not element.IsInitialized(): 1241 if errors is not None: 1242 errors.extend(self.FindInitializationErrors()) 1243 return False 1244 elif value._is_present_in_parent and not value.IsInitialized(): 1245 if errors is not None: 1246 errors.extend(self.FindInitializationErrors()) 1247 return False 1248 1249 return True
Checks if all required fields of a message are set.
Arguments:
- errors: A list which, if provided, will be populated with the field paths of all missing required fields.
Returns:
True iff the specified message has all required fields set.
1119 def MergeFromString(self, serialized): 1120 serialized = memoryview(serialized) 1121 length = len(serialized) 1122 try: 1123 if self._InternalParse(serialized, 0, length) != length: 1124 # The only reason _InternalParse would return early is if it 1125 # encountered an end-group tag. 1126 raise message_mod.DecodeError('Unexpected end-group tag.') 1127 except (IndexError, TypeError): 1128 # Now ord(buf[p:p+1]) == ord('') gets TypeError. 1129 raise message_mod.DecodeError('Truncated message.') 1130 except struct.error as e: 1131 raise message_mod.DecodeError(e) 1132 return length # Return this for legacy reasons.
Merges serialized protocol buffer data into this message.
When we find a field in serialized
that is already present
in this message:
- If it's a "repeated" field, we append to the end of our list.
- Else, if it's a scalar, we overwrite our field.
- Else, (it's a nonrepeated composite), we recursively merge into the existing composite.
Arguments:
- serialized (bytes): Any object that allows us to call
memoryview(serialized)
to access a string of bytes using the buffer interface.
Returns:
int: The number of bytes read from
serialized
. For non-group messages, this will always belen(serialized)
, but for messages which are actually groups, this will generally be less thanlen(serialized)
, since we must stop when we reach anEND_GROUP
tag. Note that if we do stop because of anEND_GROUP
tag, the number of bytes returned does not include the bytes for theEND_GROUP
tag information.
Raises:
- DecodeError: if the input cannot be parsed.
1072 def SerializeToString(self, **kwargs): 1073 # Check if the message has all of its required fields set. 1074 if not self.IsInitialized(): 1075 raise message_mod.EncodeError( 1076 'Message %s is missing required fields: %s' % ( 1077 self.DESCRIPTOR.full_name, ','.join(self.FindInitializationErrors()))) 1078 return self.SerializePartialToString(**kwargs)
Serializes the protocol message to a binary string.
Keyword Args:
deterministic (bool): If true, requests deterministic serialization of the protobuf, with predictable ordering of map keys.
Returns:
A binary string representation of the message if all of the required fields in the message are set (i.e. the message is initialized).
Raises:
- EncodeError: if the message isn't initialized (see
IsInitialized()
).
1085 def SerializePartialToString(self, **kwargs): 1086 out = BytesIO() 1087 self._InternalSerialize(out.write, **kwargs) 1088 return out.getvalue()
Serializes the protocol message to a binary string.
This method is similar to SerializeToString but doesn't check if the message is initialized.
Keyword Args:
deterministic (bool): If true, requests deterministic serialization of the protobuf, with predictable ordering of map keys.
Returns:
bytes: A serialized representation of the partial message.
814 def ListFields(self): 815 all_fields = [item for item in self._fields.items() if _IsPresent(item)] 816 all_fields.sort(key = lambda item: item[0].number) 817 return all_fields
Returns a list of (FieldDescriptor, value) tuples for present fields.
A message field is non-empty if HasField() would return true. A singular primitive field is non-empty if HasField() would return true in proto2 or it is non zero in proto3. A repeated field is non-empty if it contains at least one element. The fields are ordered by field number.
Returns:
list[tuple(FieldDescriptor, value)]: field descriptors and values for all fields in the message which are not empty. The values vary by field type.
838 def HasField(self, field_name): 839 try: 840 field = hassable_fields[field_name] 841 except KeyError as exc: 842 raise ValueError('Protocol message %s has no non-repeated field "%s" ' 843 'nor has presence is not available for this field.' % ( 844 message_descriptor.full_name, field_name)) from exc 845 846 if isinstance(field, descriptor_mod.OneofDescriptor): 847 try: 848 return HasField(self, self._oneofs[field].name) 849 except KeyError: 850 return False 851 else: 852 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 853 value = self._fields.get(field) 854 return value is not None and value._is_present_in_parent 855 else: 856 return field in self._fields
Checks if a certain field is set for the message.
For a oneof group, checks if any field inside is set. Note that if the
field_name is not defined in the message descriptor, ValueError
will
be raised.
Arguments:
- field_name (str): The name of the field to check for presence.
Returns:
bool: Whether a value has been set for the named field.
Raises:
- ValueError: if the
field_name
is not a member of this message.
863 def ClearField(self, field_name): 864 try: 865 field = message_descriptor.fields_by_name[field_name] 866 except KeyError: 867 try: 868 field = message_descriptor.oneofs_by_name[field_name] 869 if field in self._oneofs: 870 field = self._oneofs[field] 871 else: 872 return 873 except KeyError: 874 raise ValueError('Protocol message %s has no "%s" field.' % 875 (message_descriptor.name, field_name)) 876 877 if field in self._fields: 878 # To match the C++ implementation, we need to invalidate iterators 879 # for map fields when ClearField() happens. 880 if hasattr(self._fields[field], 'InvalidateIterators'): 881 self._fields[field].InvalidateIterators() 882 883 # Note: If the field is a sub-message, its listener will still point 884 # at us. That's fine, because the worst than can happen is that it 885 # will call _Modified() and invalidate our byte size. Big deal. 886 del self._fields[field] 887 888 if self._oneofs.get(field.containing_oneof, None) is field: 889 del self._oneofs[field.containing_oneof] 890 891 # Always call _Modified() -- even if nothing was changed, this is 892 # a mutating method, and thus calling it should cause the field to become 893 # present in the parent message. 894 self._Modified()
Clears the contents of a given field.
Inside a oneof group, clears the field set. If the name neither refers to a
defined field or oneof group, ValueError
is raised.
Arguments:
- field_name (str): The name of the field to check for presence.
Raises:
- ValueError: if the
field_name
is not a member of this message.
1358 def WhichOneof(self, oneof_name): 1359 """Returns the name of the currently set field inside a oneof, or None.""" 1360 try: 1361 field = message_descriptor.oneofs_by_name[oneof_name] 1362 except KeyError: 1363 raise ValueError( 1364 'Protocol message has no oneof "%s" field.' % oneof_name) 1365 1366 nested_field = self._oneofs.get(field, None) 1367 if nested_field is not None and self.HasField(nested_field.name): 1368 return nested_field.name 1369 else: 1370 return None
Returns the name of the currently set field inside a oneof, or None.
1388def _UnknownFields(self): 1389 warnings.warn( 1390 'message.UnknownFields() is deprecated. Please use the add one ' 1391 'feature unknown_fields.UnknownFieldSet(message) in ' 1392 'unknown_fields.py instead.' 1393 ) 1394 if self._unknown_field_set is None: # pylint: disable=protected-access 1395 # pylint: disable=protected-access 1396 self._unknown_field_set = containers.UnknownFieldSet() 1397 return self._unknown_field_set # pylint: disable=protected-access
Returns the UnknownFieldSet.
Returns:
UnknownFieldSet: The unknown fields stored in this message.
1400def _DiscardUnknownFields(self): 1401 self._unknown_fields = [] 1402 self._unknown_field_set = None # pylint: disable=protected-access 1403 for field, value in self.ListFields(): 1404 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 1405 if _IsMapField(field): 1406 if _IsMessageMapField(field): 1407 for key in value: 1408 value[key].DiscardUnknownFields() 1409 elif field.label == _FieldDescriptor.LABEL_REPEATED: 1410 for sub_message in value: 1411 sub_message.DiscardUnknownFields() 1412 else: 1413 value.DiscardUnknownFields()
Clears all fields in the UnknownFieldSet
.
This operation is recursive for nested message.
1040 def ByteSize(self): 1041 if not self._cached_byte_size_dirty: 1042 return self._cached_byte_size 1043 1044 size = 0 1045 descriptor = self.DESCRIPTOR 1046 if descriptor._is_map_entry: 1047 # Fields of map entry should always be serialized. 1048 key_field = descriptor.fields_by_name['key'] 1049 _MaybeAddEncoder(cls, key_field) 1050 size = key_field._sizer(self.key) 1051 value_field = descriptor.fields_by_name['value'] 1052 _MaybeAddEncoder(cls, value_field) 1053 size += value_field._sizer(self.value) 1054 else: 1055 for field_descriptor, field_value in self.ListFields(): 1056 _MaybeAddEncoder(cls, field_descriptor) 1057 size += field_descriptor._sizer(field_value) 1058 for tag_bytes, value_bytes in self._unknown_fields: 1059 size += len(tag_bytes) + len(value_bytes) 1060 1061 self._cached_byte_size = size 1062 self._cached_byte_size_dirty = False 1063 self._listener_for_children.dirty = False 1064 return size
Returns the serialized size of this message.
Recursively calls ByteSize() on all contained messages.
Returns:
int: The number of bytes required to serialize this message.
784 def RegisterExtension(field_descriptor): 785 field_descriptor.containing_type = cls.DESCRIPTOR 786 # TODO: Use cls.MESSAGE_FACTORY.pool when available. 787 # pylint: disable=protected-access 788 cls.DESCRIPTOR.file.pool._AddExtensionDescriptor(field_descriptor) 789 _AttachFieldHelpers(cls, field_descriptor)
642 def getter(self): 643 field_value = self._fields.get(field) 644 if field_value is None: 645 # Construct a new object to represent this field. 646 field_value = field._default_constructor(self) 647 648 # Atomically check if another thread has preempted us and, if not, swap 649 # in the new object we just created. If someone has preempted us, we 650 # take that object and discard ours. 651 # WARNING: We are relying on setdefault() being atomic. This is true 652 # in CPython but we haven't investigated others. This warning appears 653 # in several other locations in this file. 654 field_value = self._fields.setdefault(field, field_value) 655 return field_value
Getter for f_direct.
642 def getter(self): 643 field_value = self._fields.get(field) 644 if field_value is None: 645 # Construct a new object to represent this field. 646 field_value = field._default_constructor(self) 647 648 # Atomically check if another thread has preempted us and, if not, swap 649 # in the new object we just created. If someone has preempted us, we 650 # take that object and discard ours. 651 # WARNING: We are relying on setdefault() being atomic. This is true 652 # in CPython but we haven't investigated others. This warning appears 653 # in several other locations in this file. 654 field_value = self._fields.setdefault(field, field_value) 655 return field_value
Getter for f_inverse.
1253 def FindInitializationErrors(self): 1254 """Finds required fields which are not initialized. 1255 1256 Returns: 1257 A list of strings. Each string is a path to an uninitialized field from 1258 the top-level message, e.g. "foo.bar[5].baz". 1259 """ 1260 1261 errors = [] # simplify things 1262 1263 for field in required_fields: 1264 if not self.HasField(field.name): 1265 errors.append(field.name) 1266 1267 for field, value in self.ListFields(): 1268 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 1269 if field.is_extension: 1270 name = '(%s)' % field.full_name 1271 else: 1272 name = field.name 1273 1274 if _IsMapField(field): 1275 if _IsMessageMapField(field): 1276 for key in value: 1277 element = value[key] 1278 prefix = '%s[%s].' % (name, key) 1279 sub_errors = element.FindInitializationErrors() 1280 errors += [prefix + error for error in sub_errors] 1281 else: 1282 # ScalarMaps can't have any initialization errors. 1283 pass 1284 elif field.label == _FieldDescriptor.LABEL_REPEATED: 1285 for i in range(len(value)): 1286 element = value[i] 1287 prefix = '%s[%d].' % (name, i) 1288 sub_errors = element.FindInitializationErrors() 1289 errors += [prefix + error for error in sub_errors] 1290 else: 1291 prefix = name + '.' 1292 sub_errors = value.FindInitializationErrors() 1293 errors += [prefix + error for error in sub_errors] 1294 1295 return errors
Finds required fields which are not initialized.
Returns:
A list of strings. Each string is a path to an uninitialized field from the top-level message, e.g. "foo.bar[5].baz".
Inherited Members
- google.protobuf.message.Message
- CopyFrom
- ParseFromString
- HasExtension
- ClearExtension
Abstract base class for protocol messages.
Protocol message classes are almost always generated by the protocol compiler. These generated types subclass Message and implement the methods shown below.
496 def init(self, **kwargs): 497 self._cached_byte_size = 0 498 self._cached_byte_size_dirty = len(kwargs) > 0 499 self._fields = {} 500 # Contains a mapping from oneof field descriptors to the descriptor 501 # of the currently set field in that oneof field. 502 self._oneofs = {} 503 504 # _unknown_fields is () when empty for efficiency, and will be turned into 505 # a list if fields are added. 506 self._unknown_fields = () 507 # _unknown_field_set is None when empty for efficiency, and will be 508 # turned into UnknownFieldSet struct if fields are added. 509 self._unknown_field_set = None # pylint: disable=protected-access 510 self._is_present_in_parent = False 511 self._listener = message_listener_mod.NullMessageListener() 512 self._listener_for_children = _Listener(self) 513 for field_name, field_value in kwargs.items(): 514 field = _GetFieldByName(message_descriptor, field_name) 515 if field is None: 516 raise TypeError('%s() got an unexpected keyword argument "%s"' % 517 (message_descriptor.name, field_name)) 518 if field_value is None: 519 # field=None is the same as no field at all. 520 continue 521 if field.label == _FieldDescriptor.LABEL_REPEATED: 522 copy = field._default_constructor(self) 523 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: # Composite 524 if _IsMapField(field): 525 if _IsMessageMapField(field): 526 for key in field_value: 527 copy[key].MergeFrom(field_value[key]) 528 else: 529 copy.update(field_value) 530 else: 531 for val in field_value: 532 if isinstance(val, dict): 533 copy.add(**val) 534 else: 535 copy.add().MergeFrom(val) 536 else: # Scalar 537 if field.cpp_type == _FieldDescriptor.CPPTYPE_ENUM: 538 field_value = [_GetIntegerEnumValue(field.enum_type, val) 539 for val in field_value] 540 copy.extend(field_value) 541 self._fields[field] = copy 542 elif field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 543 copy = field._default_constructor(self) 544 new_val = field_value 545 if isinstance(field_value, dict): 546 new_val = field.message_type._concrete_class(**field_value) 547 try: 548 copy.MergeFrom(new_val) 549 except TypeError: 550 _ReraiseTypeErrorWithFieldName(message_descriptor.name, field_name) 551 self._fields[field] = copy 552 else: 553 if field.cpp_type == _FieldDescriptor.CPPTYPE_ENUM: 554 field_value = _GetIntegerEnumValue(field.enum_type, field_value) 555 try: 556 setattr(self, field_name, field_value) 557 except TypeError: 558 _ReraiseTypeErrorWithFieldName(message_descriptor.name, field_name)
1312 def MergeFrom(self, msg): 1313 if not isinstance(msg, cls): 1314 raise TypeError( 1315 'Parameter to MergeFrom() must be instance of same class: ' 1316 'expected %s got %s.' % (_FullyQualifiedClassName(cls), 1317 _FullyQualifiedClassName(msg.__class__))) 1318 1319 assert msg is not self 1320 self._Modified() 1321 1322 fields = self._fields 1323 1324 for field, value in msg._fields.items(): 1325 if field.label == LABEL_REPEATED: 1326 field_value = fields.get(field) 1327 if field_value is None: 1328 # Construct a new object to represent this field. 1329 field_value = field._default_constructor(self) 1330 fields[field] = field_value 1331 field_value.MergeFrom(value) 1332 elif field.cpp_type == CPPTYPE_MESSAGE: 1333 if value._is_present_in_parent: 1334 field_value = fields.get(field) 1335 if field_value is None: 1336 # Construct a new object to represent this field. 1337 field_value = field._default_constructor(self) 1338 fields[field] = field_value 1339 field_value.MergeFrom(value) 1340 else: 1341 self._fields[field] = value 1342 if field.containing_oneof: 1343 self._UpdateOneofState(field) 1344 1345 if msg._unknown_fields: 1346 if not self._unknown_fields: 1347 self._unknown_fields = [] 1348 self._unknown_fields.extend(msg._unknown_fields) 1349 # pylint: disable=protected-access 1350 if self._unknown_field_set is None: 1351 self._unknown_field_set = containers.UnknownFieldSet() 1352 self._unknown_field_set._extend(msg._unknown_field_set)
Merges the contents of the specified message into current message.
This method merges the contents of the specified message into the current message. Singular fields that are set in the specified message overwrite the corresponding fields in the current message. Repeated fields are appended. Singular sub-messages and groups are recursively merged.
Arguments:
- other_msg (Message): A message to merge into the current message.
1375def _Clear(self): 1376 # Clear fields. 1377 self._fields = {} 1378 self._unknown_fields = () 1379 # pylint: disable=protected-access 1380 if self._unknown_field_set is not None: 1381 self._unknown_field_set._clear() 1382 self._unknown_field_set = None 1383 1384 self._oneofs = {} 1385 self._Modified()
Clears all data that was set in the message.
1452 def Modified(self): 1453 """Sets the _cached_byte_size_dirty bit to true, 1454 and propagates this to our listener iff this was a state change. 1455 """ 1456 1457 # Note: Some callers check _cached_byte_size_dirty before calling 1458 # _Modified() as an extra optimization. So, if this method is ever 1459 # changed such that it does stuff even when _cached_byte_size_dirty is 1460 # already true, the callers need to be updated. 1461 if not self._cached_byte_size_dirty: 1462 self._cached_byte_size_dirty = True 1463 self._listener_for_children.dirty = True 1464 self._is_present_in_parent = True 1465 self._listener.Modified()
Sets the _cached_byte_size_dirty bit to true, and propagates this to our listener iff this was a state change.
1213 def IsInitialized(self, errors=None): 1214 """Checks if all required fields of a message are set. 1215 1216 Args: 1217 errors: A list which, if provided, will be populated with the field 1218 paths of all missing required fields. 1219 1220 Returns: 1221 True iff the specified message has all required fields set. 1222 """ 1223 1224 # Performance is critical so we avoid HasField() and ListFields(). 1225 1226 for field in required_fields: 1227 if (field not in self._fields or 1228 (field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE and 1229 not self._fields[field]._is_present_in_parent)): 1230 if errors is not None: 1231 errors.extend(self.FindInitializationErrors()) 1232 return False 1233 1234 for field, value in list(self._fields.items()): # dict can change size! 1235 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 1236 if field.label == _FieldDescriptor.LABEL_REPEATED: 1237 if (field.message_type._is_map_entry): 1238 continue 1239 for element in value: 1240 if not element.IsInitialized(): 1241 if errors is not None: 1242 errors.extend(self.FindInitializationErrors()) 1243 return False 1244 elif value._is_present_in_parent and not value.IsInitialized(): 1245 if errors is not None: 1246 errors.extend(self.FindInitializationErrors()) 1247 return False 1248 1249 return True
Checks if all required fields of a message are set.
Arguments:
- errors: A list which, if provided, will be populated with the field paths of all missing required fields.
Returns:
True iff the specified message has all required fields set.
1119 def MergeFromString(self, serialized): 1120 serialized = memoryview(serialized) 1121 length = len(serialized) 1122 try: 1123 if self._InternalParse(serialized, 0, length) != length: 1124 # The only reason _InternalParse would return early is if it 1125 # encountered an end-group tag. 1126 raise message_mod.DecodeError('Unexpected end-group tag.') 1127 except (IndexError, TypeError): 1128 # Now ord(buf[p:p+1]) == ord('') gets TypeError. 1129 raise message_mod.DecodeError('Truncated message.') 1130 except struct.error as e: 1131 raise message_mod.DecodeError(e) 1132 return length # Return this for legacy reasons.
Merges serialized protocol buffer data into this message.
When we find a field in serialized
that is already present
in this message:
- If it's a "repeated" field, we append to the end of our list.
- Else, if it's a scalar, we overwrite our field.
- Else, (it's a nonrepeated composite), we recursively merge into the existing composite.
Arguments:
- serialized (bytes): Any object that allows us to call
memoryview(serialized)
to access a string of bytes using the buffer interface.
Returns:
int: The number of bytes read from
serialized
. For non-group messages, this will always belen(serialized)
, but for messages which are actually groups, this will generally be less thanlen(serialized)
, since we must stop when we reach anEND_GROUP
tag. Note that if we do stop because of anEND_GROUP
tag, the number of bytes returned does not include the bytes for theEND_GROUP
tag information.
Raises:
- DecodeError: if the input cannot be parsed.
1072 def SerializeToString(self, **kwargs): 1073 # Check if the message has all of its required fields set. 1074 if not self.IsInitialized(): 1075 raise message_mod.EncodeError( 1076 'Message %s is missing required fields: %s' % ( 1077 self.DESCRIPTOR.full_name, ','.join(self.FindInitializationErrors()))) 1078 return self.SerializePartialToString(**kwargs)
Serializes the protocol message to a binary string.
Keyword Args:
deterministic (bool): If true, requests deterministic serialization of the protobuf, with predictable ordering of map keys.
Returns:
A binary string representation of the message if all of the required fields in the message are set (i.e. the message is initialized).
Raises:
- EncodeError: if the message isn't initialized (see
IsInitialized()
).
1085 def SerializePartialToString(self, **kwargs): 1086 out = BytesIO() 1087 self._InternalSerialize(out.write, **kwargs) 1088 return out.getvalue()
Serializes the protocol message to a binary string.
This method is similar to SerializeToString but doesn't check if the message is initialized.
Keyword Args:
deterministic (bool): If true, requests deterministic serialization of the protobuf, with predictable ordering of map keys.
Returns:
bytes: A serialized representation of the partial message.
814 def ListFields(self): 815 all_fields = [item for item in self._fields.items() if _IsPresent(item)] 816 all_fields.sort(key = lambda item: item[0].number) 817 return all_fields
Returns a list of (FieldDescriptor, value) tuples for present fields.
A message field is non-empty if HasField() would return true. A singular primitive field is non-empty if HasField() would return true in proto2 or it is non zero in proto3. A repeated field is non-empty if it contains at least one element. The fields are ordered by field number.
Returns:
list[tuple(FieldDescriptor, value)]: field descriptors and values for all fields in the message which are not empty. The values vary by field type.
838 def HasField(self, field_name): 839 try: 840 field = hassable_fields[field_name] 841 except KeyError as exc: 842 raise ValueError('Protocol message %s has no non-repeated field "%s" ' 843 'nor has presence is not available for this field.' % ( 844 message_descriptor.full_name, field_name)) from exc 845 846 if isinstance(field, descriptor_mod.OneofDescriptor): 847 try: 848 return HasField(self, self._oneofs[field].name) 849 except KeyError: 850 return False 851 else: 852 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 853 value = self._fields.get(field) 854 return value is not None and value._is_present_in_parent 855 else: 856 return field in self._fields
Checks if a certain field is set for the message.
For a oneof group, checks if any field inside is set. Note that if the
field_name is not defined in the message descriptor, ValueError
will
be raised.
Arguments:
- field_name (str): The name of the field to check for presence.
Returns:
bool: Whether a value has been set for the named field.
Raises:
- ValueError: if the
field_name
is not a member of this message.
863 def ClearField(self, field_name): 864 try: 865 field = message_descriptor.fields_by_name[field_name] 866 except KeyError: 867 try: 868 field = message_descriptor.oneofs_by_name[field_name] 869 if field in self._oneofs: 870 field = self._oneofs[field] 871 else: 872 return 873 except KeyError: 874 raise ValueError('Protocol message %s has no "%s" field.' % 875 (message_descriptor.name, field_name)) 876 877 if field in self._fields: 878 # To match the C++ implementation, we need to invalidate iterators 879 # for map fields when ClearField() happens. 880 if hasattr(self._fields[field], 'InvalidateIterators'): 881 self._fields[field].InvalidateIterators() 882 883 # Note: If the field is a sub-message, its listener will still point 884 # at us. That's fine, because the worst than can happen is that it 885 # will call _Modified() and invalidate our byte size. Big deal. 886 del self._fields[field] 887 888 if self._oneofs.get(field.containing_oneof, None) is field: 889 del self._oneofs[field.containing_oneof] 890 891 # Always call _Modified() -- even if nothing was changed, this is 892 # a mutating method, and thus calling it should cause the field to become 893 # present in the parent message. 894 self._Modified()
Clears the contents of a given field.
Inside a oneof group, clears the field set. If the name neither refers to a
defined field or oneof group, ValueError
is raised.
Arguments:
- field_name (str): The name of the field to check for presence.
Raises:
- ValueError: if the
field_name
is not a member of this message.
1358 def WhichOneof(self, oneof_name): 1359 """Returns the name of the currently set field inside a oneof, or None.""" 1360 try: 1361 field = message_descriptor.oneofs_by_name[oneof_name] 1362 except KeyError: 1363 raise ValueError( 1364 'Protocol message has no oneof "%s" field.' % oneof_name) 1365 1366 nested_field = self._oneofs.get(field, None) 1367 if nested_field is not None and self.HasField(nested_field.name): 1368 return nested_field.name 1369 else: 1370 return None
Returns the name of the currently set field inside a oneof, or None.
1388def _UnknownFields(self): 1389 warnings.warn( 1390 'message.UnknownFields() is deprecated. Please use the add one ' 1391 'feature unknown_fields.UnknownFieldSet(message) in ' 1392 'unknown_fields.py instead.' 1393 ) 1394 if self._unknown_field_set is None: # pylint: disable=protected-access 1395 # pylint: disable=protected-access 1396 self._unknown_field_set = containers.UnknownFieldSet() 1397 return self._unknown_field_set # pylint: disable=protected-access
Returns the UnknownFieldSet.
Returns:
UnknownFieldSet: The unknown fields stored in this message.
1400def _DiscardUnknownFields(self): 1401 self._unknown_fields = [] 1402 self._unknown_field_set = None # pylint: disable=protected-access 1403 for field, value in self.ListFields(): 1404 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 1405 if _IsMapField(field): 1406 if _IsMessageMapField(field): 1407 for key in value: 1408 value[key].DiscardUnknownFields() 1409 elif field.label == _FieldDescriptor.LABEL_REPEATED: 1410 for sub_message in value: 1411 sub_message.DiscardUnknownFields() 1412 else: 1413 value.DiscardUnknownFields()
Clears all fields in the UnknownFieldSet
.
This operation is recursive for nested message.
1040 def ByteSize(self): 1041 if not self._cached_byte_size_dirty: 1042 return self._cached_byte_size 1043 1044 size = 0 1045 descriptor = self.DESCRIPTOR 1046 if descriptor._is_map_entry: 1047 # Fields of map entry should always be serialized. 1048 key_field = descriptor.fields_by_name['key'] 1049 _MaybeAddEncoder(cls, key_field) 1050 size = key_field._sizer(self.key) 1051 value_field = descriptor.fields_by_name['value'] 1052 _MaybeAddEncoder(cls, value_field) 1053 size += value_field._sizer(self.value) 1054 else: 1055 for field_descriptor, field_value in self.ListFields(): 1056 _MaybeAddEncoder(cls, field_descriptor) 1057 size += field_descriptor._sizer(field_value) 1058 for tag_bytes, value_bytes in self._unknown_fields: 1059 size += len(tag_bytes) + len(value_bytes) 1060 1061 self._cached_byte_size = size 1062 self._cached_byte_size_dirty = False 1063 self._listener_for_children.dirty = False 1064 return size
Returns the serialized size of this message.
Recursively calls ByteSize() on all contained messages.
Returns:
int: The number of bytes required to serialize this message.
784 def RegisterExtension(field_descriptor): 785 field_descriptor.containing_type = cls.DESCRIPTOR 786 # TODO: Use cls.MESSAGE_FACTORY.pool when available. 787 # pylint: disable=protected-access 788 cls.DESCRIPTOR.file.pool._AddExtensionDescriptor(field_descriptor) 789 _AttachFieldHelpers(cls, field_descriptor)
685 def getter(self): 686 # TODO: This may be broken since there may not be 687 # default_value. Combine with has_default_value somehow. 688 return self._fields.get(field, default_value)
Getter for starting_state.
642 def getter(self): 643 field_value = self._fields.get(field) 644 if field_value is None: 645 # Construct a new object to represent this field. 646 field_value = field._default_constructor(self) 647 648 # Atomically check if another thread has preempted us and, if not, swap 649 # in the new object we just created. If someone has preempted us, we 650 # take that object and discard ours. 651 # WARNING: We are relying on setdefault() being atomic. This is true 652 # in CPython but we haven't investigated others. This warning appears 653 # in several other locations in this file. 654 field_value = self._fields.setdefault(field, field_value) 655 return field_value
Getter for final_states.
642 def getter(self): 643 field_value = self._fields.get(field) 644 if field_value is None: 645 # Construct a new object to represent this field. 646 field_value = field._default_constructor(self) 647 648 # Atomically check if another thread has preempted us and, if not, swap 649 # in the new object we just created. If someone has preempted us, we 650 # take that object and discard ours. 651 # WARNING: We are relying on setdefault() being atomic. This is true 652 # in CPython but we haven't investigated others. This warning appears 653 # in several other locations in this file. 654 field_value = self._fields.setdefault(field, field_value) 655 return field_value
Getter for transition_tail.
642 def getter(self): 643 field_value = self._fields.get(field) 644 if field_value is None: 645 # Construct a new object to represent this field. 646 field_value = field._default_constructor(self) 647 648 # Atomically check if another thread has preempted us and, if not, swap 649 # in the new object we just created. If someone has preempted us, we 650 # take that object and discard ours. 651 # WARNING: We are relying on setdefault() being atomic. This is true 652 # in CPython but we haven't investigated others. This warning appears 653 # in several other locations in this file. 654 field_value = self._fields.setdefault(field, field_value) 655 return field_value
Getter for transition_head.
642 def getter(self): 643 field_value = self._fields.get(field) 644 if field_value is None: 645 # Construct a new object to represent this field. 646 field_value = field._default_constructor(self) 647 648 # Atomically check if another thread has preempted us and, if not, swap 649 # in the new object we just created. If someone has preempted us, we 650 # take that object and discard ours. 651 # WARNING: We are relying on setdefault() being atomic. This is true 652 # in CPython but we haven't investigated others. This warning appears 653 # in several other locations in this file. 654 field_value = self._fields.setdefault(field, field_value) 655 return field_value
Getter for transition_label.
642 def getter(self): 643 field_value = self._fields.get(field) 644 if field_value is None: 645 # Construct a new object to represent this field. 646 field_value = field._default_constructor(self) 647 648 # Atomically check if another thread has preempted us and, if not, swap 649 # in the new object we just created. If someone has preempted us, we 650 # take that object and discard ours. 651 # WARNING: We are relying on setdefault() being atomic. This is true 652 # in CPython but we haven't investigated others. This warning appears 653 # in several other locations in this file. 654 field_value = self._fields.setdefault(field, field_value) 655 return field_value
Getter for vars.
1253 def FindInitializationErrors(self): 1254 """Finds required fields which are not initialized. 1255 1256 Returns: 1257 A list of strings. Each string is a path to an uninitialized field from 1258 the top-level message, e.g. "foo.bar[5].baz". 1259 """ 1260 1261 errors = [] # simplify things 1262 1263 for field in required_fields: 1264 if not self.HasField(field.name): 1265 errors.append(field.name) 1266 1267 for field, value in self.ListFields(): 1268 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 1269 if field.is_extension: 1270 name = '(%s)' % field.full_name 1271 else: 1272 name = field.name 1273 1274 if _IsMapField(field): 1275 if _IsMessageMapField(field): 1276 for key in value: 1277 element = value[key] 1278 prefix = '%s[%s].' % (name, key) 1279 sub_errors = element.FindInitializationErrors() 1280 errors += [prefix + error for error in sub_errors] 1281 else: 1282 # ScalarMaps can't have any initialization errors. 1283 pass 1284 elif field.label == _FieldDescriptor.LABEL_REPEATED: 1285 for i in range(len(value)): 1286 element = value[i] 1287 prefix = '%s[%d].' % (name, i) 1288 sub_errors = element.FindInitializationErrors() 1289 errors += [prefix + error for error in sub_errors] 1290 else: 1291 prefix = name + '.' 1292 sub_errors = value.FindInitializationErrors() 1293 errors += [prefix + error for error in sub_errors] 1294 1295 return errors
Finds required fields which are not initialized.
Returns:
A list of strings. Each string is a path to an uninitialized field from the top-level message, e.g. "foo.bar[5].baz".
Inherited Members
- google.protobuf.message.Message
- CopyFrom
- ParseFromString
- HasExtension
- ClearExtension
Abstract base class for protocol messages.
Protocol message classes are almost always generated by the protocol compiler. These generated types subclass Message and implement the methods shown below.
496 def init(self, **kwargs): 497 self._cached_byte_size = 0 498 self._cached_byte_size_dirty = len(kwargs) > 0 499 self._fields = {} 500 # Contains a mapping from oneof field descriptors to the descriptor 501 # of the currently set field in that oneof field. 502 self._oneofs = {} 503 504 # _unknown_fields is () when empty for efficiency, and will be turned into 505 # a list if fields are added. 506 self._unknown_fields = () 507 # _unknown_field_set is None when empty for efficiency, and will be 508 # turned into UnknownFieldSet struct if fields are added. 509 self._unknown_field_set = None # pylint: disable=protected-access 510 self._is_present_in_parent = False 511 self._listener = message_listener_mod.NullMessageListener() 512 self._listener_for_children = _Listener(self) 513 for field_name, field_value in kwargs.items(): 514 field = _GetFieldByName(message_descriptor, field_name) 515 if field is None: 516 raise TypeError('%s() got an unexpected keyword argument "%s"' % 517 (message_descriptor.name, field_name)) 518 if field_value is None: 519 # field=None is the same as no field at all. 520 continue 521 if field.label == _FieldDescriptor.LABEL_REPEATED: 522 copy = field._default_constructor(self) 523 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: # Composite 524 if _IsMapField(field): 525 if _IsMessageMapField(field): 526 for key in field_value: 527 copy[key].MergeFrom(field_value[key]) 528 else: 529 copy.update(field_value) 530 else: 531 for val in field_value: 532 if isinstance(val, dict): 533 copy.add(**val) 534 else: 535 copy.add().MergeFrom(val) 536 else: # Scalar 537 if field.cpp_type == _FieldDescriptor.CPPTYPE_ENUM: 538 field_value = [_GetIntegerEnumValue(field.enum_type, val) 539 for val in field_value] 540 copy.extend(field_value) 541 self._fields[field] = copy 542 elif field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 543 copy = field._default_constructor(self) 544 new_val = field_value 545 if isinstance(field_value, dict): 546 new_val = field.message_type._concrete_class(**field_value) 547 try: 548 copy.MergeFrom(new_val) 549 except TypeError: 550 _ReraiseTypeErrorWithFieldName(message_descriptor.name, field_name) 551 self._fields[field] = copy 552 else: 553 if field.cpp_type == _FieldDescriptor.CPPTYPE_ENUM: 554 field_value = _GetIntegerEnumValue(field.enum_type, field_value) 555 try: 556 setattr(self, field_name, field_value) 557 except TypeError: 558 _ReraiseTypeErrorWithFieldName(message_descriptor.name, field_name)
1312 def MergeFrom(self, msg): 1313 if not isinstance(msg, cls): 1314 raise TypeError( 1315 'Parameter to MergeFrom() must be instance of same class: ' 1316 'expected %s got %s.' % (_FullyQualifiedClassName(cls), 1317 _FullyQualifiedClassName(msg.__class__))) 1318 1319 assert msg is not self 1320 self._Modified() 1321 1322 fields = self._fields 1323 1324 for field, value in msg._fields.items(): 1325 if field.label == LABEL_REPEATED: 1326 field_value = fields.get(field) 1327 if field_value is None: 1328 # Construct a new object to represent this field. 1329 field_value = field._default_constructor(self) 1330 fields[field] = field_value 1331 field_value.MergeFrom(value) 1332 elif field.cpp_type == CPPTYPE_MESSAGE: 1333 if value._is_present_in_parent: 1334 field_value = fields.get(field) 1335 if field_value is None: 1336 # Construct a new object to represent this field. 1337 field_value = field._default_constructor(self) 1338 fields[field] = field_value 1339 field_value.MergeFrom(value) 1340 else: 1341 self._fields[field] = value 1342 if field.containing_oneof: 1343 self._UpdateOneofState(field) 1344 1345 if msg._unknown_fields: 1346 if not self._unknown_fields: 1347 self._unknown_fields = [] 1348 self._unknown_fields.extend(msg._unknown_fields) 1349 # pylint: disable=protected-access 1350 if self._unknown_field_set is None: 1351 self._unknown_field_set = containers.UnknownFieldSet() 1352 self._unknown_field_set._extend(msg._unknown_field_set)
Merges the contents of the specified message into current message.
This method merges the contents of the specified message into the current message. Singular fields that are set in the specified message overwrite the corresponding fields in the current message. Repeated fields are appended. Singular sub-messages and groups are recursively merged.
Arguments:
- other_msg (Message): A message to merge into the current message.
1375def _Clear(self): 1376 # Clear fields. 1377 self._fields = {} 1378 self._unknown_fields = () 1379 # pylint: disable=protected-access 1380 if self._unknown_field_set is not None: 1381 self._unknown_field_set._clear() 1382 self._unknown_field_set = None 1383 1384 self._oneofs = {} 1385 self._Modified()
Clears all data that was set in the message.
1452 def Modified(self): 1453 """Sets the _cached_byte_size_dirty bit to true, 1454 and propagates this to our listener iff this was a state change. 1455 """ 1456 1457 # Note: Some callers check _cached_byte_size_dirty before calling 1458 # _Modified() as an extra optimization. So, if this method is ever 1459 # changed such that it does stuff even when _cached_byte_size_dirty is 1460 # already true, the callers need to be updated. 1461 if not self._cached_byte_size_dirty: 1462 self._cached_byte_size_dirty = True 1463 self._listener_for_children.dirty = True 1464 self._is_present_in_parent = True 1465 self._listener.Modified()
Sets the _cached_byte_size_dirty bit to true, and propagates this to our listener iff this was a state change.
1213 def IsInitialized(self, errors=None): 1214 """Checks if all required fields of a message are set. 1215 1216 Args: 1217 errors: A list which, if provided, will be populated with the field 1218 paths of all missing required fields. 1219 1220 Returns: 1221 True iff the specified message has all required fields set. 1222 """ 1223 1224 # Performance is critical so we avoid HasField() and ListFields(). 1225 1226 for field in required_fields: 1227 if (field not in self._fields or 1228 (field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE and 1229 not self._fields[field]._is_present_in_parent)): 1230 if errors is not None: 1231 errors.extend(self.FindInitializationErrors()) 1232 return False 1233 1234 for field, value in list(self._fields.items()): # dict can change size! 1235 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 1236 if field.label == _FieldDescriptor.LABEL_REPEATED: 1237 if (field.message_type._is_map_entry): 1238 continue 1239 for element in value: 1240 if not element.IsInitialized(): 1241 if errors is not None: 1242 errors.extend(self.FindInitializationErrors()) 1243 return False 1244 elif value._is_present_in_parent and not value.IsInitialized(): 1245 if errors is not None: 1246 errors.extend(self.FindInitializationErrors()) 1247 return False 1248 1249 return True
Checks if all required fields of a message are set.
Arguments:
- errors: A list which, if provided, will be populated with the field paths of all missing required fields.
Returns:
True iff the specified message has all required fields set.
1119 def MergeFromString(self, serialized): 1120 serialized = memoryview(serialized) 1121 length = len(serialized) 1122 try: 1123 if self._InternalParse(serialized, 0, length) != length: 1124 # The only reason _InternalParse would return early is if it 1125 # encountered an end-group tag. 1126 raise message_mod.DecodeError('Unexpected end-group tag.') 1127 except (IndexError, TypeError): 1128 # Now ord(buf[p:p+1]) == ord('') gets TypeError. 1129 raise message_mod.DecodeError('Truncated message.') 1130 except struct.error as e: 1131 raise message_mod.DecodeError(e) 1132 return length # Return this for legacy reasons.
Merges serialized protocol buffer data into this message.
When we find a field in serialized
that is already present
in this message:
- If it's a "repeated" field, we append to the end of our list.
- Else, if it's a scalar, we overwrite our field.
- Else, (it's a nonrepeated composite), we recursively merge into the existing composite.
Arguments:
- serialized (bytes): Any object that allows us to call
memoryview(serialized)
to access a string of bytes using the buffer interface.
Returns:
int: The number of bytes read from
serialized
. For non-group messages, this will always belen(serialized)
, but for messages which are actually groups, this will generally be less thanlen(serialized)
, since we must stop when we reach anEND_GROUP
tag. Note that if we do stop because of anEND_GROUP
tag, the number of bytes returned does not include the bytes for theEND_GROUP
tag information.
Raises:
- DecodeError: if the input cannot be parsed.
1072 def SerializeToString(self, **kwargs): 1073 # Check if the message has all of its required fields set. 1074 if not self.IsInitialized(): 1075 raise message_mod.EncodeError( 1076 'Message %s is missing required fields: %s' % ( 1077 self.DESCRIPTOR.full_name, ','.join(self.FindInitializationErrors()))) 1078 return self.SerializePartialToString(**kwargs)
Serializes the protocol message to a binary string.
Keyword Args:
deterministic (bool): If true, requests deterministic serialization of the protobuf, with predictable ordering of map keys.
Returns:
A binary string representation of the message if all of the required fields in the message are set (i.e. the message is initialized).
Raises:
- EncodeError: if the message isn't initialized (see
IsInitialized()
).
1085 def SerializePartialToString(self, **kwargs): 1086 out = BytesIO() 1087 self._InternalSerialize(out.write, **kwargs) 1088 return out.getvalue()
Serializes the protocol message to a binary string.
This method is similar to SerializeToString but doesn't check if the message is initialized.
Keyword Args:
deterministic (bool): If true, requests deterministic serialization of the protobuf, with predictable ordering of map keys.
Returns:
bytes: A serialized representation of the partial message.
814 def ListFields(self): 815 all_fields = [item for item in self._fields.items() if _IsPresent(item)] 816 all_fields.sort(key = lambda item: item[0].number) 817 return all_fields
Returns a list of (FieldDescriptor, value) tuples for present fields.
A message field is non-empty if HasField() would return true. A singular primitive field is non-empty if HasField() would return true in proto2 or it is non zero in proto3. A repeated field is non-empty if it contains at least one element. The fields are ordered by field number.
Returns:
list[tuple(FieldDescriptor, value)]: field descriptors and values for all fields in the message which are not empty. The values vary by field type.
838 def HasField(self, field_name): 839 try: 840 field = hassable_fields[field_name] 841 except KeyError as exc: 842 raise ValueError('Protocol message %s has no non-repeated field "%s" ' 843 'nor has presence is not available for this field.' % ( 844 message_descriptor.full_name, field_name)) from exc 845 846 if isinstance(field, descriptor_mod.OneofDescriptor): 847 try: 848 return HasField(self, self._oneofs[field].name) 849 except KeyError: 850 return False 851 else: 852 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 853 value = self._fields.get(field) 854 return value is not None and value._is_present_in_parent 855 else: 856 return field in self._fields
Checks if a certain field is set for the message.
For a oneof group, checks if any field inside is set. Note that if the
field_name is not defined in the message descriptor, ValueError
will
be raised.
Arguments:
- field_name (str): The name of the field to check for presence.
Returns:
bool: Whether a value has been set for the named field.
Raises:
- ValueError: if the
field_name
is not a member of this message.
863 def ClearField(self, field_name): 864 try: 865 field = message_descriptor.fields_by_name[field_name] 866 except KeyError: 867 try: 868 field = message_descriptor.oneofs_by_name[field_name] 869 if field in self._oneofs: 870 field = self._oneofs[field] 871 else: 872 return 873 except KeyError: 874 raise ValueError('Protocol message %s has no "%s" field.' % 875 (message_descriptor.name, field_name)) 876 877 if field in self._fields: 878 # To match the C++ implementation, we need to invalidate iterators 879 # for map fields when ClearField() happens. 880 if hasattr(self._fields[field], 'InvalidateIterators'): 881 self._fields[field].InvalidateIterators() 882 883 # Note: If the field is a sub-message, its listener will still point 884 # at us. That's fine, because the worst than can happen is that it 885 # will call _Modified() and invalidate our byte size. Big deal. 886 del self._fields[field] 887 888 if self._oneofs.get(field.containing_oneof, None) is field: 889 del self._oneofs[field.containing_oneof] 890 891 # Always call _Modified() -- even if nothing was changed, this is 892 # a mutating method, and thus calling it should cause the field to become 893 # present in the parent message. 894 self._Modified()
Clears the contents of a given field.
Inside a oneof group, clears the field set. If the name neither refers to a
defined field or oneof group, ValueError
is raised.
Arguments:
- field_name (str): The name of the field to check for presence.
Raises:
- ValueError: if the
field_name
is not a member of this message.
1358 def WhichOneof(self, oneof_name): 1359 """Returns the name of the currently set field inside a oneof, or None.""" 1360 try: 1361 field = message_descriptor.oneofs_by_name[oneof_name] 1362 except KeyError: 1363 raise ValueError( 1364 'Protocol message has no oneof "%s" field.' % oneof_name) 1365 1366 nested_field = self._oneofs.get(field, None) 1367 if nested_field is not None and self.HasField(nested_field.name): 1368 return nested_field.name 1369 else: 1370 return None
Returns the name of the currently set field inside a oneof, or None.
1388def _UnknownFields(self): 1389 warnings.warn( 1390 'message.UnknownFields() is deprecated. Please use the add one ' 1391 'feature unknown_fields.UnknownFieldSet(message) in ' 1392 'unknown_fields.py instead.' 1393 ) 1394 if self._unknown_field_set is None: # pylint: disable=protected-access 1395 # pylint: disable=protected-access 1396 self._unknown_field_set = containers.UnknownFieldSet() 1397 return self._unknown_field_set # pylint: disable=protected-access
Returns the UnknownFieldSet.
Returns:
UnknownFieldSet: The unknown fields stored in this message.
1400def _DiscardUnknownFields(self): 1401 self._unknown_fields = [] 1402 self._unknown_field_set = None # pylint: disable=protected-access 1403 for field, value in self.ListFields(): 1404 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 1405 if _IsMapField(field): 1406 if _IsMessageMapField(field): 1407 for key in value: 1408 value[key].DiscardUnknownFields() 1409 elif field.label == _FieldDescriptor.LABEL_REPEATED: 1410 for sub_message in value: 1411 sub_message.DiscardUnknownFields() 1412 else: 1413 value.DiscardUnknownFields()
Clears all fields in the UnknownFieldSet
.
This operation is recursive for nested message.
1040 def ByteSize(self): 1041 if not self._cached_byte_size_dirty: 1042 return self._cached_byte_size 1043 1044 size = 0 1045 descriptor = self.DESCRIPTOR 1046 if descriptor._is_map_entry: 1047 # Fields of map entry should always be serialized. 1048 key_field = descriptor.fields_by_name['key'] 1049 _MaybeAddEncoder(cls, key_field) 1050 size = key_field._sizer(self.key) 1051 value_field = descriptor.fields_by_name['value'] 1052 _MaybeAddEncoder(cls, value_field) 1053 size += value_field._sizer(self.value) 1054 else: 1055 for field_descriptor, field_value in self.ListFields(): 1056 _MaybeAddEncoder(cls, field_descriptor) 1057 size += field_descriptor._sizer(field_value) 1058 for tag_bytes, value_bytes in self._unknown_fields: 1059 size += len(tag_bytes) + len(value_bytes) 1060 1061 self._cached_byte_size = size 1062 self._cached_byte_size_dirty = False 1063 self._listener_for_children.dirty = False 1064 return size
Returns the serialized size of this message.
Recursively calls ByteSize() on all contained messages.
Returns:
int: The number of bytes required to serialize this message.
784 def RegisterExtension(field_descriptor): 785 field_descriptor.containing_type = cls.DESCRIPTOR 786 # TODO: Use cls.MESSAGE_FACTORY.pool when available. 787 # pylint: disable=protected-access 788 cls.DESCRIPTOR.file.pool._AddExtensionDescriptor(field_descriptor) 789 _AttachFieldHelpers(cls, field_descriptor)
642 def getter(self): 643 field_value = self._fields.get(field) 644 if field_value is None: 645 # Construct a new object to represent this field. 646 field_value = field._default_constructor(self) 647 648 # Atomically check if another thread has preempted us and, if not, swap 649 # in the new object we just created. If someone has preempted us, we 650 # take that object and discard ours. 651 # WARNING: We are relying on setdefault() being atomic. This is true 652 # in CPython but we haven't investigated others. This warning appears 653 # in several other locations in this file. 654 field_value = self._fields.setdefault(field, field_value) 655 return field_value
Getter for vars.
1253 def FindInitializationErrors(self): 1254 """Finds required fields which are not initialized. 1255 1256 Returns: 1257 A list of strings. Each string is a path to an uninitialized field from 1258 the top-level message, e.g. "foo.bar[5].baz". 1259 """ 1260 1261 errors = [] # simplify things 1262 1263 for field in required_fields: 1264 if not self.HasField(field.name): 1265 errors.append(field.name) 1266 1267 for field, value in self.ListFields(): 1268 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 1269 if field.is_extension: 1270 name = '(%s)' % field.full_name 1271 else: 1272 name = field.name 1273 1274 if _IsMapField(field): 1275 if _IsMessageMapField(field): 1276 for key in value: 1277 element = value[key] 1278 prefix = '%s[%s].' % (name, key) 1279 sub_errors = element.FindInitializationErrors() 1280 errors += [prefix + error for error in sub_errors] 1281 else: 1282 # ScalarMaps can't have any initialization errors. 1283 pass 1284 elif field.label == _FieldDescriptor.LABEL_REPEATED: 1285 for i in range(len(value)): 1286 element = value[i] 1287 prefix = '%s[%d].' % (name, i) 1288 sub_errors = element.FindInitializationErrors() 1289 errors += [prefix + error for error in sub_errors] 1290 else: 1291 prefix = name + '.' 1292 sub_errors = value.FindInitializationErrors() 1293 errors += [prefix + error for error in sub_errors] 1294 1295 return errors
Finds required fields which are not initialized.
Returns:
A list of strings. Each string is a path to an uninitialized field from the top-level message, e.g. "foo.bar[5].baz".
Inherited Members
- google.protobuf.message.Message
- CopyFrom
- ParseFromString
- HasExtension
- ClearExtension
Abstract base class for protocol messages.
Protocol message classes are almost always generated by the protocol compiler. These generated types subclass Message and implement the methods shown below.
496 def init(self, **kwargs): 497 self._cached_byte_size = 0 498 self._cached_byte_size_dirty = len(kwargs) > 0 499 self._fields = {} 500 # Contains a mapping from oneof field descriptors to the descriptor 501 # of the currently set field in that oneof field. 502 self._oneofs = {} 503 504 # _unknown_fields is () when empty for efficiency, and will be turned into 505 # a list if fields are added. 506 self._unknown_fields = () 507 # _unknown_field_set is None when empty for efficiency, and will be 508 # turned into UnknownFieldSet struct if fields are added. 509 self._unknown_field_set = None # pylint: disable=protected-access 510 self._is_present_in_parent = False 511 self._listener = message_listener_mod.NullMessageListener() 512 self._listener_for_children = _Listener(self) 513 for field_name, field_value in kwargs.items(): 514 field = _GetFieldByName(message_descriptor, field_name) 515 if field is None: 516 raise TypeError('%s() got an unexpected keyword argument "%s"' % 517 (message_descriptor.name, field_name)) 518 if field_value is None: 519 # field=None is the same as no field at all. 520 continue 521 if field.label == _FieldDescriptor.LABEL_REPEATED: 522 copy = field._default_constructor(self) 523 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: # Composite 524 if _IsMapField(field): 525 if _IsMessageMapField(field): 526 for key in field_value: 527 copy[key].MergeFrom(field_value[key]) 528 else: 529 copy.update(field_value) 530 else: 531 for val in field_value: 532 if isinstance(val, dict): 533 copy.add(**val) 534 else: 535 copy.add().MergeFrom(val) 536 else: # Scalar 537 if field.cpp_type == _FieldDescriptor.CPPTYPE_ENUM: 538 field_value = [_GetIntegerEnumValue(field.enum_type, val) 539 for val in field_value] 540 copy.extend(field_value) 541 self._fields[field] = copy 542 elif field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 543 copy = field._default_constructor(self) 544 new_val = field_value 545 if isinstance(field_value, dict): 546 new_val = field.message_type._concrete_class(**field_value) 547 try: 548 copy.MergeFrom(new_val) 549 except TypeError: 550 _ReraiseTypeErrorWithFieldName(message_descriptor.name, field_name) 551 self._fields[field] = copy 552 else: 553 if field.cpp_type == _FieldDescriptor.CPPTYPE_ENUM: 554 field_value = _GetIntegerEnumValue(field.enum_type, field_value) 555 try: 556 setattr(self, field_name, field_value) 557 except TypeError: 558 _ReraiseTypeErrorWithFieldName(message_descriptor.name, field_name)
1312 def MergeFrom(self, msg): 1313 if not isinstance(msg, cls): 1314 raise TypeError( 1315 'Parameter to MergeFrom() must be instance of same class: ' 1316 'expected %s got %s.' % (_FullyQualifiedClassName(cls), 1317 _FullyQualifiedClassName(msg.__class__))) 1318 1319 assert msg is not self 1320 self._Modified() 1321 1322 fields = self._fields 1323 1324 for field, value in msg._fields.items(): 1325 if field.label == LABEL_REPEATED: 1326 field_value = fields.get(field) 1327 if field_value is None: 1328 # Construct a new object to represent this field. 1329 field_value = field._default_constructor(self) 1330 fields[field] = field_value 1331 field_value.MergeFrom(value) 1332 elif field.cpp_type == CPPTYPE_MESSAGE: 1333 if value._is_present_in_parent: 1334 field_value = fields.get(field) 1335 if field_value is None: 1336 # Construct a new object to represent this field. 1337 field_value = field._default_constructor(self) 1338 fields[field] = field_value 1339 field_value.MergeFrom(value) 1340 else: 1341 self._fields[field] = value 1342 if field.containing_oneof: 1343 self._UpdateOneofState(field) 1344 1345 if msg._unknown_fields: 1346 if not self._unknown_fields: 1347 self._unknown_fields = [] 1348 self._unknown_fields.extend(msg._unknown_fields) 1349 # pylint: disable=protected-access 1350 if self._unknown_field_set is None: 1351 self._unknown_field_set = containers.UnknownFieldSet() 1352 self._unknown_field_set._extend(msg._unknown_field_set)
Merges the contents of the specified message into current message.
This method merges the contents of the specified message into the current message. Singular fields that are set in the specified message overwrite the corresponding fields in the current message. Repeated fields are appended. Singular sub-messages and groups are recursively merged.
Arguments:
- other_msg (Message): A message to merge into the current message.
1375def _Clear(self): 1376 # Clear fields. 1377 self._fields = {} 1378 self._unknown_fields = () 1379 # pylint: disable=protected-access 1380 if self._unknown_field_set is not None: 1381 self._unknown_field_set._clear() 1382 self._unknown_field_set = None 1383 1384 self._oneofs = {} 1385 self._Modified()
Clears all data that was set in the message.
1452 def Modified(self): 1453 """Sets the _cached_byte_size_dirty bit to true, 1454 and propagates this to our listener iff this was a state change. 1455 """ 1456 1457 # Note: Some callers check _cached_byte_size_dirty before calling 1458 # _Modified() as an extra optimization. So, if this method is ever 1459 # changed such that it does stuff even when _cached_byte_size_dirty is 1460 # already true, the callers need to be updated. 1461 if not self._cached_byte_size_dirty: 1462 self._cached_byte_size_dirty = True 1463 self._listener_for_children.dirty = True 1464 self._is_present_in_parent = True 1465 self._listener.Modified()
Sets the _cached_byte_size_dirty bit to true, and propagates this to our listener iff this was a state change.
1213 def IsInitialized(self, errors=None): 1214 """Checks if all required fields of a message are set. 1215 1216 Args: 1217 errors: A list which, if provided, will be populated with the field 1218 paths of all missing required fields. 1219 1220 Returns: 1221 True iff the specified message has all required fields set. 1222 """ 1223 1224 # Performance is critical so we avoid HasField() and ListFields(). 1225 1226 for field in required_fields: 1227 if (field not in self._fields or 1228 (field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE and 1229 not self._fields[field]._is_present_in_parent)): 1230 if errors is not None: 1231 errors.extend(self.FindInitializationErrors()) 1232 return False 1233 1234 for field, value in list(self._fields.items()): # dict can change size! 1235 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 1236 if field.label == _FieldDescriptor.LABEL_REPEATED: 1237 if (field.message_type._is_map_entry): 1238 continue 1239 for element in value: 1240 if not element.IsInitialized(): 1241 if errors is not None: 1242 errors.extend(self.FindInitializationErrors()) 1243 return False 1244 elif value._is_present_in_parent and not value.IsInitialized(): 1245 if errors is not None: 1246 errors.extend(self.FindInitializationErrors()) 1247 return False 1248 1249 return True
Checks if all required fields of a message are set.
Arguments:
- errors: A list which, if provided, will be populated with the field paths of all missing required fields.
Returns:
True iff the specified message has all required fields set.
1119 def MergeFromString(self, serialized): 1120 serialized = memoryview(serialized) 1121 length = len(serialized) 1122 try: 1123 if self._InternalParse(serialized, 0, length) != length: 1124 # The only reason _InternalParse would return early is if it 1125 # encountered an end-group tag. 1126 raise message_mod.DecodeError('Unexpected end-group tag.') 1127 except (IndexError, TypeError): 1128 # Now ord(buf[p:p+1]) == ord('') gets TypeError. 1129 raise message_mod.DecodeError('Truncated message.') 1130 except struct.error as e: 1131 raise message_mod.DecodeError(e) 1132 return length # Return this for legacy reasons.
Merges serialized protocol buffer data into this message.
When we find a field in serialized
that is already present
in this message:
- If it's a "repeated" field, we append to the end of our list.
- Else, if it's a scalar, we overwrite our field.
- Else, (it's a nonrepeated composite), we recursively merge into the existing composite.
Arguments:
- serialized (bytes): Any object that allows us to call
memoryview(serialized)
to access a string of bytes using the buffer interface.
Returns:
int: The number of bytes read from
serialized
. For non-group messages, this will always belen(serialized)
, but for messages which are actually groups, this will generally be less thanlen(serialized)
, since we must stop when we reach anEND_GROUP
tag. Note that if we do stop because of anEND_GROUP
tag, the number of bytes returned does not include the bytes for theEND_GROUP
tag information.
Raises:
- DecodeError: if the input cannot be parsed.
1072 def SerializeToString(self, **kwargs): 1073 # Check if the message has all of its required fields set. 1074 if not self.IsInitialized(): 1075 raise message_mod.EncodeError( 1076 'Message %s is missing required fields: %s' % ( 1077 self.DESCRIPTOR.full_name, ','.join(self.FindInitializationErrors()))) 1078 return self.SerializePartialToString(**kwargs)
Serializes the protocol message to a binary string.
Keyword Args:
deterministic (bool): If true, requests deterministic serialization of the protobuf, with predictable ordering of map keys.
Returns:
A binary string representation of the message if all of the required fields in the message are set (i.e. the message is initialized).
Raises:
- EncodeError: if the message isn't initialized (see
IsInitialized()
).
1085 def SerializePartialToString(self, **kwargs): 1086 out = BytesIO() 1087 self._InternalSerialize(out.write, **kwargs) 1088 return out.getvalue()
Serializes the protocol message to a binary string.
This method is similar to SerializeToString but doesn't check if the message is initialized.
Keyword Args:
deterministic (bool): If true, requests deterministic serialization of the protobuf, with predictable ordering of map keys.
Returns:
bytes: A serialized representation of the partial message.
814 def ListFields(self): 815 all_fields = [item for item in self._fields.items() if _IsPresent(item)] 816 all_fields.sort(key = lambda item: item[0].number) 817 return all_fields
Returns a list of (FieldDescriptor, value) tuples for present fields.
A message field is non-empty if HasField() would return true. A singular primitive field is non-empty if HasField() would return true in proto2 or it is non zero in proto3. A repeated field is non-empty if it contains at least one element. The fields are ordered by field number.
Returns:
list[tuple(FieldDescriptor, value)]: field descriptors and values for all fields in the message which are not empty. The values vary by field type.
838 def HasField(self, field_name): 839 try: 840 field = hassable_fields[field_name] 841 except KeyError as exc: 842 raise ValueError('Protocol message %s has no non-repeated field "%s" ' 843 'nor has presence is not available for this field.' % ( 844 message_descriptor.full_name, field_name)) from exc 845 846 if isinstance(field, descriptor_mod.OneofDescriptor): 847 try: 848 return HasField(self, self._oneofs[field].name) 849 except KeyError: 850 return False 851 else: 852 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 853 value = self._fields.get(field) 854 return value is not None and value._is_present_in_parent 855 else: 856 return field in self._fields
Checks if a certain field is set for the message.
For a oneof group, checks if any field inside is set. Note that if the
field_name is not defined in the message descriptor, ValueError
will
be raised.
Arguments:
- field_name (str): The name of the field to check for presence.
Returns:
bool: Whether a value has been set for the named field.
Raises:
- ValueError: if the
field_name
is not a member of this message.
863 def ClearField(self, field_name): 864 try: 865 field = message_descriptor.fields_by_name[field_name] 866 except KeyError: 867 try: 868 field = message_descriptor.oneofs_by_name[field_name] 869 if field in self._oneofs: 870 field = self._oneofs[field] 871 else: 872 return 873 except KeyError: 874 raise ValueError('Protocol message %s has no "%s" field.' % 875 (message_descriptor.name, field_name)) 876 877 if field in self._fields: 878 # To match the C++ implementation, we need to invalidate iterators 879 # for map fields when ClearField() happens. 880 if hasattr(self._fields[field], 'InvalidateIterators'): 881 self._fields[field].InvalidateIterators() 882 883 # Note: If the field is a sub-message, its listener will still point 884 # at us. That's fine, because the worst than can happen is that it 885 # will call _Modified() and invalidate our byte size. Big deal. 886 del self._fields[field] 887 888 if self._oneofs.get(field.containing_oneof, None) is field: 889 del self._oneofs[field.containing_oneof] 890 891 # Always call _Modified() -- even if nothing was changed, this is 892 # a mutating method, and thus calling it should cause the field to become 893 # present in the parent message. 894 self._Modified()
Clears the contents of a given field.
Inside a oneof group, clears the field set. If the name neither refers to a
defined field or oneof group, ValueError
is raised.
Arguments:
- field_name (str): The name of the field to check for presence.
Raises:
- ValueError: if the
field_name
is not a member of this message.
1358 def WhichOneof(self, oneof_name): 1359 """Returns the name of the currently set field inside a oneof, or None.""" 1360 try: 1361 field = message_descriptor.oneofs_by_name[oneof_name] 1362 except KeyError: 1363 raise ValueError( 1364 'Protocol message has no oneof "%s" field.' % oneof_name) 1365 1366 nested_field = self._oneofs.get(field, None) 1367 if nested_field is not None and self.HasField(nested_field.name): 1368 return nested_field.name 1369 else: 1370 return None
Returns the name of the currently set field inside a oneof, or None.
1388def _UnknownFields(self): 1389 warnings.warn( 1390 'message.UnknownFields() is deprecated. Please use the add one ' 1391 'feature unknown_fields.UnknownFieldSet(message) in ' 1392 'unknown_fields.py instead.' 1393 ) 1394 if self._unknown_field_set is None: # pylint: disable=protected-access 1395 # pylint: disable=protected-access 1396 self._unknown_field_set = containers.UnknownFieldSet() 1397 return self._unknown_field_set # pylint: disable=protected-access
Returns the UnknownFieldSet.
Returns:
UnknownFieldSet: The unknown fields stored in this message.
1400def _DiscardUnknownFields(self): 1401 self._unknown_fields = [] 1402 self._unknown_field_set = None # pylint: disable=protected-access 1403 for field, value in self.ListFields(): 1404 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 1405 if _IsMapField(field): 1406 if _IsMessageMapField(field): 1407 for key in value: 1408 value[key].DiscardUnknownFields() 1409 elif field.label == _FieldDescriptor.LABEL_REPEATED: 1410 for sub_message in value: 1411 sub_message.DiscardUnknownFields() 1412 else: 1413 value.DiscardUnknownFields()
Clears all fields in the UnknownFieldSet
.
This operation is recursive for nested message.
1040 def ByteSize(self): 1041 if not self._cached_byte_size_dirty: 1042 return self._cached_byte_size 1043 1044 size = 0 1045 descriptor = self.DESCRIPTOR 1046 if descriptor._is_map_entry: 1047 # Fields of map entry should always be serialized. 1048 key_field = descriptor.fields_by_name['key'] 1049 _MaybeAddEncoder(cls, key_field) 1050 size = key_field._sizer(self.key) 1051 value_field = descriptor.fields_by_name['value'] 1052 _MaybeAddEncoder(cls, value_field) 1053 size += value_field._sizer(self.value) 1054 else: 1055 for field_descriptor, field_value in self.ListFields(): 1056 _MaybeAddEncoder(cls, field_descriptor) 1057 size += field_descriptor._sizer(field_value) 1058 for tag_bytes, value_bytes in self._unknown_fields: 1059 size += len(tag_bytes) + len(value_bytes) 1060 1061 self._cached_byte_size = size 1062 self._cached_byte_size_dirty = False 1063 self._listener_for_children.dirty = False 1064 return size
Returns the serialized size of this message.
Recursively calls ByteSize() on all contained messages.
Returns:
int: The number of bytes required to serialize this message.
784 def RegisterExtension(field_descriptor): 785 field_descriptor.containing_type = cls.DESCRIPTOR 786 # TODO: Use cls.MESSAGE_FACTORY.pool when available. 787 # pylint: disable=protected-access 788 cls.DESCRIPTOR.file.pool._AddExtensionDescriptor(field_descriptor) 789 _AttachFieldHelpers(cls, field_descriptor)
685 def getter(self): 686 # TODO: This may be broken since there may not be 687 # default_value. Combine with has_default_value somehow. 688 return self._fields.get(field, default_value)
Getter for name.
642 def getter(self): 643 field_value = self._fields.get(field) 644 if field_value is None: 645 # Construct a new object to represent this field. 646 field_value = field._default_constructor(self) 647 648 # Atomically check if another thread has preempted us and, if not, swap 649 # in the new object we just created. If someone has preempted us, we 650 # take that object and discard ours. 651 # WARNING: We are relying on setdefault() being atomic. This is true 652 # in CPython but we haven't investigated others. This warning appears 653 # in several other locations in this file. 654 field_value = self._fields.setdefault(field, field_value) 655 return field_value
Getter for enforcement_literal.
741 def getter(self): 742 field_value = self._fields.get(field) 743 if field_value is None: 744 # Construct a new object to represent this field. 745 field_value = field._default_constructor(self) 746 747 # Atomically check if another thread has preempted us and, if not, swap 748 # in the new object we just created. If someone has preempted us, we 749 # take that object and discard ours. 750 # WARNING: We are relying on setdefault() being atomic. This is true 751 # in CPython but we haven't investigated others. This warning appears 752 # in several other locations in this file. 753 field_value = self._fields.setdefault(field, field_value) 754 return field_value
Getter for bool_or.
741 def getter(self): 742 field_value = self._fields.get(field) 743 if field_value is None: 744 # Construct a new object to represent this field. 745 field_value = field._default_constructor(self) 746 747 # Atomically check if another thread has preempted us and, if not, swap 748 # in the new object we just created. If someone has preempted us, we 749 # take that object and discard ours. 750 # WARNING: We are relying on setdefault() being atomic. This is true 751 # in CPython but we haven't investigated others. This warning appears 752 # in several other locations in this file. 753 field_value = self._fields.setdefault(field, field_value) 754 return field_value
Getter for bool_and.
741 def getter(self): 742 field_value = self._fields.get(field) 743 if field_value is None: 744 # Construct a new object to represent this field. 745 field_value = field._default_constructor(self) 746 747 # Atomically check if another thread has preempted us and, if not, swap 748 # in the new object we just created. If someone has preempted us, we 749 # take that object and discard ours. 750 # WARNING: We are relying on setdefault() being atomic. This is true 751 # in CPython but we haven't investigated others. This warning appears 752 # in several other locations in this file. 753 field_value = self._fields.setdefault(field, field_value) 754 return field_value
Getter for at_most_one.
741 def getter(self): 742 field_value = self._fields.get(field) 743 if field_value is None: 744 # Construct a new object to represent this field. 745 field_value = field._default_constructor(self) 746 747 # Atomically check if another thread has preempted us and, if not, swap 748 # in the new object we just created. If someone has preempted us, we 749 # take that object and discard ours. 750 # WARNING: We are relying on setdefault() being atomic. This is true 751 # in CPython but we haven't investigated others. This warning appears 752 # in several other locations in this file. 753 field_value = self._fields.setdefault(field, field_value) 754 return field_value
Getter for exactly_one.
741 def getter(self): 742 field_value = self._fields.get(field) 743 if field_value is None: 744 # Construct a new object to represent this field. 745 field_value = field._default_constructor(self) 746 747 # Atomically check if another thread has preempted us and, if not, swap 748 # in the new object we just created. If someone has preempted us, we 749 # take that object and discard ours. 750 # WARNING: We are relying on setdefault() being atomic. This is true 751 # in CPython but we haven't investigated others. This warning appears 752 # in several other locations in this file. 753 field_value = self._fields.setdefault(field, field_value) 754 return field_value
Getter for bool_xor.
741 def getter(self): 742 field_value = self._fields.get(field) 743 if field_value is None: 744 # Construct a new object to represent this field. 745 field_value = field._default_constructor(self) 746 747 # Atomically check if another thread has preempted us and, if not, swap 748 # in the new object we just created. If someone has preempted us, we 749 # take that object and discard ours. 750 # WARNING: We are relying on setdefault() being atomic. This is true 751 # in CPython but we haven't investigated others. This warning appears 752 # in several other locations in this file. 753 field_value = self._fields.setdefault(field, field_value) 754 return field_value
Getter for int_div.
741 def getter(self): 742 field_value = self._fields.get(field) 743 if field_value is None: 744 # Construct a new object to represent this field. 745 field_value = field._default_constructor(self) 746 747 # Atomically check if another thread has preempted us and, if not, swap 748 # in the new object we just created. If someone has preempted us, we 749 # take that object and discard ours. 750 # WARNING: We are relying on setdefault() being atomic. This is true 751 # in CPython but we haven't investigated others. This warning appears 752 # in several other locations in this file. 753 field_value = self._fields.setdefault(field, field_value) 754 return field_value
Getter for int_mod.
741 def getter(self): 742 field_value = self._fields.get(field) 743 if field_value is None: 744 # Construct a new object to represent this field. 745 field_value = field._default_constructor(self) 746 747 # Atomically check if another thread has preempted us and, if not, swap 748 # in the new object we just created. If someone has preempted us, we 749 # take that object and discard ours. 750 # WARNING: We are relying on setdefault() being atomic. This is true 751 # in CPython but we haven't investigated others. This warning appears 752 # in several other locations in this file. 753 field_value = self._fields.setdefault(field, field_value) 754 return field_value
Getter for int_prod.
741 def getter(self): 742 field_value = self._fields.get(field) 743 if field_value is None: 744 # Construct a new object to represent this field. 745 field_value = field._default_constructor(self) 746 747 # Atomically check if another thread has preempted us and, if not, swap 748 # in the new object we just created. If someone has preempted us, we 749 # take that object and discard ours. 750 # WARNING: We are relying on setdefault() being atomic. This is true 751 # in CPython but we haven't investigated others. This warning appears 752 # in several other locations in this file. 753 field_value = self._fields.setdefault(field, field_value) 754 return field_value
Getter for lin_max.
741 def getter(self): 742 field_value = self._fields.get(field) 743 if field_value is None: 744 # Construct a new object to represent this field. 745 field_value = field._default_constructor(self) 746 747 # Atomically check if another thread has preempted us and, if not, swap 748 # in the new object we just created. If someone has preempted us, we 749 # take that object and discard ours. 750 # WARNING: We are relying on setdefault() being atomic. This is true 751 # in CPython but we haven't investigated others. This warning appears 752 # in several other locations in this file. 753 field_value = self._fields.setdefault(field, field_value) 754 return field_value
Getter for linear.
741 def getter(self): 742 field_value = self._fields.get(field) 743 if field_value is None: 744 # Construct a new object to represent this field. 745 field_value = field._default_constructor(self) 746 747 # Atomically check if another thread has preempted us and, if not, swap 748 # in the new object we just created. If someone has preempted us, we 749 # take that object and discard ours. 750 # WARNING: We are relying on setdefault() being atomic. This is true 751 # in CPython but we haven't investigated others. This warning appears 752 # in several other locations in this file. 753 field_value = self._fields.setdefault(field, field_value) 754 return field_value
Getter for all_diff.
741 def getter(self): 742 field_value = self._fields.get(field) 743 if field_value is None: 744 # Construct a new object to represent this field. 745 field_value = field._default_constructor(self) 746 747 # Atomically check if another thread has preempted us and, if not, swap 748 # in the new object we just created. If someone has preempted us, we 749 # take that object and discard ours. 750 # WARNING: We are relying on setdefault() being atomic. This is true 751 # in CPython but we haven't investigated others. This warning appears 752 # in several other locations in this file. 753 field_value = self._fields.setdefault(field, field_value) 754 return field_value
Getter for element.
741 def getter(self): 742 field_value = self._fields.get(field) 743 if field_value is None: 744 # Construct a new object to represent this field. 745 field_value = field._default_constructor(self) 746 747 # Atomically check if another thread has preempted us and, if not, swap 748 # in the new object we just created. If someone has preempted us, we 749 # take that object and discard ours. 750 # WARNING: We are relying on setdefault() being atomic. This is true 751 # in CPython but we haven't investigated others. This warning appears 752 # in several other locations in this file. 753 field_value = self._fields.setdefault(field, field_value) 754 return field_value
Getter for circuit.
741 def getter(self): 742 field_value = self._fields.get(field) 743 if field_value is None: 744 # Construct a new object to represent this field. 745 field_value = field._default_constructor(self) 746 747 # Atomically check if another thread has preempted us and, if not, swap 748 # in the new object we just created. If someone has preempted us, we 749 # take that object and discard ours. 750 # WARNING: We are relying on setdefault() being atomic. This is true 751 # in CPython but we haven't investigated others. This warning appears 752 # in several other locations in this file. 753 field_value = self._fields.setdefault(field, field_value) 754 return field_value
Getter for routes.
741 def getter(self): 742 field_value = self._fields.get(field) 743 if field_value is None: 744 # Construct a new object to represent this field. 745 field_value = field._default_constructor(self) 746 747 # Atomically check if another thread has preempted us and, if not, swap 748 # in the new object we just created. If someone has preempted us, we 749 # take that object and discard ours. 750 # WARNING: We are relying on setdefault() being atomic. This is true 751 # in CPython but we haven't investigated others. This warning appears 752 # in several other locations in this file. 753 field_value = self._fields.setdefault(field, field_value) 754 return field_value
Getter for table.
741 def getter(self): 742 field_value = self._fields.get(field) 743 if field_value is None: 744 # Construct a new object to represent this field. 745 field_value = field._default_constructor(self) 746 747 # Atomically check if another thread has preempted us and, if not, swap 748 # in the new object we just created. If someone has preempted us, we 749 # take that object and discard ours. 750 # WARNING: We are relying on setdefault() being atomic. This is true 751 # in CPython but we haven't investigated others. This warning appears 752 # in several other locations in this file. 753 field_value = self._fields.setdefault(field, field_value) 754 return field_value
Getter for automaton.
741 def getter(self): 742 field_value = self._fields.get(field) 743 if field_value is None: 744 # Construct a new object to represent this field. 745 field_value = field._default_constructor(self) 746 747 # Atomically check if another thread has preempted us and, if not, swap 748 # in the new object we just created. If someone has preempted us, we 749 # take that object and discard ours. 750 # WARNING: We are relying on setdefault() being atomic. This is true 751 # in CPython but we haven't investigated others. This warning appears 752 # in several other locations in this file. 753 field_value = self._fields.setdefault(field, field_value) 754 return field_value
Getter for inverse.
741 def getter(self): 742 field_value = self._fields.get(field) 743 if field_value is None: 744 # Construct a new object to represent this field. 745 field_value = field._default_constructor(self) 746 747 # Atomically check if another thread has preempted us and, if not, swap 748 # in the new object we just created. If someone has preempted us, we 749 # take that object and discard ours. 750 # WARNING: We are relying on setdefault() being atomic. This is true 751 # in CPython but we haven't investigated others. This warning appears 752 # in several other locations in this file. 753 field_value = self._fields.setdefault(field, field_value) 754 return field_value
Getter for reservoir.
741 def getter(self): 742 field_value = self._fields.get(field) 743 if field_value is None: 744 # Construct a new object to represent this field. 745 field_value = field._default_constructor(self) 746 747 # Atomically check if another thread has preempted us and, if not, swap 748 # in the new object we just created. If someone has preempted us, we 749 # take that object and discard ours. 750 # WARNING: We are relying on setdefault() being atomic. This is true 751 # in CPython but we haven't investigated others. This warning appears 752 # in several other locations in this file. 753 field_value = self._fields.setdefault(field, field_value) 754 return field_value
Getter for interval.
741 def getter(self): 742 field_value = self._fields.get(field) 743 if field_value is None: 744 # Construct a new object to represent this field. 745 field_value = field._default_constructor(self) 746 747 # Atomically check if another thread has preempted us and, if not, swap 748 # in the new object we just created. If someone has preempted us, we 749 # take that object and discard ours. 750 # WARNING: We are relying on setdefault() being atomic. This is true 751 # in CPython but we haven't investigated others. This warning appears 752 # in several other locations in this file. 753 field_value = self._fields.setdefault(field, field_value) 754 return field_value
Getter for no_overlap.
741 def getter(self): 742 field_value = self._fields.get(field) 743 if field_value is None: 744 # Construct a new object to represent this field. 745 field_value = field._default_constructor(self) 746 747 # Atomically check if another thread has preempted us and, if not, swap 748 # in the new object we just created. If someone has preempted us, we 749 # take that object and discard ours. 750 # WARNING: We are relying on setdefault() being atomic. This is true 751 # in CPython but we haven't investigated others. This warning appears 752 # in several other locations in this file. 753 field_value = self._fields.setdefault(field, field_value) 754 return field_value
Getter for no_overlap_2d.
741 def getter(self): 742 field_value = self._fields.get(field) 743 if field_value is None: 744 # Construct a new object to represent this field. 745 field_value = field._default_constructor(self) 746 747 # Atomically check if another thread has preempted us and, if not, swap 748 # in the new object we just created. If someone has preempted us, we 749 # take that object and discard ours. 750 # WARNING: We are relying on setdefault() being atomic. This is true 751 # in CPython but we haven't investigated others. This warning appears 752 # in several other locations in this file. 753 field_value = self._fields.setdefault(field, field_value) 754 return field_value
Getter for cumulative.
741 def getter(self): 742 field_value = self._fields.get(field) 743 if field_value is None: 744 # Construct a new object to represent this field. 745 field_value = field._default_constructor(self) 746 747 # Atomically check if another thread has preempted us and, if not, swap 748 # in the new object we just created. If someone has preempted us, we 749 # take that object and discard ours. 750 # WARNING: We are relying on setdefault() being atomic. This is true 751 # in CPython but we haven't investigated others. This warning appears 752 # in several other locations in this file. 753 field_value = self._fields.setdefault(field, field_value) 754 return field_value
Getter for dummy_constraint.
1253 def FindInitializationErrors(self): 1254 """Finds required fields which are not initialized. 1255 1256 Returns: 1257 A list of strings. Each string is a path to an uninitialized field from 1258 the top-level message, e.g. "foo.bar[5].baz". 1259 """ 1260 1261 errors = [] # simplify things 1262 1263 for field in required_fields: 1264 if not self.HasField(field.name): 1265 errors.append(field.name) 1266 1267 for field, value in self.ListFields(): 1268 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 1269 if field.is_extension: 1270 name = '(%s)' % field.full_name 1271 else: 1272 name = field.name 1273 1274 if _IsMapField(field): 1275 if _IsMessageMapField(field): 1276 for key in value: 1277 element = value[key] 1278 prefix = '%s[%s].' % (name, key) 1279 sub_errors = element.FindInitializationErrors() 1280 errors += [prefix + error for error in sub_errors] 1281 else: 1282 # ScalarMaps can't have any initialization errors. 1283 pass 1284 elif field.label == _FieldDescriptor.LABEL_REPEATED: 1285 for i in range(len(value)): 1286 element = value[i] 1287 prefix = '%s[%d].' % (name, i) 1288 sub_errors = element.FindInitializationErrors() 1289 errors += [prefix + error for error in sub_errors] 1290 else: 1291 prefix = name + '.' 1292 sub_errors = value.FindInitializationErrors() 1293 errors += [prefix + error for error in sub_errors] 1294 1295 return errors
Finds required fields which are not initialized.
Returns:
A list of strings. Each string is a path to an uninitialized field from the top-level message, e.g. "foo.bar[5].baz".
Inherited Members
- google.protobuf.message.Message
- CopyFrom
- ParseFromString
- HasExtension
- ClearExtension
Abstract base class for protocol messages.
Protocol message classes are almost always generated by the protocol compiler. These generated types subclass Message and implement the methods shown below.
496 def init(self, **kwargs): 497 self._cached_byte_size = 0 498 self._cached_byte_size_dirty = len(kwargs) > 0 499 self._fields = {} 500 # Contains a mapping from oneof field descriptors to the descriptor 501 # of the currently set field in that oneof field. 502 self._oneofs = {} 503 504 # _unknown_fields is () when empty for efficiency, and will be turned into 505 # a list if fields are added. 506 self._unknown_fields = () 507 # _unknown_field_set is None when empty for efficiency, and will be 508 # turned into UnknownFieldSet struct if fields are added. 509 self._unknown_field_set = None # pylint: disable=protected-access 510 self._is_present_in_parent = False 511 self._listener = message_listener_mod.NullMessageListener() 512 self._listener_for_children = _Listener(self) 513 for field_name, field_value in kwargs.items(): 514 field = _GetFieldByName(message_descriptor, field_name) 515 if field is None: 516 raise TypeError('%s() got an unexpected keyword argument "%s"' % 517 (message_descriptor.name, field_name)) 518 if field_value is None: 519 # field=None is the same as no field at all. 520 continue 521 if field.label == _FieldDescriptor.LABEL_REPEATED: 522 copy = field._default_constructor(self) 523 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: # Composite 524 if _IsMapField(field): 525 if _IsMessageMapField(field): 526 for key in field_value: 527 copy[key].MergeFrom(field_value[key]) 528 else: 529 copy.update(field_value) 530 else: 531 for val in field_value: 532 if isinstance(val, dict): 533 copy.add(**val) 534 else: 535 copy.add().MergeFrom(val) 536 else: # Scalar 537 if field.cpp_type == _FieldDescriptor.CPPTYPE_ENUM: 538 field_value = [_GetIntegerEnumValue(field.enum_type, val) 539 for val in field_value] 540 copy.extend(field_value) 541 self._fields[field] = copy 542 elif field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 543 copy = field._default_constructor(self) 544 new_val = field_value 545 if isinstance(field_value, dict): 546 new_val = field.message_type._concrete_class(**field_value) 547 try: 548 copy.MergeFrom(new_val) 549 except TypeError: 550 _ReraiseTypeErrorWithFieldName(message_descriptor.name, field_name) 551 self._fields[field] = copy 552 else: 553 if field.cpp_type == _FieldDescriptor.CPPTYPE_ENUM: 554 field_value = _GetIntegerEnumValue(field.enum_type, field_value) 555 try: 556 setattr(self, field_name, field_value) 557 except TypeError: 558 _ReraiseTypeErrorWithFieldName(message_descriptor.name, field_name)
1312 def MergeFrom(self, msg): 1313 if not isinstance(msg, cls): 1314 raise TypeError( 1315 'Parameter to MergeFrom() must be instance of same class: ' 1316 'expected %s got %s.' % (_FullyQualifiedClassName(cls), 1317 _FullyQualifiedClassName(msg.__class__))) 1318 1319 assert msg is not self 1320 self._Modified() 1321 1322 fields = self._fields 1323 1324 for field, value in msg._fields.items(): 1325 if field.label == LABEL_REPEATED: 1326 field_value = fields.get(field) 1327 if field_value is None: 1328 # Construct a new object to represent this field. 1329 field_value = field._default_constructor(self) 1330 fields[field] = field_value 1331 field_value.MergeFrom(value) 1332 elif field.cpp_type == CPPTYPE_MESSAGE: 1333 if value._is_present_in_parent: 1334 field_value = fields.get(field) 1335 if field_value is None: 1336 # Construct a new object to represent this field. 1337 field_value = field._default_constructor(self) 1338 fields[field] = field_value 1339 field_value.MergeFrom(value) 1340 else: 1341 self._fields[field] = value 1342 if field.containing_oneof: 1343 self._UpdateOneofState(field) 1344 1345 if msg._unknown_fields: 1346 if not self._unknown_fields: 1347 self._unknown_fields = [] 1348 self._unknown_fields.extend(msg._unknown_fields) 1349 # pylint: disable=protected-access 1350 if self._unknown_field_set is None: 1351 self._unknown_field_set = containers.UnknownFieldSet() 1352 self._unknown_field_set._extend(msg._unknown_field_set)
Merges the contents of the specified message into current message.
This method merges the contents of the specified message into the current message. Singular fields that are set in the specified message overwrite the corresponding fields in the current message. Repeated fields are appended. Singular sub-messages and groups are recursively merged.
Arguments:
- other_msg (Message): A message to merge into the current message.
1375def _Clear(self): 1376 # Clear fields. 1377 self._fields = {} 1378 self._unknown_fields = () 1379 # pylint: disable=protected-access 1380 if self._unknown_field_set is not None: 1381 self._unknown_field_set._clear() 1382 self._unknown_field_set = None 1383 1384 self._oneofs = {} 1385 self._Modified()
Clears all data that was set in the message.
1452 def Modified(self): 1453 """Sets the _cached_byte_size_dirty bit to true, 1454 and propagates this to our listener iff this was a state change. 1455 """ 1456 1457 # Note: Some callers check _cached_byte_size_dirty before calling 1458 # _Modified() as an extra optimization. So, if this method is ever 1459 # changed such that it does stuff even when _cached_byte_size_dirty is 1460 # already true, the callers need to be updated. 1461 if not self._cached_byte_size_dirty: 1462 self._cached_byte_size_dirty = True 1463 self._listener_for_children.dirty = True 1464 self._is_present_in_parent = True 1465 self._listener.Modified()
Sets the _cached_byte_size_dirty bit to true, and propagates this to our listener iff this was a state change.
1213 def IsInitialized(self, errors=None): 1214 """Checks if all required fields of a message are set. 1215 1216 Args: 1217 errors: A list which, if provided, will be populated with the field 1218 paths of all missing required fields. 1219 1220 Returns: 1221 True iff the specified message has all required fields set. 1222 """ 1223 1224 # Performance is critical so we avoid HasField() and ListFields(). 1225 1226 for field in required_fields: 1227 if (field not in self._fields or 1228 (field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE and 1229 not self._fields[field]._is_present_in_parent)): 1230 if errors is not None: 1231 errors.extend(self.FindInitializationErrors()) 1232 return False 1233 1234 for field, value in list(self._fields.items()): # dict can change size! 1235 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 1236 if field.label == _FieldDescriptor.LABEL_REPEATED: 1237 if (field.message_type._is_map_entry): 1238 continue 1239 for element in value: 1240 if not element.IsInitialized(): 1241 if errors is not None: 1242 errors.extend(self.FindInitializationErrors()) 1243 return False 1244 elif value._is_present_in_parent and not value.IsInitialized(): 1245 if errors is not None: 1246 errors.extend(self.FindInitializationErrors()) 1247 return False 1248 1249 return True
Checks if all required fields of a message are set.
Arguments:
- errors: A list which, if provided, will be populated with the field paths of all missing required fields.
Returns:
True iff the specified message has all required fields set.
1119 def MergeFromString(self, serialized): 1120 serialized = memoryview(serialized) 1121 length = len(serialized) 1122 try: 1123 if self._InternalParse(serialized, 0, length) != length: 1124 # The only reason _InternalParse would return early is if it 1125 # encountered an end-group tag. 1126 raise message_mod.DecodeError('Unexpected end-group tag.') 1127 except (IndexError, TypeError): 1128 # Now ord(buf[p:p+1]) == ord('') gets TypeError. 1129 raise message_mod.DecodeError('Truncated message.') 1130 except struct.error as e: 1131 raise message_mod.DecodeError(e) 1132 return length # Return this for legacy reasons.
Merges serialized protocol buffer data into this message.
When we find a field in serialized
that is already present
in this message:
- If it's a "repeated" field, we append to the end of our list.
- Else, if it's a scalar, we overwrite our field.
- Else, (it's a nonrepeated composite), we recursively merge into the existing composite.
Arguments:
- serialized (bytes): Any object that allows us to call
memoryview(serialized)
to access a string of bytes using the buffer interface.
Returns:
int: The number of bytes read from
serialized
. For non-group messages, this will always belen(serialized)
, but for messages which are actually groups, this will generally be less thanlen(serialized)
, since we must stop when we reach anEND_GROUP
tag. Note that if we do stop because of anEND_GROUP
tag, the number of bytes returned does not include the bytes for theEND_GROUP
tag information.
Raises:
- DecodeError: if the input cannot be parsed.
1072 def SerializeToString(self, **kwargs): 1073 # Check if the message has all of its required fields set. 1074 if not self.IsInitialized(): 1075 raise message_mod.EncodeError( 1076 'Message %s is missing required fields: %s' % ( 1077 self.DESCRIPTOR.full_name, ','.join(self.FindInitializationErrors()))) 1078 return self.SerializePartialToString(**kwargs)
Serializes the protocol message to a binary string.
Keyword Args:
deterministic (bool): If true, requests deterministic serialization of the protobuf, with predictable ordering of map keys.
Returns:
A binary string representation of the message if all of the required fields in the message are set (i.e. the message is initialized).
Raises:
- EncodeError: if the message isn't initialized (see
IsInitialized()
).
1085 def SerializePartialToString(self, **kwargs): 1086 out = BytesIO() 1087 self._InternalSerialize(out.write, **kwargs) 1088 return out.getvalue()
Serializes the protocol message to a binary string.
This method is similar to SerializeToString but doesn't check if the message is initialized.
Keyword Args:
deterministic (bool): If true, requests deterministic serialization of the protobuf, with predictable ordering of map keys.
Returns:
bytes: A serialized representation of the partial message.
814 def ListFields(self): 815 all_fields = [item for item in self._fields.items() if _IsPresent(item)] 816 all_fields.sort(key = lambda item: item[0].number) 817 return all_fields
Returns a list of (FieldDescriptor, value) tuples for present fields.
A message field is non-empty if HasField() would return true. A singular primitive field is non-empty if HasField() would return true in proto2 or it is non zero in proto3. A repeated field is non-empty if it contains at least one element. The fields are ordered by field number.
Returns:
list[tuple(FieldDescriptor, value)]: field descriptors and values for all fields in the message which are not empty. The values vary by field type.
838 def HasField(self, field_name): 839 try: 840 field = hassable_fields[field_name] 841 except KeyError as exc: 842 raise ValueError('Protocol message %s has no non-repeated field "%s" ' 843 'nor has presence is not available for this field.' % ( 844 message_descriptor.full_name, field_name)) from exc 845 846 if isinstance(field, descriptor_mod.OneofDescriptor): 847 try: 848 return HasField(self, self._oneofs[field].name) 849 except KeyError: 850 return False 851 else: 852 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 853 value = self._fields.get(field) 854 return value is not None and value._is_present_in_parent 855 else: 856 return field in self._fields
Checks if a certain field is set for the message.
For a oneof group, checks if any field inside is set. Note that if the
field_name is not defined in the message descriptor, ValueError
will
be raised.
Arguments:
- field_name (str): The name of the field to check for presence.
Returns:
bool: Whether a value has been set for the named field.
Raises:
- ValueError: if the
field_name
is not a member of this message.
863 def ClearField(self, field_name): 864 try: 865 field = message_descriptor.fields_by_name[field_name] 866 except KeyError: 867 try: 868 field = message_descriptor.oneofs_by_name[field_name] 869 if field in self._oneofs: 870 field = self._oneofs[field] 871 else: 872 return 873 except KeyError: 874 raise ValueError('Protocol message %s has no "%s" field.' % 875 (message_descriptor.name, field_name)) 876 877 if field in self._fields: 878 # To match the C++ implementation, we need to invalidate iterators 879 # for map fields when ClearField() happens. 880 if hasattr(self._fields[field], 'InvalidateIterators'): 881 self._fields[field].InvalidateIterators() 882 883 # Note: If the field is a sub-message, its listener will still point 884 # at us. That's fine, because the worst than can happen is that it 885 # will call _Modified() and invalidate our byte size. Big deal. 886 del self._fields[field] 887 888 if self._oneofs.get(field.containing_oneof, None) is field: 889 del self._oneofs[field.containing_oneof] 890 891 # Always call _Modified() -- even if nothing was changed, this is 892 # a mutating method, and thus calling it should cause the field to become 893 # present in the parent message. 894 self._Modified()
Clears the contents of a given field.
Inside a oneof group, clears the field set. If the name neither refers to a
defined field or oneof group, ValueError
is raised.
Arguments:
- field_name (str): The name of the field to check for presence.
Raises:
- ValueError: if the
field_name
is not a member of this message.
1358 def WhichOneof(self, oneof_name): 1359 """Returns the name of the currently set field inside a oneof, or None.""" 1360 try: 1361 field = message_descriptor.oneofs_by_name[oneof_name] 1362 except KeyError: 1363 raise ValueError( 1364 'Protocol message has no oneof "%s" field.' % oneof_name) 1365 1366 nested_field = self._oneofs.get(field, None) 1367 if nested_field is not None and self.HasField(nested_field.name): 1368 return nested_field.name 1369 else: 1370 return None
Returns the name of the currently set field inside a oneof, or None.
1388def _UnknownFields(self): 1389 warnings.warn( 1390 'message.UnknownFields() is deprecated. Please use the add one ' 1391 'feature unknown_fields.UnknownFieldSet(message) in ' 1392 'unknown_fields.py instead.' 1393 ) 1394 if self._unknown_field_set is None: # pylint: disable=protected-access 1395 # pylint: disable=protected-access 1396 self._unknown_field_set = containers.UnknownFieldSet() 1397 return self._unknown_field_set # pylint: disable=protected-access
Returns the UnknownFieldSet.
Returns:
UnknownFieldSet: The unknown fields stored in this message.
1400def _DiscardUnknownFields(self): 1401 self._unknown_fields = [] 1402 self._unknown_field_set = None # pylint: disable=protected-access 1403 for field, value in self.ListFields(): 1404 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 1405 if _IsMapField(field): 1406 if _IsMessageMapField(field): 1407 for key in value: 1408 value[key].DiscardUnknownFields() 1409 elif field.label == _FieldDescriptor.LABEL_REPEATED: 1410 for sub_message in value: 1411 sub_message.DiscardUnknownFields() 1412 else: 1413 value.DiscardUnknownFields()
Clears all fields in the UnknownFieldSet
.
This operation is recursive for nested message.
1040 def ByteSize(self): 1041 if not self._cached_byte_size_dirty: 1042 return self._cached_byte_size 1043 1044 size = 0 1045 descriptor = self.DESCRIPTOR 1046 if descriptor._is_map_entry: 1047 # Fields of map entry should always be serialized. 1048 key_field = descriptor.fields_by_name['key'] 1049 _MaybeAddEncoder(cls, key_field) 1050 size = key_field._sizer(self.key) 1051 value_field = descriptor.fields_by_name['value'] 1052 _MaybeAddEncoder(cls, value_field) 1053 size += value_field._sizer(self.value) 1054 else: 1055 for field_descriptor, field_value in self.ListFields(): 1056 _MaybeAddEncoder(cls, field_descriptor) 1057 size += field_descriptor._sizer(field_value) 1058 for tag_bytes, value_bytes in self._unknown_fields: 1059 size += len(tag_bytes) + len(value_bytes) 1060 1061 self._cached_byte_size = size 1062 self._cached_byte_size_dirty = False 1063 self._listener_for_children.dirty = False 1064 return size
Returns the serialized size of this message.
Recursively calls ByteSize() on all contained messages.
Returns:
int: The number of bytes required to serialize this message.
784 def RegisterExtension(field_descriptor): 785 field_descriptor.containing_type = cls.DESCRIPTOR 786 # TODO: Use cls.MESSAGE_FACTORY.pool when available. 787 # pylint: disable=protected-access 788 cls.DESCRIPTOR.file.pool._AddExtensionDescriptor(field_descriptor) 789 _AttachFieldHelpers(cls, field_descriptor)
642 def getter(self): 643 field_value = self._fields.get(field) 644 if field_value is None: 645 # Construct a new object to represent this field. 646 field_value = field._default_constructor(self) 647 648 # Atomically check if another thread has preempted us and, if not, swap 649 # in the new object we just created. If someone has preempted us, we 650 # take that object and discard ours. 651 # WARNING: We are relying on setdefault() being atomic. This is true 652 # in CPython but we haven't investigated others. This warning appears 653 # in several other locations in this file. 654 field_value = self._fields.setdefault(field, field_value) 655 return field_value
Getter for vars.
642 def getter(self): 643 field_value = self._fields.get(field) 644 if field_value is None: 645 # Construct a new object to represent this field. 646 field_value = field._default_constructor(self) 647 648 # Atomically check if another thread has preempted us and, if not, swap 649 # in the new object we just created. If someone has preempted us, we 650 # take that object and discard ours. 651 # WARNING: We are relying on setdefault() being atomic. This is true 652 # in CPython but we haven't investigated others. This warning appears 653 # in several other locations in this file. 654 field_value = self._fields.setdefault(field, field_value) 655 return field_value
Getter for coeffs.
685 def getter(self): 686 # TODO: This may be broken since there may not be 687 # default_value. Combine with has_default_value somehow. 688 return self._fields.get(field, default_value)
Getter for offset.
685 def getter(self): 686 # TODO: This may be broken since there may not be 687 # default_value. Combine with has_default_value somehow. 688 return self._fields.get(field, default_value)
Getter for scaling_factor.
642 def getter(self): 643 field_value = self._fields.get(field) 644 if field_value is None: 645 # Construct a new object to represent this field. 646 field_value = field._default_constructor(self) 647 648 # Atomically check if another thread has preempted us and, if not, swap 649 # in the new object we just created. If someone has preempted us, we 650 # take that object and discard ours. 651 # WARNING: We are relying on setdefault() being atomic. This is true 652 # in CPython but we haven't investigated others. This warning appears 653 # in several other locations in this file. 654 field_value = self._fields.setdefault(field, field_value) 655 return field_value
Getter for domain.
685 def getter(self): 686 # TODO: This may be broken since there may not be 687 # default_value. Combine with has_default_value somehow. 688 return self._fields.get(field, default_value)
Getter for scaling_was_exact.
685 def getter(self): 686 # TODO: This may be broken since there may not be 687 # default_value. Combine with has_default_value somehow. 688 return self._fields.get(field, default_value)
Getter for integer_before_offset.
685 def getter(self): 686 # TODO: This may be broken since there may not be 687 # default_value. Combine with has_default_value somehow. 688 return self._fields.get(field, default_value)
Getter for integer_after_offset.
685 def getter(self): 686 # TODO: This may be broken since there may not be 687 # default_value. Combine with has_default_value somehow. 688 return self._fields.get(field, default_value)
Getter for integer_scaling_factor.
1253 def FindInitializationErrors(self): 1254 """Finds required fields which are not initialized. 1255 1256 Returns: 1257 A list of strings. Each string is a path to an uninitialized field from 1258 the top-level message, e.g. "foo.bar[5].baz". 1259 """ 1260 1261 errors = [] # simplify things 1262 1263 for field in required_fields: 1264 if not self.HasField(field.name): 1265 errors.append(field.name) 1266 1267 for field, value in self.ListFields(): 1268 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 1269 if field.is_extension: 1270 name = '(%s)' % field.full_name 1271 else: 1272 name = field.name 1273 1274 if _IsMapField(field): 1275 if _IsMessageMapField(field): 1276 for key in value: 1277 element = value[key] 1278 prefix = '%s[%s].' % (name, key) 1279 sub_errors = element.FindInitializationErrors() 1280 errors += [prefix + error for error in sub_errors] 1281 else: 1282 # ScalarMaps can't have any initialization errors. 1283 pass 1284 elif field.label == _FieldDescriptor.LABEL_REPEATED: 1285 for i in range(len(value)): 1286 element = value[i] 1287 prefix = '%s[%d].' % (name, i) 1288 sub_errors = element.FindInitializationErrors() 1289 errors += [prefix + error for error in sub_errors] 1290 else: 1291 prefix = name + '.' 1292 sub_errors = value.FindInitializationErrors() 1293 errors += [prefix + error for error in sub_errors] 1294 1295 return errors
Finds required fields which are not initialized.
Returns:
A list of strings. Each string is a path to an uninitialized field from the top-level message, e.g. "foo.bar[5].baz".
Inherited Members
- google.protobuf.message.Message
- CopyFrom
- ParseFromString
- HasExtension
- ClearExtension
Abstract base class for protocol messages.
Protocol message classes are almost always generated by the protocol compiler. These generated types subclass Message and implement the methods shown below.
496 def init(self, **kwargs): 497 self._cached_byte_size = 0 498 self._cached_byte_size_dirty = len(kwargs) > 0 499 self._fields = {} 500 # Contains a mapping from oneof field descriptors to the descriptor 501 # of the currently set field in that oneof field. 502 self._oneofs = {} 503 504 # _unknown_fields is () when empty for efficiency, and will be turned into 505 # a list if fields are added. 506 self._unknown_fields = () 507 # _unknown_field_set is None when empty for efficiency, and will be 508 # turned into UnknownFieldSet struct if fields are added. 509 self._unknown_field_set = None # pylint: disable=protected-access 510 self._is_present_in_parent = False 511 self._listener = message_listener_mod.NullMessageListener() 512 self._listener_for_children = _Listener(self) 513 for field_name, field_value in kwargs.items(): 514 field = _GetFieldByName(message_descriptor, field_name) 515 if field is None: 516 raise TypeError('%s() got an unexpected keyword argument "%s"' % 517 (message_descriptor.name, field_name)) 518 if field_value is None: 519 # field=None is the same as no field at all. 520 continue 521 if field.label == _FieldDescriptor.LABEL_REPEATED: 522 copy = field._default_constructor(self) 523 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: # Composite 524 if _IsMapField(field): 525 if _IsMessageMapField(field): 526 for key in field_value: 527 copy[key].MergeFrom(field_value[key]) 528 else: 529 copy.update(field_value) 530 else: 531 for val in field_value: 532 if isinstance(val, dict): 533 copy.add(**val) 534 else: 535 copy.add().MergeFrom(val) 536 else: # Scalar 537 if field.cpp_type == _FieldDescriptor.CPPTYPE_ENUM: 538 field_value = [_GetIntegerEnumValue(field.enum_type, val) 539 for val in field_value] 540 copy.extend(field_value) 541 self._fields[field] = copy 542 elif field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 543 copy = field._default_constructor(self) 544 new_val = field_value 545 if isinstance(field_value, dict): 546 new_val = field.message_type._concrete_class(**field_value) 547 try: 548 copy.MergeFrom(new_val) 549 except TypeError: 550 _ReraiseTypeErrorWithFieldName(message_descriptor.name, field_name) 551 self._fields[field] = copy 552 else: 553 if field.cpp_type == _FieldDescriptor.CPPTYPE_ENUM: 554 field_value = _GetIntegerEnumValue(field.enum_type, field_value) 555 try: 556 setattr(self, field_name, field_value) 557 except TypeError: 558 _ReraiseTypeErrorWithFieldName(message_descriptor.name, field_name)
1312 def MergeFrom(self, msg): 1313 if not isinstance(msg, cls): 1314 raise TypeError( 1315 'Parameter to MergeFrom() must be instance of same class: ' 1316 'expected %s got %s.' % (_FullyQualifiedClassName(cls), 1317 _FullyQualifiedClassName(msg.__class__))) 1318 1319 assert msg is not self 1320 self._Modified() 1321 1322 fields = self._fields 1323 1324 for field, value in msg._fields.items(): 1325 if field.label == LABEL_REPEATED: 1326 field_value = fields.get(field) 1327 if field_value is None: 1328 # Construct a new object to represent this field. 1329 field_value = field._default_constructor(self) 1330 fields[field] = field_value 1331 field_value.MergeFrom(value) 1332 elif field.cpp_type == CPPTYPE_MESSAGE: 1333 if value._is_present_in_parent: 1334 field_value = fields.get(field) 1335 if field_value is None: 1336 # Construct a new object to represent this field. 1337 field_value = field._default_constructor(self) 1338 fields[field] = field_value 1339 field_value.MergeFrom(value) 1340 else: 1341 self._fields[field] = value 1342 if field.containing_oneof: 1343 self._UpdateOneofState(field) 1344 1345 if msg._unknown_fields: 1346 if not self._unknown_fields: 1347 self._unknown_fields = [] 1348 self._unknown_fields.extend(msg._unknown_fields) 1349 # pylint: disable=protected-access 1350 if self._unknown_field_set is None: 1351 self._unknown_field_set = containers.UnknownFieldSet() 1352 self._unknown_field_set._extend(msg._unknown_field_set)
Merges the contents of the specified message into current message.
This method merges the contents of the specified message into the current message. Singular fields that are set in the specified message overwrite the corresponding fields in the current message. Repeated fields are appended. Singular sub-messages and groups are recursively merged.
Arguments:
- other_msg (Message): A message to merge into the current message.
1375def _Clear(self): 1376 # Clear fields. 1377 self._fields = {} 1378 self._unknown_fields = () 1379 # pylint: disable=protected-access 1380 if self._unknown_field_set is not None: 1381 self._unknown_field_set._clear() 1382 self._unknown_field_set = None 1383 1384 self._oneofs = {} 1385 self._Modified()
Clears all data that was set in the message.
1452 def Modified(self): 1453 """Sets the _cached_byte_size_dirty bit to true, 1454 and propagates this to our listener iff this was a state change. 1455 """ 1456 1457 # Note: Some callers check _cached_byte_size_dirty before calling 1458 # _Modified() as an extra optimization. So, if this method is ever 1459 # changed such that it does stuff even when _cached_byte_size_dirty is 1460 # already true, the callers need to be updated. 1461 if not self._cached_byte_size_dirty: 1462 self._cached_byte_size_dirty = True 1463 self._listener_for_children.dirty = True 1464 self._is_present_in_parent = True 1465 self._listener.Modified()
Sets the _cached_byte_size_dirty bit to true, and propagates this to our listener iff this was a state change.
1213 def IsInitialized(self, errors=None): 1214 """Checks if all required fields of a message are set. 1215 1216 Args: 1217 errors: A list which, if provided, will be populated with the field 1218 paths of all missing required fields. 1219 1220 Returns: 1221 True iff the specified message has all required fields set. 1222 """ 1223 1224 # Performance is critical so we avoid HasField() and ListFields(). 1225 1226 for field in required_fields: 1227 if (field not in self._fields or 1228 (field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE and 1229 not self._fields[field]._is_present_in_parent)): 1230 if errors is not None: 1231 errors.extend(self.FindInitializationErrors()) 1232 return False 1233 1234 for field, value in list(self._fields.items()): # dict can change size! 1235 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 1236 if field.label == _FieldDescriptor.LABEL_REPEATED: 1237 if (field.message_type._is_map_entry): 1238 continue 1239 for element in value: 1240 if not element.IsInitialized(): 1241 if errors is not None: 1242 errors.extend(self.FindInitializationErrors()) 1243 return False 1244 elif value._is_present_in_parent and not value.IsInitialized(): 1245 if errors is not None: 1246 errors.extend(self.FindInitializationErrors()) 1247 return False 1248 1249 return True
Checks if all required fields of a message are set.
Arguments:
- errors: A list which, if provided, will be populated with the field paths of all missing required fields.
Returns:
True iff the specified message has all required fields set.
1119 def MergeFromString(self, serialized): 1120 serialized = memoryview(serialized) 1121 length = len(serialized) 1122 try: 1123 if self._InternalParse(serialized, 0, length) != length: 1124 # The only reason _InternalParse would return early is if it 1125 # encountered an end-group tag. 1126 raise message_mod.DecodeError('Unexpected end-group tag.') 1127 except (IndexError, TypeError): 1128 # Now ord(buf[p:p+1]) == ord('') gets TypeError. 1129 raise message_mod.DecodeError('Truncated message.') 1130 except struct.error as e: 1131 raise message_mod.DecodeError(e) 1132 return length # Return this for legacy reasons.
Merges serialized protocol buffer data into this message.
When we find a field in serialized
that is already present
in this message:
- If it's a "repeated" field, we append to the end of our list.
- Else, if it's a scalar, we overwrite our field.
- Else, (it's a nonrepeated composite), we recursively merge into the existing composite.
Arguments:
- serialized (bytes): Any object that allows us to call
memoryview(serialized)
to access a string of bytes using the buffer interface.
Returns:
int: The number of bytes read from
serialized
. For non-group messages, this will always belen(serialized)
, but for messages which are actually groups, this will generally be less thanlen(serialized)
, since we must stop when we reach anEND_GROUP
tag. Note that if we do stop because of anEND_GROUP
tag, the number of bytes returned does not include the bytes for theEND_GROUP
tag information.
Raises:
- DecodeError: if the input cannot be parsed.
1072 def SerializeToString(self, **kwargs): 1073 # Check if the message has all of its required fields set. 1074 if not self.IsInitialized(): 1075 raise message_mod.EncodeError( 1076 'Message %s is missing required fields: %s' % ( 1077 self.DESCRIPTOR.full_name, ','.join(self.FindInitializationErrors()))) 1078 return self.SerializePartialToString(**kwargs)
Serializes the protocol message to a binary string.
Keyword Args:
deterministic (bool): If true, requests deterministic serialization of the protobuf, with predictable ordering of map keys.
Returns:
A binary string representation of the message if all of the required fields in the message are set (i.e. the message is initialized).
Raises:
- EncodeError: if the message isn't initialized (see
IsInitialized()
).
1085 def SerializePartialToString(self, **kwargs): 1086 out = BytesIO() 1087 self._InternalSerialize(out.write, **kwargs) 1088 return out.getvalue()
Serializes the protocol message to a binary string.
This method is similar to SerializeToString but doesn't check if the message is initialized.
Keyword Args:
deterministic (bool): If true, requests deterministic serialization of the protobuf, with predictable ordering of map keys.
Returns:
bytes: A serialized representation of the partial message.
814 def ListFields(self): 815 all_fields = [item for item in self._fields.items() if _IsPresent(item)] 816 all_fields.sort(key = lambda item: item[0].number) 817 return all_fields
Returns a list of (FieldDescriptor, value) tuples for present fields.
A message field is non-empty if HasField() would return true. A singular primitive field is non-empty if HasField() would return true in proto2 or it is non zero in proto3. A repeated field is non-empty if it contains at least one element. The fields are ordered by field number.
Returns:
list[tuple(FieldDescriptor, value)]: field descriptors and values for all fields in the message which are not empty. The values vary by field type.
838 def HasField(self, field_name): 839 try: 840 field = hassable_fields[field_name] 841 except KeyError as exc: 842 raise ValueError('Protocol message %s has no non-repeated field "%s" ' 843 'nor has presence is not available for this field.' % ( 844 message_descriptor.full_name, field_name)) from exc 845 846 if isinstance(field, descriptor_mod.OneofDescriptor): 847 try: 848 return HasField(self, self._oneofs[field].name) 849 except KeyError: 850 return False 851 else: 852 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 853 value = self._fields.get(field) 854 return value is not None and value._is_present_in_parent 855 else: 856 return field in self._fields
Checks if a certain field is set for the message.
For a oneof group, checks if any field inside is set. Note that if the
field_name is not defined in the message descriptor, ValueError
will
be raised.
Arguments:
- field_name (str): The name of the field to check for presence.
Returns:
bool: Whether a value has been set for the named field.
Raises:
- ValueError: if the
field_name
is not a member of this message.
863 def ClearField(self, field_name): 864 try: 865 field = message_descriptor.fields_by_name[field_name] 866 except KeyError: 867 try: 868 field = message_descriptor.oneofs_by_name[field_name] 869 if field in self._oneofs: 870 field = self._oneofs[field] 871 else: 872 return 873 except KeyError: 874 raise ValueError('Protocol message %s has no "%s" field.' % 875 (message_descriptor.name, field_name)) 876 877 if field in self._fields: 878 # To match the C++ implementation, we need to invalidate iterators 879 # for map fields when ClearField() happens. 880 if hasattr(self._fields[field], 'InvalidateIterators'): 881 self._fields[field].InvalidateIterators() 882 883 # Note: If the field is a sub-message, its listener will still point 884 # at us. That's fine, because the worst than can happen is that it 885 # will call _Modified() and invalidate our byte size. Big deal. 886 del self._fields[field] 887 888 if self._oneofs.get(field.containing_oneof, None) is field: 889 del self._oneofs[field.containing_oneof] 890 891 # Always call _Modified() -- even if nothing was changed, this is 892 # a mutating method, and thus calling it should cause the field to become 893 # present in the parent message. 894 self._Modified()
Clears the contents of a given field.
Inside a oneof group, clears the field set. If the name neither refers to a
defined field or oneof group, ValueError
is raised.
Arguments:
- field_name (str): The name of the field to check for presence.
Raises:
- ValueError: if the
field_name
is not a member of this message.
1358 def WhichOneof(self, oneof_name): 1359 """Returns the name of the currently set field inside a oneof, or None.""" 1360 try: 1361 field = message_descriptor.oneofs_by_name[oneof_name] 1362 except KeyError: 1363 raise ValueError( 1364 'Protocol message has no oneof "%s" field.' % oneof_name) 1365 1366 nested_field = self._oneofs.get(field, None) 1367 if nested_field is not None and self.HasField(nested_field.name): 1368 return nested_field.name 1369 else: 1370 return None
Returns the name of the currently set field inside a oneof, or None.
1388def _UnknownFields(self): 1389 warnings.warn( 1390 'message.UnknownFields() is deprecated. Please use the add one ' 1391 'feature unknown_fields.UnknownFieldSet(message) in ' 1392 'unknown_fields.py instead.' 1393 ) 1394 if self._unknown_field_set is None: # pylint: disable=protected-access 1395 # pylint: disable=protected-access 1396 self._unknown_field_set = containers.UnknownFieldSet() 1397 return self._unknown_field_set # pylint: disable=protected-access
Returns the UnknownFieldSet.
Returns:
UnknownFieldSet: The unknown fields stored in this message.
1400def _DiscardUnknownFields(self): 1401 self._unknown_fields = [] 1402 self._unknown_field_set = None # pylint: disable=protected-access 1403 for field, value in self.ListFields(): 1404 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 1405 if _IsMapField(field): 1406 if _IsMessageMapField(field): 1407 for key in value: 1408 value[key].DiscardUnknownFields() 1409 elif field.label == _FieldDescriptor.LABEL_REPEATED: 1410 for sub_message in value: 1411 sub_message.DiscardUnknownFields() 1412 else: 1413 value.DiscardUnknownFields()
Clears all fields in the UnknownFieldSet
.
This operation is recursive for nested message.
1040 def ByteSize(self): 1041 if not self._cached_byte_size_dirty: 1042 return self._cached_byte_size 1043 1044 size = 0 1045 descriptor = self.DESCRIPTOR 1046 if descriptor._is_map_entry: 1047 # Fields of map entry should always be serialized. 1048 key_field = descriptor.fields_by_name['key'] 1049 _MaybeAddEncoder(cls, key_field) 1050 size = key_field._sizer(self.key) 1051 value_field = descriptor.fields_by_name['value'] 1052 _MaybeAddEncoder(cls, value_field) 1053 size += value_field._sizer(self.value) 1054 else: 1055 for field_descriptor, field_value in self.ListFields(): 1056 _MaybeAddEncoder(cls, field_descriptor) 1057 size += field_descriptor._sizer(field_value) 1058 for tag_bytes, value_bytes in self._unknown_fields: 1059 size += len(tag_bytes) + len(value_bytes) 1060 1061 self._cached_byte_size = size 1062 self._cached_byte_size_dirty = False 1063 self._listener_for_children.dirty = False 1064 return size
Returns the serialized size of this message.
Recursively calls ByteSize() on all contained messages.
Returns:
int: The number of bytes required to serialize this message.
784 def RegisterExtension(field_descriptor): 785 field_descriptor.containing_type = cls.DESCRIPTOR 786 # TODO: Use cls.MESSAGE_FACTORY.pool when available. 787 # pylint: disable=protected-access 788 cls.DESCRIPTOR.file.pool._AddExtensionDescriptor(field_descriptor) 789 _AttachFieldHelpers(cls, field_descriptor)
642 def getter(self): 643 field_value = self._fields.get(field) 644 if field_value is None: 645 # Construct a new object to represent this field. 646 field_value = field._default_constructor(self) 647 648 # Atomically check if another thread has preempted us and, if not, swap 649 # in the new object we just created. If someone has preempted us, we 650 # take that object and discard ours. 651 # WARNING: We are relying on setdefault() being atomic. This is true 652 # in CPython but we haven't investigated others. This warning appears 653 # in several other locations in this file. 654 field_value = self._fields.setdefault(field, field_value) 655 return field_value
Getter for vars.
642 def getter(self): 643 field_value = self._fields.get(field) 644 if field_value is None: 645 # Construct a new object to represent this field. 646 field_value = field._default_constructor(self) 647 648 # Atomically check if another thread has preempted us and, if not, swap 649 # in the new object we just created. If someone has preempted us, we 650 # take that object and discard ours. 651 # WARNING: We are relying on setdefault() being atomic. This is true 652 # in CPython but we haven't investigated others. This warning appears 653 # in several other locations in this file. 654 field_value = self._fields.setdefault(field, field_value) 655 return field_value
Getter for coeffs.
685 def getter(self): 686 # TODO: This may be broken since there may not be 687 # default_value. Combine with has_default_value somehow. 688 return self._fields.get(field, default_value)
Getter for offset.
685 def getter(self): 686 # TODO: This may be broken since there may not be 687 # default_value. Combine with has_default_value somehow. 688 return self._fields.get(field, default_value)
Getter for maximize.
1253 def FindInitializationErrors(self): 1254 """Finds required fields which are not initialized. 1255 1256 Returns: 1257 A list of strings. Each string is a path to an uninitialized field from 1258 the top-level message, e.g. "foo.bar[5].baz". 1259 """ 1260 1261 errors = [] # simplify things 1262 1263 for field in required_fields: 1264 if not self.HasField(field.name): 1265 errors.append(field.name) 1266 1267 for field, value in self.ListFields(): 1268 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 1269 if field.is_extension: 1270 name = '(%s)' % field.full_name 1271 else: 1272 name = field.name 1273 1274 if _IsMapField(field): 1275 if _IsMessageMapField(field): 1276 for key in value: 1277 element = value[key] 1278 prefix = '%s[%s].' % (name, key) 1279 sub_errors = element.FindInitializationErrors() 1280 errors += [prefix + error for error in sub_errors] 1281 else: 1282 # ScalarMaps can't have any initialization errors. 1283 pass 1284 elif field.label == _FieldDescriptor.LABEL_REPEATED: 1285 for i in range(len(value)): 1286 element = value[i] 1287 prefix = '%s[%d].' % (name, i) 1288 sub_errors = element.FindInitializationErrors() 1289 errors += [prefix + error for error in sub_errors] 1290 else: 1291 prefix = name + '.' 1292 sub_errors = value.FindInitializationErrors() 1293 errors += [prefix + error for error in sub_errors] 1294 1295 return errors
Finds required fields which are not initialized.
Returns:
A list of strings. Each string is a path to an uninitialized field from the top-level message, e.g. "foo.bar[5].baz".
Inherited Members
- google.protobuf.message.Message
- CopyFrom
- ParseFromString
- HasExtension
- ClearExtension
Abstract base class for protocol messages.
Protocol message classes are almost always generated by the protocol compiler. These generated types subclass Message and implement the methods shown below.
496 def init(self, **kwargs): 497 self._cached_byte_size = 0 498 self._cached_byte_size_dirty = len(kwargs) > 0 499 self._fields = {} 500 # Contains a mapping from oneof field descriptors to the descriptor 501 # of the currently set field in that oneof field. 502 self._oneofs = {} 503 504 # _unknown_fields is () when empty for efficiency, and will be turned into 505 # a list if fields are added. 506 self._unknown_fields = () 507 # _unknown_field_set is None when empty for efficiency, and will be 508 # turned into UnknownFieldSet struct if fields are added. 509 self._unknown_field_set = None # pylint: disable=protected-access 510 self._is_present_in_parent = False 511 self._listener = message_listener_mod.NullMessageListener() 512 self._listener_for_children = _Listener(self) 513 for field_name, field_value in kwargs.items(): 514 field = _GetFieldByName(message_descriptor, field_name) 515 if field is None: 516 raise TypeError('%s() got an unexpected keyword argument "%s"' % 517 (message_descriptor.name, field_name)) 518 if field_value is None: 519 # field=None is the same as no field at all. 520 continue 521 if field.label == _FieldDescriptor.LABEL_REPEATED: 522 copy = field._default_constructor(self) 523 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: # Composite 524 if _IsMapField(field): 525 if _IsMessageMapField(field): 526 for key in field_value: 527 copy[key].MergeFrom(field_value[key]) 528 else: 529 copy.update(field_value) 530 else: 531 for val in field_value: 532 if isinstance(val, dict): 533 copy.add(**val) 534 else: 535 copy.add().MergeFrom(val) 536 else: # Scalar 537 if field.cpp_type == _FieldDescriptor.CPPTYPE_ENUM: 538 field_value = [_GetIntegerEnumValue(field.enum_type, val) 539 for val in field_value] 540 copy.extend(field_value) 541 self._fields[field] = copy 542 elif field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 543 copy = field._default_constructor(self) 544 new_val = field_value 545 if isinstance(field_value, dict): 546 new_val = field.message_type._concrete_class(**field_value) 547 try: 548 copy.MergeFrom(new_val) 549 except TypeError: 550 _ReraiseTypeErrorWithFieldName(message_descriptor.name, field_name) 551 self._fields[field] = copy 552 else: 553 if field.cpp_type == _FieldDescriptor.CPPTYPE_ENUM: 554 field_value = _GetIntegerEnumValue(field.enum_type, field_value) 555 try: 556 setattr(self, field_name, field_value) 557 except TypeError: 558 _ReraiseTypeErrorWithFieldName(message_descriptor.name, field_name)
1312 def MergeFrom(self, msg): 1313 if not isinstance(msg, cls): 1314 raise TypeError( 1315 'Parameter to MergeFrom() must be instance of same class: ' 1316 'expected %s got %s.' % (_FullyQualifiedClassName(cls), 1317 _FullyQualifiedClassName(msg.__class__))) 1318 1319 assert msg is not self 1320 self._Modified() 1321 1322 fields = self._fields 1323 1324 for field, value in msg._fields.items(): 1325 if field.label == LABEL_REPEATED: 1326 field_value = fields.get(field) 1327 if field_value is None: 1328 # Construct a new object to represent this field. 1329 field_value = field._default_constructor(self) 1330 fields[field] = field_value 1331 field_value.MergeFrom(value) 1332 elif field.cpp_type == CPPTYPE_MESSAGE: 1333 if value._is_present_in_parent: 1334 field_value = fields.get(field) 1335 if field_value is None: 1336 # Construct a new object to represent this field. 1337 field_value = field._default_constructor(self) 1338 fields[field] = field_value 1339 field_value.MergeFrom(value) 1340 else: 1341 self._fields[field] = value 1342 if field.containing_oneof: 1343 self._UpdateOneofState(field) 1344 1345 if msg._unknown_fields: 1346 if not self._unknown_fields: 1347 self._unknown_fields = [] 1348 self._unknown_fields.extend(msg._unknown_fields) 1349 # pylint: disable=protected-access 1350 if self._unknown_field_set is None: 1351 self._unknown_field_set = containers.UnknownFieldSet() 1352 self._unknown_field_set._extend(msg._unknown_field_set)
Merges the contents of the specified message into current message.
This method merges the contents of the specified message into the current message. Singular fields that are set in the specified message overwrite the corresponding fields in the current message. Repeated fields are appended. Singular sub-messages and groups are recursively merged.
Arguments:
- other_msg (Message): A message to merge into the current message.
1375def _Clear(self): 1376 # Clear fields. 1377 self._fields = {} 1378 self._unknown_fields = () 1379 # pylint: disable=protected-access 1380 if self._unknown_field_set is not None: 1381 self._unknown_field_set._clear() 1382 self._unknown_field_set = None 1383 1384 self._oneofs = {} 1385 self._Modified()
Clears all data that was set in the message.
1452 def Modified(self): 1453 """Sets the _cached_byte_size_dirty bit to true, 1454 and propagates this to our listener iff this was a state change. 1455 """ 1456 1457 # Note: Some callers check _cached_byte_size_dirty before calling 1458 # _Modified() as an extra optimization. So, if this method is ever 1459 # changed such that it does stuff even when _cached_byte_size_dirty is 1460 # already true, the callers need to be updated. 1461 if not self._cached_byte_size_dirty: 1462 self._cached_byte_size_dirty = True 1463 self._listener_for_children.dirty = True 1464 self._is_present_in_parent = True 1465 self._listener.Modified()
Sets the _cached_byte_size_dirty bit to true, and propagates this to our listener iff this was a state change.
1213 def IsInitialized(self, errors=None): 1214 """Checks if all required fields of a message are set. 1215 1216 Args: 1217 errors: A list which, if provided, will be populated with the field 1218 paths of all missing required fields. 1219 1220 Returns: 1221 True iff the specified message has all required fields set. 1222 """ 1223 1224 # Performance is critical so we avoid HasField() and ListFields(). 1225 1226 for field in required_fields: 1227 if (field not in self._fields or 1228 (field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE and 1229 not self._fields[field]._is_present_in_parent)): 1230 if errors is not None: 1231 errors.extend(self.FindInitializationErrors()) 1232 return False 1233 1234 for field, value in list(self._fields.items()): # dict can change size! 1235 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 1236 if field.label == _FieldDescriptor.LABEL_REPEATED: 1237 if (field.message_type._is_map_entry): 1238 continue 1239 for element in value: 1240 if not element.IsInitialized(): 1241 if errors is not None: 1242 errors.extend(self.FindInitializationErrors()) 1243 return False 1244 elif value._is_present_in_parent and not value.IsInitialized(): 1245 if errors is not None: 1246 errors.extend(self.FindInitializationErrors()) 1247 return False 1248 1249 return True
Checks if all required fields of a message are set.
Arguments:
- errors: A list which, if provided, will be populated with the field paths of all missing required fields.
Returns:
True iff the specified message has all required fields set.
1119 def MergeFromString(self, serialized): 1120 serialized = memoryview(serialized) 1121 length = len(serialized) 1122 try: 1123 if self._InternalParse(serialized, 0, length) != length: 1124 # The only reason _InternalParse would return early is if it 1125 # encountered an end-group tag. 1126 raise message_mod.DecodeError('Unexpected end-group tag.') 1127 except (IndexError, TypeError): 1128 # Now ord(buf[p:p+1]) == ord('') gets TypeError. 1129 raise message_mod.DecodeError('Truncated message.') 1130 except struct.error as e: 1131 raise message_mod.DecodeError(e) 1132 return length # Return this for legacy reasons.
Merges serialized protocol buffer data into this message.
When we find a field in serialized
that is already present
in this message:
- If it's a "repeated" field, we append to the end of our list.
- Else, if it's a scalar, we overwrite our field.
- Else, (it's a nonrepeated composite), we recursively merge into the existing composite.
Arguments:
- serialized (bytes): Any object that allows us to call
memoryview(serialized)
to access a string of bytes using the buffer interface.
Returns:
int: The number of bytes read from
serialized
. For non-group messages, this will always belen(serialized)
, but for messages which are actually groups, this will generally be less thanlen(serialized)
, since we must stop when we reach anEND_GROUP
tag. Note that if we do stop because of anEND_GROUP
tag, the number of bytes returned does not include the bytes for theEND_GROUP
tag information.
Raises:
- DecodeError: if the input cannot be parsed.
1072 def SerializeToString(self, **kwargs): 1073 # Check if the message has all of its required fields set. 1074 if not self.IsInitialized(): 1075 raise message_mod.EncodeError( 1076 'Message %s is missing required fields: %s' % ( 1077 self.DESCRIPTOR.full_name, ','.join(self.FindInitializationErrors()))) 1078 return self.SerializePartialToString(**kwargs)
Serializes the protocol message to a binary string.
Keyword Args:
deterministic (bool): If true, requests deterministic serialization of the protobuf, with predictable ordering of map keys.
Returns:
A binary string representation of the message if all of the required fields in the message are set (i.e. the message is initialized).
Raises:
- EncodeError: if the message isn't initialized (see
IsInitialized()
).
1085 def SerializePartialToString(self, **kwargs): 1086 out = BytesIO() 1087 self._InternalSerialize(out.write, **kwargs) 1088 return out.getvalue()
Serializes the protocol message to a binary string.
This method is similar to SerializeToString but doesn't check if the message is initialized.
Keyword Args:
deterministic (bool): If true, requests deterministic serialization of the protobuf, with predictable ordering of map keys.
Returns:
bytes: A serialized representation of the partial message.
814 def ListFields(self): 815 all_fields = [item for item in self._fields.items() if _IsPresent(item)] 816 all_fields.sort(key = lambda item: item[0].number) 817 return all_fields
Returns a list of (FieldDescriptor, value) tuples for present fields.
A message field is non-empty if HasField() would return true. A singular primitive field is non-empty if HasField() would return true in proto2 or it is non zero in proto3. A repeated field is non-empty if it contains at least one element. The fields are ordered by field number.
Returns:
list[tuple(FieldDescriptor, value)]: field descriptors and values for all fields in the message which are not empty. The values vary by field type.
838 def HasField(self, field_name): 839 try: 840 field = hassable_fields[field_name] 841 except KeyError as exc: 842 raise ValueError('Protocol message %s has no non-repeated field "%s" ' 843 'nor has presence is not available for this field.' % ( 844 message_descriptor.full_name, field_name)) from exc 845 846 if isinstance(field, descriptor_mod.OneofDescriptor): 847 try: 848 return HasField(self, self._oneofs[field].name) 849 except KeyError: 850 return False 851 else: 852 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 853 value = self._fields.get(field) 854 return value is not None and value._is_present_in_parent 855 else: 856 return field in self._fields
Checks if a certain field is set for the message.
For a oneof group, checks if any field inside is set. Note that if the
field_name is not defined in the message descriptor, ValueError
will
be raised.
Arguments:
- field_name (str): The name of the field to check for presence.
Returns:
bool: Whether a value has been set for the named field.
Raises:
- ValueError: if the
field_name
is not a member of this message.
863 def ClearField(self, field_name): 864 try: 865 field = message_descriptor.fields_by_name[field_name] 866 except KeyError: 867 try: 868 field = message_descriptor.oneofs_by_name[field_name] 869 if field in self._oneofs: 870 field = self._oneofs[field] 871 else: 872 return 873 except KeyError: 874 raise ValueError('Protocol message %s has no "%s" field.' % 875 (message_descriptor.name, field_name)) 876 877 if field in self._fields: 878 # To match the C++ implementation, we need to invalidate iterators 879 # for map fields when ClearField() happens. 880 if hasattr(self._fields[field], 'InvalidateIterators'): 881 self._fields[field].InvalidateIterators() 882 883 # Note: If the field is a sub-message, its listener will still point 884 # at us. That's fine, because the worst than can happen is that it 885 # will call _Modified() and invalidate our byte size. Big deal. 886 del self._fields[field] 887 888 if self._oneofs.get(field.containing_oneof, None) is field: 889 del self._oneofs[field.containing_oneof] 890 891 # Always call _Modified() -- even if nothing was changed, this is 892 # a mutating method, and thus calling it should cause the field to become 893 # present in the parent message. 894 self._Modified()
Clears the contents of a given field.
Inside a oneof group, clears the field set. If the name neither refers to a
defined field or oneof group, ValueError
is raised.
Arguments:
- field_name (str): The name of the field to check for presence.
Raises:
- ValueError: if the
field_name
is not a member of this message.
1358 def WhichOneof(self, oneof_name): 1359 """Returns the name of the currently set field inside a oneof, or None.""" 1360 try: 1361 field = message_descriptor.oneofs_by_name[oneof_name] 1362 except KeyError: 1363 raise ValueError( 1364 'Protocol message has no oneof "%s" field.' % oneof_name) 1365 1366 nested_field = self._oneofs.get(field, None) 1367 if nested_field is not None and self.HasField(nested_field.name): 1368 return nested_field.name 1369 else: 1370 return None
Returns the name of the currently set field inside a oneof, or None.
1388def _UnknownFields(self): 1389 warnings.warn( 1390 'message.UnknownFields() is deprecated. Please use the add one ' 1391 'feature unknown_fields.UnknownFieldSet(message) in ' 1392 'unknown_fields.py instead.' 1393 ) 1394 if self._unknown_field_set is None: # pylint: disable=protected-access 1395 # pylint: disable=protected-access 1396 self._unknown_field_set = containers.UnknownFieldSet() 1397 return self._unknown_field_set # pylint: disable=protected-access
Returns the UnknownFieldSet.
Returns:
UnknownFieldSet: The unknown fields stored in this message.
1400def _DiscardUnknownFields(self): 1401 self._unknown_fields = [] 1402 self._unknown_field_set = None # pylint: disable=protected-access 1403 for field, value in self.ListFields(): 1404 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 1405 if _IsMapField(field): 1406 if _IsMessageMapField(field): 1407 for key in value: 1408 value[key].DiscardUnknownFields() 1409 elif field.label == _FieldDescriptor.LABEL_REPEATED: 1410 for sub_message in value: 1411 sub_message.DiscardUnknownFields() 1412 else: 1413 value.DiscardUnknownFields()
Clears all fields in the UnknownFieldSet
.
This operation is recursive for nested message.
1040 def ByteSize(self): 1041 if not self._cached_byte_size_dirty: 1042 return self._cached_byte_size 1043 1044 size = 0 1045 descriptor = self.DESCRIPTOR 1046 if descriptor._is_map_entry: 1047 # Fields of map entry should always be serialized. 1048 key_field = descriptor.fields_by_name['key'] 1049 _MaybeAddEncoder(cls, key_field) 1050 size = key_field._sizer(self.key) 1051 value_field = descriptor.fields_by_name['value'] 1052 _MaybeAddEncoder(cls, value_field) 1053 size += value_field._sizer(self.value) 1054 else: 1055 for field_descriptor, field_value in self.ListFields(): 1056 _MaybeAddEncoder(cls, field_descriptor) 1057 size += field_descriptor._sizer(field_value) 1058 for tag_bytes, value_bytes in self._unknown_fields: 1059 size += len(tag_bytes) + len(value_bytes) 1060 1061 self._cached_byte_size = size 1062 self._cached_byte_size_dirty = False 1063 self._listener_for_children.dirty = False 1064 return size
Returns the serialized size of this message.
Recursively calls ByteSize() on all contained messages.
Returns:
int: The number of bytes required to serialize this message.
784 def RegisterExtension(field_descriptor): 785 field_descriptor.containing_type = cls.DESCRIPTOR 786 # TODO: Use cls.MESSAGE_FACTORY.pool when available. 787 # pylint: disable=protected-access 788 cls.DESCRIPTOR.file.pool._AddExtensionDescriptor(field_descriptor) 789 _AttachFieldHelpers(cls, field_descriptor)
642 def getter(self): 643 field_value = self._fields.get(field) 644 if field_value is None: 645 # Construct a new object to represent this field. 646 field_value = field._default_constructor(self) 647 648 # Atomically check if another thread has preempted us and, if not, swap 649 # in the new object we just created. If someone has preempted us, we 650 # take that object and discard ours. 651 # WARNING: We are relying on setdefault() being atomic. This is true 652 # in CPython but we haven't investigated others. This warning appears 653 # in several other locations in this file. 654 field_value = self._fields.setdefault(field, field_value) 655 return field_value
Getter for variables.
642 def getter(self): 643 field_value = self._fields.get(field) 644 if field_value is None: 645 # Construct a new object to represent this field. 646 field_value = field._default_constructor(self) 647 648 # Atomically check if another thread has preempted us and, if not, swap 649 # in the new object we just created. If someone has preempted us, we 650 # take that object and discard ours. 651 # WARNING: We are relying on setdefault() being atomic. This is true 652 # in CPython but we haven't investigated others. This warning appears 653 # in several other locations in this file. 654 field_value = self._fields.setdefault(field, field_value) 655 return field_value
Getter for exprs.
685 def getter(self): 686 # TODO: This may be broken since there may not be 687 # default_value. Combine with has_default_value somehow. 688 return self._fields.get(field, default_value)
Getter for variable_selection_strategy.
685 def getter(self): 686 # TODO: This may be broken since there may not be 687 # default_value. Combine with has_default_value somehow. 688 return self._fields.get(field, default_value)
Getter for domain_reduction_strategy.
1253 def FindInitializationErrors(self): 1254 """Finds required fields which are not initialized. 1255 1256 Returns: 1257 A list of strings. Each string is a path to an uninitialized field from 1258 the top-level message, e.g. "foo.bar[5].baz". 1259 """ 1260 1261 errors = [] # simplify things 1262 1263 for field in required_fields: 1264 if not self.HasField(field.name): 1265 errors.append(field.name) 1266 1267 for field, value in self.ListFields(): 1268 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 1269 if field.is_extension: 1270 name = '(%s)' % field.full_name 1271 else: 1272 name = field.name 1273 1274 if _IsMapField(field): 1275 if _IsMessageMapField(field): 1276 for key in value: 1277 element = value[key] 1278 prefix = '%s[%s].' % (name, key) 1279 sub_errors = element.FindInitializationErrors() 1280 errors += [prefix + error for error in sub_errors] 1281 else: 1282 # ScalarMaps can't have any initialization errors. 1283 pass 1284 elif field.label == _FieldDescriptor.LABEL_REPEATED: 1285 for i in range(len(value)): 1286 element = value[i] 1287 prefix = '%s[%d].' % (name, i) 1288 sub_errors = element.FindInitializationErrors() 1289 errors += [prefix + error for error in sub_errors] 1290 else: 1291 prefix = name + '.' 1292 sub_errors = value.FindInitializationErrors() 1293 errors += [prefix + error for error in sub_errors] 1294 1295 return errors
Finds required fields which are not initialized.
Returns:
A list of strings. Each string is a path to an uninitialized field from the top-level message, e.g. "foo.bar[5].baz".
Inherited Members
- google.protobuf.message.Message
- CopyFrom
- ParseFromString
- HasExtension
- ClearExtension
Abstract base class for protocol messages.
Protocol message classes are almost always generated by the protocol compiler. These generated types subclass Message and implement the methods shown below.
496 def init(self, **kwargs): 497 self._cached_byte_size = 0 498 self._cached_byte_size_dirty = len(kwargs) > 0 499 self._fields = {} 500 # Contains a mapping from oneof field descriptors to the descriptor 501 # of the currently set field in that oneof field. 502 self._oneofs = {} 503 504 # _unknown_fields is () when empty for efficiency, and will be turned into 505 # a list if fields are added. 506 self._unknown_fields = () 507 # _unknown_field_set is None when empty for efficiency, and will be 508 # turned into UnknownFieldSet struct if fields are added. 509 self._unknown_field_set = None # pylint: disable=protected-access 510 self._is_present_in_parent = False 511 self._listener = message_listener_mod.NullMessageListener() 512 self._listener_for_children = _Listener(self) 513 for field_name, field_value in kwargs.items(): 514 field = _GetFieldByName(message_descriptor, field_name) 515 if field is None: 516 raise TypeError('%s() got an unexpected keyword argument "%s"' % 517 (message_descriptor.name, field_name)) 518 if field_value is None: 519 # field=None is the same as no field at all. 520 continue 521 if field.label == _FieldDescriptor.LABEL_REPEATED: 522 copy = field._default_constructor(self) 523 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: # Composite 524 if _IsMapField(field): 525 if _IsMessageMapField(field): 526 for key in field_value: 527 copy[key].MergeFrom(field_value[key]) 528 else: 529 copy.update(field_value) 530 else: 531 for val in field_value: 532 if isinstance(val, dict): 533 copy.add(**val) 534 else: 535 copy.add().MergeFrom(val) 536 else: # Scalar 537 if field.cpp_type == _FieldDescriptor.CPPTYPE_ENUM: 538 field_value = [_GetIntegerEnumValue(field.enum_type, val) 539 for val in field_value] 540 copy.extend(field_value) 541 self._fields[field] = copy 542 elif field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 543 copy = field._default_constructor(self) 544 new_val = field_value 545 if isinstance(field_value, dict): 546 new_val = field.message_type._concrete_class(**field_value) 547 try: 548 copy.MergeFrom(new_val) 549 except TypeError: 550 _ReraiseTypeErrorWithFieldName(message_descriptor.name, field_name) 551 self._fields[field] = copy 552 else: 553 if field.cpp_type == _FieldDescriptor.CPPTYPE_ENUM: 554 field_value = _GetIntegerEnumValue(field.enum_type, field_value) 555 try: 556 setattr(self, field_name, field_value) 557 except TypeError: 558 _ReraiseTypeErrorWithFieldName(message_descriptor.name, field_name)
1312 def MergeFrom(self, msg): 1313 if not isinstance(msg, cls): 1314 raise TypeError( 1315 'Parameter to MergeFrom() must be instance of same class: ' 1316 'expected %s got %s.' % (_FullyQualifiedClassName(cls), 1317 _FullyQualifiedClassName(msg.__class__))) 1318 1319 assert msg is not self 1320 self._Modified() 1321 1322 fields = self._fields 1323 1324 for field, value in msg._fields.items(): 1325 if field.label == LABEL_REPEATED: 1326 field_value = fields.get(field) 1327 if field_value is None: 1328 # Construct a new object to represent this field. 1329 field_value = field._default_constructor(self) 1330 fields[field] = field_value 1331 field_value.MergeFrom(value) 1332 elif field.cpp_type == CPPTYPE_MESSAGE: 1333 if value._is_present_in_parent: 1334 field_value = fields.get(field) 1335 if field_value is None: 1336 # Construct a new object to represent this field. 1337 field_value = field._default_constructor(self) 1338 fields[field] = field_value 1339 field_value.MergeFrom(value) 1340 else: 1341 self._fields[field] = value 1342 if field.containing_oneof: 1343 self._UpdateOneofState(field) 1344 1345 if msg._unknown_fields: 1346 if not self._unknown_fields: 1347 self._unknown_fields = [] 1348 self._unknown_fields.extend(msg._unknown_fields) 1349 # pylint: disable=protected-access 1350 if self._unknown_field_set is None: 1351 self._unknown_field_set = containers.UnknownFieldSet() 1352 self._unknown_field_set._extend(msg._unknown_field_set)
Merges the contents of the specified message into current message.
This method merges the contents of the specified message into the current message. Singular fields that are set in the specified message overwrite the corresponding fields in the current message. Repeated fields are appended. Singular sub-messages and groups are recursively merged.
Arguments:
- other_msg (Message): A message to merge into the current message.
1375def _Clear(self): 1376 # Clear fields. 1377 self._fields = {} 1378 self._unknown_fields = () 1379 # pylint: disable=protected-access 1380 if self._unknown_field_set is not None: 1381 self._unknown_field_set._clear() 1382 self._unknown_field_set = None 1383 1384 self._oneofs = {} 1385 self._Modified()
Clears all data that was set in the message.
1452 def Modified(self): 1453 """Sets the _cached_byte_size_dirty bit to true, 1454 and propagates this to our listener iff this was a state change. 1455 """ 1456 1457 # Note: Some callers check _cached_byte_size_dirty before calling 1458 # _Modified() as an extra optimization. So, if this method is ever 1459 # changed such that it does stuff even when _cached_byte_size_dirty is 1460 # already true, the callers need to be updated. 1461 if not self._cached_byte_size_dirty: 1462 self._cached_byte_size_dirty = True 1463 self._listener_for_children.dirty = True 1464 self._is_present_in_parent = True 1465 self._listener.Modified()
Sets the _cached_byte_size_dirty bit to true, and propagates this to our listener iff this was a state change.
1213 def IsInitialized(self, errors=None): 1214 """Checks if all required fields of a message are set. 1215 1216 Args: 1217 errors: A list which, if provided, will be populated with the field 1218 paths of all missing required fields. 1219 1220 Returns: 1221 True iff the specified message has all required fields set. 1222 """ 1223 1224 # Performance is critical so we avoid HasField() and ListFields(). 1225 1226 for field in required_fields: 1227 if (field not in self._fields or 1228 (field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE and 1229 not self._fields[field]._is_present_in_parent)): 1230 if errors is not None: 1231 errors.extend(self.FindInitializationErrors()) 1232 return False 1233 1234 for field, value in list(self._fields.items()): # dict can change size! 1235 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 1236 if field.label == _FieldDescriptor.LABEL_REPEATED: 1237 if (field.message_type._is_map_entry): 1238 continue 1239 for element in value: 1240 if not element.IsInitialized(): 1241 if errors is not None: 1242 errors.extend(self.FindInitializationErrors()) 1243 return False 1244 elif value._is_present_in_parent and not value.IsInitialized(): 1245 if errors is not None: 1246 errors.extend(self.FindInitializationErrors()) 1247 return False 1248 1249 return True
Checks if all required fields of a message are set.
Arguments:
- errors: A list which, if provided, will be populated with the field paths of all missing required fields.
Returns:
True iff the specified message has all required fields set.
1119 def MergeFromString(self, serialized): 1120 serialized = memoryview(serialized) 1121 length = len(serialized) 1122 try: 1123 if self._InternalParse(serialized, 0, length) != length: 1124 # The only reason _InternalParse would return early is if it 1125 # encountered an end-group tag. 1126 raise message_mod.DecodeError('Unexpected end-group tag.') 1127 except (IndexError, TypeError): 1128 # Now ord(buf[p:p+1]) == ord('') gets TypeError. 1129 raise message_mod.DecodeError('Truncated message.') 1130 except struct.error as e: 1131 raise message_mod.DecodeError(e) 1132 return length # Return this for legacy reasons.
Merges serialized protocol buffer data into this message.
When we find a field in serialized
that is already present
in this message:
- If it's a "repeated" field, we append to the end of our list.
- Else, if it's a scalar, we overwrite our field.
- Else, (it's a nonrepeated composite), we recursively merge into the existing composite.
Arguments:
- serialized (bytes): Any object that allows us to call
memoryview(serialized)
to access a string of bytes using the buffer interface.
Returns:
int: The number of bytes read from
serialized
. For non-group messages, this will always belen(serialized)
, but for messages which are actually groups, this will generally be less thanlen(serialized)
, since we must stop when we reach anEND_GROUP
tag. Note that if we do stop because of anEND_GROUP
tag, the number of bytes returned does not include the bytes for theEND_GROUP
tag information.
Raises:
- DecodeError: if the input cannot be parsed.
1072 def SerializeToString(self, **kwargs): 1073 # Check if the message has all of its required fields set. 1074 if not self.IsInitialized(): 1075 raise message_mod.EncodeError( 1076 'Message %s is missing required fields: %s' % ( 1077 self.DESCRIPTOR.full_name, ','.join(self.FindInitializationErrors()))) 1078 return self.SerializePartialToString(**kwargs)
Serializes the protocol message to a binary string.
Keyword Args:
deterministic (bool): If true, requests deterministic serialization of the protobuf, with predictable ordering of map keys.
Returns:
A binary string representation of the message if all of the required fields in the message are set (i.e. the message is initialized).
Raises:
- EncodeError: if the message isn't initialized (see
IsInitialized()
).
1085 def SerializePartialToString(self, **kwargs): 1086 out = BytesIO() 1087 self._InternalSerialize(out.write, **kwargs) 1088 return out.getvalue()
Serializes the protocol message to a binary string.
This method is similar to SerializeToString but doesn't check if the message is initialized.
Keyword Args:
deterministic (bool): If true, requests deterministic serialization of the protobuf, with predictable ordering of map keys.
Returns:
bytes: A serialized representation of the partial message.
814 def ListFields(self): 815 all_fields = [item for item in self._fields.items() if _IsPresent(item)] 816 all_fields.sort(key = lambda item: item[0].number) 817 return all_fields
Returns a list of (FieldDescriptor, value) tuples for present fields.
A message field is non-empty if HasField() would return true. A singular primitive field is non-empty if HasField() would return true in proto2 or it is non zero in proto3. A repeated field is non-empty if it contains at least one element. The fields are ordered by field number.
Returns:
list[tuple(FieldDescriptor, value)]: field descriptors and values for all fields in the message which are not empty. The values vary by field type.
838 def HasField(self, field_name): 839 try: 840 field = hassable_fields[field_name] 841 except KeyError as exc: 842 raise ValueError('Protocol message %s has no non-repeated field "%s" ' 843 'nor has presence is not available for this field.' % ( 844 message_descriptor.full_name, field_name)) from exc 845 846 if isinstance(field, descriptor_mod.OneofDescriptor): 847 try: 848 return HasField(self, self._oneofs[field].name) 849 except KeyError: 850 return False 851 else: 852 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 853 value = self._fields.get(field) 854 return value is not None and value._is_present_in_parent 855 else: 856 return field in self._fields
Checks if a certain field is set for the message.
For a oneof group, checks if any field inside is set. Note that if the
field_name is not defined in the message descriptor, ValueError
will
be raised.
Arguments:
- field_name (str): The name of the field to check for presence.
Returns:
bool: Whether a value has been set for the named field.
Raises:
- ValueError: if the
field_name
is not a member of this message.
863 def ClearField(self, field_name): 864 try: 865 field = message_descriptor.fields_by_name[field_name] 866 except KeyError: 867 try: 868 field = message_descriptor.oneofs_by_name[field_name] 869 if field in self._oneofs: 870 field = self._oneofs[field] 871 else: 872 return 873 except KeyError: 874 raise ValueError('Protocol message %s has no "%s" field.' % 875 (message_descriptor.name, field_name)) 876 877 if field in self._fields: 878 # To match the C++ implementation, we need to invalidate iterators 879 # for map fields when ClearField() happens. 880 if hasattr(self._fields[field], 'InvalidateIterators'): 881 self._fields[field].InvalidateIterators() 882 883 # Note: If the field is a sub-message, its listener will still point 884 # at us. That's fine, because the worst than can happen is that it 885 # will call _Modified() and invalidate our byte size. Big deal. 886 del self._fields[field] 887 888 if self._oneofs.get(field.containing_oneof, None) is field: 889 del self._oneofs[field.containing_oneof] 890 891 # Always call _Modified() -- even if nothing was changed, this is 892 # a mutating method, and thus calling it should cause the field to become 893 # present in the parent message. 894 self._Modified()
Clears the contents of a given field.
Inside a oneof group, clears the field set. If the name neither refers to a
defined field or oneof group, ValueError
is raised.
Arguments:
- field_name (str): The name of the field to check for presence.
Raises:
- ValueError: if the
field_name
is not a member of this message.
1358 def WhichOneof(self, oneof_name): 1359 """Returns the name of the currently set field inside a oneof, or None.""" 1360 try: 1361 field = message_descriptor.oneofs_by_name[oneof_name] 1362 except KeyError: 1363 raise ValueError( 1364 'Protocol message has no oneof "%s" field.' % oneof_name) 1365 1366 nested_field = self._oneofs.get(field, None) 1367 if nested_field is not None and self.HasField(nested_field.name): 1368 return nested_field.name 1369 else: 1370 return None
Returns the name of the currently set field inside a oneof, or None.
1388def _UnknownFields(self): 1389 warnings.warn( 1390 'message.UnknownFields() is deprecated. Please use the add one ' 1391 'feature unknown_fields.UnknownFieldSet(message) in ' 1392 'unknown_fields.py instead.' 1393 ) 1394 if self._unknown_field_set is None: # pylint: disable=protected-access 1395 # pylint: disable=protected-access 1396 self._unknown_field_set = containers.UnknownFieldSet() 1397 return self._unknown_field_set # pylint: disable=protected-access
Returns the UnknownFieldSet.
Returns:
UnknownFieldSet: The unknown fields stored in this message.
1400def _DiscardUnknownFields(self): 1401 self._unknown_fields = [] 1402 self._unknown_field_set = None # pylint: disable=protected-access 1403 for field, value in self.ListFields(): 1404 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 1405 if _IsMapField(field): 1406 if _IsMessageMapField(field): 1407 for key in value: 1408 value[key].DiscardUnknownFields() 1409 elif field.label == _FieldDescriptor.LABEL_REPEATED: 1410 for sub_message in value: 1411 sub_message.DiscardUnknownFields() 1412 else: 1413 value.DiscardUnknownFields()
Clears all fields in the UnknownFieldSet
.
This operation is recursive for nested message.
1040 def ByteSize(self): 1041 if not self._cached_byte_size_dirty: 1042 return self._cached_byte_size 1043 1044 size = 0 1045 descriptor = self.DESCRIPTOR 1046 if descriptor._is_map_entry: 1047 # Fields of map entry should always be serialized. 1048 key_field = descriptor.fields_by_name['key'] 1049 _MaybeAddEncoder(cls, key_field) 1050 size = key_field._sizer(self.key) 1051 value_field = descriptor.fields_by_name['value'] 1052 _MaybeAddEncoder(cls, value_field) 1053 size += value_field._sizer(self.value) 1054 else: 1055 for field_descriptor, field_value in self.ListFields(): 1056 _MaybeAddEncoder(cls, field_descriptor) 1057 size += field_descriptor._sizer(field_value) 1058 for tag_bytes, value_bytes in self._unknown_fields: 1059 size += len(tag_bytes) + len(value_bytes) 1060 1061 self._cached_byte_size = size 1062 self._cached_byte_size_dirty = False 1063 self._listener_for_children.dirty = False 1064 return size
Returns the serialized size of this message.
Recursively calls ByteSize() on all contained messages.
Returns:
int: The number of bytes required to serialize this message.
784 def RegisterExtension(field_descriptor): 785 field_descriptor.containing_type = cls.DESCRIPTOR 786 # TODO: Use cls.MESSAGE_FACTORY.pool when available. 787 # pylint: disable=protected-access 788 cls.DESCRIPTOR.file.pool._AddExtensionDescriptor(field_descriptor) 789 _AttachFieldHelpers(cls, field_descriptor)
642 def getter(self): 643 field_value = self._fields.get(field) 644 if field_value is None: 645 # Construct a new object to represent this field. 646 field_value = field._default_constructor(self) 647 648 # Atomically check if another thread has preempted us and, if not, swap 649 # in the new object we just created. If someone has preempted us, we 650 # take that object and discard ours. 651 # WARNING: We are relying on setdefault() being atomic. This is true 652 # in CPython but we haven't investigated others. This warning appears 653 # in several other locations in this file. 654 field_value = self._fields.setdefault(field, field_value) 655 return field_value
Getter for vars.
642 def getter(self): 643 field_value = self._fields.get(field) 644 if field_value is None: 645 # Construct a new object to represent this field. 646 field_value = field._default_constructor(self) 647 648 # Atomically check if another thread has preempted us and, if not, swap 649 # in the new object we just created. If someone has preempted us, we 650 # take that object and discard ours. 651 # WARNING: We are relying on setdefault() being atomic. This is true 652 # in CPython but we haven't investigated others. This warning appears 653 # in several other locations in this file. 654 field_value = self._fields.setdefault(field, field_value) 655 return field_value
Getter for values.
1253 def FindInitializationErrors(self): 1254 """Finds required fields which are not initialized. 1255 1256 Returns: 1257 A list of strings. Each string is a path to an uninitialized field from 1258 the top-level message, e.g. "foo.bar[5].baz". 1259 """ 1260 1261 errors = [] # simplify things 1262 1263 for field in required_fields: 1264 if not self.HasField(field.name): 1265 errors.append(field.name) 1266 1267 for field, value in self.ListFields(): 1268 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 1269 if field.is_extension: 1270 name = '(%s)' % field.full_name 1271 else: 1272 name = field.name 1273 1274 if _IsMapField(field): 1275 if _IsMessageMapField(field): 1276 for key in value: 1277 element = value[key] 1278 prefix = '%s[%s].' % (name, key) 1279 sub_errors = element.FindInitializationErrors() 1280 errors += [prefix + error for error in sub_errors] 1281 else: 1282 # ScalarMaps can't have any initialization errors. 1283 pass 1284 elif field.label == _FieldDescriptor.LABEL_REPEATED: 1285 for i in range(len(value)): 1286 element = value[i] 1287 prefix = '%s[%d].' % (name, i) 1288 sub_errors = element.FindInitializationErrors() 1289 errors += [prefix + error for error in sub_errors] 1290 else: 1291 prefix = name + '.' 1292 sub_errors = value.FindInitializationErrors() 1293 errors += [prefix + error for error in sub_errors] 1294 1295 return errors
Finds required fields which are not initialized.
Returns:
A list of strings. Each string is a path to an uninitialized field from the top-level message, e.g. "foo.bar[5].baz".
Inherited Members
- google.protobuf.message.Message
- CopyFrom
- ParseFromString
- HasExtension
- ClearExtension
Abstract base class for protocol messages.
Protocol message classes are almost always generated by the protocol compiler. These generated types subclass Message and implement the methods shown below.
496 def init(self, **kwargs): 497 self._cached_byte_size = 0 498 self._cached_byte_size_dirty = len(kwargs) > 0 499 self._fields = {} 500 # Contains a mapping from oneof field descriptors to the descriptor 501 # of the currently set field in that oneof field. 502 self._oneofs = {} 503 504 # _unknown_fields is () when empty for efficiency, and will be turned into 505 # a list if fields are added. 506 self._unknown_fields = () 507 # _unknown_field_set is None when empty for efficiency, and will be 508 # turned into UnknownFieldSet struct if fields are added. 509 self._unknown_field_set = None # pylint: disable=protected-access 510 self._is_present_in_parent = False 511 self._listener = message_listener_mod.NullMessageListener() 512 self._listener_for_children = _Listener(self) 513 for field_name, field_value in kwargs.items(): 514 field = _GetFieldByName(message_descriptor, field_name) 515 if field is None: 516 raise TypeError('%s() got an unexpected keyword argument "%s"' % 517 (message_descriptor.name, field_name)) 518 if field_value is None: 519 # field=None is the same as no field at all. 520 continue 521 if field.label == _FieldDescriptor.LABEL_REPEATED: 522 copy = field._default_constructor(self) 523 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: # Composite 524 if _IsMapField(field): 525 if _IsMessageMapField(field): 526 for key in field_value: 527 copy[key].MergeFrom(field_value[key]) 528 else: 529 copy.update(field_value) 530 else: 531 for val in field_value: 532 if isinstance(val, dict): 533 copy.add(**val) 534 else: 535 copy.add().MergeFrom(val) 536 else: # Scalar 537 if field.cpp_type == _FieldDescriptor.CPPTYPE_ENUM: 538 field_value = [_GetIntegerEnumValue(field.enum_type, val) 539 for val in field_value] 540 copy.extend(field_value) 541 self._fields[field] = copy 542 elif field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 543 copy = field._default_constructor(self) 544 new_val = field_value 545 if isinstance(field_value, dict): 546 new_val = field.message_type._concrete_class(**field_value) 547 try: 548 copy.MergeFrom(new_val) 549 except TypeError: 550 _ReraiseTypeErrorWithFieldName(message_descriptor.name, field_name) 551 self._fields[field] = copy 552 else: 553 if field.cpp_type == _FieldDescriptor.CPPTYPE_ENUM: 554 field_value = _GetIntegerEnumValue(field.enum_type, field_value) 555 try: 556 setattr(self, field_name, field_value) 557 except TypeError: 558 _ReraiseTypeErrorWithFieldName(message_descriptor.name, field_name)
1312 def MergeFrom(self, msg): 1313 if not isinstance(msg, cls): 1314 raise TypeError( 1315 'Parameter to MergeFrom() must be instance of same class: ' 1316 'expected %s got %s.' % (_FullyQualifiedClassName(cls), 1317 _FullyQualifiedClassName(msg.__class__))) 1318 1319 assert msg is not self 1320 self._Modified() 1321 1322 fields = self._fields 1323 1324 for field, value in msg._fields.items(): 1325 if field.label == LABEL_REPEATED: 1326 field_value = fields.get(field) 1327 if field_value is None: 1328 # Construct a new object to represent this field. 1329 field_value = field._default_constructor(self) 1330 fields[field] = field_value 1331 field_value.MergeFrom(value) 1332 elif field.cpp_type == CPPTYPE_MESSAGE: 1333 if value._is_present_in_parent: 1334 field_value = fields.get(field) 1335 if field_value is None: 1336 # Construct a new object to represent this field. 1337 field_value = field._default_constructor(self) 1338 fields[field] = field_value 1339 field_value.MergeFrom(value) 1340 else: 1341 self._fields[field] = value 1342 if field.containing_oneof: 1343 self._UpdateOneofState(field) 1344 1345 if msg._unknown_fields: 1346 if not self._unknown_fields: 1347 self._unknown_fields = [] 1348 self._unknown_fields.extend(msg._unknown_fields) 1349 # pylint: disable=protected-access 1350 if self._unknown_field_set is None: 1351 self._unknown_field_set = containers.UnknownFieldSet() 1352 self._unknown_field_set._extend(msg._unknown_field_set)
Merges the contents of the specified message into current message.
This method merges the contents of the specified message into the current message. Singular fields that are set in the specified message overwrite the corresponding fields in the current message. Repeated fields are appended. Singular sub-messages and groups are recursively merged.
Arguments:
- other_msg (Message): A message to merge into the current message.
1375def _Clear(self): 1376 # Clear fields. 1377 self._fields = {} 1378 self._unknown_fields = () 1379 # pylint: disable=protected-access 1380 if self._unknown_field_set is not None: 1381 self._unknown_field_set._clear() 1382 self._unknown_field_set = None 1383 1384 self._oneofs = {} 1385 self._Modified()
Clears all data that was set in the message.
1452 def Modified(self): 1453 """Sets the _cached_byte_size_dirty bit to true, 1454 and propagates this to our listener iff this was a state change. 1455 """ 1456 1457 # Note: Some callers check _cached_byte_size_dirty before calling 1458 # _Modified() as an extra optimization. So, if this method is ever 1459 # changed such that it does stuff even when _cached_byte_size_dirty is 1460 # already true, the callers need to be updated. 1461 if not self._cached_byte_size_dirty: 1462 self._cached_byte_size_dirty = True 1463 self._listener_for_children.dirty = True 1464 self._is_present_in_parent = True 1465 self._listener.Modified()
Sets the _cached_byte_size_dirty bit to true, and propagates this to our listener iff this was a state change.
1213 def IsInitialized(self, errors=None): 1214 """Checks if all required fields of a message are set. 1215 1216 Args: 1217 errors: A list which, if provided, will be populated with the field 1218 paths of all missing required fields. 1219 1220 Returns: 1221 True iff the specified message has all required fields set. 1222 """ 1223 1224 # Performance is critical so we avoid HasField() and ListFields(). 1225 1226 for field in required_fields: 1227 if (field not in self._fields or 1228 (field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE and 1229 not self._fields[field]._is_present_in_parent)): 1230 if errors is not None: 1231 errors.extend(self.FindInitializationErrors()) 1232 return False 1233 1234 for field, value in list(self._fields.items()): # dict can change size! 1235 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 1236 if field.label == _FieldDescriptor.LABEL_REPEATED: 1237 if (field.message_type._is_map_entry): 1238 continue 1239 for element in value: 1240 if not element.IsInitialized(): 1241 if errors is not None: 1242 errors.extend(self.FindInitializationErrors()) 1243 return False 1244 elif value._is_present_in_parent and not value.IsInitialized(): 1245 if errors is not None: 1246 errors.extend(self.FindInitializationErrors()) 1247 return False 1248 1249 return True
Checks if all required fields of a message are set.
Arguments:
- errors: A list which, if provided, will be populated with the field paths of all missing required fields.
Returns:
True iff the specified message has all required fields set.
1119 def MergeFromString(self, serialized): 1120 serialized = memoryview(serialized) 1121 length = len(serialized) 1122 try: 1123 if self._InternalParse(serialized, 0, length) != length: 1124 # The only reason _InternalParse would return early is if it 1125 # encountered an end-group tag. 1126 raise message_mod.DecodeError('Unexpected end-group tag.') 1127 except (IndexError, TypeError): 1128 # Now ord(buf[p:p+1]) == ord('') gets TypeError. 1129 raise message_mod.DecodeError('Truncated message.') 1130 except struct.error as e: 1131 raise message_mod.DecodeError(e) 1132 return length # Return this for legacy reasons.
Merges serialized protocol buffer data into this message.
When we find a field in serialized
that is already present
in this message:
- If it's a "repeated" field, we append to the end of our list.
- Else, if it's a scalar, we overwrite our field.
- Else, (it's a nonrepeated composite), we recursively merge into the existing composite.
Arguments:
- serialized (bytes): Any object that allows us to call
memoryview(serialized)
to access a string of bytes using the buffer interface.
Returns:
int: The number of bytes read from
serialized
. For non-group messages, this will always belen(serialized)
, but for messages which are actually groups, this will generally be less thanlen(serialized)
, since we must stop when we reach anEND_GROUP
tag. Note that if we do stop because of anEND_GROUP
tag, the number of bytes returned does not include the bytes for theEND_GROUP
tag information.
Raises:
- DecodeError: if the input cannot be parsed.
1072 def SerializeToString(self, **kwargs): 1073 # Check if the message has all of its required fields set. 1074 if not self.IsInitialized(): 1075 raise message_mod.EncodeError( 1076 'Message %s is missing required fields: %s' % ( 1077 self.DESCRIPTOR.full_name, ','.join(self.FindInitializationErrors()))) 1078 return self.SerializePartialToString(**kwargs)
Serializes the protocol message to a binary string.
Keyword Args:
deterministic (bool): If true, requests deterministic serialization of the protobuf, with predictable ordering of map keys.
Returns:
A binary string representation of the message if all of the required fields in the message are set (i.e. the message is initialized).
Raises:
- EncodeError: if the message isn't initialized (see
IsInitialized()
).
1085 def SerializePartialToString(self, **kwargs): 1086 out = BytesIO() 1087 self._InternalSerialize(out.write, **kwargs) 1088 return out.getvalue()
Serializes the protocol message to a binary string.
This method is similar to SerializeToString but doesn't check if the message is initialized.
Keyword Args:
deterministic (bool): If true, requests deterministic serialization of the protobuf, with predictable ordering of map keys.
Returns:
bytes: A serialized representation of the partial message.
814 def ListFields(self): 815 all_fields = [item for item in self._fields.items() if _IsPresent(item)] 816 all_fields.sort(key = lambda item: item[0].number) 817 return all_fields
Returns a list of (FieldDescriptor, value) tuples for present fields.
A message field is non-empty if HasField() would return true. A singular primitive field is non-empty if HasField() would return true in proto2 or it is non zero in proto3. A repeated field is non-empty if it contains at least one element. The fields are ordered by field number.
Returns:
list[tuple(FieldDescriptor, value)]: field descriptors and values for all fields in the message which are not empty. The values vary by field type.
838 def HasField(self, field_name): 839 try: 840 field = hassable_fields[field_name] 841 except KeyError as exc: 842 raise ValueError('Protocol message %s has no non-repeated field "%s" ' 843 'nor has presence is not available for this field.' % ( 844 message_descriptor.full_name, field_name)) from exc 845 846 if isinstance(field, descriptor_mod.OneofDescriptor): 847 try: 848 return HasField(self, self._oneofs[field].name) 849 except KeyError: 850 return False 851 else: 852 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 853 value = self._fields.get(field) 854 return value is not None and value._is_present_in_parent 855 else: 856 return field in self._fields
Checks if a certain field is set for the message.
For a oneof group, checks if any field inside is set. Note that if the
field_name is not defined in the message descriptor, ValueError
will
be raised.
Arguments:
- field_name (str): The name of the field to check for presence.
Returns:
bool: Whether a value has been set for the named field.
Raises:
- ValueError: if the
field_name
is not a member of this message.
863 def ClearField(self, field_name): 864 try: 865 field = message_descriptor.fields_by_name[field_name] 866 except KeyError: 867 try: 868 field = message_descriptor.oneofs_by_name[field_name] 869 if field in self._oneofs: 870 field = self._oneofs[field] 871 else: 872 return 873 except KeyError: 874 raise ValueError('Protocol message %s has no "%s" field.' % 875 (message_descriptor.name, field_name)) 876 877 if field in self._fields: 878 # To match the C++ implementation, we need to invalidate iterators 879 # for map fields when ClearField() happens. 880 if hasattr(self._fields[field], 'InvalidateIterators'): 881 self._fields[field].InvalidateIterators() 882 883 # Note: If the field is a sub-message, its listener will still point 884 # at us. That's fine, because the worst than can happen is that it 885 # will call _Modified() and invalidate our byte size. Big deal. 886 del self._fields[field] 887 888 if self._oneofs.get(field.containing_oneof, None) is field: 889 del self._oneofs[field.containing_oneof] 890 891 # Always call _Modified() -- even if nothing was changed, this is 892 # a mutating method, and thus calling it should cause the field to become 893 # present in the parent message. 894 self._Modified()
Clears the contents of a given field.
Inside a oneof group, clears the field set. If the name neither refers to a
defined field or oneof group, ValueError
is raised.
Arguments:
- field_name (str): The name of the field to check for presence.
Raises:
- ValueError: if the
field_name
is not a member of this message.
1358 def WhichOneof(self, oneof_name): 1359 """Returns the name of the currently set field inside a oneof, or None.""" 1360 try: 1361 field = message_descriptor.oneofs_by_name[oneof_name] 1362 except KeyError: 1363 raise ValueError( 1364 'Protocol message has no oneof "%s" field.' % oneof_name) 1365 1366 nested_field = self._oneofs.get(field, None) 1367 if nested_field is not None and self.HasField(nested_field.name): 1368 return nested_field.name 1369 else: 1370 return None
Returns the name of the currently set field inside a oneof, or None.
1388def _UnknownFields(self): 1389 warnings.warn( 1390 'message.UnknownFields() is deprecated. Please use the add one ' 1391 'feature unknown_fields.UnknownFieldSet(message) in ' 1392 'unknown_fields.py instead.' 1393 ) 1394 if self._unknown_field_set is None: # pylint: disable=protected-access 1395 # pylint: disable=protected-access 1396 self._unknown_field_set = containers.UnknownFieldSet() 1397 return self._unknown_field_set # pylint: disable=protected-access
Returns the UnknownFieldSet.
Returns:
UnknownFieldSet: The unknown fields stored in this message.
1400def _DiscardUnknownFields(self): 1401 self._unknown_fields = [] 1402 self._unknown_field_set = None # pylint: disable=protected-access 1403 for field, value in self.ListFields(): 1404 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 1405 if _IsMapField(field): 1406 if _IsMessageMapField(field): 1407 for key in value: 1408 value[key].DiscardUnknownFields() 1409 elif field.label == _FieldDescriptor.LABEL_REPEATED: 1410 for sub_message in value: 1411 sub_message.DiscardUnknownFields() 1412 else: 1413 value.DiscardUnknownFields()
Clears all fields in the UnknownFieldSet
.
This operation is recursive for nested message.
1040 def ByteSize(self): 1041 if not self._cached_byte_size_dirty: 1042 return self._cached_byte_size 1043 1044 size = 0 1045 descriptor = self.DESCRIPTOR 1046 if descriptor._is_map_entry: 1047 # Fields of map entry should always be serialized. 1048 key_field = descriptor.fields_by_name['key'] 1049 _MaybeAddEncoder(cls, key_field) 1050 size = key_field._sizer(self.key) 1051 value_field = descriptor.fields_by_name['value'] 1052 _MaybeAddEncoder(cls, value_field) 1053 size += value_field._sizer(self.value) 1054 else: 1055 for field_descriptor, field_value in self.ListFields(): 1056 _MaybeAddEncoder(cls, field_descriptor) 1057 size += field_descriptor._sizer(field_value) 1058 for tag_bytes, value_bytes in self._unknown_fields: 1059 size += len(tag_bytes) + len(value_bytes) 1060 1061 self._cached_byte_size = size 1062 self._cached_byte_size_dirty = False 1063 self._listener_for_children.dirty = False 1064 return size
Returns the serialized size of this message.
Recursively calls ByteSize() on all contained messages.
Returns:
int: The number of bytes required to serialize this message.
784 def RegisterExtension(field_descriptor): 785 field_descriptor.containing_type = cls.DESCRIPTOR 786 # TODO: Use cls.MESSAGE_FACTORY.pool when available. 787 # pylint: disable=protected-access 788 cls.DESCRIPTOR.file.pool._AddExtensionDescriptor(field_descriptor) 789 _AttachFieldHelpers(cls, field_descriptor)
642 def getter(self): 643 field_value = self._fields.get(field) 644 if field_value is None: 645 # Construct a new object to represent this field. 646 field_value = field._default_constructor(self) 647 648 # Atomically check if another thread has preempted us and, if not, swap 649 # in the new object we just created. If someone has preempted us, we 650 # take that object and discard ours. 651 # WARNING: We are relying on setdefault() being atomic. This is true 652 # in CPython but we haven't investigated others. This warning appears 653 # in several other locations in this file. 654 field_value = self._fields.setdefault(field, field_value) 655 return field_value
Getter for support.
642 def getter(self): 643 field_value = self._fields.get(field) 644 if field_value is None: 645 # Construct a new object to represent this field. 646 field_value = field._default_constructor(self) 647 648 # Atomically check if another thread has preempted us and, if not, swap 649 # in the new object we just created. If someone has preempted us, we 650 # take that object and discard ours. 651 # WARNING: We are relying on setdefault() being atomic. This is true 652 # in CPython but we haven't investigated others. This warning appears 653 # in several other locations in this file. 654 field_value = self._fields.setdefault(field, field_value) 655 return field_value
Getter for cycle_sizes.
1253 def FindInitializationErrors(self): 1254 """Finds required fields which are not initialized. 1255 1256 Returns: 1257 A list of strings. Each string is a path to an uninitialized field from 1258 the top-level message, e.g. "foo.bar[5].baz". 1259 """ 1260 1261 errors = [] # simplify things 1262 1263 for field in required_fields: 1264 if not self.HasField(field.name): 1265 errors.append(field.name) 1266 1267 for field, value in self.ListFields(): 1268 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 1269 if field.is_extension: 1270 name = '(%s)' % field.full_name 1271 else: 1272 name = field.name 1273 1274 if _IsMapField(field): 1275 if _IsMessageMapField(field): 1276 for key in value: 1277 element = value[key] 1278 prefix = '%s[%s].' % (name, key) 1279 sub_errors = element.FindInitializationErrors() 1280 errors += [prefix + error for error in sub_errors] 1281 else: 1282 # ScalarMaps can't have any initialization errors. 1283 pass 1284 elif field.label == _FieldDescriptor.LABEL_REPEATED: 1285 for i in range(len(value)): 1286 element = value[i] 1287 prefix = '%s[%d].' % (name, i) 1288 sub_errors = element.FindInitializationErrors() 1289 errors += [prefix + error for error in sub_errors] 1290 else: 1291 prefix = name + '.' 1292 sub_errors = value.FindInitializationErrors() 1293 errors += [prefix + error for error in sub_errors] 1294 1295 return errors
Finds required fields which are not initialized.
Returns:
A list of strings. Each string is a path to an uninitialized field from the top-level message, e.g. "foo.bar[5].baz".
Inherited Members
- google.protobuf.message.Message
- CopyFrom
- ParseFromString
- HasExtension
- ClearExtension
Abstract base class for protocol messages.
Protocol message classes are almost always generated by the protocol compiler. These generated types subclass Message and implement the methods shown below.
496 def init(self, **kwargs): 497 self._cached_byte_size = 0 498 self._cached_byte_size_dirty = len(kwargs) > 0 499 self._fields = {} 500 # Contains a mapping from oneof field descriptors to the descriptor 501 # of the currently set field in that oneof field. 502 self._oneofs = {} 503 504 # _unknown_fields is () when empty for efficiency, and will be turned into 505 # a list if fields are added. 506 self._unknown_fields = () 507 # _unknown_field_set is None when empty for efficiency, and will be 508 # turned into UnknownFieldSet struct if fields are added. 509 self._unknown_field_set = None # pylint: disable=protected-access 510 self._is_present_in_parent = False 511 self._listener = message_listener_mod.NullMessageListener() 512 self._listener_for_children = _Listener(self) 513 for field_name, field_value in kwargs.items(): 514 field = _GetFieldByName(message_descriptor, field_name) 515 if field is None: 516 raise TypeError('%s() got an unexpected keyword argument "%s"' % 517 (message_descriptor.name, field_name)) 518 if field_value is None: 519 # field=None is the same as no field at all. 520 continue 521 if field.label == _FieldDescriptor.LABEL_REPEATED: 522 copy = field._default_constructor(self) 523 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: # Composite 524 if _IsMapField(field): 525 if _IsMessageMapField(field): 526 for key in field_value: 527 copy[key].MergeFrom(field_value[key]) 528 else: 529 copy.update(field_value) 530 else: 531 for val in field_value: 532 if isinstance(val, dict): 533 copy.add(**val) 534 else: 535 copy.add().MergeFrom(val) 536 else: # Scalar 537 if field.cpp_type == _FieldDescriptor.CPPTYPE_ENUM: 538 field_value = [_GetIntegerEnumValue(field.enum_type, val) 539 for val in field_value] 540 copy.extend(field_value) 541 self._fields[field] = copy 542 elif field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 543 copy = field._default_constructor(self) 544 new_val = field_value 545 if isinstance(field_value, dict): 546 new_val = field.message_type._concrete_class(**field_value) 547 try: 548 copy.MergeFrom(new_val) 549 except TypeError: 550 _ReraiseTypeErrorWithFieldName(message_descriptor.name, field_name) 551 self._fields[field] = copy 552 else: 553 if field.cpp_type == _FieldDescriptor.CPPTYPE_ENUM: 554 field_value = _GetIntegerEnumValue(field.enum_type, field_value) 555 try: 556 setattr(self, field_name, field_value) 557 except TypeError: 558 _ReraiseTypeErrorWithFieldName(message_descriptor.name, field_name)
1312 def MergeFrom(self, msg): 1313 if not isinstance(msg, cls): 1314 raise TypeError( 1315 'Parameter to MergeFrom() must be instance of same class: ' 1316 'expected %s got %s.' % (_FullyQualifiedClassName(cls), 1317 _FullyQualifiedClassName(msg.__class__))) 1318 1319 assert msg is not self 1320 self._Modified() 1321 1322 fields = self._fields 1323 1324 for field, value in msg._fields.items(): 1325 if field.label == LABEL_REPEATED: 1326 field_value = fields.get(field) 1327 if field_value is None: 1328 # Construct a new object to represent this field. 1329 field_value = field._default_constructor(self) 1330 fields[field] = field_value 1331 field_value.MergeFrom(value) 1332 elif field.cpp_type == CPPTYPE_MESSAGE: 1333 if value._is_present_in_parent: 1334 field_value = fields.get(field) 1335 if field_value is None: 1336 # Construct a new object to represent this field. 1337 field_value = field._default_constructor(self) 1338 fields[field] = field_value 1339 field_value.MergeFrom(value) 1340 else: 1341 self._fields[field] = value 1342 if field.containing_oneof: 1343 self._UpdateOneofState(field) 1344 1345 if msg._unknown_fields: 1346 if not self._unknown_fields: 1347 self._unknown_fields = [] 1348 self._unknown_fields.extend(msg._unknown_fields) 1349 # pylint: disable=protected-access 1350 if self._unknown_field_set is None: 1351 self._unknown_field_set = containers.UnknownFieldSet() 1352 self._unknown_field_set._extend(msg._unknown_field_set)
Merges the contents of the specified message into current message.
This method merges the contents of the specified message into the current message. Singular fields that are set in the specified message overwrite the corresponding fields in the current message. Repeated fields are appended. Singular sub-messages and groups are recursively merged.
Arguments:
- other_msg (Message): A message to merge into the current message.
1375def _Clear(self): 1376 # Clear fields. 1377 self._fields = {} 1378 self._unknown_fields = () 1379 # pylint: disable=protected-access 1380 if self._unknown_field_set is not None: 1381 self._unknown_field_set._clear() 1382 self._unknown_field_set = None 1383 1384 self._oneofs = {} 1385 self._Modified()
Clears all data that was set in the message.
1452 def Modified(self): 1453 """Sets the _cached_byte_size_dirty bit to true, 1454 and propagates this to our listener iff this was a state change. 1455 """ 1456 1457 # Note: Some callers check _cached_byte_size_dirty before calling 1458 # _Modified() as an extra optimization. So, if this method is ever 1459 # changed such that it does stuff even when _cached_byte_size_dirty is 1460 # already true, the callers need to be updated. 1461 if not self._cached_byte_size_dirty: 1462 self._cached_byte_size_dirty = True 1463 self._listener_for_children.dirty = True 1464 self._is_present_in_parent = True 1465 self._listener.Modified()
Sets the _cached_byte_size_dirty bit to true, and propagates this to our listener iff this was a state change.
1213 def IsInitialized(self, errors=None): 1214 """Checks if all required fields of a message are set. 1215 1216 Args: 1217 errors: A list which, if provided, will be populated with the field 1218 paths of all missing required fields. 1219 1220 Returns: 1221 True iff the specified message has all required fields set. 1222 """ 1223 1224 # Performance is critical so we avoid HasField() and ListFields(). 1225 1226 for field in required_fields: 1227 if (field not in self._fields or 1228 (field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE and 1229 not self._fields[field]._is_present_in_parent)): 1230 if errors is not None: 1231 errors.extend(self.FindInitializationErrors()) 1232 return False 1233 1234 for field, value in list(self._fields.items()): # dict can change size! 1235 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 1236 if field.label == _FieldDescriptor.LABEL_REPEATED: 1237 if (field.message_type._is_map_entry): 1238 continue 1239 for element in value: 1240 if not element.IsInitialized(): 1241 if errors is not None: 1242 errors.extend(self.FindInitializationErrors()) 1243 return False 1244 elif value._is_present_in_parent and not value.IsInitialized(): 1245 if errors is not None: 1246 errors.extend(self.FindInitializationErrors()) 1247 return False 1248 1249 return True
Checks if all required fields of a message are set.
Arguments:
- errors: A list which, if provided, will be populated with the field paths of all missing required fields.
Returns:
True iff the specified message has all required fields set.
1119 def MergeFromString(self, serialized): 1120 serialized = memoryview(serialized) 1121 length = len(serialized) 1122 try: 1123 if self._InternalParse(serialized, 0, length) != length: 1124 # The only reason _InternalParse would return early is if it 1125 # encountered an end-group tag. 1126 raise message_mod.DecodeError('Unexpected end-group tag.') 1127 except (IndexError, TypeError): 1128 # Now ord(buf[p:p+1]) == ord('') gets TypeError. 1129 raise message_mod.DecodeError('Truncated message.') 1130 except struct.error as e: 1131 raise message_mod.DecodeError(e) 1132 return length # Return this for legacy reasons.
Merges serialized protocol buffer data into this message.
When we find a field in serialized
that is already present
in this message:
- If it's a "repeated" field, we append to the end of our list.
- Else, if it's a scalar, we overwrite our field.
- Else, (it's a nonrepeated composite), we recursively merge into the existing composite.
Arguments:
- serialized (bytes): Any object that allows us to call
memoryview(serialized)
to access a string of bytes using the buffer interface.
Returns:
int: The number of bytes read from
serialized
. For non-group messages, this will always belen(serialized)
, but for messages which are actually groups, this will generally be less thanlen(serialized)
, since we must stop when we reach anEND_GROUP
tag. Note that if we do stop because of anEND_GROUP
tag, the number of bytes returned does not include the bytes for theEND_GROUP
tag information.
Raises:
- DecodeError: if the input cannot be parsed.
1072 def SerializeToString(self, **kwargs): 1073 # Check if the message has all of its required fields set. 1074 if not self.IsInitialized(): 1075 raise message_mod.EncodeError( 1076 'Message %s is missing required fields: %s' % ( 1077 self.DESCRIPTOR.full_name, ','.join(self.FindInitializationErrors()))) 1078 return self.SerializePartialToString(**kwargs)
Serializes the protocol message to a binary string.
Keyword Args:
deterministic (bool): If true, requests deterministic serialization of the protobuf, with predictable ordering of map keys.
Returns:
A binary string representation of the message if all of the required fields in the message are set (i.e. the message is initialized).
Raises:
- EncodeError: if the message isn't initialized (see
IsInitialized()
).
1085 def SerializePartialToString(self, **kwargs): 1086 out = BytesIO() 1087 self._InternalSerialize(out.write, **kwargs) 1088 return out.getvalue()
Serializes the protocol message to a binary string.
This method is similar to SerializeToString but doesn't check if the message is initialized.
Keyword Args:
deterministic (bool): If true, requests deterministic serialization of the protobuf, with predictable ordering of map keys.
Returns:
bytes: A serialized representation of the partial message.
814 def ListFields(self): 815 all_fields = [item for item in self._fields.items() if _IsPresent(item)] 816 all_fields.sort(key = lambda item: item[0].number) 817 return all_fields
Returns a list of (FieldDescriptor, value) tuples for present fields.
A message field is non-empty if HasField() would return true. A singular primitive field is non-empty if HasField() would return true in proto2 or it is non zero in proto3. A repeated field is non-empty if it contains at least one element. The fields are ordered by field number.
Returns:
list[tuple(FieldDescriptor, value)]: field descriptors and values for all fields in the message which are not empty. The values vary by field type.
838 def HasField(self, field_name): 839 try: 840 field = hassable_fields[field_name] 841 except KeyError as exc: 842 raise ValueError('Protocol message %s has no non-repeated field "%s" ' 843 'nor has presence is not available for this field.' % ( 844 message_descriptor.full_name, field_name)) from exc 845 846 if isinstance(field, descriptor_mod.OneofDescriptor): 847 try: 848 return HasField(self, self._oneofs[field].name) 849 except KeyError: 850 return False 851 else: 852 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 853 value = self._fields.get(field) 854 return value is not None and value._is_present_in_parent 855 else: 856 return field in self._fields
Checks if a certain field is set for the message.
For a oneof group, checks if any field inside is set. Note that if the
field_name is not defined in the message descriptor, ValueError
will
be raised.
Arguments:
- field_name (str): The name of the field to check for presence.
Returns:
bool: Whether a value has been set for the named field.
Raises:
- ValueError: if the
field_name
is not a member of this message.
863 def ClearField(self, field_name): 864 try: 865 field = message_descriptor.fields_by_name[field_name] 866 except KeyError: 867 try: 868 field = message_descriptor.oneofs_by_name[field_name] 869 if field in self._oneofs: 870 field = self._oneofs[field] 871 else: 872 return 873 except KeyError: 874 raise ValueError('Protocol message %s has no "%s" field.' % 875 (message_descriptor.name, field_name)) 876 877 if field in self._fields: 878 # To match the C++ implementation, we need to invalidate iterators 879 # for map fields when ClearField() happens. 880 if hasattr(self._fields[field], 'InvalidateIterators'): 881 self._fields[field].InvalidateIterators() 882 883 # Note: If the field is a sub-message, its listener will still point 884 # at us. That's fine, because the worst than can happen is that it 885 # will call _Modified() and invalidate our byte size. Big deal. 886 del self._fields[field] 887 888 if self._oneofs.get(field.containing_oneof, None) is field: 889 del self._oneofs[field.containing_oneof] 890 891 # Always call _Modified() -- even if nothing was changed, this is 892 # a mutating method, and thus calling it should cause the field to become 893 # present in the parent message. 894 self._Modified()
Clears the contents of a given field.
Inside a oneof group, clears the field set. If the name neither refers to a
defined field or oneof group, ValueError
is raised.
Arguments:
- field_name (str): The name of the field to check for presence.
Raises:
- ValueError: if the
field_name
is not a member of this message.
1358 def WhichOneof(self, oneof_name): 1359 """Returns the name of the currently set field inside a oneof, or None.""" 1360 try: 1361 field = message_descriptor.oneofs_by_name[oneof_name] 1362 except KeyError: 1363 raise ValueError( 1364 'Protocol message has no oneof "%s" field.' % oneof_name) 1365 1366 nested_field = self._oneofs.get(field, None) 1367 if nested_field is not None and self.HasField(nested_field.name): 1368 return nested_field.name 1369 else: 1370 return None
Returns the name of the currently set field inside a oneof, or None.
1388def _UnknownFields(self): 1389 warnings.warn( 1390 'message.UnknownFields() is deprecated. Please use the add one ' 1391 'feature unknown_fields.UnknownFieldSet(message) in ' 1392 'unknown_fields.py instead.' 1393 ) 1394 if self._unknown_field_set is None: # pylint: disable=protected-access 1395 # pylint: disable=protected-access 1396 self._unknown_field_set = containers.UnknownFieldSet() 1397 return self._unknown_field_set # pylint: disable=protected-access
Returns the UnknownFieldSet.
Returns:
UnknownFieldSet: The unknown fields stored in this message.
1400def _DiscardUnknownFields(self): 1401 self._unknown_fields = [] 1402 self._unknown_field_set = None # pylint: disable=protected-access 1403 for field, value in self.ListFields(): 1404 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 1405 if _IsMapField(field): 1406 if _IsMessageMapField(field): 1407 for key in value: 1408 value[key].DiscardUnknownFields() 1409 elif field.label == _FieldDescriptor.LABEL_REPEATED: 1410 for sub_message in value: 1411 sub_message.DiscardUnknownFields() 1412 else: 1413 value.DiscardUnknownFields()
Clears all fields in the UnknownFieldSet
.
This operation is recursive for nested message.
1040 def ByteSize(self): 1041 if not self._cached_byte_size_dirty: 1042 return self._cached_byte_size 1043 1044 size = 0 1045 descriptor = self.DESCRIPTOR 1046 if descriptor._is_map_entry: 1047 # Fields of map entry should always be serialized. 1048 key_field = descriptor.fields_by_name['key'] 1049 _MaybeAddEncoder(cls, key_field) 1050 size = key_field._sizer(self.key) 1051 value_field = descriptor.fields_by_name['value'] 1052 _MaybeAddEncoder(cls, value_field) 1053 size += value_field._sizer(self.value) 1054 else: 1055 for field_descriptor, field_value in self.ListFields(): 1056 _MaybeAddEncoder(cls, field_descriptor) 1057 size += field_descriptor._sizer(field_value) 1058 for tag_bytes, value_bytes in self._unknown_fields: 1059 size += len(tag_bytes) + len(value_bytes) 1060 1061 self._cached_byte_size = size 1062 self._cached_byte_size_dirty = False 1063 self._listener_for_children.dirty = False 1064 return size
Returns the serialized size of this message.
Recursively calls ByteSize() on all contained messages.
Returns:
int: The number of bytes required to serialize this message.
784 def RegisterExtension(field_descriptor): 785 field_descriptor.containing_type = cls.DESCRIPTOR 786 # TODO: Use cls.MESSAGE_FACTORY.pool when available. 787 # pylint: disable=protected-access 788 cls.DESCRIPTOR.file.pool._AddExtensionDescriptor(field_descriptor) 789 _AttachFieldHelpers(cls, field_descriptor)
685 def getter(self): 686 # TODO: This may be broken since there may not be 687 # default_value. Combine with has_default_value somehow. 688 return self._fields.get(field, default_value)
Getter for num_rows.
685 def getter(self): 686 # TODO: This may be broken since there may not be 687 # default_value. Combine with has_default_value somehow. 688 return self._fields.get(field, default_value)
Getter for num_cols.
642 def getter(self): 643 field_value = self._fields.get(field) 644 if field_value is None: 645 # Construct a new object to represent this field. 646 field_value = field._default_constructor(self) 647 648 # Atomically check if another thread has preempted us and, if not, swap 649 # in the new object we just created. If someone has preempted us, we 650 # take that object and discard ours. 651 # WARNING: We are relying on setdefault() being atomic. This is true 652 # in CPython but we haven't investigated others. This warning appears 653 # in several other locations in this file. 654 field_value = self._fields.setdefault(field, field_value) 655 return field_value
Getter for entries.
1253 def FindInitializationErrors(self): 1254 """Finds required fields which are not initialized. 1255 1256 Returns: 1257 A list of strings. Each string is a path to an uninitialized field from 1258 the top-level message, e.g. "foo.bar[5].baz". 1259 """ 1260 1261 errors = [] # simplify things 1262 1263 for field in required_fields: 1264 if not self.HasField(field.name): 1265 errors.append(field.name) 1266 1267 for field, value in self.ListFields(): 1268 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 1269 if field.is_extension: 1270 name = '(%s)' % field.full_name 1271 else: 1272 name = field.name 1273 1274 if _IsMapField(field): 1275 if _IsMessageMapField(field): 1276 for key in value: 1277 element = value[key] 1278 prefix = '%s[%s].' % (name, key) 1279 sub_errors = element.FindInitializationErrors() 1280 errors += [prefix + error for error in sub_errors] 1281 else: 1282 # ScalarMaps can't have any initialization errors. 1283 pass 1284 elif field.label == _FieldDescriptor.LABEL_REPEATED: 1285 for i in range(len(value)): 1286 element = value[i] 1287 prefix = '%s[%d].' % (name, i) 1288 sub_errors = element.FindInitializationErrors() 1289 errors += [prefix + error for error in sub_errors] 1290 else: 1291 prefix = name + '.' 1292 sub_errors = value.FindInitializationErrors() 1293 errors += [prefix + error for error in sub_errors] 1294 1295 return errors
Finds required fields which are not initialized.
Returns:
A list of strings. Each string is a path to an uninitialized field from the top-level message, e.g. "foo.bar[5].baz".
Inherited Members
- google.protobuf.message.Message
- CopyFrom
- ParseFromString
- HasExtension
- ClearExtension
Abstract base class for protocol messages.
Protocol message classes are almost always generated by the protocol compiler. These generated types subclass Message and implement the methods shown below.
496 def init(self, **kwargs): 497 self._cached_byte_size = 0 498 self._cached_byte_size_dirty = len(kwargs) > 0 499 self._fields = {} 500 # Contains a mapping from oneof field descriptors to the descriptor 501 # of the currently set field in that oneof field. 502 self._oneofs = {} 503 504 # _unknown_fields is () when empty for efficiency, and will be turned into 505 # a list if fields are added. 506 self._unknown_fields = () 507 # _unknown_field_set is None when empty for efficiency, and will be 508 # turned into UnknownFieldSet struct if fields are added. 509 self._unknown_field_set = None # pylint: disable=protected-access 510 self._is_present_in_parent = False 511 self._listener = message_listener_mod.NullMessageListener() 512 self._listener_for_children = _Listener(self) 513 for field_name, field_value in kwargs.items(): 514 field = _GetFieldByName(message_descriptor, field_name) 515 if field is None: 516 raise TypeError('%s() got an unexpected keyword argument "%s"' % 517 (message_descriptor.name, field_name)) 518 if field_value is None: 519 # field=None is the same as no field at all. 520 continue 521 if field.label == _FieldDescriptor.LABEL_REPEATED: 522 copy = field._default_constructor(self) 523 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: # Composite 524 if _IsMapField(field): 525 if _IsMessageMapField(field): 526 for key in field_value: 527 copy[key].MergeFrom(field_value[key]) 528 else: 529 copy.update(field_value) 530 else: 531 for val in field_value: 532 if isinstance(val, dict): 533 copy.add(**val) 534 else: 535 copy.add().MergeFrom(val) 536 else: # Scalar 537 if field.cpp_type == _FieldDescriptor.CPPTYPE_ENUM: 538 field_value = [_GetIntegerEnumValue(field.enum_type, val) 539 for val in field_value] 540 copy.extend(field_value) 541 self._fields[field] = copy 542 elif field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 543 copy = field._default_constructor(self) 544 new_val = field_value 545 if isinstance(field_value, dict): 546 new_val = field.message_type._concrete_class(**field_value) 547 try: 548 copy.MergeFrom(new_val) 549 except TypeError: 550 _ReraiseTypeErrorWithFieldName(message_descriptor.name, field_name) 551 self._fields[field] = copy 552 else: 553 if field.cpp_type == _FieldDescriptor.CPPTYPE_ENUM: 554 field_value = _GetIntegerEnumValue(field.enum_type, field_value) 555 try: 556 setattr(self, field_name, field_value) 557 except TypeError: 558 _ReraiseTypeErrorWithFieldName(message_descriptor.name, field_name)
1312 def MergeFrom(self, msg): 1313 if not isinstance(msg, cls): 1314 raise TypeError( 1315 'Parameter to MergeFrom() must be instance of same class: ' 1316 'expected %s got %s.' % (_FullyQualifiedClassName(cls), 1317 _FullyQualifiedClassName(msg.__class__))) 1318 1319 assert msg is not self 1320 self._Modified() 1321 1322 fields = self._fields 1323 1324 for field, value in msg._fields.items(): 1325 if field.label == LABEL_REPEATED: 1326 field_value = fields.get(field) 1327 if field_value is None: 1328 # Construct a new object to represent this field. 1329 field_value = field._default_constructor(self) 1330 fields[field] = field_value 1331 field_value.MergeFrom(value) 1332 elif field.cpp_type == CPPTYPE_MESSAGE: 1333 if value._is_present_in_parent: 1334 field_value = fields.get(field) 1335 if field_value is None: 1336 # Construct a new object to represent this field. 1337 field_value = field._default_constructor(self) 1338 fields[field] = field_value 1339 field_value.MergeFrom(value) 1340 else: 1341 self._fields[field] = value 1342 if field.containing_oneof: 1343 self._UpdateOneofState(field) 1344 1345 if msg._unknown_fields: 1346 if not self._unknown_fields: 1347 self._unknown_fields = [] 1348 self._unknown_fields.extend(msg._unknown_fields) 1349 # pylint: disable=protected-access 1350 if self._unknown_field_set is None: 1351 self._unknown_field_set = containers.UnknownFieldSet() 1352 self._unknown_field_set._extend(msg._unknown_field_set)
Merges the contents of the specified message into current message.
This method merges the contents of the specified message into the current message. Singular fields that are set in the specified message overwrite the corresponding fields in the current message. Repeated fields are appended. Singular sub-messages and groups are recursively merged.
Arguments:
- other_msg (Message): A message to merge into the current message.
1375def _Clear(self): 1376 # Clear fields. 1377 self._fields = {} 1378 self._unknown_fields = () 1379 # pylint: disable=protected-access 1380 if self._unknown_field_set is not None: 1381 self._unknown_field_set._clear() 1382 self._unknown_field_set = None 1383 1384 self._oneofs = {} 1385 self._Modified()
Clears all data that was set in the message.
1452 def Modified(self): 1453 """Sets the _cached_byte_size_dirty bit to true, 1454 and propagates this to our listener iff this was a state change. 1455 """ 1456 1457 # Note: Some callers check _cached_byte_size_dirty before calling 1458 # _Modified() as an extra optimization. So, if this method is ever 1459 # changed such that it does stuff even when _cached_byte_size_dirty is 1460 # already true, the callers need to be updated. 1461 if not self._cached_byte_size_dirty: 1462 self._cached_byte_size_dirty = True 1463 self._listener_for_children.dirty = True 1464 self._is_present_in_parent = True 1465 self._listener.Modified()
Sets the _cached_byte_size_dirty bit to true, and propagates this to our listener iff this was a state change.
1213 def IsInitialized(self, errors=None): 1214 """Checks if all required fields of a message are set. 1215 1216 Args: 1217 errors: A list which, if provided, will be populated with the field 1218 paths of all missing required fields. 1219 1220 Returns: 1221 True iff the specified message has all required fields set. 1222 """ 1223 1224 # Performance is critical so we avoid HasField() and ListFields(). 1225 1226 for field in required_fields: 1227 if (field not in self._fields or 1228 (field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE and 1229 not self._fields[field]._is_present_in_parent)): 1230 if errors is not None: 1231 errors.extend(self.FindInitializationErrors()) 1232 return False 1233 1234 for field, value in list(self._fields.items()): # dict can change size! 1235 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 1236 if field.label == _FieldDescriptor.LABEL_REPEATED: 1237 if (field.message_type._is_map_entry): 1238 continue 1239 for element in value: 1240 if not element.IsInitialized(): 1241 if errors is not None: 1242 errors.extend(self.FindInitializationErrors()) 1243 return False 1244 elif value._is_present_in_parent and not value.IsInitialized(): 1245 if errors is not None: 1246 errors.extend(self.FindInitializationErrors()) 1247 return False 1248 1249 return True
Checks if all required fields of a message are set.
Arguments:
- errors: A list which, if provided, will be populated with the field paths of all missing required fields.
Returns:
True iff the specified message has all required fields set.
1119 def MergeFromString(self, serialized): 1120 serialized = memoryview(serialized) 1121 length = len(serialized) 1122 try: 1123 if self._InternalParse(serialized, 0, length) != length: 1124 # The only reason _InternalParse would return early is if it 1125 # encountered an end-group tag. 1126 raise message_mod.DecodeError('Unexpected end-group tag.') 1127 except (IndexError, TypeError): 1128 # Now ord(buf[p:p+1]) == ord('') gets TypeError. 1129 raise message_mod.DecodeError('Truncated message.') 1130 except struct.error as e: 1131 raise message_mod.DecodeError(e) 1132 return length # Return this for legacy reasons.
Merges serialized protocol buffer data into this message.
When we find a field in serialized
that is already present
in this message:
- If it's a "repeated" field, we append to the end of our list.
- Else, if it's a scalar, we overwrite our field.
- Else, (it's a nonrepeated composite), we recursively merge into the existing composite.
Arguments:
- serialized (bytes): Any object that allows us to call
memoryview(serialized)
to access a string of bytes using the buffer interface.
Returns:
int: The number of bytes read from
serialized
. For non-group messages, this will always belen(serialized)
, but for messages which are actually groups, this will generally be less thanlen(serialized)
, since we must stop when we reach anEND_GROUP
tag. Note that if we do stop because of anEND_GROUP
tag, the number of bytes returned does not include the bytes for theEND_GROUP
tag information.
Raises:
- DecodeError: if the input cannot be parsed.
1072 def SerializeToString(self, **kwargs): 1073 # Check if the message has all of its required fields set. 1074 if not self.IsInitialized(): 1075 raise message_mod.EncodeError( 1076 'Message %s is missing required fields: %s' % ( 1077 self.DESCRIPTOR.full_name, ','.join(self.FindInitializationErrors()))) 1078 return self.SerializePartialToString(**kwargs)
Serializes the protocol message to a binary string.
Keyword Args:
deterministic (bool): If true, requests deterministic serialization of the protobuf, with predictable ordering of map keys.
Returns:
A binary string representation of the message if all of the required fields in the message are set (i.e. the message is initialized).
Raises:
- EncodeError: if the message isn't initialized (see
IsInitialized()
).
1085 def SerializePartialToString(self, **kwargs): 1086 out = BytesIO() 1087 self._InternalSerialize(out.write, **kwargs) 1088 return out.getvalue()
Serializes the protocol message to a binary string.
This method is similar to SerializeToString but doesn't check if the message is initialized.
Keyword Args:
deterministic (bool): If true, requests deterministic serialization of the protobuf, with predictable ordering of map keys.
Returns:
bytes: A serialized representation of the partial message.
814 def ListFields(self): 815 all_fields = [item for item in self._fields.items() if _IsPresent(item)] 816 all_fields.sort(key = lambda item: item[0].number) 817 return all_fields
Returns a list of (FieldDescriptor, value) tuples for present fields.
A message field is non-empty if HasField() would return true. A singular primitive field is non-empty if HasField() would return true in proto2 or it is non zero in proto3. A repeated field is non-empty if it contains at least one element. The fields are ordered by field number.
Returns:
list[tuple(FieldDescriptor, value)]: field descriptors and values for all fields in the message which are not empty. The values vary by field type.
838 def HasField(self, field_name): 839 try: 840 field = hassable_fields[field_name] 841 except KeyError as exc: 842 raise ValueError('Protocol message %s has no non-repeated field "%s" ' 843 'nor has presence is not available for this field.' % ( 844 message_descriptor.full_name, field_name)) from exc 845 846 if isinstance(field, descriptor_mod.OneofDescriptor): 847 try: 848 return HasField(self, self._oneofs[field].name) 849 except KeyError: 850 return False 851 else: 852 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 853 value = self._fields.get(field) 854 return value is not None and value._is_present_in_parent 855 else: 856 return field in self._fields
Checks if a certain field is set for the message.
For a oneof group, checks if any field inside is set. Note that if the
field_name is not defined in the message descriptor, ValueError
will
be raised.
Arguments:
- field_name (str): The name of the field to check for presence.
Returns:
bool: Whether a value has been set for the named field.
Raises:
- ValueError: if the
field_name
is not a member of this message.
863 def ClearField(self, field_name): 864 try: 865 field = message_descriptor.fields_by_name[field_name] 866 except KeyError: 867 try: 868 field = message_descriptor.oneofs_by_name[field_name] 869 if field in self._oneofs: 870 field = self._oneofs[field] 871 else: 872 return 873 except KeyError: 874 raise ValueError('Protocol message %s has no "%s" field.' % 875 (message_descriptor.name, field_name)) 876 877 if field in self._fields: 878 # To match the C++ implementation, we need to invalidate iterators 879 # for map fields when ClearField() happens. 880 if hasattr(self._fields[field], 'InvalidateIterators'): 881 self._fields[field].InvalidateIterators() 882 883 # Note: If the field is a sub-message, its listener will still point 884 # at us. That's fine, because the worst than can happen is that it 885 # will call _Modified() and invalidate our byte size. Big deal. 886 del self._fields[field] 887 888 if self._oneofs.get(field.containing_oneof, None) is field: 889 del self._oneofs[field.containing_oneof] 890 891 # Always call _Modified() -- even if nothing was changed, this is 892 # a mutating method, and thus calling it should cause the field to become 893 # present in the parent message. 894 self._Modified()
Clears the contents of a given field.
Inside a oneof group, clears the field set. If the name neither refers to a
defined field or oneof group, ValueError
is raised.
Arguments:
- field_name (str): The name of the field to check for presence.
Raises:
- ValueError: if the
field_name
is not a member of this message.
1358 def WhichOneof(self, oneof_name): 1359 """Returns the name of the currently set field inside a oneof, or None.""" 1360 try: 1361 field = message_descriptor.oneofs_by_name[oneof_name] 1362 except KeyError: 1363 raise ValueError( 1364 'Protocol message has no oneof "%s" field.' % oneof_name) 1365 1366 nested_field = self._oneofs.get(field, None) 1367 if nested_field is not None and self.HasField(nested_field.name): 1368 return nested_field.name 1369 else: 1370 return None
Returns the name of the currently set field inside a oneof, or None.
1388def _UnknownFields(self): 1389 warnings.warn( 1390 'message.UnknownFields() is deprecated. Please use the add one ' 1391 'feature unknown_fields.UnknownFieldSet(message) in ' 1392 'unknown_fields.py instead.' 1393 ) 1394 if self._unknown_field_set is None: # pylint: disable=protected-access 1395 # pylint: disable=protected-access 1396 self._unknown_field_set = containers.UnknownFieldSet() 1397 return self._unknown_field_set # pylint: disable=protected-access
Returns the UnknownFieldSet.
Returns:
UnknownFieldSet: The unknown fields stored in this message.
1400def _DiscardUnknownFields(self): 1401 self._unknown_fields = [] 1402 self._unknown_field_set = None # pylint: disable=protected-access 1403 for field, value in self.ListFields(): 1404 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 1405 if _IsMapField(field): 1406 if _IsMessageMapField(field): 1407 for key in value: 1408 value[key].DiscardUnknownFields() 1409 elif field.label == _FieldDescriptor.LABEL_REPEATED: 1410 for sub_message in value: 1411 sub_message.DiscardUnknownFields() 1412 else: 1413 value.DiscardUnknownFields()
Clears all fields in the UnknownFieldSet
.
This operation is recursive for nested message.
1040 def ByteSize(self): 1041 if not self._cached_byte_size_dirty: 1042 return self._cached_byte_size 1043 1044 size = 0 1045 descriptor = self.DESCRIPTOR 1046 if descriptor._is_map_entry: 1047 # Fields of map entry should always be serialized. 1048 key_field = descriptor.fields_by_name['key'] 1049 _MaybeAddEncoder(cls, key_field) 1050 size = key_field._sizer(self.key) 1051 value_field = descriptor.fields_by_name['value'] 1052 _MaybeAddEncoder(cls, value_field) 1053 size += value_field._sizer(self.value) 1054 else: 1055 for field_descriptor, field_value in self.ListFields(): 1056 _MaybeAddEncoder(cls, field_descriptor) 1057 size += field_descriptor._sizer(field_value) 1058 for tag_bytes, value_bytes in self._unknown_fields: 1059 size += len(tag_bytes) + len(value_bytes) 1060 1061 self._cached_byte_size = size 1062 self._cached_byte_size_dirty = False 1063 self._listener_for_children.dirty = False 1064 return size
Returns the serialized size of this message.
Recursively calls ByteSize() on all contained messages.
Returns:
int: The number of bytes required to serialize this message.
784 def RegisterExtension(field_descriptor): 785 field_descriptor.containing_type = cls.DESCRIPTOR 786 # TODO: Use cls.MESSAGE_FACTORY.pool when available. 787 # pylint: disable=protected-access 788 cls.DESCRIPTOR.file.pool._AddExtensionDescriptor(field_descriptor) 789 _AttachFieldHelpers(cls, field_descriptor)
642 def getter(self): 643 field_value = self._fields.get(field) 644 if field_value is None: 645 # Construct a new object to represent this field. 646 field_value = field._default_constructor(self) 647 648 # Atomically check if another thread has preempted us and, if not, swap 649 # in the new object we just created. If someone has preempted us, we 650 # take that object and discard ours. 651 # WARNING: We are relying on setdefault() being atomic. This is true 652 # in CPython but we haven't investigated others. This warning appears 653 # in several other locations in this file. 654 field_value = self._fields.setdefault(field, field_value) 655 return field_value
Getter for permutations.
642 def getter(self): 643 field_value = self._fields.get(field) 644 if field_value is None: 645 # Construct a new object to represent this field. 646 field_value = field._default_constructor(self) 647 648 # Atomically check if another thread has preempted us and, if not, swap 649 # in the new object we just created. If someone has preempted us, we 650 # take that object and discard ours. 651 # WARNING: We are relying on setdefault() being atomic. This is true 652 # in CPython but we haven't investigated others. This warning appears 653 # in several other locations in this file. 654 field_value = self._fields.setdefault(field, field_value) 655 return field_value
Getter for orbitopes.
1253 def FindInitializationErrors(self): 1254 """Finds required fields which are not initialized. 1255 1256 Returns: 1257 A list of strings. Each string is a path to an uninitialized field from 1258 the top-level message, e.g. "foo.bar[5].baz". 1259 """ 1260 1261 errors = [] # simplify things 1262 1263 for field in required_fields: 1264 if not self.HasField(field.name): 1265 errors.append(field.name) 1266 1267 for field, value in self.ListFields(): 1268 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 1269 if field.is_extension: 1270 name = '(%s)' % field.full_name 1271 else: 1272 name = field.name 1273 1274 if _IsMapField(field): 1275 if _IsMessageMapField(field): 1276 for key in value: 1277 element = value[key] 1278 prefix = '%s[%s].' % (name, key) 1279 sub_errors = element.FindInitializationErrors() 1280 errors += [prefix + error for error in sub_errors] 1281 else: 1282 # ScalarMaps can't have any initialization errors. 1283 pass 1284 elif field.label == _FieldDescriptor.LABEL_REPEATED: 1285 for i in range(len(value)): 1286 element = value[i] 1287 prefix = '%s[%d].' % (name, i) 1288 sub_errors = element.FindInitializationErrors() 1289 errors += [prefix + error for error in sub_errors] 1290 else: 1291 prefix = name + '.' 1292 sub_errors = value.FindInitializationErrors() 1293 errors += [prefix + error for error in sub_errors] 1294 1295 return errors
Finds required fields which are not initialized.
Returns:
A list of strings. Each string is a path to an uninitialized field from the top-level message, e.g. "foo.bar[5].baz".
Inherited Members
- google.protobuf.message.Message
- CopyFrom
- ParseFromString
- HasExtension
- ClearExtension
Abstract base class for protocol messages.
Protocol message classes are almost always generated by the protocol compiler. These generated types subclass Message and implement the methods shown below.
496 def init(self, **kwargs): 497 self._cached_byte_size = 0 498 self._cached_byte_size_dirty = len(kwargs) > 0 499 self._fields = {} 500 # Contains a mapping from oneof field descriptors to the descriptor 501 # of the currently set field in that oneof field. 502 self._oneofs = {} 503 504 # _unknown_fields is () when empty for efficiency, and will be turned into 505 # a list if fields are added. 506 self._unknown_fields = () 507 # _unknown_field_set is None when empty for efficiency, and will be 508 # turned into UnknownFieldSet struct if fields are added. 509 self._unknown_field_set = None # pylint: disable=protected-access 510 self._is_present_in_parent = False 511 self._listener = message_listener_mod.NullMessageListener() 512 self._listener_for_children = _Listener(self) 513 for field_name, field_value in kwargs.items(): 514 field = _GetFieldByName(message_descriptor, field_name) 515 if field is None: 516 raise TypeError('%s() got an unexpected keyword argument "%s"' % 517 (message_descriptor.name, field_name)) 518 if field_value is None: 519 # field=None is the same as no field at all. 520 continue 521 if field.label == _FieldDescriptor.LABEL_REPEATED: 522 copy = field._default_constructor(self) 523 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: # Composite 524 if _IsMapField(field): 525 if _IsMessageMapField(field): 526 for key in field_value: 527 copy[key].MergeFrom(field_value[key]) 528 else: 529 copy.update(field_value) 530 else: 531 for val in field_value: 532 if isinstance(val, dict): 533 copy.add(**val) 534 else: 535 copy.add().MergeFrom(val) 536 else: # Scalar 537 if field.cpp_type == _FieldDescriptor.CPPTYPE_ENUM: 538 field_value = [_GetIntegerEnumValue(field.enum_type, val) 539 for val in field_value] 540 copy.extend(field_value) 541 self._fields[field] = copy 542 elif field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 543 copy = field._default_constructor(self) 544 new_val = field_value 545 if isinstance(field_value, dict): 546 new_val = field.message_type._concrete_class(**field_value) 547 try: 548 copy.MergeFrom(new_val) 549 except TypeError: 550 _ReraiseTypeErrorWithFieldName(message_descriptor.name, field_name) 551 self._fields[field] = copy 552 else: 553 if field.cpp_type == _FieldDescriptor.CPPTYPE_ENUM: 554 field_value = _GetIntegerEnumValue(field.enum_type, field_value) 555 try: 556 setattr(self, field_name, field_value) 557 except TypeError: 558 _ReraiseTypeErrorWithFieldName(message_descriptor.name, field_name)
1312 def MergeFrom(self, msg): 1313 if not isinstance(msg, cls): 1314 raise TypeError( 1315 'Parameter to MergeFrom() must be instance of same class: ' 1316 'expected %s got %s.' % (_FullyQualifiedClassName(cls), 1317 _FullyQualifiedClassName(msg.__class__))) 1318 1319 assert msg is not self 1320 self._Modified() 1321 1322 fields = self._fields 1323 1324 for field, value in msg._fields.items(): 1325 if field.label == LABEL_REPEATED: 1326 field_value = fields.get(field) 1327 if field_value is None: 1328 # Construct a new object to represent this field. 1329 field_value = field._default_constructor(self) 1330 fields[field] = field_value 1331 field_value.MergeFrom(value) 1332 elif field.cpp_type == CPPTYPE_MESSAGE: 1333 if value._is_present_in_parent: 1334 field_value = fields.get(field) 1335 if field_value is None: 1336 # Construct a new object to represent this field. 1337 field_value = field._default_constructor(self) 1338 fields[field] = field_value 1339 field_value.MergeFrom(value) 1340 else: 1341 self._fields[field] = value 1342 if field.containing_oneof: 1343 self._UpdateOneofState(field) 1344 1345 if msg._unknown_fields: 1346 if not self._unknown_fields: 1347 self._unknown_fields = [] 1348 self._unknown_fields.extend(msg._unknown_fields) 1349 # pylint: disable=protected-access 1350 if self._unknown_field_set is None: 1351 self._unknown_field_set = containers.UnknownFieldSet() 1352 self._unknown_field_set._extend(msg._unknown_field_set)
Merges the contents of the specified message into current message.
This method merges the contents of the specified message into the current message. Singular fields that are set in the specified message overwrite the corresponding fields in the current message. Repeated fields are appended. Singular sub-messages and groups are recursively merged.
Arguments:
- other_msg (Message): A message to merge into the current message.
1375def _Clear(self): 1376 # Clear fields. 1377 self._fields = {} 1378 self._unknown_fields = () 1379 # pylint: disable=protected-access 1380 if self._unknown_field_set is not None: 1381 self._unknown_field_set._clear() 1382 self._unknown_field_set = None 1383 1384 self._oneofs = {} 1385 self._Modified()
Clears all data that was set in the message.
1452 def Modified(self): 1453 """Sets the _cached_byte_size_dirty bit to true, 1454 and propagates this to our listener iff this was a state change. 1455 """ 1456 1457 # Note: Some callers check _cached_byte_size_dirty before calling 1458 # _Modified() as an extra optimization. So, if this method is ever 1459 # changed such that it does stuff even when _cached_byte_size_dirty is 1460 # already true, the callers need to be updated. 1461 if not self._cached_byte_size_dirty: 1462 self._cached_byte_size_dirty = True 1463 self._listener_for_children.dirty = True 1464 self._is_present_in_parent = True 1465 self._listener.Modified()
Sets the _cached_byte_size_dirty bit to true, and propagates this to our listener iff this was a state change.
1213 def IsInitialized(self, errors=None): 1214 """Checks if all required fields of a message are set. 1215 1216 Args: 1217 errors: A list which, if provided, will be populated with the field 1218 paths of all missing required fields. 1219 1220 Returns: 1221 True iff the specified message has all required fields set. 1222 """ 1223 1224 # Performance is critical so we avoid HasField() and ListFields(). 1225 1226 for field in required_fields: 1227 if (field not in self._fields or 1228 (field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE and 1229 not self._fields[field]._is_present_in_parent)): 1230 if errors is not None: 1231 errors.extend(self.FindInitializationErrors()) 1232 return False 1233 1234 for field, value in list(self._fields.items()): # dict can change size! 1235 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 1236 if field.label == _FieldDescriptor.LABEL_REPEATED: 1237 if (field.message_type._is_map_entry): 1238 continue 1239 for element in value: 1240 if not element.IsInitialized(): 1241 if errors is not None: 1242 errors.extend(self.FindInitializationErrors()) 1243 return False 1244 elif value._is_present_in_parent and not value.IsInitialized(): 1245 if errors is not None: 1246 errors.extend(self.FindInitializationErrors()) 1247 return False 1248 1249 return True
Checks if all required fields of a message are set.
Arguments:
- errors: A list which, if provided, will be populated with the field paths of all missing required fields.
Returns:
True iff the specified message has all required fields set.
1119 def MergeFromString(self, serialized): 1120 serialized = memoryview(serialized) 1121 length = len(serialized) 1122 try: 1123 if self._InternalParse(serialized, 0, length) != length: 1124 # The only reason _InternalParse would return early is if it 1125 # encountered an end-group tag. 1126 raise message_mod.DecodeError('Unexpected end-group tag.') 1127 except (IndexError, TypeError): 1128 # Now ord(buf[p:p+1]) == ord('') gets TypeError. 1129 raise message_mod.DecodeError('Truncated message.') 1130 except struct.error as e: 1131 raise message_mod.DecodeError(e) 1132 return length # Return this for legacy reasons.
Merges serialized protocol buffer data into this message.
When we find a field in serialized
that is already present
in this message:
- If it's a "repeated" field, we append to the end of our list.
- Else, if it's a scalar, we overwrite our field.
- Else, (it's a nonrepeated composite), we recursively merge into the existing composite.
Arguments:
- serialized (bytes): Any object that allows us to call
memoryview(serialized)
to access a string of bytes using the buffer interface.
Returns:
int: The number of bytes read from
serialized
. For non-group messages, this will always belen(serialized)
, but for messages which are actually groups, this will generally be less thanlen(serialized)
, since we must stop when we reach anEND_GROUP
tag. Note that if we do stop because of anEND_GROUP
tag, the number of bytes returned does not include the bytes for theEND_GROUP
tag information.
Raises:
- DecodeError: if the input cannot be parsed.
1072 def SerializeToString(self, **kwargs): 1073 # Check if the message has all of its required fields set. 1074 if not self.IsInitialized(): 1075 raise message_mod.EncodeError( 1076 'Message %s is missing required fields: %s' % ( 1077 self.DESCRIPTOR.full_name, ','.join(self.FindInitializationErrors()))) 1078 return self.SerializePartialToString(**kwargs)
Serializes the protocol message to a binary string.
Keyword Args:
deterministic (bool): If true, requests deterministic serialization of the protobuf, with predictable ordering of map keys.
Returns:
A binary string representation of the message if all of the required fields in the message are set (i.e. the message is initialized).
Raises:
- EncodeError: if the message isn't initialized (see
IsInitialized()
).
1085 def SerializePartialToString(self, **kwargs): 1086 out = BytesIO() 1087 self._InternalSerialize(out.write, **kwargs) 1088 return out.getvalue()
Serializes the protocol message to a binary string.
This method is similar to SerializeToString but doesn't check if the message is initialized.
Keyword Args:
deterministic (bool): If true, requests deterministic serialization of the protobuf, with predictable ordering of map keys.
Returns:
bytes: A serialized representation of the partial message.
814 def ListFields(self): 815 all_fields = [item for item in self._fields.items() if _IsPresent(item)] 816 all_fields.sort(key = lambda item: item[0].number) 817 return all_fields
Returns a list of (FieldDescriptor, value) tuples for present fields.
A message field is non-empty if HasField() would return true. A singular primitive field is non-empty if HasField() would return true in proto2 or it is non zero in proto3. A repeated field is non-empty if it contains at least one element. The fields are ordered by field number.
Returns:
list[tuple(FieldDescriptor, value)]: field descriptors and values for all fields in the message which are not empty. The values vary by field type.
838 def HasField(self, field_name): 839 try: 840 field = hassable_fields[field_name] 841 except KeyError as exc: 842 raise ValueError('Protocol message %s has no non-repeated field "%s" ' 843 'nor has presence is not available for this field.' % ( 844 message_descriptor.full_name, field_name)) from exc 845 846 if isinstance(field, descriptor_mod.OneofDescriptor): 847 try: 848 return HasField(self, self._oneofs[field].name) 849 except KeyError: 850 return False 851 else: 852 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 853 value = self._fields.get(field) 854 return value is not None and value._is_present_in_parent 855 else: 856 return field in self._fields
Checks if a certain field is set for the message.
For a oneof group, checks if any field inside is set. Note that if the
field_name is not defined in the message descriptor, ValueError
will
be raised.
Arguments:
- field_name (str): The name of the field to check for presence.
Returns:
bool: Whether a value has been set for the named field.
Raises:
- ValueError: if the
field_name
is not a member of this message.
863 def ClearField(self, field_name): 864 try: 865 field = message_descriptor.fields_by_name[field_name] 866 except KeyError: 867 try: 868 field = message_descriptor.oneofs_by_name[field_name] 869 if field in self._oneofs: 870 field = self._oneofs[field] 871 else: 872 return 873 except KeyError: 874 raise ValueError('Protocol message %s has no "%s" field.' % 875 (message_descriptor.name, field_name)) 876 877 if field in self._fields: 878 # To match the C++ implementation, we need to invalidate iterators 879 # for map fields when ClearField() happens. 880 if hasattr(self._fields[field], 'InvalidateIterators'): 881 self._fields[field].InvalidateIterators() 882 883 # Note: If the field is a sub-message, its listener will still point 884 # at us. That's fine, because the worst than can happen is that it 885 # will call _Modified() and invalidate our byte size. Big deal. 886 del self._fields[field] 887 888 if self._oneofs.get(field.containing_oneof, None) is field: 889 del self._oneofs[field.containing_oneof] 890 891 # Always call _Modified() -- even if nothing was changed, this is 892 # a mutating method, and thus calling it should cause the field to become 893 # present in the parent message. 894 self._Modified()
Clears the contents of a given field.
Inside a oneof group, clears the field set. If the name neither refers to a
defined field or oneof group, ValueError
is raised.
Arguments:
- field_name (str): The name of the field to check for presence.
Raises:
- ValueError: if the
field_name
is not a member of this message.
1358 def WhichOneof(self, oneof_name): 1359 """Returns the name of the currently set field inside a oneof, or None.""" 1360 try: 1361 field = message_descriptor.oneofs_by_name[oneof_name] 1362 except KeyError: 1363 raise ValueError( 1364 'Protocol message has no oneof "%s" field.' % oneof_name) 1365 1366 nested_field = self._oneofs.get(field, None) 1367 if nested_field is not None and self.HasField(nested_field.name): 1368 return nested_field.name 1369 else: 1370 return None
Returns the name of the currently set field inside a oneof, or None.
1388def _UnknownFields(self): 1389 warnings.warn( 1390 'message.UnknownFields() is deprecated. Please use the add one ' 1391 'feature unknown_fields.UnknownFieldSet(message) in ' 1392 'unknown_fields.py instead.' 1393 ) 1394 if self._unknown_field_set is None: # pylint: disable=protected-access 1395 # pylint: disable=protected-access 1396 self._unknown_field_set = containers.UnknownFieldSet() 1397 return self._unknown_field_set # pylint: disable=protected-access
Returns the UnknownFieldSet.
Returns:
UnknownFieldSet: The unknown fields stored in this message.
1400def _DiscardUnknownFields(self): 1401 self._unknown_fields = [] 1402 self._unknown_field_set = None # pylint: disable=protected-access 1403 for field, value in self.ListFields(): 1404 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 1405 if _IsMapField(field): 1406 if _IsMessageMapField(field): 1407 for key in value: 1408 value[key].DiscardUnknownFields() 1409 elif field.label == _FieldDescriptor.LABEL_REPEATED: 1410 for sub_message in value: 1411 sub_message.DiscardUnknownFields() 1412 else: 1413 value.DiscardUnknownFields()
Clears all fields in the UnknownFieldSet
.
This operation is recursive for nested message.
1040 def ByteSize(self): 1041 if not self._cached_byte_size_dirty: 1042 return self._cached_byte_size 1043 1044 size = 0 1045 descriptor = self.DESCRIPTOR 1046 if descriptor._is_map_entry: 1047 # Fields of map entry should always be serialized. 1048 key_field = descriptor.fields_by_name['key'] 1049 _MaybeAddEncoder(cls, key_field) 1050 size = key_field._sizer(self.key) 1051 value_field = descriptor.fields_by_name['value'] 1052 _MaybeAddEncoder(cls, value_field) 1053 size += value_field._sizer(self.value) 1054 else: 1055 for field_descriptor, field_value in self.ListFields(): 1056 _MaybeAddEncoder(cls, field_descriptor) 1057 size += field_descriptor._sizer(field_value) 1058 for tag_bytes, value_bytes in self._unknown_fields: 1059 size += len(tag_bytes) + len(value_bytes) 1060 1061 self._cached_byte_size = size 1062 self._cached_byte_size_dirty = False 1063 self._listener_for_children.dirty = False 1064 return size
Returns the serialized size of this message.
Recursively calls ByteSize() on all contained messages.
Returns:
int: The number of bytes required to serialize this message.
784 def RegisterExtension(field_descriptor): 785 field_descriptor.containing_type = cls.DESCRIPTOR 786 # TODO: Use cls.MESSAGE_FACTORY.pool when available. 787 # pylint: disable=protected-access 788 cls.DESCRIPTOR.file.pool._AddExtensionDescriptor(field_descriptor) 789 _AttachFieldHelpers(cls, field_descriptor)
685 def getter(self): 686 # TODO: This may be broken since there may not be 687 # default_value. Combine with has_default_value somehow. 688 return self._fields.get(field, default_value)
Getter for name.
642 def getter(self): 643 field_value = self._fields.get(field) 644 if field_value is None: 645 # Construct a new object to represent this field. 646 field_value = field._default_constructor(self) 647 648 # Atomically check if another thread has preempted us and, if not, swap 649 # in the new object we just created. If someone has preempted us, we 650 # take that object and discard ours. 651 # WARNING: We are relying on setdefault() being atomic. This is true 652 # in CPython but we haven't investigated others. This warning appears 653 # in several other locations in this file. 654 field_value = self._fields.setdefault(field, field_value) 655 return field_value
Getter for variables.
642 def getter(self): 643 field_value = self._fields.get(field) 644 if field_value is None: 645 # Construct a new object to represent this field. 646 field_value = field._default_constructor(self) 647 648 # Atomically check if another thread has preempted us and, if not, swap 649 # in the new object we just created. If someone has preempted us, we 650 # take that object and discard ours. 651 # WARNING: We are relying on setdefault() being atomic. This is true 652 # in CPython but we haven't investigated others. This warning appears 653 # in several other locations in this file. 654 field_value = self._fields.setdefault(field, field_value) 655 return field_value
Getter for constraints.
741 def getter(self): 742 field_value = self._fields.get(field) 743 if field_value is None: 744 # Construct a new object to represent this field. 745 field_value = field._default_constructor(self) 746 747 # Atomically check if another thread has preempted us and, if not, swap 748 # in the new object we just created. If someone has preempted us, we 749 # take that object and discard ours. 750 # WARNING: We are relying on setdefault() being atomic. This is true 751 # in CPython but we haven't investigated others. This warning appears 752 # in several other locations in this file. 753 field_value = self._fields.setdefault(field, field_value) 754 return field_value
Getter for objective.
741 def getter(self): 742 field_value = self._fields.get(field) 743 if field_value is None: 744 # Construct a new object to represent this field. 745 field_value = field._default_constructor(self) 746 747 # Atomically check if another thread has preempted us and, if not, swap 748 # in the new object we just created. If someone has preempted us, we 749 # take that object and discard ours. 750 # WARNING: We are relying on setdefault() being atomic. This is true 751 # in CPython but we haven't investigated others. This warning appears 752 # in several other locations in this file. 753 field_value = self._fields.setdefault(field, field_value) 754 return field_value
Getter for floating_point_objective.
642 def getter(self): 643 field_value = self._fields.get(field) 644 if field_value is None: 645 # Construct a new object to represent this field. 646 field_value = field._default_constructor(self) 647 648 # Atomically check if another thread has preempted us and, if not, swap 649 # in the new object we just created. If someone has preempted us, we 650 # take that object and discard ours. 651 # WARNING: We are relying on setdefault() being atomic. This is true 652 # in CPython but we haven't investigated others. This warning appears 653 # in several other locations in this file. 654 field_value = self._fields.setdefault(field, field_value) 655 return field_value
Getter for search_strategy.
741 def getter(self): 742 field_value = self._fields.get(field) 743 if field_value is None: 744 # Construct a new object to represent this field. 745 field_value = field._default_constructor(self) 746 747 # Atomically check if another thread has preempted us and, if not, swap 748 # in the new object we just created. If someone has preempted us, we 749 # take that object and discard ours. 750 # WARNING: We are relying on setdefault() being atomic. This is true 751 # in CPython but we haven't investigated others. This warning appears 752 # in several other locations in this file. 753 field_value = self._fields.setdefault(field, field_value) 754 return field_value
Getter for solution_hint.
642 def getter(self): 643 field_value = self._fields.get(field) 644 if field_value is None: 645 # Construct a new object to represent this field. 646 field_value = field._default_constructor(self) 647 648 # Atomically check if another thread has preempted us and, if not, swap 649 # in the new object we just created. If someone has preempted us, we 650 # take that object and discard ours. 651 # WARNING: We are relying on setdefault() being atomic. This is true 652 # in CPython but we haven't investigated others. This warning appears 653 # in several other locations in this file. 654 field_value = self._fields.setdefault(field, field_value) 655 return field_value
Getter for assumptions.
741 def getter(self): 742 field_value = self._fields.get(field) 743 if field_value is None: 744 # Construct a new object to represent this field. 745 field_value = field._default_constructor(self) 746 747 # Atomically check if another thread has preempted us and, if not, swap 748 # in the new object we just created. If someone has preempted us, we 749 # take that object and discard ours. 750 # WARNING: We are relying on setdefault() being atomic. This is true 751 # in CPython but we haven't investigated others. This warning appears 752 # in several other locations in this file. 753 field_value = self._fields.setdefault(field, field_value) 754 return field_value
Getter for symmetry.
1253 def FindInitializationErrors(self): 1254 """Finds required fields which are not initialized. 1255 1256 Returns: 1257 A list of strings. Each string is a path to an uninitialized field from 1258 the top-level message, e.g. "foo.bar[5].baz". 1259 """ 1260 1261 errors = [] # simplify things 1262 1263 for field in required_fields: 1264 if not self.HasField(field.name): 1265 errors.append(field.name) 1266 1267 for field, value in self.ListFields(): 1268 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 1269 if field.is_extension: 1270 name = '(%s)' % field.full_name 1271 else: 1272 name = field.name 1273 1274 if _IsMapField(field): 1275 if _IsMessageMapField(field): 1276 for key in value: 1277 element = value[key] 1278 prefix = '%s[%s].' % (name, key) 1279 sub_errors = element.FindInitializationErrors() 1280 errors += [prefix + error for error in sub_errors] 1281 else: 1282 # ScalarMaps can't have any initialization errors. 1283 pass 1284 elif field.label == _FieldDescriptor.LABEL_REPEATED: 1285 for i in range(len(value)): 1286 element = value[i] 1287 prefix = '%s[%d].' % (name, i) 1288 sub_errors = element.FindInitializationErrors() 1289 errors += [prefix + error for error in sub_errors] 1290 else: 1291 prefix = name + '.' 1292 sub_errors = value.FindInitializationErrors() 1293 errors += [prefix + error for error in sub_errors] 1294 1295 return errors
Finds required fields which are not initialized.
Returns:
A list of strings. Each string is a path to an uninitialized field from the top-level message, e.g. "foo.bar[5].baz".
Inherited Members
- google.protobuf.message.Message
- CopyFrom
- ParseFromString
- HasExtension
- ClearExtension
Abstract base class for protocol messages.
Protocol message classes are almost always generated by the protocol compiler. These generated types subclass Message and implement the methods shown below.
496 def init(self, **kwargs): 497 self._cached_byte_size = 0 498 self._cached_byte_size_dirty = len(kwargs) > 0 499 self._fields = {} 500 # Contains a mapping from oneof field descriptors to the descriptor 501 # of the currently set field in that oneof field. 502 self._oneofs = {} 503 504 # _unknown_fields is () when empty for efficiency, and will be turned into 505 # a list if fields are added. 506 self._unknown_fields = () 507 # _unknown_field_set is None when empty for efficiency, and will be 508 # turned into UnknownFieldSet struct if fields are added. 509 self._unknown_field_set = None # pylint: disable=protected-access 510 self._is_present_in_parent = False 511 self._listener = message_listener_mod.NullMessageListener() 512 self._listener_for_children = _Listener(self) 513 for field_name, field_value in kwargs.items(): 514 field = _GetFieldByName(message_descriptor, field_name) 515 if field is None: 516 raise TypeError('%s() got an unexpected keyword argument "%s"' % 517 (message_descriptor.name, field_name)) 518 if field_value is None: 519 # field=None is the same as no field at all. 520 continue 521 if field.label == _FieldDescriptor.LABEL_REPEATED: 522 copy = field._default_constructor(self) 523 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: # Composite 524 if _IsMapField(field): 525 if _IsMessageMapField(field): 526 for key in field_value: 527 copy[key].MergeFrom(field_value[key]) 528 else: 529 copy.update(field_value) 530 else: 531 for val in field_value: 532 if isinstance(val, dict): 533 copy.add(**val) 534 else: 535 copy.add().MergeFrom(val) 536 else: # Scalar 537 if field.cpp_type == _FieldDescriptor.CPPTYPE_ENUM: 538 field_value = [_GetIntegerEnumValue(field.enum_type, val) 539 for val in field_value] 540 copy.extend(field_value) 541 self._fields[field] = copy 542 elif field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 543 copy = field._default_constructor(self) 544 new_val = field_value 545 if isinstance(field_value, dict): 546 new_val = field.message_type._concrete_class(**field_value) 547 try: 548 copy.MergeFrom(new_val) 549 except TypeError: 550 _ReraiseTypeErrorWithFieldName(message_descriptor.name, field_name) 551 self._fields[field] = copy 552 else: 553 if field.cpp_type == _FieldDescriptor.CPPTYPE_ENUM: 554 field_value = _GetIntegerEnumValue(field.enum_type, field_value) 555 try: 556 setattr(self, field_name, field_value) 557 except TypeError: 558 _ReraiseTypeErrorWithFieldName(message_descriptor.name, field_name)
1312 def MergeFrom(self, msg): 1313 if not isinstance(msg, cls): 1314 raise TypeError( 1315 'Parameter to MergeFrom() must be instance of same class: ' 1316 'expected %s got %s.' % (_FullyQualifiedClassName(cls), 1317 _FullyQualifiedClassName(msg.__class__))) 1318 1319 assert msg is not self 1320 self._Modified() 1321 1322 fields = self._fields 1323 1324 for field, value in msg._fields.items(): 1325 if field.label == LABEL_REPEATED: 1326 field_value = fields.get(field) 1327 if field_value is None: 1328 # Construct a new object to represent this field. 1329 field_value = field._default_constructor(self) 1330 fields[field] = field_value 1331 field_value.MergeFrom(value) 1332 elif field.cpp_type == CPPTYPE_MESSAGE: 1333 if value._is_present_in_parent: 1334 field_value = fields.get(field) 1335 if field_value is None: 1336 # Construct a new object to represent this field. 1337 field_value = field._default_constructor(self) 1338 fields[field] = field_value 1339 field_value.MergeFrom(value) 1340 else: 1341 self._fields[field] = value 1342 if field.containing_oneof: 1343 self._UpdateOneofState(field) 1344 1345 if msg._unknown_fields: 1346 if not self._unknown_fields: 1347 self._unknown_fields = [] 1348 self._unknown_fields.extend(msg._unknown_fields) 1349 # pylint: disable=protected-access 1350 if self._unknown_field_set is None: 1351 self._unknown_field_set = containers.UnknownFieldSet() 1352 self._unknown_field_set._extend(msg._unknown_field_set)
Merges the contents of the specified message into current message.
This method merges the contents of the specified message into the current message. Singular fields that are set in the specified message overwrite the corresponding fields in the current message. Repeated fields are appended. Singular sub-messages and groups are recursively merged.
Arguments:
- other_msg (Message): A message to merge into the current message.
1375def _Clear(self): 1376 # Clear fields. 1377 self._fields = {} 1378 self._unknown_fields = () 1379 # pylint: disable=protected-access 1380 if self._unknown_field_set is not None: 1381 self._unknown_field_set._clear() 1382 self._unknown_field_set = None 1383 1384 self._oneofs = {} 1385 self._Modified()
Clears all data that was set in the message.
1452 def Modified(self): 1453 """Sets the _cached_byte_size_dirty bit to true, 1454 and propagates this to our listener iff this was a state change. 1455 """ 1456 1457 # Note: Some callers check _cached_byte_size_dirty before calling 1458 # _Modified() as an extra optimization. So, if this method is ever 1459 # changed such that it does stuff even when _cached_byte_size_dirty is 1460 # already true, the callers need to be updated. 1461 if not self._cached_byte_size_dirty: 1462 self._cached_byte_size_dirty = True 1463 self._listener_for_children.dirty = True 1464 self._is_present_in_parent = True 1465 self._listener.Modified()
Sets the _cached_byte_size_dirty bit to true, and propagates this to our listener iff this was a state change.
1213 def IsInitialized(self, errors=None): 1214 """Checks if all required fields of a message are set. 1215 1216 Args: 1217 errors: A list which, if provided, will be populated with the field 1218 paths of all missing required fields. 1219 1220 Returns: 1221 True iff the specified message has all required fields set. 1222 """ 1223 1224 # Performance is critical so we avoid HasField() and ListFields(). 1225 1226 for field in required_fields: 1227 if (field not in self._fields or 1228 (field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE and 1229 not self._fields[field]._is_present_in_parent)): 1230 if errors is not None: 1231 errors.extend(self.FindInitializationErrors()) 1232 return False 1233 1234 for field, value in list(self._fields.items()): # dict can change size! 1235 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 1236 if field.label == _FieldDescriptor.LABEL_REPEATED: 1237 if (field.message_type._is_map_entry): 1238 continue 1239 for element in value: 1240 if not element.IsInitialized(): 1241 if errors is not None: 1242 errors.extend(self.FindInitializationErrors()) 1243 return False 1244 elif value._is_present_in_parent and not value.IsInitialized(): 1245 if errors is not None: 1246 errors.extend(self.FindInitializationErrors()) 1247 return False 1248 1249 return True
Checks if all required fields of a message are set.
Arguments:
- errors: A list which, if provided, will be populated with the field paths of all missing required fields.
Returns:
True iff the specified message has all required fields set.
1119 def MergeFromString(self, serialized): 1120 serialized = memoryview(serialized) 1121 length = len(serialized) 1122 try: 1123 if self._InternalParse(serialized, 0, length) != length: 1124 # The only reason _InternalParse would return early is if it 1125 # encountered an end-group tag. 1126 raise message_mod.DecodeError('Unexpected end-group tag.') 1127 except (IndexError, TypeError): 1128 # Now ord(buf[p:p+1]) == ord('') gets TypeError. 1129 raise message_mod.DecodeError('Truncated message.') 1130 except struct.error as e: 1131 raise message_mod.DecodeError(e) 1132 return length # Return this for legacy reasons.
Merges serialized protocol buffer data into this message.
When we find a field in serialized
that is already present
in this message:
- If it's a "repeated" field, we append to the end of our list.
- Else, if it's a scalar, we overwrite our field.
- Else, (it's a nonrepeated composite), we recursively merge into the existing composite.
Arguments:
- serialized (bytes): Any object that allows us to call
memoryview(serialized)
to access a string of bytes using the buffer interface.
Returns:
int: The number of bytes read from
serialized
. For non-group messages, this will always belen(serialized)
, but for messages which are actually groups, this will generally be less thanlen(serialized)
, since we must stop when we reach anEND_GROUP
tag. Note that if we do stop because of anEND_GROUP
tag, the number of bytes returned does not include the bytes for theEND_GROUP
tag information.
Raises:
- DecodeError: if the input cannot be parsed.
1072 def SerializeToString(self, **kwargs): 1073 # Check if the message has all of its required fields set. 1074 if not self.IsInitialized(): 1075 raise message_mod.EncodeError( 1076 'Message %s is missing required fields: %s' % ( 1077 self.DESCRIPTOR.full_name, ','.join(self.FindInitializationErrors()))) 1078 return self.SerializePartialToString(**kwargs)
Serializes the protocol message to a binary string.
Keyword Args:
deterministic (bool): If true, requests deterministic serialization of the protobuf, with predictable ordering of map keys.
Returns:
A binary string representation of the message if all of the required fields in the message are set (i.e. the message is initialized).
Raises:
- EncodeError: if the message isn't initialized (see
IsInitialized()
).
1085 def SerializePartialToString(self, **kwargs): 1086 out = BytesIO() 1087 self._InternalSerialize(out.write, **kwargs) 1088 return out.getvalue()
Serializes the protocol message to a binary string.
This method is similar to SerializeToString but doesn't check if the message is initialized.
Keyword Args:
deterministic (bool): If true, requests deterministic serialization of the protobuf, with predictable ordering of map keys.
Returns:
bytes: A serialized representation of the partial message.
814 def ListFields(self): 815 all_fields = [item for item in self._fields.items() if _IsPresent(item)] 816 all_fields.sort(key = lambda item: item[0].number) 817 return all_fields
Returns a list of (FieldDescriptor, value) tuples for present fields.
A message field is non-empty if HasField() would return true. A singular primitive field is non-empty if HasField() would return true in proto2 or it is non zero in proto3. A repeated field is non-empty if it contains at least one element. The fields are ordered by field number.
Returns:
list[tuple(FieldDescriptor, value)]: field descriptors and values for all fields in the message which are not empty. The values vary by field type.
838 def HasField(self, field_name): 839 try: 840 field = hassable_fields[field_name] 841 except KeyError as exc: 842 raise ValueError('Protocol message %s has no non-repeated field "%s" ' 843 'nor has presence is not available for this field.' % ( 844 message_descriptor.full_name, field_name)) from exc 845 846 if isinstance(field, descriptor_mod.OneofDescriptor): 847 try: 848 return HasField(self, self._oneofs[field].name) 849 except KeyError: 850 return False 851 else: 852 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 853 value = self._fields.get(field) 854 return value is not None and value._is_present_in_parent 855 else: 856 return field in self._fields
Checks if a certain field is set for the message.
For a oneof group, checks if any field inside is set. Note that if the
field_name is not defined in the message descriptor, ValueError
will
be raised.
Arguments:
- field_name (str): The name of the field to check for presence.
Returns:
bool: Whether a value has been set for the named field.
Raises:
- ValueError: if the
field_name
is not a member of this message.
863 def ClearField(self, field_name): 864 try: 865 field = message_descriptor.fields_by_name[field_name] 866 except KeyError: 867 try: 868 field = message_descriptor.oneofs_by_name[field_name] 869 if field in self._oneofs: 870 field = self._oneofs[field] 871 else: 872 return 873 except KeyError: 874 raise ValueError('Protocol message %s has no "%s" field.' % 875 (message_descriptor.name, field_name)) 876 877 if field in self._fields: 878 # To match the C++ implementation, we need to invalidate iterators 879 # for map fields when ClearField() happens. 880 if hasattr(self._fields[field], 'InvalidateIterators'): 881 self._fields[field].InvalidateIterators() 882 883 # Note: If the field is a sub-message, its listener will still point 884 # at us. That's fine, because the worst than can happen is that it 885 # will call _Modified() and invalidate our byte size. Big deal. 886 del self._fields[field] 887 888 if self._oneofs.get(field.containing_oneof, None) is field: 889 del self._oneofs[field.containing_oneof] 890 891 # Always call _Modified() -- even if nothing was changed, this is 892 # a mutating method, and thus calling it should cause the field to become 893 # present in the parent message. 894 self._Modified()
Clears the contents of a given field.
Inside a oneof group, clears the field set. If the name neither refers to a
defined field or oneof group, ValueError
is raised.
Arguments:
- field_name (str): The name of the field to check for presence.
Raises:
- ValueError: if the
field_name
is not a member of this message.
1358 def WhichOneof(self, oneof_name): 1359 """Returns the name of the currently set field inside a oneof, or None.""" 1360 try: 1361 field = message_descriptor.oneofs_by_name[oneof_name] 1362 except KeyError: 1363 raise ValueError( 1364 'Protocol message has no oneof "%s" field.' % oneof_name) 1365 1366 nested_field = self._oneofs.get(field, None) 1367 if nested_field is not None and self.HasField(nested_field.name): 1368 return nested_field.name 1369 else: 1370 return None
Returns the name of the currently set field inside a oneof, or None.
1388def _UnknownFields(self): 1389 warnings.warn( 1390 'message.UnknownFields() is deprecated. Please use the add one ' 1391 'feature unknown_fields.UnknownFieldSet(message) in ' 1392 'unknown_fields.py instead.' 1393 ) 1394 if self._unknown_field_set is None: # pylint: disable=protected-access 1395 # pylint: disable=protected-access 1396 self._unknown_field_set = containers.UnknownFieldSet() 1397 return self._unknown_field_set # pylint: disable=protected-access
Returns the UnknownFieldSet.
Returns:
UnknownFieldSet: The unknown fields stored in this message.
1400def _DiscardUnknownFields(self): 1401 self._unknown_fields = [] 1402 self._unknown_field_set = None # pylint: disable=protected-access 1403 for field, value in self.ListFields(): 1404 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 1405 if _IsMapField(field): 1406 if _IsMessageMapField(field): 1407 for key in value: 1408 value[key].DiscardUnknownFields() 1409 elif field.label == _FieldDescriptor.LABEL_REPEATED: 1410 for sub_message in value: 1411 sub_message.DiscardUnknownFields() 1412 else: 1413 value.DiscardUnknownFields()
Clears all fields in the UnknownFieldSet
.
This operation is recursive for nested message.
1040 def ByteSize(self): 1041 if not self._cached_byte_size_dirty: 1042 return self._cached_byte_size 1043 1044 size = 0 1045 descriptor = self.DESCRIPTOR 1046 if descriptor._is_map_entry: 1047 # Fields of map entry should always be serialized. 1048 key_field = descriptor.fields_by_name['key'] 1049 _MaybeAddEncoder(cls, key_field) 1050 size = key_field._sizer(self.key) 1051 value_field = descriptor.fields_by_name['value'] 1052 _MaybeAddEncoder(cls, value_field) 1053 size += value_field._sizer(self.value) 1054 else: 1055 for field_descriptor, field_value in self.ListFields(): 1056 _MaybeAddEncoder(cls, field_descriptor) 1057 size += field_descriptor._sizer(field_value) 1058 for tag_bytes, value_bytes in self._unknown_fields: 1059 size += len(tag_bytes) + len(value_bytes) 1060 1061 self._cached_byte_size = size 1062 self._cached_byte_size_dirty = False 1063 self._listener_for_children.dirty = False 1064 return size
Returns the serialized size of this message.
Recursively calls ByteSize() on all contained messages.
Returns:
int: The number of bytes required to serialize this message.
784 def RegisterExtension(field_descriptor): 785 field_descriptor.containing_type = cls.DESCRIPTOR 786 # TODO: Use cls.MESSAGE_FACTORY.pool when available. 787 # pylint: disable=protected-access 788 cls.DESCRIPTOR.file.pool._AddExtensionDescriptor(field_descriptor) 789 _AttachFieldHelpers(cls, field_descriptor)
642 def getter(self): 643 field_value = self._fields.get(field) 644 if field_value is None: 645 # Construct a new object to represent this field. 646 field_value = field._default_constructor(self) 647 648 # Atomically check if another thread has preempted us and, if not, swap 649 # in the new object we just created. If someone has preempted us, we 650 # take that object and discard ours. 651 # WARNING: We are relying on setdefault() being atomic. This is true 652 # in CPython but we haven't investigated others. This warning appears 653 # in several other locations in this file. 654 field_value = self._fields.setdefault(field, field_value) 655 return field_value
Getter for values.
1253 def FindInitializationErrors(self): 1254 """Finds required fields which are not initialized. 1255 1256 Returns: 1257 A list of strings. Each string is a path to an uninitialized field from 1258 the top-level message, e.g. "foo.bar[5].baz". 1259 """ 1260 1261 errors = [] # simplify things 1262 1263 for field in required_fields: 1264 if not self.HasField(field.name): 1265 errors.append(field.name) 1266 1267 for field, value in self.ListFields(): 1268 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 1269 if field.is_extension: 1270 name = '(%s)' % field.full_name 1271 else: 1272 name = field.name 1273 1274 if _IsMapField(field): 1275 if _IsMessageMapField(field): 1276 for key in value: 1277 element = value[key] 1278 prefix = '%s[%s].' % (name, key) 1279 sub_errors = element.FindInitializationErrors() 1280 errors += [prefix + error for error in sub_errors] 1281 else: 1282 # ScalarMaps can't have any initialization errors. 1283 pass 1284 elif field.label == _FieldDescriptor.LABEL_REPEATED: 1285 for i in range(len(value)): 1286 element = value[i] 1287 prefix = '%s[%d].' % (name, i) 1288 sub_errors = element.FindInitializationErrors() 1289 errors += [prefix + error for error in sub_errors] 1290 else: 1291 prefix = name + '.' 1292 sub_errors = value.FindInitializationErrors() 1293 errors += [prefix + error for error in sub_errors] 1294 1295 return errors
Finds required fields which are not initialized.
Returns:
A list of strings. Each string is a path to an uninitialized field from the top-level message, e.g. "foo.bar[5].baz".
Inherited Members
- google.protobuf.message.Message
- CopyFrom
- ParseFromString
- HasExtension
- ClearExtension
Abstract base class for protocol messages.
Protocol message classes are almost always generated by the protocol compiler. These generated types subclass Message and implement the methods shown below.
496 def init(self, **kwargs): 497 self._cached_byte_size = 0 498 self._cached_byte_size_dirty = len(kwargs) > 0 499 self._fields = {} 500 # Contains a mapping from oneof field descriptors to the descriptor 501 # of the currently set field in that oneof field. 502 self._oneofs = {} 503 504 # _unknown_fields is () when empty for efficiency, and will be turned into 505 # a list if fields are added. 506 self._unknown_fields = () 507 # _unknown_field_set is None when empty for efficiency, and will be 508 # turned into UnknownFieldSet struct if fields are added. 509 self._unknown_field_set = None # pylint: disable=protected-access 510 self._is_present_in_parent = False 511 self._listener = message_listener_mod.NullMessageListener() 512 self._listener_for_children = _Listener(self) 513 for field_name, field_value in kwargs.items(): 514 field = _GetFieldByName(message_descriptor, field_name) 515 if field is None: 516 raise TypeError('%s() got an unexpected keyword argument "%s"' % 517 (message_descriptor.name, field_name)) 518 if field_value is None: 519 # field=None is the same as no field at all. 520 continue 521 if field.label == _FieldDescriptor.LABEL_REPEATED: 522 copy = field._default_constructor(self) 523 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: # Composite 524 if _IsMapField(field): 525 if _IsMessageMapField(field): 526 for key in field_value: 527 copy[key].MergeFrom(field_value[key]) 528 else: 529 copy.update(field_value) 530 else: 531 for val in field_value: 532 if isinstance(val, dict): 533 copy.add(**val) 534 else: 535 copy.add().MergeFrom(val) 536 else: # Scalar 537 if field.cpp_type == _FieldDescriptor.CPPTYPE_ENUM: 538 field_value = [_GetIntegerEnumValue(field.enum_type, val) 539 for val in field_value] 540 copy.extend(field_value) 541 self._fields[field] = copy 542 elif field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 543 copy = field._default_constructor(self) 544 new_val = field_value 545 if isinstance(field_value, dict): 546 new_val = field.message_type._concrete_class(**field_value) 547 try: 548 copy.MergeFrom(new_val) 549 except TypeError: 550 _ReraiseTypeErrorWithFieldName(message_descriptor.name, field_name) 551 self._fields[field] = copy 552 else: 553 if field.cpp_type == _FieldDescriptor.CPPTYPE_ENUM: 554 field_value = _GetIntegerEnumValue(field.enum_type, field_value) 555 try: 556 setattr(self, field_name, field_value) 557 except TypeError: 558 _ReraiseTypeErrorWithFieldName(message_descriptor.name, field_name)
1312 def MergeFrom(self, msg): 1313 if not isinstance(msg, cls): 1314 raise TypeError( 1315 'Parameter to MergeFrom() must be instance of same class: ' 1316 'expected %s got %s.' % (_FullyQualifiedClassName(cls), 1317 _FullyQualifiedClassName(msg.__class__))) 1318 1319 assert msg is not self 1320 self._Modified() 1321 1322 fields = self._fields 1323 1324 for field, value in msg._fields.items(): 1325 if field.label == LABEL_REPEATED: 1326 field_value = fields.get(field) 1327 if field_value is None: 1328 # Construct a new object to represent this field. 1329 field_value = field._default_constructor(self) 1330 fields[field] = field_value 1331 field_value.MergeFrom(value) 1332 elif field.cpp_type == CPPTYPE_MESSAGE: 1333 if value._is_present_in_parent: 1334 field_value = fields.get(field) 1335 if field_value is None: 1336 # Construct a new object to represent this field. 1337 field_value = field._default_constructor(self) 1338 fields[field] = field_value 1339 field_value.MergeFrom(value) 1340 else: 1341 self._fields[field] = value 1342 if field.containing_oneof: 1343 self._UpdateOneofState(field) 1344 1345 if msg._unknown_fields: 1346 if not self._unknown_fields: 1347 self._unknown_fields = [] 1348 self._unknown_fields.extend(msg._unknown_fields) 1349 # pylint: disable=protected-access 1350 if self._unknown_field_set is None: 1351 self._unknown_field_set = containers.UnknownFieldSet() 1352 self._unknown_field_set._extend(msg._unknown_field_set)
Merges the contents of the specified message into current message.
This method merges the contents of the specified message into the current message. Singular fields that are set in the specified message overwrite the corresponding fields in the current message. Repeated fields are appended. Singular sub-messages and groups are recursively merged.
Arguments:
- other_msg (Message): A message to merge into the current message.
1375def _Clear(self): 1376 # Clear fields. 1377 self._fields = {} 1378 self._unknown_fields = () 1379 # pylint: disable=protected-access 1380 if self._unknown_field_set is not None: 1381 self._unknown_field_set._clear() 1382 self._unknown_field_set = None 1383 1384 self._oneofs = {} 1385 self._Modified()
Clears all data that was set in the message.
1452 def Modified(self): 1453 """Sets the _cached_byte_size_dirty bit to true, 1454 and propagates this to our listener iff this was a state change. 1455 """ 1456 1457 # Note: Some callers check _cached_byte_size_dirty before calling 1458 # _Modified() as an extra optimization. So, if this method is ever 1459 # changed such that it does stuff even when _cached_byte_size_dirty is 1460 # already true, the callers need to be updated. 1461 if not self._cached_byte_size_dirty: 1462 self._cached_byte_size_dirty = True 1463 self._listener_for_children.dirty = True 1464 self._is_present_in_parent = True 1465 self._listener.Modified()
Sets the _cached_byte_size_dirty bit to true, and propagates this to our listener iff this was a state change.
1213 def IsInitialized(self, errors=None): 1214 """Checks if all required fields of a message are set. 1215 1216 Args: 1217 errors: A list which, if provided, will be populated with the field 1218 paths of all missing required fields. 1219 1220 Returns: 1221 True iff the specified message has all required fields set. 1222 """ 1223 1224 # Performance is critical so we avoid HasField() and ListFields(). 1225 1226 for field in required_fields: 1227 if (field not in self._fields or 1228 (field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE and 1229 not self._fields[field]._is_present_in_parent)): 1230 if errors is not None: 1231 errors.extend(self.FindInitializationErrors()) 1232 return False 1233 1234 for field, value in list(self._fields.items()): # dict can change size! 1235 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 1236 if field.label == _FieldDescriptor.LABEL_REPEATED: 1237 if (field.message_type._is_map_entry): 1238 continue 1239 for element in value: 1240 if not element.IsInitialized(): 1241 if errors is not None: 1242 errors.extend(self.FindInitializationErrors()) 1243 return False 1244 elif value._is_present_in_parent and not value.IsInitialized(): 1245 if errors is not None: 1246 errors.extend(self.FindInitializationErrors()) 1247 return False 1248 1249 return True
Checks if all required fields of a message are set.
Arguments:
- errors: A list which, if provided, will be populated with the field paths of all missing required fields.
Returns:
True iff the specified message has all required fields set.
1119 def MergeFromString(self, serialized): 1120 serialized = memoryview(serialized) 1121 length = len(serialized) 1122 try: 1123 if self._InternalParse(serialized, 0, length) != length: 1124 # The only reason _InternalParse would return early is if it 1125 # encountered an end-group tag. 1126 raise message_mod.DecodeError('Unexpected end-group tag.') 1127 except (IndexError, TypeError): 1128 # Now ord(buf[p:p+1]) == ord('') gets TypeError. 1129 raise message_mod.DecodeError('Truncated message.') 1130 except struct.error as e: 1131 raise message_mod.DecodeError(e) 1132 return length # Return this for legacy reasons.
Merges serialized protocol buffer data into this message.
When we find a field in serialized
that is already present
in this message:
- If it's a "repeated" field, we append to the end of our list.
- Else, if it's a scalar, we overwrite our field.
- Else, (it's a nonrepeated composite), we recursively merge into the existing composite.
Arguments:
- serialized (bytes): Any object that allows us to call
memoryview(serialized)
to access a string of bytes using the buffer interface.
Returns:
int: The number of bytes read from
serialized
. For non-group messages, this will always belen(serialized)
, but for messages which are actually groups, this will generally be less thanlen(serialized)
, since we must stop when we reach anEND_GROUP
tag. Note that if we do stop because of anEND_GROUP
tag, the number of bytes returned does not include the bytes for theEND_GROUP
tag information.
Raises:
- DecodeError: if the input cannot be parsed.
1072 def SerializeToString(self, **kwargs): 1073 # Check if the message has all of its required fields set. 1074 if not self.IsInitialized(): 1075 raise message_mod.EncodeError( 1076 'Message %s is missing required fields: %s' % ( 1077 self.DESCRIPTOR.full_name, ','.join(self.FindInitializationErrors()))) 1078 return self.SerializePartialToString(**kwargs)
Serializes the protocol message to a binary string.
Keyword Args:
deterministic (bool): If true, requests deterministic serialization of the protobuf, with predictable ordering of map keys.
Returns:
A binary string representation of the message if all of the required fields in the message are set (i.e. the message is initialized).
Raises:
- EncodeError: if the message isn't initialized (see
IsInitialized()
).
1085 def SerializePartialToString(self, **kwargs): 1086 out = BytesIO() 1087 self._InternalSerialize(out.write, **kwargs) 1088 return out.getvalue()
Serializes the protocol message to a binary string.
This method is similar to SerializeToString but doesn't check if the message is initialized.
Keyword Args:
deterministic (bool): If true, requests deterministic serialization of the protobuf, with predictable ordering of map keys.
Returns:
bytes: A serialized representation of the partial message.
814 def ListFields(self): 815 all_fields = [item for item in self._fields.items() if _IsPresent(item)] 816 all_fields.sort(key = lambda item: item[0].number) 817 return all_fields
Returns a list of (FieldDescriptor, value) tuples for present fields.
A message field is non-empty if HasField() would return true. A singular primitive field is non-empty if HasField() would return true in proto2 or it is non zero in proto3. A repeated field is non-empty if it contains at least one element. The fields are ordered by field number.
Returns:
list[tuple(FieldDescriptor, value)]: field descriptors and values for all fields in the message which are not empty. The values vary by field type.
838 def HasField(self, field_name): 839 try: 840 field = hassable_fields[field_name] 841 except KeyError as exc: 842 raise ValueError('Protocol message %s has no non-repeated field "%s" ' 843 'nor has presence is not available for this field.' % ( 844 message_descriptor.full_name, field_name)) from exc 845 846 if isinstance(field, descriptor_mod.OneofDescriptor): 847 try: 848 return HasField(self, self._oneofs[field].name) 849 except KeyError: 850 return False 851 else: 852 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 853 value = self._fields.get(field) 854 return value is not None and value._is_present_in_parent 855 else: 856 return field in self._fields
Checks if a certain field is set for the message.
For a oneof group, checks if any field inside is set. Note that if the
field_name is not defined in the message descriptor, ValueError
will
be raised.
Arguments:
- field_name (str): The name of the field to check for presence.
Returns:
bool: Whether a value has been set for the named field.
Raises:
- ValueError: if the
field_name
is not a member of this message.
863 def ClearField(self, field_name): 864 try: 865 field = message_descriptor.fields_by_name[field_name] 866 except KeyError: 867 try: 868 field = message_descriptor.oneofs_by_name[field_name] 869 if field in self._oneofs: 870 field = self._oneofs[field] 871 else: 872 return 873 except KeyError: 874 raise ValueError('Protocol message %s has no "%s" field.' % 875 (message_descriptor.name, field_name)) 876 877 if field in self._fields: 878 # To match the C++ implementation, we need to invalidate iterators 879 # for map fields when ClearField() happens. 880 if hasattr(self._fields[field], 'InvalidateIterators'): 881 self._fields[field].InvalidateIterators() 882 883 # Note: If the field is a sub-message, its listener will still point 884 # at us. That's fine, because the worst than can happen is that it 885 # will call _Modified() and invalidate our byte size. Big deal. 886 del self._fields[field] 887 888 if self._oneofs.get(field.containing_oneof, None) is field: 889 del self._oneofs[field.containing_oneof] 890 891 # Always call _Modified() -- even if nothing was changed, this is 892 # a mutating method, and thus calling it should cause the field to become 893 # present in the parent message. 894 self._Modified()
Clears the contents of a given field.
Inside a oneof group, clears the field set. If the name neither refers to a
defined field or oneof group, ValueError
is raised.
Arguments:
- field_name (str): The name of the field to check for presence.
Raises:
- ValueError: if the
field_name
is not a member of this message.
1358 def WhichOneof(self, oneof_name): 1359 """Returns the name of the currently set field inside a oneof, or None.""" 1360 try: 1361 field = message_descriptor.oneofs_by_name[oneof_name] 1362 except KeyError: 1363 raise ValueError( 1364 'Protocol message has no oneof "%s" field.' % oneof_name) 1365 1366 nested_field = self._oneofs.get(field, None) 1367 if nested_field is not None and self.HasField(nested_field.name): 1368 return nested_field.name 1369 else: 1370 return None
Returns the name of the currently set field inside a oneof, or None.
1388def _UnknownFields(self): 1389 warnings.warn( 1390 'message.UnknownFields() is deprecated. Please use the add one ' 1391 'feature unknown_fields.UnknownFieldSet(message) in ' 1392 'unknown_fields.py instead.' 1393 ) 1394 if self._unknown_field_set is None: # pylint: disable=protected-access 1395 # pylint: disable=protected-access 1396 self._unknown_field_set = containers.UnknownFieldSet() 1397 return self._unknown_field_set # pylint: disable=protected-access
Returns the UnknownFieldSet.
Returns:
UnknownFieldSet: The unknown fields stored in this message.
1400def _DiscardUnknownFields(self): 1401 self._unknown_fields = [] 1402 self._unknown_field_set = None # pylint: disable=protected-access 1403 for field, value in self.ListFields(): 1404 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 1405 if _IsMapField(field): 1406 if _IsMessageMapField(field): 1407 for key in value: 1408 value[key].DiscardUnknownFields() 1409 elif field.label == _FieldDescriptor.LABEL_REPEATED: 1410 for sub_message in value: 1411 sub_message.DiscardUnknownFields() 1412 else: 1413 value.DiscardUnknownFields()
Clears all fields in the UnknownFieldSet
.
This operation is recursive for nested message.
1040 def ByteSize(self): 1041 if not self._cached_byte_size_dirty: 1042 return self._cached_byte_size 1043 1044 size = 0 1045 descriptor = self.DESCRIPTOR 1046 if descriptor._is_map_entry: 1047 # Fields of map entry should always be serialized. 1048 key_field = descriptor.fields_by_name['key'] 1049 _MaybeAddEncoder(cls, key_field) 1050 size = key_field._sizer(self.key) 1051 value_field = descriptor.fields_by_name['value'] 1052 _MaybeAddEncoder(cls, value_field) 1053 size += value_field._sizer(self.value) 1054 else: 1055 for field_descriptor, field_value in self.ListFields(): 1056 _MaybeAddEncoder(cls, field_descriptor) 1057 size += field_descriptor._sizer(field_value) 1058 for tag_bytes, value_bytes in self._unknown_fields: 1059 size += len(tag_bytes) + len(value_bytes) 1060 1061 self._cached_byte_size = size 1062 self._cached_byte_size_dirty = False 1063 self._listener_for_children.dirty = False 1064 return size
Returns the serialized size of this message.
Recursively calls ByteSize() on all contained messages.
Returns:
int: The number of bytes required to serialize this message.
784 def RegisterExtension(field_descriptor): 785 field_descriptor.containing_type = cls.DESCRIPTOR 786 # TODO: Use cls.MESSAGE_FACTORY.pool when available. 787 # pylint: disable=protected-access 788 cls.DESCRIPTOR.file.pool._AddExtensionDescriptor(field_descriptor) 789 _AttachFieldHelpers(cls, field_descriptor)
685 def getter(self): 686 # TODO: This may be broken since there may not be 687 # default_value. Combine with has_default_value somehow. 688 return self._fields.get(field, default_value)
Getter for status.
642 def getter(self): 643 field_value = self._fields.get(field) 644 if field_value is None: 645 # Construct a new object to represent this field. 646 field_value = field._default_constructor(self) 647 648 # Atomically check if another thread has preempted us and, if not, swap 649 # in the new object we just created. If someone has preempted us, we 650 # take that object and discard ours. 651 # WARNING: We are relying on setdefault() being atomic. This is true 652 # in CPython but we haven't investigated others. This warning appears 653 # in several other locations in this file. 654 field_value = self._fields.setdefault(field, field_value) 655 return field_value
Getter for solution.
685 def getter(self): 686 # TODO: This may be broken since there may not be 687 # default_value. Combine with has_default_value somehow. 688 return self._fields.get(field, default_value)
Getter for objective_value.
685 def getter(self): 686 # TODO: This may be broken since there may not be 687 # default_value. Combine with has_default_value somehow. 688 return self._fields.get(field, default_value)
Getter for best_objective_bound.
642 def getter(self): 643 field_value = self._fields.get(field) 644 if field_value is None: 645 # Construct a new object to represent this field. 646 field_value = field._default_constructor(self) 647 648 # Atomically check if another thread has preempted us and, if not, swap 649 # in the new object we just created. If someone has preempted us, we 650 # take that object and discard ours. 651 # WARNING: We are relying on setdefault() being atomic. This is true 652 # in CPython but we haven't investigated others. This warning appears 653 # in several other locations in this file. 654 field_value = self._fields.setdefault(field, field_value) 655 return field_value
Getter for additional_solutions.
642 def getter(self): 643 field_value = self._fields.get(field) 644 if field_value is None: 645 # Construct a new object to represent this field. 646 field_value = field._default_constructor(self) 647 648 # Atomically check if another thread has preempted us and, if not, swap 649 # in the new object we just created. If someone has preempted us, we 650 # take that object and discard ours. 651 # WARNING: We are relying on setdefault() being atomic. This is true 652 # in CPython but we haven't investigated others. This warning appears 653 # in several other locations in this file. 654 field_value = self._fields.setdefault(field, field_value) 655 return field_value
Getter for tightened_variables.
642 def getter(self): 643 field_value = self._fields.get(field) 644 if field_value is None: 645 # Construct a new object to represent this field. 646 field_value = field._default_constructor(self) 647 648 # Atomically check if another thread has preempted us and, if not, swap 649 # in the new object we just created. If someone has preempted us, we 650 # take that object and discard ours. 651 # WARNING: We are relying on setdefault() being atomic. This is true 652 # in CPython but we haven't investigated others. This warning appears 653 # in several other locations in this file. 654 field_value = self._fields.setdefault(field, field_value) 655 return field_value
Getter for sufficient_assumptions_for_infeasibility.
741 def getter(self): 742 field_value = self._fields.get(field) 743 if field_value is None: 744 # Construct a new object to represent this field. 745 field_value = field._default_constructor(self) 746 747 # Atomically check if another thread has preempted us and, if not, swap 748 # in the new object we just created. If someone has preempted us, we 749 # take that object and discard ours. 750 # WARNING: We are relying on setdefault() being atomic. This is true 751 # in CPython but we haven't investigated others. This warning appears 752 # in several other locations in this file. 753 field_value = self._fields.setdefault(field, field_value) 754 return field_value
Getter for integer_objective.
685 def getter(self): 686 # TODO: This may be broken since there may not be 687 # default_value. Combine with has_default_value somehow. 688 return self._fields.get(field, default_value)
Getter for inner_objective_lower_bound.
685 def getter(self): 686 # TODO: This may be broken since there may not be 687 # default_value. Combine with has_default_value somehow. 688 return self._fields.get(field, default_value)
Getter for num_integers.
685 def getter(self): 686 # TODO: This may be broken since there may not be 687 # default_value. Combine with has_default_value somehow. 688 return self._fields.get(field, default_value)
Getter for num_booleans.
685 def getter(self): 686 # TODO: This may be broken since there may not be 687 # default_value. Combine with has_default_value somehow. 688 return self._fields.get(field, default_value)
Getter for num_conflicts.
685 def getter(self): 686 # TODO: This may be broken since there may not be 687 # default_value. Combine with has_default_value somehow. 688 return self._fields.get(field, default_value)
Getter for num_branches.
685 def getter(self): 686 # TODO: This may be broken since there may not be 687 # default_value. Combine with has_default_value somehow. 688 return self._fields.get(field, default_value)
Getter for num_binary_propagations.
685 def getter(self): 686 # TODO: This may be broken since there may not be 687 # default_value. Combine with has_default_value somehow. 688 return self._fields.get(field, default_value)
Getter for num_integer_propagations.
685 def getter(self): 686 # TODO: This may be broken since there may not be 687 # default_value. Combine with has_default_value somehow. 688 return self._fields.get(field, default_value)
Getter for num_restarts.
685 def getter(self): 686 # TODO: This may be broken since there may not be 687 # default_value. Combine with has_default_value somehow. 688 return self._fields.get(field, default_value)
Getter for num_lp_iterations.
685 def getter(self): 686 # TODO: This may be broken since there may not be 687 # default_value. Combine with has_default_value somehow. 688 return self._fields.get(field, default_value)
Getter for wall_time.
685 def getter(self): 686 # TODO: This may be broken since there may not be 687 # default_value. Combine with has_default_value somehow. 688 return self._fields.get(field, default_value)
Getter for user_time.
685 def getter(self): 686 # TODO: This may be broken since there may not be 687 # default_value. Combine with has_default_value somehow. 688 return self._fields.get(field, default_value)
Getter for deterministic_time.
685 def getter(self): 686 # TODO: This may be broken since there may not be 687 # default_value. Combine with has_default_value somehow. 688 return self._fields.get(field, default_value)
Getter for gap_integral.
685 def getter(self): 686 # TODO: This may be broken since there may not be 687 # default_value. Combine with has_default_value somehow. 688 return self._fields.get(field, default_value)
Getter for solution_info.
685 def getter(self): 686 # TODO: This may be broken since there may not be 687 # default_value. Combine with has_default_value somehow. 688 return self._fields.get(field, default_value)
Getter for solve_log.
1253 def FindInitializationErrors(self): 1254 """Finds required fields which are not initialized. 1255 1256 Returns: 1257 A list of strings. Each string is a path to an uninitialized field from 1258 the top-level message, e.g. "foo.bar[5].baz". 1259 """ 1260 1261 errors = [] # simplify things 1262 1263 for field in required_fields: 1264 if not self.HasField(field.name): 1265 errors.append(field.name) 1266 1267 for field, value in self.ListFields(): 1268 if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: 1269 if field.is_extension: 1270 name = '(%s)' % field.full_name 1271 else: 1272 name = field.name 1273 1274 if _IsMapField(field): 1275 if _IsMessageMapField(field): 1276 for key in value: 1277 element = value[key] 1278 prefix = '%s[%s].' % (name, key) 1279 sub_errors = element.FindInitializationErrors() 1280 errors += [prefix + error for error in sub_errors] 1281 else: 1282 # ScalarMaps can't have any initialization errors. 1283 pass 1284 elif field.label == _FieldDescriptor.LABEL_REPEATED: 1285 for i in range(len(value)): 1286 element = value[i] 1287 prefix = '%s[%d].' % (name, i) 1288 sub_errors = element.FindInitializationErrors() 1289 errors += [prefix + error for error in sub_errors] 1290 else: 1291 prefix = name + '.' 1292 sub_errors = value.FindInitializationErrors() 1293 errors += [prefix + error for error in sub_errors] 1294 1295 return errors
Finds required fields which are not initialized.
Returns:
A list of strings. Each string is a path to an uninitialized field from the top-level message, e.g. "foo.bar[5].baz".
Inherited Members
- google.protobuf.message.Message
- CopyFrom
- ParseFromString
- HasExtension
- ClearExtension