Commits

Anonymous committed 3a318f5

a much easier way to ArgSingleton

  • Participants
  • Parent commits fac0e3f

Comments (0)

Files changed (3)

File lib/sqlalchemy/orm/dependency.py

         self.passive_updates = prop.passive_updates
         self.enable_typechecks = prop.enable_typechecks
         self.key = prop.key
+        self.dependency_marker = MapperStub(self.parent, self.mapper, self.key)
         if not self.prop.synchronize_pairs:
             raise sa_exc.ArgumentError("Can't build a DependencyProcessor for relation %s.  No target attributes to populate between parent and child are present" % self.prop)
 
     def register_dependencies(self, uowcommit):
         if self.post_update:
             if not self.is_backref:
-                stub = MapperStub(self.parent, self.mapper, self.key)
-                uowcommit.register_dependency(self.mapper, stub)
-                uowcommit.register_dependency(self.parent, stub)
-                uowcommit.register_processor(stub, self, self.parent)
+                uowcommit.register_dependency(self.mapper, self.dependency_marker)
+                uowcommit.register_dependency(self.parent, self.dependency_marker)
+                uowcommit.register_processor(self.dependency_marker, self, self.parent)
         else:
             uowcommit.register_dependency(self.parent, self.mapper)
             uowcommit.register_processor(self.parent, self, self.parent)
     def register_dependencies(self, uowcommit):
         if self.post_update:
             if not self.is_backref:
-                stub = MapperStub(self.parent, self.mapper, self.key)
-                uowcommit.register_dependency(self.mapper, stub)
-                uowcommit.register_dependency(self.parent, stub)
-                uowcommit.register_processor(stub, self, self.parent)
+                uowcommit.register_dependency(self.mapper, self.dependency_marker)
+                uowcommit.register_dependency(self.parent, self.dependency_marker)
+                uowcommit.register_processor(self.dependency_marker, self, self.parent)
         else:
             uowcommit.register_dependency(self.mapper, self.parent)
             uowcommit.register_processor(self.mapper, self, self.parent)
         # related mappers.  its dependency processor then populates the
         # association table.
 
-        stub = MapperStub(self.parent, self.mapper, self.key)
-        uowcommit.register_dependency(self.parent, stub)
-        uowcommit.register_dependency(self.mapper, stub)
-        uowcommit.register_processor(stub, self, self.parent)
+        uowcommit.register_dependency(self.parent, self.dependency_marker)
+        uowcommit.register_dependency(self.mapper, self.dependency_marker)
+        uowcommit.register_processor(self.dependency_marker, self, self.parent)
 
     def process_dependencies(self, task, deplist, uowcommit, delete = False):
         #print self.mapper.mapped_table.name + " " + self.key + " " + repr(len(deplist)) + " process_dep isdelete " + repr(delete) + " direction " + repr(self.direction)
     so that a depedendency can be corresponded to it.
 
     """
-    __metaclass__ = util.ArgSingleton
-
+    
     def __init__(self, parent, mapper, key):
         self.mapper = mapper
         self.base_mapper = self

File lib/sqlalchemy/util.py

         else:
             yield elem
 
-class ArgSingleton(type):
-    instances = weakref.WeakValueDictionary()
-
-    def dispose(cls):
-        for key in list(ArgSingleton.instances):
-            if key[0] is cls:
-                del ArgSingleton.instances[key]
-    dispose = staticmethod(dispose)
-
-    def __call__(self, *args):
-        hashkey = (self, args)
-        try:
-            return ArgSingleton.instances[hashkey]
-        except KeyError:
-            instance = type.__call__(self, *args)
-            ArgSingleton.instances[hashkey] = instance
-            return instance
-
 def get_cls_kwargs(cls):
     """Return the full set of inherited kwargs for the given `cls`.
 

File test/base/utils.py

         assert (cc1==cc2).compare(c1 == c2)
         assert not (cc1==cc3).compare(c2 == c3)
 
-class ArgSingletonTest(unittest.TestCase):
-    def test_cleanout(self):
-        util.ArgSingleton.instances.clear()
-
-        class MyClass(object):
-            __metaclass__ = util.ArgSingleton
-            def __init__(self, x, y):
-                self.x = x
-                self.y = y
-
-        m1 = MyClass(3, 4)
-        m2 = MyClass(1, 5)
-        m3 = MyClass(3, 4)
-        assert m1 is m3
-        assert m2 is not m3
-        eq_(len(util.ArgSingleton.instances), 2)
-
-        m1 = m2 = m3 = None
-        MyClass.dispose(MyClass)
-        eq_(len(util.ArgSingleton.instances), 0)
-
-
 class ImmutableSubclass(str):
     pass