def _defaultvalueconstructorforfield(field): """returns a function which returns a default value for a field. args: field: fielddescriptor object for this field. the returned function has one argument: message: message instance containing this field, or a weakref proxy of same. that function in turn returns a default value for this field. the default value may refer back to |message| via a weak reference. """ if field.label == _fielddescriptor.label_repeated: if field.has_default_value and field.default_value != []: raise valueerror('repeated field default value not empty list: %s' % ( field.default_value)) if field.cpp_type == _fielddescriptor.cpptype_message: # we can't look at _concrete_class yet since it might not have # been set. (depends on order in which we initialize the classes). message_type = field.message_type def makerepeatedmessagedefault(message): return containers.repeatedcompositefieldcontainer( message._listener_for_children, field.message_type) return makerepeatedmessagedefault else: type_checker = type_checkers.gettypechecker(field.cpp_type, field.type) def makerepeatedscalardefault(message): return containers.repeatedscalarfieldcontainer( message._listener_for_children, type_checker) return makerepeatedscalardefault if field.cpp_type == _fielddescriptor.cpptype_message: # _concrete_class may not yet be initialized. message_type = field.message_type def makesubmessagedefault(message): result = message_type._concrete_class() result._setlistener(message._listener_for_children) return result return makesubmessagedefault def makescalardefault(message): # todo(protobuf-team): this may be broken since there may not be # default_value. combine with has_default_value somehow. return field.default_value return makescalardefault
def __setitem__(self, extension_handle, value): """if extension_handle specifies a non-repeated, scalar extension field, sets the value of that field. """ _verifyextensionhandle(self._extended_message, extension_handle) if (extension_handle.label == _fielddescriptor.label_repeated or extension_handle.cpp_type == _fielddescriptor.cpptype_message): raise typeerror( 'cannot assign to extension "%s" because it is a repeated or ' 'composite type.' % extension_handle.full_name) # it's slightly wasteful to lookup the type checker each time, # but we expect this to be a vanishingly uncommon case anyway. type_checker = type_checkers.gettypechecker( extension_handle.cpp_type, extension_handle.type) type_checker.checkvalue(value) self._extended_message._fields[extension_handle] = value self._extended_message._modified()
def _addpropertiesfornonrepeatedscalarfield(field, cls): """adds a public property for a nonrepeated, scalar protocol message field. clients can use this property to get and directly set the value of the field. note that when the client sets the value of a field by using this property, all necessary "has" bits are set as a side-effect, and we also perform type-checking. args: field: a fielddescriptor for this field. cls: the class we're constructing. """ proto_field_name = field.name property_name = _propertyname(proto_field_name) type_checker = type_checkers.gettypechecker(field.cpp_type, field.type) default_value = field.default_value valid_values = set() def getter(self): # todo(protobuf-team): this may be broken since there may not be # default_value. combine with has_default_value somehow. return self._fields.get(field, default_value) getter.__module__ = none getter.__doc__ = 'getter for %s.' % proto_field_name def setter(self, new_value): type_checker.checkvalue(new_value) self._fields[field] = new_value # check _cached_byte_size_dirty inline to improve performance, since scalar # setters are called frequently. if not self._cached_byte_size_dirty: self._modified() setter.__module__ = none setter.__doc__ = 'setter for %s.' % proto_field_name # add a property to encapsulate the getter/setter. doc = 'magic attribute generated for "%s" proto field.' % proto_field_name setattr(cls, property_name, property(getter, setter, doc=doc))