when new mappers are created, they will be assigned to their classes as their primary mapper."""
for mapper in mapper_registry.values():
attribute_manager.reset_class_managed(mapper.class_)
+ mapper.class_key.dispose()
+ if hasattr(mapper.class_, 'c'):
+ del mapper.class_.c
mapper_registry.clear()
- mapperlib.ClassKey.instances.clear()
def clear_mapper(m):
"""remove the given mapper from the storage of mappers.
when a new mapper is created for the previous mapper's class, it will be used as that classes'
new primary mapper."""
del mapper_registry[m.class_key]
-
+ attribute_manager.reset_class_managed(m.class_)
+ if hasattr(m.class_, 'c'):
+ del m.class_.c
+ m.class_key.dispose()
+
def extension(ext):
"""return a MapperOption that will insert the given MapperExtension to the
beginning of the list of extensions that will be called in the context of the Query.
--- /dev/null
+from sqlalchemy import *
+from sqlalchemy.orm import mapperlib, session, unitofwork, attributes
+Mapper = mapperlib.Mapper
+import gc
+import testbase
+import tables
+
+class A(object):pass
+class B(object):pass
+
+class MapperCleanoutTest(testbase.AssertMixin):
+ """test that clear_mappers() removes everything related to the class.
+
+ does not include classes that use the assignmapper extension."""
+ def setUp(self):
+ global engine
+ engine = testbase.db
+
+ def test_mapper_cleanup(self):
+ for x in range(0, 5):
+ self.do_test()
+ gc.collect()
+ for o in gc.get_objects():
+ if isinstance(o, Mapper):
+ # the classes in the 'tables' package have assign_mapper called on them
+ # which is particularly sticky
+ # if getattr(tables, o.class_.__name__, None) is o.class_:
+ # continue
+ # well really we are just testing our own classes here
+ if (o.class_ not in [A,B]):
+ continue
+ assert False
+ assert True
+
+ def do_test(self):
+ metadata = BoundMetaData(engine)
+
+ table1 = Table("mytable", metadata,
+ Column('col1', Integer, primary_key=True),
+ Column('col2', String(30))
+ )
+
+ table2 = Table("mytable2", metadata,
+ Column('col1', Integer, primary_key=True),
+ Column('col2', String(30)),
+ Column('col3', String(30), ForeignKey("mytable.col1"))
+ )
+
+ metadata.create_all()
+
+
+ m1 = mapper(A, table1, properties={
+ "bs":relation(B)
+ })
+ m2 = mapper(B, table2)
+
+ m3 = mapper(A, table1, non_primary=True)
+
+ sess = create_session()
+ a1 = A()
+ a2 = A()
+ a3 = A()
+ a1.bs.append(B())
+ a1.bs.append(B())
+ a3.bs.append(B())
+ for x in [a1,a2,a3]:
+ sess.save(x)
+ sess.flush()
+ sess.clear()
+
+ alist = sess.query(A).select()
+ for a in alist:
+ print "A", a, "BS", [b for b in a.bs]
+
+ metadata.drop_all()
+ clear_mappers()
+
+if __name__ == '__main__':
+ testbase.main()
\ No newline at end of file