#NEWLINE# @ classmethod #NEWLINE# def serialize_safe( cls , val , protocol_version ) : #NEWLINE# #INDENT# proto_version = max ( 3 , protocol_version ) #NEWLINE# buf = io . BytesIO ( ) #NEWLINE# for ( i , ( fieldname , subtype ) ) in enumerate ( zip ( cls . fieldnames , cls . subtypes ) ) : #NEWLINE# #INDENT# try : #NEWLINE# #INDENT# item = val [ i ] #NEWLINE# #UNINDENT# except TypeError : #NEWLINE# #INDENT# item = getattr ( val , fieldname ) #NEWLINE# #UNINDENT# if ( item is not None ) : #NEWLINE# #INDENT# packed_item = subtype . to_binary ( item , proto_version ) #NEWLINE# buf . write ( int32_pack ( len ( packed_item ) ) ) #NEWLINE# buf . write ( packed_item ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# buf . write ( int32_pack ( ( - 1 ) ) ) #NEWLINE# #UNINDENT# #UNINDENT# return buf . getvalue ( )