From: Mike Bayer Date: Mon, 22 Sep 2008 22:15:24 +0000 (+0000) Subject: random cleanup X-Git-Tag: rel_0_5rc2~33 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=1cc094a0044a1b5cfc8d031f19c43c9dedb06d2b;p=thirdparty%2Fsqlalchemy%2Fsqlalchemy.git random cleanup --- diff --git a/lib/sqlalchemy/orm/mapper.py b/lib/sqlalchemy/orm/mapper.py index 63521280d3..9c8fcf97eb 100644 --- a/lib/sqlalchemy/orm/mapper.py +++ b/lib/sqlalchemy/orm/mapper.py @@ -1041,8 +1041,8 @@ class Mapper(object): ordering among a polymorphic chain of instances. Therefore _save_obj is typically called only on a *base mapper*, or a mapper which does not inherit from any other mapper. + """ - if self._should_log_debug: self.__log_debug("_save_obj() start, " + (single and "non-batched" or "batched")) @@ -1085,9 +1085,6 @@ class Mapper(object): self.__log_debug("detected row switch for identity %s. will update %s, remove %s from transaction" % (instance_key, state_str(state), state_str(existing))) uowtransaction.set_row_switch(existing) - inserted_objects = set() - updated_objects = set() - table_to_mapper = {} for mapper in self.base_mapper.polymorphic_iterator(): for t in mapper.tables: @@ -1107,6 +1104,7 @@ class Mapper(object): self.__log_debug("_save_obj() table '%s' instance %s identity %s" % (table.name, state_str(state), str(instance_key))) isinsert = not instance_key in uowtransaction.session.identity_map and not postupdate and not has_identity + params = {} value_params = {} hasdata = False @@ -1185,13 +1183,10 @@ class Mapper(object): statement = table.update(clause) rows = 0 - for rec in update: - (state, params, mapper, connection, value_params) = rec + for state, params, mapper, connection, value_params in update: c = connection.execute(statement.values(value_params), params) mapper._postfetch(uowtransaction, connection, table, state, c, c.last_updated_params(), value_params) - # testlib.pragma exempt:__hash__ - updated_objects.add((state, connection)) rows += c.rowcount if c.supports_sane_rowcount() and rows != len(update): @@ -1199,8 +1194,7 @@ class Mapper(object): if insert: statement = table.insert() - for rec in insert: - (state, params, mapper, connection, value_params) = rec + for state, params, mapper, connection, value_params in insert: c = connection.execute(statement.values(value_params), params) primary_key = c.last_inserted_ids() @@ -1220,9 +1214,6 @@ class Mapper(object): if m.__inherits_equated_pairs: sync.populate(state, m, state, m, m.__inherits_equated_pairs) - # testlib.pragma exempt:__hash__ - inserted_objects.add((state, connection)) - if not postupdate: for state, mapper, connection, has_identity in tups: diff --git a/lib/sqlalchemy/orm/properties.py b/lib/sqlalchemy/orm/properties.py index 92adc19cde..45360d84a2 100644 --- a/lib/sqlalchemy/orm/properties.py +++ b/lib/sqlalchemy/orm/properties.py @@ -582,10 +582,10 @@ class PropertyLoader(StrategizedProperty): # if join conditions were not specified, figure them out based on foreign keys def _search_for_join(mapper, table): - """find a join between the given mapper's mapped table and the given table. - will try the mapper's local table first for more specificity, then if not - found will try the more general mapped table, which in the case of inheritance - is a join.""" + # find a join between the given mapper's mapped table and the given table. + # will try the mapper's local table first for more specificity, then if not + # found will try the more general mapped table, which in the case of inheritance + # is a join. try: return sql.join(mapper.local_table, table) except sa_exc.ArgumentError, e: @@ -601,8 +601,10 @@ class PropertyLoader(StrategizedProperty): if self.primaryjoin is None: self.primaryjoin = _search_for_join(self.parent, self.target).onclause except sa_exc.ArgumentError, e: - raise sa_exc.ArgumentError("Could not determine join condition between parent/child tables on relation %s. " - "Specify a 'primaryjoin' expression. If this is a many-to-many relation, 'secondaryjoin' is needed as well." % (self)) + raise sa_exc.ArgumentError("Could not determine join condition between " + "parent/child tables on relation %s. " + "Specify a 'primaryjoin' expression. If this is a " + "many-to-many relation, 'secondaryjoin' is needed as well." % (self)) def _col_is_part_of_mappings(self, column): if self.secondary is None: @@ -632,16 +634,22 @@ class PropertyLoader(StrategizedProperty): if not eq_pairs: if not self.viewonly and criterion_as_pairs(self.primaryjoin, consider_as_foreign_keys=self._foreign_keys, any_operator=True): - raise sa_exc.ArgumentError("Could not locate any equated, locally mapped column pairs for primaryjoin condition '%s' on relation %s. " - "For more relaxed rules on join conditions, the relation may be marked as viewonly=True." % (self.primaryjoin, self) + raise sa_exc.ArgumentError("Could not locate any equated, locally " + "mapped column pairs for primaryjoin condition '%s' on relation %s. " + "For more relaxed rules on join conditions, the relation may be " + "marked as viewonly=True." % (self.primaryjoin, self) ) else: if self._foreign_keys: - raise sa_exc.ArgumentError("Could not determine relation direction for primaryjoin condition '%s', on relation %s. " - "Are the columns in 'foreign_keys' present within the given join condition ?" % (self.primaryjoin, self)) + raise sa_exc.ArgumentError("Could not determine relation direction for " + "primaryjoin condition '%s', on relation %s. " + "Are the columns in 'foreign_keys' present within the given " + "join condition ?" % (self.primaryjoin, self)) else: - raise sa_exc.ArgumentError("Could not determine relation direction for primaryjoin condition '%s', on relation %s. " - "Specify the 'foreign_keys' argument to indicate which columns on the relation are foreign." % (self.primaryjoin, self)) + raise sa_exc.ArgumentError("Could not determine relation direction for " + "primaryjoin condition '%s', on relation %s. " + "Specify the 'foreign_keys' argument to indicate which columns " + "on the relation are foreign." % (self.primaryjoin, self)) self.synchronize_pairs = eq_pairs @@ -651,12 +659,16 @@ class PropertyLoader(StrategizedProperty): if not sq_pairs: if not self.viewonly and criterion_as_pairs(self.secondaryjoin, consider_as_foreign_keys=self._foreign_keys, any_operator=True): - raise sa_exc.ArgumentError("Could not locate any equated, locally mapped column pairs for secondaryjoin condition '%s' on relation %s. " - "For more relaxed rules on join conditions, the relation may be marked as viewonly=True." % (self.secondaryjoin, self) + raise sa_exc.ArgumentError("Could not locate any equated, locally mapped " + "column pairs for secondaryjoin condition '%s' on relation %s. " + "For more relaxed rules on join conditions, the " + "relation may be marked as viewonly=True." % (self.secondaryjoin, self) ) else: - raise sa_exc.ArgumentError("Could not determine relation direction for secondaryjoin condition '%s', on relation %s. " - "Specify the foreign_keys argument to indicate which columns on the relation are foreign." % (self.secondaryjoin, self)) + raise sa_exc.ArgumentError("Could not determine relation direction " + "for secondaryjoin condition '%s', on relation %s. " + "Specify the foreign_keys argument to indicate which " + "columns on the relation are foreign." % (self.secondaryjoin, self)) self.secondary_synchronize_pairs = sq_pairs else: @@ -738,10 +750,14 @@ class PropertyLoader(StrategizedProperty): for l, r in self.local_remote_pairs: if self.direction is ONETOMANY and not self._col_is_part_of_mappings(l): - raise sa_exc.ArgumentError("Local column '%s' is not part of mapping %s. Specify remote_side argument to indicate which column lazy join condition should compare against." % (l, self.parent)) + raise sa_exc.ArgumentError("Local column '%s' is not part of mapping %s. " + "Specify remote_side argument to indicate which column " + "lazy join condition should compare against." % (l, self.parent)) elif self.direction is MANYTOONE and not self._col_is_part_of_mappings(r): - raise sa_exc.ArgumentError("Remote column '%s' is not part of mapping %s. Specify remote_side argument to indicate which column lazy join condition should bind." % (r, self.mapper)) + raise sa_exc.ArgumentError("Remote column '%s' is not part of mapping %s. " + "Specify remote_side argument to indicate which column lazy " + "join condition should bind." % (r, self.mapper)) self.local_side, self.remote_side = [util.OrderedSet(x) for x in zip(*list(self.local_remote_pairs))] @@ -769,7 +785,10 @@ class PropertyLoader(StrategizedProperty): if self.backref is not None: self.backref.compile(self) elif not mapper.class_mapper(self.parent.class_, compile=False)._get_property(self.key, raiseerr=False): - raise sa_exc.ArgumentError("Attempting to assign a new relation '%s' to a non-primary mapper on class '%s'. New relations can only be added to the primary mapper, i.e. the very first mapper created for class '%s' " % (self.key, self.parent.class_.__name__, self.parent.class_.__name__)) + raise sa_exc.ArgumentError("Attempting to assign a new relation '%s' to " + "a non-primary mapper on class '%s'. New relations can only be " + "added to the primary mapper, i.e. the very first " + "mapper created for class '%s' " % (self.key, self.parent.class_.__name__, self.parent.class_.__name__)) super(PropertyLoader, self).do_init() @@ -851,7 +870,9 @@ class PropertyLoader(StrategizedProperty): else: target_adapter = None - self.__join_cache[key] = ret = (primaryjoin, secondaryjoin, (source_selectable or self.parent.local_table), (dest_selectable or self.mapper.local_table), secondary, target_adapter) + self.__join_cache[key] = ret = (primaryjoin, secondaryjoin, + (source_selectable or self.parent.local_table), + (dest_selectable or self.mapper.local_table), secondary, target_adapter) return ret def _get_join(self, parent, primary=True, secondary=True, polymorphic_parent=True): @@ -868,7 +889,6 @@ class PropertyLoader(StrategizedProperty): else: raise AssertionError("illegal condition") - def register_dependencies(self, uowcommit): if not self.viewonly: self._dependency_processor.register_dependencies(uowcommit) @@ -920,7 +940,8 @@ class BackRef(object): mapper._get_property(self.key)._reverse_property = prop else: - raise sa_exc.ArgumentError("Error creating backref '%s' on relation '%s': property of that name exists on mapper '%s'" % (self.key, prop, mapper)) + raise sa_exc.ArgumentError("Error creating backref '%s' on relation '%s': " + "property of that name exists on mapper '%s'" % (self.key, prop, mapper)) mapper.ColumnProperty = ColumnProperty mapper.SynonymProperty = SynonymProperty diff --git a/lib/sqlalchemy/orm/unitofwork.py b/lib/sqlalchemy/orm/unitofwork.py index b12e146f9f..8412887a45 100644 --- a/lib/sqlalchemy/orm/unitofwork.py +++ b/lib/sqlalchemy/orm/unitofwork.py @@ -70,10 +70,12 @@ class UOWEventHandler(interfaces.AttributeExtension): return newvalue def register_attribute(class_, key, *args, **kwargs): - """overrides attributes.register_attribute() to add UOW event handlers - to new InstrumentedAttributes. + """Register an attribute with the attributes module. + + Overrides attributes.register_attribute() to add + unitofwork-specific event handlers. + """ - useobject = kwargs.get('useobject', False) if useobject: # for object-holding attributes, instrument UOWEventHandler @@ -159,6 +161,7 @@ class UOWTransaction(object): this indicates that an INSERT statement elsewhere corresponds to this DELETE; the INSERT is converted to an UPDATE and the DELETE does not occur. + """ mapper = _state_mapper(state) task = self.get_task_by_mapper(mapper) @@ -177,6 +180,7 @@ class UOWTransaction(object): Will create a new UOWTask, including a UOWTask corresponding to the "base" inherited mapper, if needed, unless the dontcreate flag is True. + """ try: return self.tasks[mapper] @@ -205,8 +209,8 @@ class UOWTransaction(object): Called by ``mapper.PropertyLoader`` to register the objects handled by one mapper being dependent on the objects handled by another. - """ + """ # correct for primary mapper # also convert to the "base mapper", the parentmost task at the top of an inheritance chain # dependency sorting is done via non-inheriting mappers only, dependencies between mappers @@ -221,7 +225,6 @@ class UOWTransaction(object): the two given mappers. """ - # correct for primary mapper mapper = mapper.primary_mapper() mapperfrom = mapperfrom.primary_mapper() @@ -734,20 +737,18 @@ class UOWExecutor(object): def execute_save_steps(self, trans, task): self.save_objects(trans, task) self.execute_cyclical_dependencies(trans, task, False) - self.execute_dependencies(trans, task, False) - self.execute_dependencies(trans, task, True) + self.execute_dependencies(trans, task) def execute_delete_steps(self, trans, task): self.execute_cyclical_dependencies(trans, task, True) self.delete_objects(trans, task) - def execute_dependencies(self, trans, task, isdelete=None): - if isdelete is not True: - for dep in task.polymorphic_dependencies: - self.execute_dependency(trans, dep, False) - if isdelete is not False: - for dep in reversed(list(task.polymorphic_dependencies)): - self.execute_dependency(trans, dep, True) + def execute_dependencies(self, trans, task): + polymorphic_dependencies = list(task.polymorphic_dependencies) + for dep in polymorphic_dependencies: + self.execute_dependency(trans, dep, False) + for dep in reversed(polymorphic_dependencies): + self.execute_dependency(trans, dep, True) def execute_cyclical_dependencies(self, trans, task, isdelete): for dep in task.polymorphic_cyclical_dependencies: