<s> """<STR_LIT>""" <EOL> import weakref <EOL> from . . import util <EOL> from . import exc as orm_exc , interfaces <EOL> from . path_registry import PathRegistry <EOL> from . base import PASSIVE_NO_RESULT , SQL_OK , NEVER_SET , ATTR_WAS_SET , NO_VALUE , PASSIVE_NO_INITIALIZE , INIT_OK , PASSIVE_OFF <EOL> from . import base <EOL> class InstanceState ( interfaces . _InspectionAttr ) : <EOL> """<STR_LIT>""" <EOL> session_id = None <EOL> key = None <EOL> runid = None <EOL> load_options = util . EMPTY_SET <EOL> load_path = ( ) <EOL> insert_order = None <EOL> _strong_obj = None <EOL> modified = False <EOL> expired = False <EOL> deleted = False <EOL> _load_pending = False <EOL> is_instance = True <EOL> def __init__ ( self , obj , manager ) : <EOL> self . class_ = obj . __class__ <EOL> self . manager = manager <EOL> self . obj = weakref . ref ( obj , self . _cleanup ) <EOL> self . callables = { } <EOL> self . committed_state = { } <EOL> @ util . memoized_property <EOL> def attrs ( self ) : <EOL> """<STR_LIT>""" <EOL> return util . ImmutableProperties ( <EOL> dict ( <EOL> ( key , AttributeState ( self , key ) ) <EOL> for key in self . manager <EOL> ) <EOL> ) <EOL> @ property <EOL> def transient ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . key is None and not self . _attached <EOL> @ property <EOL> def pending ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . key is None and self . _attached <EOL> @ property <EOL> def persistent ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . key is not None and self . _attached <EOL> @ property <EOL> def detached ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . key is not None and not self . _attached <EOL> @ property <EOL> @ util . dependencies ( "<STR_LIT>" ) <EOL> def _attached ( self , sessionlib ) : <EOL> return self . session_id is not None and self . session_id in sessionlib . _sessions <EOL> @ property <EOL> @ util . dependencies ( "<STR_LIT>" ) <EOL> def session ( self , sessionlib ) : <EOL> """<STR_LIT>""" <EOL> return sessionlib . _state_session ( self ) <EOL> @ property <EOL> def object ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . obj ( ) <EOL> @ property <EOL> def identity ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . key is None : <EOL> return None <EOL> else : <EOL> return self . key [ <NUM_LIT:1> ] <EOL> @ property <EOL> def identity_key ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . key <EOL> @ util . memoized_property <EOL> def parents ( self ) : <EOL> return { } <EOL> @ util . memoized_property <EOL> def _pending_mutations ( self ) : <EOL> return { } <EOL> @ util . memoized_property <EOL> def mapper ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . manager . mapper <EOL> @ property <EOL> def has_identity ( self ) : <EOL> """<STR_LIT>""" <EOL> return bool ( self . key ) <EOL> def _detach ( self ) : <EOL> self . session_id = self . _strong_obj = None <EOL> def _dispose ( self ) : <EOL> self . _detach ( ) <EOL> del self . obj <EOL> def _cleanup ( self , ref ) : <EOL> instance_dict = self . _instance_dict ( ) <EOL> if instance_dict : <EOL> instance_dict . discard ( self ) <EOL> self . callables = { } <EOL> self . session_id = self . _strong_obj = None <EOL> del self . obj <EOL> def obj ( self ) : <EOL> return None <EOL> @ property <EOL> def dict ( self ) : <EOL> """<STR_LIT>""" <EOL> o = self . obj ( ) <EOL> if o is not None : <EOL> return base . instance_dict ( o ) <EOL> else : <EOL> return { } <EOL> def _initialize_instance ( * mixed , ** kwargs ) : <EOL> self , instance , args = mixed [ <NUM_LIT:0> ] , mixed [ <NUM_LIT:1> ] , mixed [ <NUM_LIT:2> : ] <EOL> manager = self . manager <EOL> manager . dispatch . init ( self , args , kwargs ) <EOL> try : <EOL> return manager . original_init ( * mixed [ <NUM_LIT:1> : ] , ** kwargs ) <EOL> except : <EOL> manager . dispatch . init_failure ( self , args , kwargs ) <EOL> raise <EOL> def get_history ( self , key , passive ) : <EOL> return self . manager [ key ] . impl . get_history ( self , self . dict , passive ) <EOL> def get_impl ( self , key ) : <EOL> return self . manager [ key ] . impl <EOL> def _get_pending_mutation ( self , key ) : <EOL> if key not in self . _pending_mutations : <EOL> self . _pending_mutations [ key ] = PendingCollection ( ) <EOL> return self . _pending_mutations [ key ] <EOL> def __getstate__ ( self ) : <EOL> state_dict = { '<STR_LIT>' : self . obj ( ) } <EOL> state_dict . update ( <EOL> ( k , self . __dict__ [ k ] ) for k in ( <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:key>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> ) if k in self . __dict__ <EOL> ) <EOL> if self . load_path : <EOL> state_dict [ '<STR_LIT>' ] = self . load_path . serialize ( ) <EOL> state_dict [ '<STR_LIT>' ] = self . manager . _serialize ( self , state_dict ) <EOL> return state_dict <EOL> def __setstate__ ( self , state_dict ) : <EOL> inst = state_dict [ '<STR_LIT>' ] <EOL> if inst is not None : <EOL> self . obj = weakref . ref ( inst , self . _cleanup ) <EOL> self . class_ = inst . __class__ <EOL> else : <EOL> self . obj = None <EOL> self . class_ = state_dict [ '<STR_LIT>' ] <EOL> self . committed_state = state_dict . get ( '<STR_LIT>' , { } ) <EOL> self . _pending_mutations = state_dict . get ( '<STR_LIT>' , { } ) <EOL> self . parents = state_dict . get ( '<STR_LIT>' , { } ) <EOL> self . modified = state_dict . get ( '<STR_LIT>' , False ) <EOL> self . expired = state_dict . get ( '<STR_LIT>' , False ) <EOL> self . callables = state_dict . get ( '<STR_LIT>' , { } ) <EOL> self . __dict__ . update ( [ <EOL> ( k , state_dict [ k ] ) for k in ( <EOL> '<STR_LIT:key>' , '<STR_LIT>' , <EOL> ) if k in state_dict <EOL> ] ) <EOL> if '<STR_LIT>' in state_dict : <EOL> self . load_path = PathRegistry . deserialize ( state_dict [ '<STR_LIT>' ] ) <EOL> state_dict [ '<STR_LIT>' ] ( self , inst , state_dict ) <EOL> def _initialize ( self , key ) : <EOL> """<STR_LIT>""" <EOL> self . manager . get_impl ( key ) . initialize ( self , self . dict ) <EOL> def _reset ( self , dict_ , key ) : <EOL> """<STR_LIT>""" <EOL> old = dict_ . pop ( key , None ) <EOL> if old is not None and self . manager [ key ] . impl . collection : <EOL> self . manager [ key ] . impl . _invalidate_collection ( old ) <EOL> self . callables . pop ( key , None ) <EOL> def _expire_attribute_pre_commit ( self , dict_ , key ) : <EOL> """<STR_LIT>""" <EOL> dict_ . pop ( key , None ) <EOL> self . callables [ key ] = self <EOL> @ classmethod <EOL> def _row_processor ( cls , manager , fn , key ) : <EOL> impl = manager [ key ] . impl <EOL> if impl . collection : <EOL> def _set_callable ( state , dict_ , row ) : <EOL> old = dict_ . pop ( key , None ) <EOL> if old is not None : <EOL> impl . _invalidate_collection ( old ) <EOL> state . callables [ key ] = fn <EOL> else : <EOL> def _set_callable ( state , dict_ , row ) : <EOL> state . callables [ key ] = fn <EOL> return _set_callable <EOL> def _expire ( self , dict_ , modified_set ) : <EOL> self . expired = True <EOL> if self . modified : <EOL> modified_set . discard ( self ) <EOL> self . modified = False <EOL> self . _strong_obj = None <EOL> self . committed_state . clear ( ) <EOL> InstanceState . _pending_mutations . _reset ( self ) <EOL> InstanceState . parents . _reset ( self ) <EOL> for key in self . manager : <EOL> impl = self . manager [ key ] . impl <EOL> if impl . accepts_scalar_loader and ( impl . expire_missing or key in dict_ ) : <EOL> self . callables [ key ] = self <EOL> old = dict_ . pop ( key , None ) <EOL> if impl . collection and old is not None : <EOL> impl . _invalidate_collection ( old ) <EOL> self . manager . dispatch . expire ( self , None ) <EOL> def _expire_attributes ( self , dict_ , attribute_names ) : <EOL> pending = self . __dict__ . get ( '<STR_LIT>' , None ) <EOL> for key in attribute_names : <EOL> impl = self . manager [ key ] . impl <EOL> if impl . accepts_scalar_loader : <EOL> self . callables [ key ] = self <EOL> old = dict_ . pop ( key , None ) <EOL> if impl . collection and old is not None : <EOL> impl . _invalidate_collection ( old ) <EOL> self . committed_state . pop ( key , None ) <EOL> if pending : <EOL> pending . pop ( key , None ) <EOL> self . manager . dispatch . expire ( self , attribute_names ) <EOL> def __call__ ( self , state , passive ) : <EOL> """<STR_LIT>""" <EOL> if not passive & SQL_OK : <EOL> return PASSIVE_NO_RESULT <EOL> toload = self . expired_attributes . intersection ( self . unmodified ) <EOL> self . manager . deferred_scalar_loader ( self , toload ) <EOL> for k in toload . intersection ( self . callables ) : <EOL> del self . callables [ k ] <EOL> return ATTR_WAS_SET <EOL> @ property <EOL> def unmodified ( self ) : <EOL> """<STR_LIT>""" <EOL> return set ( self . manager ) . difference ( self . committed_state ) <EOL> def unmodified_intersection ( self , keys ) : <EOL> """<STR_LIT>""" <EOL> return set ( keys ) . intersection ( self . manager ) . difference ( self . committed_state ) <EOL> @ property <EOL> def unloaded ( self ) : <EOL> """<STR_LIT>""" <EOL> return set ( self . manager ) . difference ( self . committed_state ) . difference ( self . dict ) <EOL> @ property <EOL> def _unloaded_non_object ( self ) : <EOL> return self . unloaded . intersection ( <EOL> attr for attr in self . manager <EOL> if self . manager [ attr ] . impl . accepts_scalar_loader <EOL> ) <EOL> @ property <EOL> def expired_attributes ( self ) : <EOL> """<STR_LIT>""" <EOL> return set ( [ k for k , v in self . callables . items ( ) if v is self ] ) <EOL> def _instance_dict ( self ) : <EOL> return None <EOL> def _modified_event ( <EOL> self , dict_ , attr , previous , collection = False , force = False ) : <EOL> if not attr . send_modified_events : <EOL> return <EOL> if attr . key not in self . committed_state or force : <EOL> if collection : <EOL> if previous is NEVER_SET : <EOL> if attr . key in dict_ : <EOL> previous = dict_ [ attr . key ] <EOL> if previous not in ( None , NO_VALUE , NEVER_SET ) : <EOL> previous = attr . copy ( previous ) <EOL> self . committed_state [ attr . key ] = previous <EOL> if ( self . session_id and self . _strong_obj is None ) or not self . modified : <EOL> instance_dict = self . _instance_dict ( ) <EOL> if instance_dict : <EOL> instance_dict . _modified . add ( self ) <EOL> inst = self . obj ( ) <EOL> if self . session_id : <EOL> self . _strong_obj = inst <EOL> if inst is None : <EOL> raise orm_exc . ObjectDereferencedError ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> % ( <EOL> self . manager [ attr . key ] , <EOL> base . state_class_str ( self ) <EOL> ) ) <EOL> self . modified = True <EOL> def _commit ( self , dict_ , keys ) : <EOL> """<STR_LIT>""" <EOL> for key in keys : <EOL> self . committed_state . pop ( key , None ) <EOL> self . expired = False <EOL> for key in set ( self . callables ) . intersection ( keys ) . intersection ( dict_ ) : <EOL> del self . callables [ key ] <EOL> def _commit_all ( self , dict_ , instance_dict = None ) : <EOL> """<STR_LIT>""" <EOL> self . _commit_all_states ( [ ( self , dict_ ) ] , instance_dict ) <EOL> @ classmethod <EOL> def _commit_all_states ( self , iter , instance_dict = None ) : <EOL> """<STR_LIT>""" <EOL> for state , dict_ in iter : <EOL> state . committed_state . clear ( ) <EOL> InstanceState . _pending_mutations . _reset ( state ) <EOL> callables = state . callables <EOL> for key in list ( callables ) : <EOL> if key in dict_ and callables [ key ] is state : <EOL> del callables [ key ] <EOL> if instance_dict and state . modified : <EOL> instance_dict . _modified . discard ( state ) <EOL> state . modified = state . expired = False <EOL> state . _strong_obj = None <EOL> class AttributeState ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , state , key ) : <EOL> self . state = state <EOL> self . key = key <EOL> @ property <EOL> def loaded_value ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . state . dict . get ( self . key , NO_VALUE ) <EOL> @ property <EOL> def value ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . state . manager [ self . key ] . __get__ ( <EOL> self . state . obj ( ) , self . state . class_ ) <EOL> @ property <EOL> def history ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . state . get_history ( self . key , <EOL> PASSIVE_NO_INITIALIZE ) <EOL> def load_history ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . state . get_history ( self . key , <EOL> PASSIVE_OFF ^ INIT_OK ) <EOL> class PendingCollection ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self ) : <EOL> self . deleted_items = util . IdentitySet ( ) <EOL> self . added_items = util . OrderedIdentitySet ( ) <EOL> def append ( self , value ) : <EOL> if value in self . deleted_items : <EOL> self . deleted_items . remove ( value ) <EOL> else : <EOL> self . added_items . add ( value ) <EOL> def remove ( self , value ) : <EOL> if value in self . added_items : <EOL> self . added_items . remove ( value ) <EOL> else : <EOL> self . deleted_items . add ( value ) </s>
