Python twisted.internet.defer.deferredGenerator() Examples

The following are 15 code examples of twisted.internet.defer.deferredGenerator(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module twisted.internet.defer , or try the search function .
Example #1
Source File: test_defgen.py    From Safejumper-for-Desktop with GNU General Public License v2.0 6 votes vote down vote up
def test_deferredGeneratorDeprecated(self):
        """
        L{deferredGenerator} is deprecated.
        """
        @deferredGenerator
        def decoratedFunction():
            yield None

        warnings = self.flushWarnings([self.test_deferredGeneratorDeprecated])
        self.assertEqual(len(warnings), 1)
        self.assertEqual(warnings[0]['category'], DeprecationWarning)
        self.assertEqual(
            warnings[0]['message'],
            "twisted.internet.defer.deferredGenerator was deprecated in "
            "Twisted 15.0.0; please use "
            "twisted.internet.defer.inlineCallbacks instead") 
Example #2
Source File: test_defgen.py    From learn_python3_spider with MIT License 6 votes vote down vote up
def test_deferredGeneratorDeprecated(self):
        """
        L{deferredGenerator} is deprecated.
        """
        @deferredGenerator
        def decoratedFunction():
            yield None

        warnings = self.flushWarnings([self.test_deferredGeneratorDeprecated])
        self.assertEqual(len(warnings), 1)
        self.assertEqual(warnings[0]['category'], DeprecationWarning)
        self.assertEqual(
            warnings[0]['message'],
            "twisted.internet.defer.deferredGenerator was deprecated in "
            "Twisted 15.0.0; please use "
            "twisted.internet.defer.inlineCallbacks instead") 
Example #3
Source File: test_defgen.py    From python-for-android with Apache License 2.0 6 votes vote down vote up
def testDeferredYielding(self):
        """
        Ensure that yielding a Deferred directly is trapped as an
        error.
        """
        # See the comment _deferGenerator about d.callback(Deferred).
        def _genDeferred():
            yield getThing()
        _genDeferred = deferredGenerator(_genDeferred)

        return self.assertFailure(_genDeferred(), TypeError)



## This has to be in a string so the new yield syntax doesn't cause a
## syntax error in Python 2.4 and before. 
Example #4
Source File: test_defgen.py    From BitTorrent with GNU General Public License v3.0 6 votes vote down vote up
def testHandledTerminalFailure(self):
        """
        Create a Deferred Generator which yields a Deferred which fails and
        handles the exception which results.  Assert that the Deferred
        Generator does not errback its Deferred.
        """
        class TerminalException(Exception):
            pass

        def _genFailure():
            x = waitForDeferred(defer.fail(TerminalException("Handled Terminal Failure")))
            yield x
            try:
                x.getResult()
            except TerminalException:
                pass
        _genFailure = deferredGenerator(_genFailure)
        return _genFailure().addCallback(self.assertEqual, None) 
Example #5
Source File: test_defgen.py    From BitTorrent with GNU General Public License v3.0 6 votes vote down vote up
def testHandledTerminalAsyncFailure(self):
        """
        Just like testHandledTerminalFailure, only with a Deferred which fires
        asynchronously with an error.
        """
        class TerminalException(Exception):
            pass


        d = defer.Deferred()
        def _genFailure():
            x = waitForDeferred(d)
            yield x
            try:
                x.getResult()
            except TerminalException:
                pass
        _genFailure = deferredGenerator(_genFailure)
        deferredGeneratorResultDeferred = _genFailure()
        d.errback(TerminalException("Handled Terminal Failure"))
        return deferredGeneratorResultDeferred.addCallback(
            self.assertEqual, None) 
Example #6
Source File: test_defgen.py    From Safejumper-for-Desktop with GNU General Public License v2.0 5 votes vote down vote up
def testBasics(self):
        """
        Test that a normal deferredGenerator works.  Tests yielding a
        deferred which callbacks, as well as a deferred errbacks. Also
        ensures returning a final value works.
        """

        return self._genBasics().addCallback(self.assertEqual, 'WOOSH') 
Example #7
Source File: test_defgen.py    From Safejumper-for-Desktop with GNU General Public License v2.0 5 votes vote down vote up
def deprecatedDeferredGenerator(f):
    """
    Calls L{deferredGenerator} while suppressing the deprecation warning.

    @param f: Function to call
    @return: Return value of function.
    """
    return runWithWarningsSuppressed(
        [ SUPPRESS(message="twisted.internet.defer.deferredGenerator was "
                          "deprecated") ],
        deferredGenerator, f) 
Example #8
Source File: test_defgen.py    From learn_python3_spider with MIT License 5 votes vote down vote up
def testBasics(self):
        """
        Test that a normal deferredGenerator works.  Tests yielding a
        deferred which callbacks, as well as a deferred errbacks. Also
        ensures returning a final value works.
        """

        return self._genBasics().addCallback(self.assertEqual, 'WOOSH') 
Example #9
Source File: test_defgen.py    From learn_python3_spider with MIT License 5 votes vote down vote up
def deprecatedDeferredGenerator(f):
    """
    Calls L{deferredGenerator} while suppressing the deprecation warning.

    @param f: Function to call
    @return: Return value of function.
    """
    return runWithWarningsSuppressed(
        [ SUPPRESS(message="twisted.internet.defer.deferredGenerator was "
                          "deprecated") ],
        deferredGenerator, f) 
Example #10
Source File: test_defgen.py    From python-for-android with Apache License 2.0 5 votes vote down vote up
def testBasics(self):
        """
        Test that a normal deferredGenerator works.  Tests yielding a
        deferred which callbacks, as well as a deferred errbacks. Also
        ensures returning a final value works.
        """

        return self._genBasics().addCallback(self.assertEqual, 'WOOSH') 
Example #11
Source File: test_defgen.py    From BitTorrent with GNU General Public License v3.0 5 votes vote down vote up
def testBuggyGen(self):
        def _genError():
            yield waitForDeferred(getThing())
            1/0
        _genError = deferredGenerator(_genError)

        return self.assertFailure(_genError(), ZeroDivisionError) 
Example #12
Source File: test_defgen.py    From BitTorrent with GNU General Public License v3.0 5 votes vote down vote up
def testNothing(self):
        def _genNothing():
            if 0: yield 1
        _genNothing = deferredGenerator(_genNothing)

        return _genNothing().addCallback(self.assertEqual, None) 
Example #13
Source File: test_defgen.py    From BitTorrent with GNU General Public License v3.0 5 votes vote down vote up
def testDeferredYielding(self):
        # See the comment _deferGenerator about d.callback(Deferred).
        def _genDeferred():
            yield getThing()
        _genDeferred = deferredGenerator(_genDeferred)

        return self.assertFailure(_genDeferred(), TypeError) 
Example #14
Source File: test_defgen.py    From BitTorrent with GNU General Public License v3.0 5 votes vote down vote up
def testStackUsage2(self):
        def _loop():
            for x in range(5000):
                # Test with yielding a random value
                yield 1
            yield 0

        _loop = deferredGenerator(_loop)
        return _loop().addCallback(self.assertEqual, 0) 
Example #15
Source File: static.py    From ccs-calendarserver with Apache License 2.0 4 votes vote down vote up
def quotaSize(self, request):
        """
        Get the size of this resource.
        TODO: Take into account size of dead-properties. Does stat
            include xattrs size?

        @return: an L{Deferred} with a C{int} result containing the size of the resource.
        """
        if self.isCollection():
            def walktree(top):
                """
                Recursively descend the directory tree rooted at top,
                calling the callback function for each regular file

                @param top: L{FilePath} for the directory to walk.
                """

                total = 0
                for f in top.listdir():
                    child = top.child(f)
                    if child.isdir():
                        # It's a directory, recurse into it
                        result = waitForDeferred(walktree(child))
                        yield result
                        total += result.getResult()
                    elif child.isfile():
                        # It's a file, call the callback function
                        total += child.getsize()
                    else:
                        # Unknown file type, print a message
                        pass

                yield total

            walktree = deferredGenerator(walktree)

            return walktree(self.fp)
        else:
            return succeed(self.fp.getsize())

    ##
    # Workarounds for issues with File
    ##