diff options
| author | Michael Merickel <michael@merickel.org> | 2011-07-14 21:04:32 -0500 |
|---|---|---|
| committer | Michael Merickel <michael@merickel.org> | 2011-07-14 21:04:32 -0500 |
| commit | eff1cb657b787771aeb2ed0be28c3709ae019fc3 (patch) | |
| tree | bf1edfb861e62cdee1a5932e7f3288b73e211ef7 | |
| parent | 00f7c6abb9ed581411044e9aee2f1647cfadfcb7 (diff) | |
| download | pyramid-eff1cb657b787771aeb2ed0be28c3709ae019fc3.tar.gz pyramid-eff1cb657b787771aeb2ed0be28c3709ae019fc3.tar.bz2 pyramid-eff1cb657b787771aeb2ed0be28c3709ae019fc3.zip | |
Modified tests to use global_registries.remove() instead of relying on gc.
| -rw-r--r-- | pyramid/tests/test_config.py | 9 | ||||
| -rw-r--r-- | pyramid/tests/test_scripting.py | 1 | ||||
| -rw-r--r-- | pyramid/tests/test_util.py | 19 | ||||
| -rw-r--r-- | pyramid/util.py | 19 |
4 files changed, 32 insertions, 16 deletions
diff --git a/pyramid/tests/test_config.py b/pyramid/tests/test_config.py index 9dd07a65b..dd5c90bf7 100644 --- a/pyramid/tests/test_config.py +++ b/pyramid/tests/test_config.py @@ -687,16 +687,15 @@ class ConfiguratorTests(unittest.TestCase): self.assertEqual(pyramid.config.global_registries.last, app.registry) self.assertEqual(len(subscriber), 1) self.assertTrue(IApplicationCreated.providedBy(subscriber[0])) + pyramid.config.global_registries.empty() def test_global_registries_empty(self): - import gc from pyramid.config import global_registries - gc.collect() # force weakref updates self.assertEqual(global_registries.last, None) def test_global_registries(self): - import gc from pyramid.config import global_registries + global_registries.empty() config1 = self._makeOne() config1.make_wsgi_app() self.assertEqual(global_registries.last, config1.registry) @@ -705,9 +704,9 @@ class ConfiguratorTests(unittest.TestCase): self.assertEqual(global_registries.last, config2.registry) self.assertEqual(list(global_registries), [config1.registry, config2.registry]) - del config2 - gc.collect() # force weakref updates + global_registries.remove(config2.registry) self.assertEqual(global_registries.last, config1.registry) + global_registries.empty() def test_include_with_dotted_name(self): from pyramid import tests diff --git a/pyramid/tests/test_scripting.py b/pyramid/tests/test_scripting.py index 9bf57be06..50dbaae85 100644 --- a/pyramid/tests/test_scripting.py +++ b/pyramid/tests/test_scripting.py @@ -100,6 +100,7 @@ class TestMakeRequest(unittest.TestCase): request = self._callFUT('/hello') self.assertEqual(request.environ['path'], '/hello') self.assertEqual(request.registry, registry) + global_registries.empty() class Dummy: pass diff --git a/pyramid/tests/test_util.py b/pyramid/tests/test_util.py index 65aca88b2..821d1ff31 100644 --- a/pyramid/tests/test_util.py +++ b/pyramid/tests/test_util.py @@ -216,28 +216,35 @@ class Test_WeakOrderedSet(unittest.TestCase): self.assertEqual(wos.last, reg) def test_weakref_removal(self): - import gc wos = self._makeOne() reg = Dummy() wos.add(reg) - del reg - gc.collect() # force gc + wos.remove(reg) self.assertEqual(len(wos), 0) self.assertEqual(list(wos), []) self.assertEqual(wos.last, None) def test_last_updated(self): - import gc wos = self._makeOne() reg = Dummy() reg2 = Dummy() wos.add(reg) wos.add(reg2) - del reg2 - gc.collect() # force gc + wos.remove(reg2) self.assertEqual(len(wos), 1) self.assertEqual(list(wos), [reg]) self.assertEqual(wos.last, reg) + def test_empty(self): + wos = self._makeOne() + reg = Dummy() + reg2 = Dummy() + wos.add(reg) + wos.add(reg2) + wos.empty() + self.assertEqual(len(wos), 0) + self.assertEqual(list(wos), []) + self.assertEqual(wos.last, None) + class Dummy(object): pass diff --git a/pyramid/util.py b/pyramid/util.py index b3fda9016..a4b69ed96 100644 --- a/pyramid/util.py +++ b/pyramid/util.py @@ -158,17 +158,26 @@ class WeakOrderedSet(object): self._order = [] def add(self, item): - """ Add a registry to the set.""" + """ Add an item to the set.""" oid = id(item) if oid in self._items: return - def cleanup(ref): - del self._items[oid] - self._order.remove(oid) - ref = weakref.ref(item, cleanup) + ref = weakref.ref(item, lambda x: self.remove(item)) self._items[oid] = ref self._order.append(oid) + def remove(self, item): + """ Remove an item from the set.""" + oid = id(item) + if oid in self._items: + del self._items[oid] + self._order.remove(oid) + + def empty(self): + """ Clear all objects from the set.""" + self._items = {} + self._order = [] + def __len__(self): return len(self._order) |
