~jbaker/storm/nose_plugin

« back to all changes in this revision

Viewing changes to storm/event.py

  • Committer: Gustavo Niemeyer
  • Date: 2008-06-26 11:57:22 UTC
  • mfrom: (235.2.24 need-for-speed-revenge)
  • Revision ID: gustavo@niemeyer.net-20080626115722-ekl4af6sx2pn08d0
Merging need-for-speed-revenge branch [a=niemeyer,jamesh,radix]
[r=jamesh,therve]

This branch introduces a number of fixes, optimizations, and extensions
on the cextensions module, with speedup purposes.  The module is now
built by default, but still disabled unless the STORM_CEXTENSIONS=1
environment variable is defined.

Besides these, Chris also provided a mechanism to cache ClassAliases,
to prevent the cost of rebuilding the ClassInfo for each alias.

Show diffs side-by-side

added added

removed removed

Lines of Context:
20
20
#
21
21
import weakref
22
22
 
 
23
from storm import has_cextensions
 
24
 
23
25
 
24
26
__all__ = ["EventSystem"]
25
27
 
29
31
    def __init__(self, owner):
30
32
        self._owner_ref = weakref.ref(owner)
31
33
        self._hooks = {}
32
 
        self._saved_hooks = {}
33
 
 
34
 
    def save(self):
35
 
        hooks = {}
36
 
        for name, callbacks in self._hooks.items():
37
 
            hooks[name] = callbacks.copy()
38
 
        self._saved_hooks = hooks
39
 
 
40
 
    def restore(self):
41
 
        hooks = self._hooks
42
 
        hooks.clear()
43
 
        for name, callbacks in self._saved_hooks.items():
44
 
            hooks[name] = callbacks.copy()
45
34
 
46
35
    def hook(self, name, callback, *data):
47
36
        callbacks = self._hooks.get(name)
59
48
        owner = self._owner_ref()
60
49
        if owner is not None:
61
50
            callbacks = self._hooks.get(name)
62
 
            if callbacks is not None:
63
 
                for callback, data in callbacks.copy():
 
51
            if callbacks:
 
52
                for callback, data in tuple(callbacks):
64
53
                    if callback(owner, *(args+data)) is False:
65
54
                        callbacks.discard((callback, data))
 
55
 
 
56
 
 
57
if has_cextensions:
 
58
    from storm.cextensions import EventSystem