[Buildbot-commits] buildbot/buildbot twcompat.py,1.12,1.13

Brian Warner warner at users.sourceforge.net
Mon Dec 11 09:11:12 UTC 2006


Update of /cvsroot/buildbot/buildbot/buildbot
In directory sc8-pr-cvs3.sourceforge.net:/tmp/cvs-serv21313/buildbot

Modified Files:
	twcompat.py 
Log Message:
[project @ remove the monkeypatched defer.waitForDeferred, unnecessary with twisted-2.0.0 and later]

Original author: warner at lothar.com
Date: 2006-12-11 08:58:09

Index: twcompat.py
===================================================================
RCS file: /cvsroot/buildbot/buildbot/buildbot/twcompat.py,v
retrieving revision 1.12
retrieving revision 1.13
diff -u -d -r1.12 -r1.13
--- twcompat.py	11 Dec 2006 09:06:34 -0000	1.12
+++ twcompat.py	11 Dec 2006 09:11:10 -0000	1.13
@@ -11,152 +11,6 @@
 
 from twisted.copyright import version
 
-# waitForDeferred and getProcessOutputAndValue are twisted-2.0 things. If
-# we're running under 1.3, patch them into place. These versions are copied
-# from twisted somewhat after 2.0.1 .
-
-from twisted.internet import defer
-if not hasattr(defer, 'waitForDeferred'):
-    Deferred = defer.Deferred
-    class waitForDeferred:
-        """
-        API Stability: semi-stable
-
-        Maintainer: U{Christopher Armstrong<mailto:radix at twistedmatrix.com>}
-
-        waitForDeferred and deferredGenerator help you write
-        Deferred-using code that looks like it's blocking (but isn't
-        really), with the help of generators.
-
-        There are two important functions involved: waitForDeferred, and
-        deferredGenerator.
-
-            def thingummy():
-                thing = waitForDeferred(makeSomeRequestResultingInDeferred())
-                yield thing
-                thing = thing.getResult()
-                print thing #the result! hoorj!
-            thingummy = deferredGenerator(thingummy)
-
-        waitForDeferred returns something that you should immediately yield;
-        when your generator is resumed, calling thing.getResult() will either
-        give you the result of the Deferred if it was a success, or raise an
-        exception if it was a failure.
-
-        deferredGenerator takes one of these waitForDeferred-using
-        generator functions and converts it into a function that returns a
-        Deferred. The result of the Deferred will be the last
-        value that your generator yielded (remember that 'return result' won't
-        work; use 'yield result; return' in place of that).
-
-        Note that not yielding anything from your generator will make the
-        Deferred result in None. Yielding a Deferred from your generator
-        is also an error condition; always yield waitForDeferred(d)
-        instead.
-
-        The Deferred returned from your deferred generator may also
-        errback if your generator raised an exception.
-
-            def thingummy():
-                thing = waitForDeferred(makeSomeRequestResultingInDeferred())
-                yield thing
-                thing = thing.getResult()
-                if thing == 'I love Twisted':
-                    # will become the result of the Deferred
-                    yield 'TWISTED IS GREAT!'
-                    return
-                else:
-                    # will trigger an errback
-                    raise Exception('DESTROY ALL LIFE')
-            thingummy = deferredGenerator(thingummy)
-
-        Put succinctly, these functions connect deferred-using code with this
-        'fake blocking' style in both directions: waitForDeferred converts from
-        a Deferred to the 'blocking' style, and deferredGenerator converts from
-        the 'blocking' style to a Deferred.
-        """
-        def __init__(self, d):
-            if not isinstance(d, Deferred):
-                raise TypeError("You must give waitForDeferred a Deferred. You gave it %r." % (d,))
-            self.d = d
-
-        def getResult(self):
-            if hasattr(self, 'failure'):
-                self.failure.raiseException()
-            return self.result
-
-    def _deferGenerator(g, deferred=None, result=None):
-        """
-        See L{waitForDeferred}.
-        """
-        while 1:
-            if deferred is None:
-                deferred = defer.Deferred()
-            try:
-                result = g.next()
-            except StopIteration:
-                deferred.callback(result)
-                return deferred
-            except:
-                deferred.errback()
-                return deferred
-
-            # Deferred.callback(Deferred) raises an error; we catch this case
-            # early here and give a nicer error message to the user in case
-            # they yield a Deferred. Perhaps eventually these semantics may
-            # change.
-            if isinstance(result, defer.Deferred):
-                return defer.fail(TypeError("Yield waitForDeferred(d), not d!"))
-
-            if isinstance(result, waitForDeferred):
-                waiting=[True, None]
-                # Pass vars in so they don't get changed going around the loop
-                def gotResult(r, waiting=waiting, result=result):
-                    result.result = r
-                    if waiting[0]:
-                        waiting[0] = False
-                        waiting[1] = r
-                    else:
-                        _deferGenerator(g, deferred, r)
-                def gotError(f, waiting=waiting, result=result):
-                    result.failure = f
-                    if waiting[0]:
-                        waiting[0] = False
-                        waiting[1] = f
-                    else:
-                        _deferGenerator(g, deferred, f)
-                result.d.addCallbacks(gotResult, gotError)
-                if waiting[0]:
-                    # Haven't called back yet, set flag so that we get reinvoked
-                    # and return from the loop
-                    waiting[0] = False
-                    return deferred
-                else:
-                    result = waiting[1]
-
-    def func_metamerge(f, g):
-        """
-        Merge function metadata from f -> g and return g
-        """
-        try:
-            g.__doc__ = f.__doc__
-            g.__dict__.update(f.__dict__)
-            g.__name__ = f.__name__
-        except (TypeError, AttributeError):
-            pass
-        return g
-
-    def deferredGenerator(f):
-        """
-        See L{waitForDeferred}.
-        """
-        def unwindGenerator(*args, **kwargs):
-            return _deferGenerator(f(*args, **kwargs))
-        return func_metamerge(f, unwindGenerator)
-
-    defer.waitForDeferred = waitForDeferred
-    defer.deferredGenerator = deferredGenerator
-
 from twisted.internet import utils
 if not hasattr(utils, "getProcessOutputAndValue"):
     from twisted.internet import reactor, protocol





More information about the Commits mailing list