[Sqlalchemy-commits] sqlalchemy: - replace all usage of True and False for passive wi...
Brought to you by:
zzzeek
From: <co...@sq...> - 2011-02-11 20:38:38
|
details: http://hg.sqlalchemy.org/sqlalchemy/sqlalchemy/rev/289480683497 changeset: 7351:289480683497 user: zzzeek date: Fri Feb 11 15:37:44 2011 -0500 description: - replace all usage of True and False for passive with PASSIVE_NO_INITIALIZE, PASSIVE_OFF, now expresed as non-boolean symbols - make "passive" available positionally on all get_history() methods, call it like that diffstat: lib/sqlalchemy/orm/attributes.py | 23 ++++------- lib/sqlalchemy/orm/dependency.py | 62 +++++++++++++++++++++------------ lib/sqlalchemy/orm/descriptor_props.py | 7 ++- lib/sqlalchemy/orm/dynamic.py | 35 ++++++++++-------- lib/sqlalchemy/orm/mapper.py | 43 ++++++++++++++--------- lib/sqlalchemy/orm/properties.py | 10 +++-- lib/sqlalchemy/orm/session.py | 2 +- lib/sqlalchemy/orm/state.py | 4 +- lib/sqlalchemy/orm/strategies.py | 4 +- lib/sqlalchemy/orm/sync.py | 8 ++- lib/sqlalchemy/orm/unitofwork.py | 11 +++-- lib/sqlalchemy/util/langhelpers.py | 1 + test/orm/test_attributes.py | 4 +- 13 files changed, 124 insertions(+), 90 deletions(-) diffs (truncated from 678 to 300 lines): diff -r eb9ed399fc10 -r 289480683497 lib/sqlalchemy/orm/attributes.py --- a/lib/sqlalchemy/orm/attributes.py Fri Feb 11 14:41:29 2011 -0500 +++ b/lib/sqlalchemy/orm/attributes.py Fri Feb 11 15:37:44 2011 -0500 @@ -28,14 +28,11 @@ NO_VALUE = util.symbol('NO_VALUE') NEVER_SET = util.symbol('NEVER_SET') -# "passive" get settings -# TODO: the True/False values need to be factored out -PASSIVE_NO_INITIALIZE = True #util.symbol('PASSIVE_NO_INITIALIZE') +PASSIVE_NO_INITIALIZE = util.symbol('PASSIVE_NO_INITIALIZE') """Symbol indicating that loader callables should not be fired off, and a non-initialized attribute should remain that way.""" -# this is used by backrefs. PASSIVE_NO_FETCH = util.symbol('PASSIVE_NO_FETCH') """Symbol indicating that loader callables should not emit SQL. Non-initialized attributes should be initialized to an empty value.""" @@ -57,7 +54,7 @@ Loads of "previous" values during change events use this flag. """ -PASSIVE_OFF = False #util.symbol('PASSIVE_OFF') +PASSIVE_OFF = util.symbol('PASSIVE_OFF') """Symbol indicating that loader callables should be executed.""" @@ -88,9 +85,9 @@ def _supports_population(self): return self.impl.supports_population - def get_history(self, instance, **kwargs): + def get_history(self, instance, passive=PASSIVE_OFF): return self.impl.get_history(instance_state(instance), - instance_dict(instance), **kwargs) + instance_dict(instance), passive) def __selectable__(self): # TODO: conditionally attach this method based on clause_element ? @@ -386,7 +383,6 @@ passive is False, the callable will be executed and the resulting value will be set as the new value for this attribute. """ - if self.key in dict_: return dict_[self.key] else: @@ -1101,7 +1097,7 @@ HISTORY_BLANK = History(None, None, None) -def get_history(obj, key, **kwargs): +def get_history(obj, key, passive=PASSIVE_OFF): """Return a :class:`.History` record for the given object and attribute key. @@ -1110,17 +1106,16 @@ :param key: string attribute name. - :param kwargs: Optional keyword arguments currently - include the ``passive`` flag, which indicates if the attribute should be + :param passive: indicates if the attribute should be loaded from the database if not already present (:attr:`PASSIVE_NO_FETCH`), and if the attribute should be not initialized to a blank value otherwise (:attr:`PASSIVE_NO_INITIALIZE`). Default is :attr:`PASSIVE_OFF`. """ - return get_state_history(instance_state(obj), key, **kwargs) + return get_state_history(instance_state(obj), key, passive) -def get_state_history(state, key, **kwargs): - return state.get_history(key, **kwargs) +def get_state_history(state, key, passive=PASSIVE_OFF): + return state.get_history(key, passive) def has_parent(cls, obj, key, optimistic=False): diff -r eb9ed399fc10 -r 289480683497 lib/sqlalchemy/orm/dependency.py --- a/lib/sqlalchemy/orm/dependency.py Fri Feb 11 14:41:29 2011 -0500 +++ b/lib/sqlalchemy/orm/dependency.py Fri Feb 11 15:37:44 2011 -0500 @@ -26,9 +26,15 @@ self.passive_deletes = prop.passive_deletes self.passive_updates = prop.passive_updates self.enable_typechecks = prop.enable_typechecks - self._passive_delete_flag = self.passive_deletes and \ - attributes.PASSIVE_NO_INITIALIZE or \ - attributes.PASSIVE_OFF + if self.passive_deletes: + self._passive_delete_flag = attributes.PASSIVE_NO_INITIALIZE + else: + self._passive_delete_flag = attributes.PASSIVE_OFF + if self.passive_updates: + self._passive_update_flag = attributes.PASSIVE_NO_INITIALIZE + else: + self._passive_update_flag= attributes.PASSIVE_OFF + self.key = prop.key if not self.prop.synchronize_pairs: raise sa_exc.ArgumentError( @@ -233,7 +239,7 @@ history = uowcommit.get_attribute_history( s, self.key, - passive=passive) + passive) if history and not history.empty(): return True else: @@ -398,7 +404,7 @@ history = uowcommit.get_attribute_history( state, self.key, - passive=self._passive_delete_flag) + self._passive_delete_flag) if history: for child in history.deleted: if child is not None and self.hasparent(child) is False: @@ -421,11 +427,15 @@ for state in states: pks_changed = self._pks_changed(uowcommit, state) + if not pks_changed or self.passive_updates: + passive = attributes.PASSIVE_NO_INITIALIZE + else: + passive = attributes.PASSIVE_OFF + history = uowcommit.get_attribute_history( state, self.key, - passive=not pks_changed - or self.passive_updates) + passive) if history: for child in history.added: if child is not None: @@ -474,7 +484,7 @@ history = uowcommit.get_attribute_history( state, self.key, - passive=self._passive_delete_flag) + self._passive_delete_flag) if history: for child in history.deleted: if child is not None and \ @@ -508,9 +518,10 @@ def process_saves(self, uowcommit, states): for state in states: - history = uowcommit.get_attribute_history(state, - self.key, - passive=True) + history = uowcommit.get_attribute_history( + state, + self.key, + attributes.PASSIVE_NO_INITIALIZE) if history: for child in history.added: self._synchronize(state, child, None, @@ -654,7 +665,7 @@ history = uowcommit.get_attribute_history( state, self.key, - passive=self._passive_delete_flag) + self._passive_delete_flag) if history: if self.cascade.delete_orphan: todelete = history.sum() @@ -677,7 +688,7 @@ history = uowcommit.get_attribute_history( state, self.key, - passive=self._passive_delete_flag) + self._passive_delete_flag) if history: ret = True for child in history.deleted: @@ -705,15 +716,16 @@ history = uowcommit.get_attribute_history( state, self.key, - passive=self._passive_delete_flag) + self._passive_delete_flag) if history: self._post_update(state, uowcommit, history.sum()) def process_saves(self, uowcommit, states): for state in states: - history = uowcommit.get_attribute_history(state, - self.key, - passive=True) + history = uowcommit.get_attribute_history( + state, + self.key, + attributes.PASSIVE_NO_INITIALIZE) if history: for child in history.added: self._synchronize(state, child, None, False, @@ -834,7 +846,7 @@ continue dict_ = state.dict related = state.get_impl(self.key).get(state, dict_, - passive=self.passive_updates) + passive=self._passive_update_flag) if related is not attributes.PASSIVE_NO_RESULT and \ related is not None: related_state = attributes.instance_state(dict_[self.key]) @@ -914,7 +926,7 @@ history = uowcommit.get_attribute_history( state, self.key, - passive=self._passive_delete_flag) + self._passive_delete_flag) def presort_saves(self, uowcommit, states): if not self.passive_updates: @@ -926,7 +938,7 @@ history = uowcommit.get_attribute_history( state, self.key, - False) + attributes.PASSIVE_OFF) if not self.cascade.delete_orphan: return @@ -937,7 +949,7 @@ history = uowcommit.get_attribute_history( state, self.key, - passive=True) + attributes.PASSIVE_NO_INITIALIZE) if history: for child in history.deleted: if self.hasparent(child) is False: @@ -962,7 +974,7 @@ history = uowcommit.get_attribute_history( state, self.key, - passive=self._passive_delete_flag) + self._passive_delete_flag) if history: for child in history.non_added(): if child is None or \ @@ -997,8 +1009,12 @@ for state in states: need_cascade_pks = not self.passive_updates and \ self._pks_changed(uowcommit, state) + if need_cascade_pks: + passive = attributes.PASSIVE_OFF + else: + passive = attributes.PASSIVE_NO_INITIALIZE history = uowcommit.get_attribute_history(state, self.key, - passive=not need_cascade_pks) + passive) if history: for child in history.added: if child is None or \ diff -r eb9ed399fc10 -r 289480683497 lib/sqlalchemy/orm/descriptor_props.py --- a/lib/sqlalchemy/orm/descriptor_props.py Fri Feb 11 14:41:29 2011 -0500 +++ b/lib/sqlalchemy/orm/descriptor_props.py Fri Feb 11 15:37:44 2011 -0500 @@ -35,8 +35,9 @@ self.key = key if hasattr(prop, 'get_history'): - def get_history(self, state, dict_, **kw): - return prop.get_history(state, dict_, **kw) + def get_history(self, state, dict_, + passive=attributes.PASSIVE_OFF): + return prop.get_history(state, dict_, passive) if self.descriptor is None: desc = getattr(mapper.class_, self.key, None) @@ -222,7 +223,7 @@ prop.key for prop in self.props ] - def get_history(self, state, dict_, **kw): + def get_history(self, state, dict_, passive=attributes.PASSIVE_OFF): """Provided for userland code that uses attributes.get_history().""" added = [] diff -r eb9ed399fc10 -r 289480683497 lib/sqlalchemy/orm/dynamic.py --- a/lib/sqlalchemy/orm/dynamic.py Fri Feb 11 14:41:29 2011 -0500 +++ b/lib/sqlalchemy/orm/dynamic.py Fri Feb 11 15:37:44 2011 -0500 @@ -56,20 +56,20 @@ else: self.query_class = mixin_user_query(query_class) - def get(self, state, dict_, passive=False): - if passive: + def get(self, state, dict_, passive=attributes.PASSIVE_OFF): + if passive is not attributes.PASSIVE_OFF: return self._get_collection_history(state, - passive=True).added_items + attributes.PASSIVE_NO_INITIALIZE).added_items else: return self.query_class(self, state) - def get_collection(self, state, dict_, user_data=None, passive=True): |