From a6b5d75ce4b6d7c785105bd1909bfb1bb536a277 Mon Sep 17 00:00:00 2001 From: Joe Shochet Date: Thu, 15 Jun 2006 16:55:28 +0000 Subject: [PATCH] weakrefs to FunctionIntervals allow control-c-v redefine to work now --- direct/src/interval/FunctionInterval.py | 31 +++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/direct/src/interval/FunctionInterval.py b/direct/src/interval/FunctionInterval.py index 6f1f9758f4..16d936cb40 100644 --- a/direct/src/interval/FunctionInterval.py +++ b/direct/src/interval/FunctionInterval.py @@ -17,6 +17,30 @@ class FunctionInterval(Interval.Interval): # Name counter functionIntervalNum = 1 + # Keep a list of function intervals currently in memory for + # Control-C-Control-V redefining. These are just weakrefs so they + # should not cause any leaks. + if __debug__: + import weakref + FunctionIntervals = weakref.WeakKeyDictionary() + + @classmethod + def replaceMethod(self, oldFunction, newFunction): + import new + import types + count = 0 + for ival in self.FunctionIntervals: + # print 'testing: ', ival.function, oldFunction + # Note: you can only replace methods currently + if type(ival.function) == types.MethodType: + if (ival.function.im_func == oldFunction): + # print 'found: ', ival.function, oldFunction + ival.function = new.instancemethod(newFunction, + ival.function.im_self, + ival.function.im_class) + count += 1 + return count + # create FunctionInterval DirectNotify category notify = directNotify.newCategory('FunctionInterval') @@ -36,6 +60,7 @@ class FunctionInterval(Interval.Interval): # Record instance variables self.function = function + # Create a unique name for the interval if necessary if (name == None): name = 'Func-%s-%d' % (function.__name__, FunctionInterval.functionIntervalNum) @@ -51,6 +76,11 @@ class FunctionInterval(Interval.Interval): # Parent, Pos, Hpr, etc intervals default to openEnded = 1 Interval.Interval.__init__(self, name, duration = 0.0, openEnded = openEnded) + # For rebinding, let's remember this function interval on the class + if __debug__: + self.FunctionIntervals[self] = 1 + + def privInstant(self): # Evaluate the function self.function(*self.extraArgs, **self.kw) @@ -58,6 +88,7 @@ class FunctionInterval(Interval.Interval): self.notify.debug( 'updateFunc() - %s: executing Function' % self.name) + ### FunctionInterval subclass for throwing events ### class EventInterval(FunctionInterval): # Initialization