#NEWLINE# def backref_listeners( attribute , key , uselist ) : #NEWLINE# #INDENT# 'Apply listeners to synchronize a two-way relationship.' #NEWLINE# parent_token = attribute . impl . parent_token #NEWLINE# parent_impl = attribute . impl #NEWLINE# def _acceptable_key_err( child_state , initiator , child_impl ) : #NEWLINE# #INDENT# raise ValueError ( ( 'Bidirectional attribute conflict detected: Passing object %s to attribute "%s" triggers a modify event on attribute "%s" via the backref "%s".' % ( state_str ( child_state ) , initiator . parent_token , child_impl . parent_token , attribute . impl . parent_token ) ) ) #NEWLINE# #UNINDENT# def emit_backref_from_scalar_set_event( state , child , oldchild , initiator ) : #NEWLINE# #INDENT# if ( oldchild is child ) : #NEWLINE# #INDENT# return child #NEWLINE# #UNINDENT# if ( ( oldchild is not None ) and ( oldchild is not PASSIVE_NO_RESULT ) and ( oldchild is not NEVER_SET ) ) : #NEWLINE# #INDENT# ( old_state , old_dict ) = ( instance_state ( oldchild ) , instance_dict ( oldchild ) ) #NEWLINE# impl = old_state . manager [ key ] . impl #NEWLINE# if ( ( initiator . impl is not impl ) or ( initiator . op not in ( OP_REPLACE , OP_REMOVE ) ) ) : #NEWLINE# #INDENT# impl . pop ( old_state , old_dict , state . obj ( ) , parent_impl . _append_token , passive = PASSIVE_NO_FETCH ) #NEWLINE# #UNINDENT# #UNINDENT# if ( child is not None ) : #NEWLINE# #INDENT# ( child_state , child_dict ) = ( instance_state ( child ) , instance_dict ( child ) ) #NEWLINE# child_impl = child_state . manager [ key ] . impl #NEWLINE# if ( ( initiator . parent_token is not parent_token ) and ( initiator . parent_token is not child_impl . parent_token ) ) : #NEWLINE# #INDENT# _acceptable_key_err ( state , initiator , child_impl ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( ( initiator . impl is not child_impl ) or ( initiator . op not in ( OP_APPEND , OP_REPLACE ) ) ) : #NEWLINE# #INDENT# child_impl . append ( child_state , child_dict , state . obj ( ) , initiator , passive = PASSIVE_NO_FETCH ) #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# return child #NEWLINE# #UNINDENT# def emit_backref_from_collection_append_event( state , child , initiator ) : #NEWLINE# #INDENT# if ( child is None ) : #NEWLINE# #INDENT# return #NEWLINE# #UNINDENT# ( child_state , child_dict ) = ( instance_state ( child ) , instance_dict ( child ) ) #NEWLINE# child_impl = child_state . manager [ key ] . impl #NEWLINE# if ( ( initiator . parent_token is not parent_token ) and ( initiator . parent_token is not child_impl . parent_token ) ) : #NEWLINE# #INDENT# _acceptable_key_err ( state , initiator , child_impl ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( ( initiator . impl is not child_impl ) or ( initiator . op not in ( OP_APPEND , OP_REPLACE ) ) ) : #NEWLINE# #INDENT# child_impl . append ( child_state , child_dict , state . obj ( ) , initiator , passive = PASSIVE_NO_FETCH ) #NEWLINE# #UNINDENT# #UNINDENT# return child #NEWLINE# #UNINDENT# def emit_backref_from_collection_remove_event( state , child , initiator ) : #NEWLINE# #INDENT# if ( child is not None ) : #NEWLINE# #INDENT# ( child_state , child_dict ) = ( instance_state ( child ) , instance_dict ( child ) ) #NEWLINE# child_impl = child_state . manager [ key ] . impl #NEWLINE# if ( ( initiator . impl is not child_impl ) or ( initiator . op not in ( OP_REMOVE , OP_REPLACE ) ) ) : #NEWLINE# #INDENT# child_impl . pop ( child_state , child_dict , state . obj ( ) , initiator , passive = PASSIVE_NO_FETCH ) #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# if uselist : #NEWLINE# #INDENT# event . listen ( attribute , 'append' , emit_backref_from_collection_append_event , retval = True , raw = True ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# event . listen ( attribute , 'set' , emit_backref_from_scalar_set_event , retval = True , raw = True ) #NEWLINE# #UNINDENT# event . listen ( attribute , 'remove' , emit_backref_from_collection_remove_event , retval = True , raw = True )