From: Mike Bayer Date: Thu, 23 Dec 2010 18:34:26 +0000 (-0500) Subject: - slight simplify to state.modified_event() X-Git-Tag: rel_0_7b1~121 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=08ef0e21e06e0f2e90d42c22d2d98846ed62bdec;p=thirdparty%2Fsqlalchemy%2Fsqlalchemy.git - slight simplify to state.modified_event() --- diff --git a/lib/sqlalchemy/orm/attributes.py b/lib/sqlalchemy/orm/attributes.py index bd7e8cf706..7f722aa444 100644 --- a/lib/sqlalchemy/orm/attributes.py +++ b/lib/sqlalchemy/orm/attributes.py @@ -438,7 +438,7 @@ class ScalarAttributeImpl(AttributeImpl): if self.dispatch.on_remove: self.fire_remove_event(state, dict_, old, None) - state.modified_event(dict_, self, False, old) + state.modified_event(dict_, self, old) del dict_[self.key] def get_history(self, state, dict_, passive=PASSIVE_OFF): @@ -457,7 +457,7 @@ class ScalarAttributeImpl(AttributeImpl): if self.dispatch.on_set: value = self.fire_replace_event(state, dict_, value, old, initiator) - state.modified_event(dict_, self, False, old) + state.modified_event(dict_, self, old) dict_[self.key] = value def fire_replace_event(self, state, dict_, value, previous, initiator): @@ -610,7 +610,7 @@ class ScalarObjectAttributeImpl(ScalarAttributeImpl): for fn in self.dispatch.on_remove: fn(state, value, initiator or self) - state.modified_event(dict_, self, False, value) + state.modified_event(dict_, self, value) def fire_replace_event(self, state, dict_, value, previous, initiator): if self.trackparent: @@ -622,7 +622,7 @@ class ScalarObjectAttributeImpl(ScalarAttributeImpl): for fn in self.dispatch.on_set: value = fn(state, value, previous, initiator or self) - state.modified_event(dict_, self, False, previous) + state.modified_event(dict_, self, previous) if self.trackparent: if value is not None: @@ -701,8 +701,7 @@ class CollectionAttributeImpl(AttributeImpl): for fn in self.dispatch.on_append: value = fn(state, value, initiator or self) - state.modified_event(dict_, self, True, - NEVER_SET, passive=PASSIVE_NO_INITIALIZE) + state.modified_event(dict_, self, NEVER_SET, True) if self.trackparent and value is not None: self.sethasparent(instance_state(value), True) @@ -710,8 +709,7 @@ class CollectionAttributeImpl(AttributeImpl): return value def fire_pre_remove_event(self, state, dict_, initiator): - state.modified_event(dict_, self, True, - NEVER_SET, passive=PASSIVE_NO_INITIALIZE) + state.modified_event(dict_, self, NEVER_SET, True) def fire_remove_event(self, state, dict_, value, initiator): if self.trackparent and value is not None: @@ -720,14 +718,13 @@ class CollectionAttributeImpl(AttributeImpl): for fn in self.dispatch.on_remove: fn(state, value, initiator or self) - state.modified_event(dict_, self, True, - NEVER_SET, passive=PASSIVE_NO_INITIALIZE) + state.modified_event(dict_, self, NEVER_SET, True) def delete(self, state, dict_): if self.key not in dict_: return - state.modified_event(dict_, self, True, NEVER_SET) + state.modified_event(dict_, self, NEVER_SET, True) collection = self.get_collection(state, state.dict) collection.clear_with_event() @@ -812,8 +809,9 @@ class CollectionAttributeImpl(AttributeImpl): # implicitly with in-place operators (foo.collection |= other) return - state.modified_event(dict_, self, True, old) - + # place a copy of "old" in state.committed_state + state.modified_event(dict_, self, old, True) + old_collection = getattr(old, '_sa_adapter') dict_[self.key] = user_data @@ -821,7 +819,6 @@ class CollectionAttributeImpl(AttributeImpl): collections.bulk_replace(new_values, old_collection, new_collection) old_collection.unlink(old) - def set_committed_value(self, state, dict_, value): """Set an attribute value on the given instance and 'commit' it.""" @@ -838,7 +835,7 @@ class CollectionAttributeImpl(AttributeImpl): # pending items exist. issue a modified event, # add/remove new items. - state.modified_event(dict_, self, True, user_data) + state.modified_event(dict_, self, user_data, True) pending = state.pending.pop(self.key) added = pending.added_items diff --git a/lib/sqlalchemy/orm/dynamic.py b/lib/sqlalchemy/orm/dynamic.py index 66f7bf7393..92bd78a583 100644 --- a/lib/sqlalchemy/orm/dynamic.py +++ b/lib/sqlalchemy/orm/dynamic.py @@ -98,10 +98,8 @@ class DynamicAttributeImpl(attributes.AttributeImpl): state.committed_state[self.key] = CollectionHistory(self, state) state.modified_event(dict_, - self, - False, - attributes.NEVER_SET, - passive=attributes.PASSIVE_NO_INITIALIZE) + self, + attributes.NEVER_SET) # this is a hack to allow the _base.ComparableEntity fixture # to work diff --git a/lib/sqlalchemy/orm/state.py b/lib/sqlalchemy/orm/state.py index 52cde3a304..6ec4239a35 100644 --- a/lib/sqlalchemy/orm/state.py +++ b/lib/sqlalchemy/orm/state.py @@ -312,22 +312,19 @@ class InstanceState(object): def _is_really_none(self): return self.obj() - - def modified_event(self, dict_, attr, should_copy, previous, passive=PASSIVE_OFF): + + def modified_event(self, dict_, attr, previous, collection=False): if attr.key not in self.committed_state: - if previous is NEVER_SET: - if passive: + if collection: + if previous is NEVER_SET: if attr.key in dict_: previous = dict_[attr.key] - else: - previous = attr.get(self, dict_) - if should_copy and previous not in (None, NO_VALUE, NEVER_SET): - previous = attr.copy(previous) + if previous not in (None, NO_VALUE, NEVER_SET): + previous = attr.copy(previous) self.committed_state[attr.key] = previous - # the "or not self.modified" is defensive at # this point. The assertion below is expected # to be True: @@ -341,7 +338,7 @@ class InstanceState(object): self._strong_obj = self.obj() self.modified = True - + def commit(self, dict_, keys): """Commit attributes.