Python twisted.internet.defer.inlineCallbacks() Examples

The following are 30 code examples of twisted.internet.defer.inlineCallbacks(). 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_inlinecallbacks_yield_statement.py    From dlint with BSD 3-Clause "New" or "Revised" License 7 votes vote down vote up
def test_raise_statement_with_docstring(self):
        python_node = self.get_ast_node(
            """
            from twisted.internet import defer

            @defer.inlineCallbacks
            def func(arg):
                '''Foo
                '''
                raise NotImplementedError()
            """
        )

        linter = dlint.linters.InlineCallbacksYieldStatementLinter()
        linter.visit(python_node)

        result = linter.get_results()
        expected = []

        assert result == expected 
Example #2
Source File: test_inlinecallbacks_yield_statement.py    From dlint with BSD 3-Clause "New" or "Revised" License 7 votes vote down vote up
def test_multiple_raise_statements(self):
        python_node = self.get_ast_node(
            """
            from twisted.internet import defer

            @defer.inlineCallbacks
            def func(arg):
                raise errors.ApiException(errors.FORBIDDEN)
                raise errors.ApiException(errors.MISSING)
            """
        )

        linter = dlint.linters.InlineCallbacksYieldStatementLinter()
        linter.visit(python_node)

        result = linter.get_results()
        expected = []

        assert result == expected 
Example #3
Source File: test_inlinecallbacks_yield_statement.py    From dlint with BSD 3-Clause "New" or "Revised" License 7 votes vote down vote up
def test_missing_yield_statement(self):
        python_node = self.get_ast_node(
            """
            from twisted.internet import defer

            @defer.inlineCallbacks
            def func(arg):
                return
            """
        )

        linter = dlint.linters.InlineCallbacksYieldStatementLinter()
        linter.visit(python_node)

        result = linter.get_results()
        expected = [
            dlint.linters.base.Flake8Result(
                lineno=5 if IS_PYTHON_3_8 else 4,
                col_offset=0,
                message=dlint.linters.InlineCallbacksYieldStatementLinter._error_tmpl
            )
        ]

        assert result == expected 
Example #4
Source File: test_retry.py    From uplink with MIT License 6 votes vote down vote up
def test_retry_with_twisted(mock_client, mock_response):
    from twisted.internet import defer

    @defer.inlineCallbacks
    def return_response():
        yield
        defer.returnValue(mock_response)

    # Setup
    mock_response.with_json({"id": 123, "name": "prkumar"})
    mock_client.with_side_effect([Exception, return_response()])
    mock_client.with_io(io.TwistedStrategy())
    github = GitHub(base_url=BASE_URL, client=mock_client)

    # Run
    response = yield github.get_user("prkumar")

    assert len(mock_client.history) == 2
    assert response.json() == {"id": 123, "name": "prkumar"} 
Example #5
Source File: inlinecb_tests.py    From python-for-android with Apache License 2.0 6 votes vote down vote up
def test_returnValueNonLocalDeferred(self):
        """
        L{returnValue} will emit a non-local warning in the case where the
        L{inlineCallbacks}-decorated function has already yielded a Deferred
        and therefore moved its generator function along.
        """
        cause = Deferred()
        @inlineCallbacks
        def inline():
            yield cause
            self.mistakenMethod()
            returnValue(2)
        effect = inline()
        results = []
        effect.addCallback(results.append)
        self.assertEquals(results, [])
        cause.callback(1)
        self.assertMistakenMethodWarning(results) 
Example #6
Source File: inlinecb_tests.py    From python-for-android with Apache License 2.0 6 votes vote down vote up
def assertMistakenMethodWarning(self, resultList):
        """
        Flush the current warnings and assert that we have been told that
        C{mistakenMethod} was invoked, and that the result from the Deferred
        that was fired (appended to the given list) is C{mistakenMethod}'s
        result.  The warning should indicate that an inlineCallbacks function
        called 'inline' was made to exit.
        """
        self.assertEqual(resultList, [1])
        warnings = self.flushWarnings(offendingFunctions=[self.mistakenMethod])
        self.assertEqual(len(warnings), 1)
        self.assertEquals(warnings[0]['category'], DeprecationWarning)
        self.assertEquals(
            warnings[0]['message'],
            "returnValue() in 'mistakenMethod' causing 'inline' to exit: "
            "returnValue should only be invoked by functions decorated with "
            "inlineCallbacks") 
Example #7
Source File: height_tracker.py    From p2pool-n with GNU General Public License v3.0 6 votes vote down vote up
def get_height_rel_highest_func(bitcoind, factory, best_block_func, net):
    if '\ngetblock ' in (yield deferral.retry()(bitcoind.rpc_help)()):
        @deferral.DeferredCacher
        @defer.inlineCallbacks
        def height_cacher(block_hash):
            try:
                x = yield bitcoind.rpc_getblock('%x' % (block_hash,))
            except jsonrpc.Error_for_code(-5): # Block not found
                if not p2pool.DEBUG:
                    raise deferral.RetrySilentlyException()
                else:
                    raise
            defer.returnValue(x['blockcount'] if 'blockcount' in x else x['height'])
        best_height_cached = variable.Variable((yield deferral.retry()(height_cacher)(best_block_func())))
        def get_height_rel_highest(block_hash):
            this_height = height_cacher.call_now(block_hash, 0)
            best_height = height_cacher.call_now(best_block_func(), 0)
            best_height_cached.set(max(best_height_cached.value, this_height, best_height))
            return this_height - best_height_cached.value
    else:
        get_height_rel_highest = HeightTracker(best_block_func, factory, 5*net.SHARE_PERIOD*net.CHAIN_LENGTH/net.PARENT.BLOCK_PERIOD).get_height_rel_highest
    defer.returnValue(get_height_rel_highest) 
Example #8
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 #9
Source File: test_twisted.py    From python-consul with MIT License 6 votes vote down vote up
def test_catalog(self, consul_port):
        c = consul.twisted.Consul(port=consul_port)

        @defer.inlineCallbacks
        def register():
            response = yield c.catalog.register('n1', '10.1.10.11')
            assert response is True
            yield sleep(50 / 1000.0)
            response = yield c.catalog.deregister('n1')
            assert response is True

        reactor.callLater(1.0 / 100, register)

        index, nodes = yield c.catalog.nodes()
        assert len(nodes) == 1
        current = nodes[0]

        index, nodes = yield c.catalog.nodes(index=index)
        nodes.remove(current)
        assert [x['Node'] for x in nodes] == ['n1']

        index, nodes = yield c.catalog.nodes(index=index)
        nodes.remove(current)
        assert [x['Node'] for x in nodes] == [] 
Example #10
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 #11
Source File: test_inlinecb.py    From Safejumper-for-Desktop with GNU General Public License v2.0 6 votes vote down vote up
def test_returnValueNonLocalDeferred(self):
        """
        L{returnValue} will emit a non-local warning in the case where the
        L{inlineCallbacks}-decorated function has already yielded a Deferred
        and therefore moved its generator function along.
        """
        cause = Deferred()
        @inlineCallbacks
        def inline():
            yield cause
            self.mistakenMethod()
            returnValue(2)
        effect = inline()
        results = []
        effect.addCallback(results.append)
        self.assertEqual(results, [])
        cause.callback(1)
        self.assertMistakenMethodWarning(results) 
Example #12
Source File: cache.py    From worker with GNU General Public License v3.0 6 votes vote down vote up
def cache(f):
    @wraps(f)
    @defer.inlineCallbacks
    def wrapper(*args, **kwargs):
        if 'cache_key' in kwargs and 'cache_ttl' in kwargs:
            key = "%s%s" % (f, kwargs['cache_key'])
            ttl = kwargs['cache_ttl']
            del kwargs['cache_key']
            del kwargs['cache_ttl']
            now = reactor.seconds()

            @defer.inlineCallbacks
            def get_value():
                result = yield f(*args, **kwargs)
                defer.returnValue(result)
            timestamp, result = CACHE.get(key, (0, None))
            if timestamp + ttl < now:
                CACHE[key] = (now, result)
                result = yield get_value()
                CACHE[key] = (now, result)
        else:
            result = yield f(*args, **kwargs)
        defer.returnValue(result)
    return wrapper 
Example #13
Source File: core.py    From python-wpa-supplicant with Mozilla Public License 2.0 6 votes vote down vote up
def _eval(deferred, reactor):
        """Evaluate a deferred on a given reactor and return the result

        This function is safe to call with a deferred that has already been evaluated.
        """

        @defer.inlineCallbacks
        def closure():
            if deferred.called:
                result = deferred.result
            else:
                result = yield deferred

            defer.returnValue(result)

        if threading.currentThread().getName() == reactor.thread_name:
            return closure()
        else:
            return threads.blockingCallFromThread(reactor, closure)


#
# Exceptions
# 
Example #14
Source File: test_retry.py    From uplink with MIT License 6 votes vote down vote up
def test_retry_fail_with_twisted(mock_client, mock_response):
    from twisted.internet import defer

    @defer.inlineCallbacks
    def return_response():
        yield
        defer.returnValue(mock_response)

    # Setup
    CustomException = type("CustomException", (Exception,), {})
    mock_response.with_json({"id": 123, "name": "prkumar"})
    mock_client.with_side_effect(
        [Exception, CustomException, return_response()]
    )
    mock_client.with_io(io.TwistedStrategy())
    github = GitHub(base_url=BASE_URL, client=mock_client)

    # Run
    with pytest.raises(CustomException):
        yield github.get_user("prkumar")

    assert len(mock_client.history) == 2 
Example #15
Source File: test_inlinecb.py    From Safejumper-for-Desktop with GNU General Public License v2.0 6 votes vote down vote up
def test_returnWithValue(self):
        """
        If the C{return} statement has a value it is propagated back to the
        L{Deferred} that the C{inlineCallbacks} function returned.
        """
        environ = {"inlineCallbacks": inlineCallbacks}
        exec("""
@inlineCallbacks
def f(d):
    yield d
    return 14
        """, environ)
        d1 = Deferred()
        d2 = environ["f"](d1)
        d1.callback(None)
        self.assertEqual(self.successResultOf(d2), 14) 
Example #16
Source File: test_defer.py    From Safejumper-for-Desktop with GNU General Public License v2.0 6 votes vote down vote up
def test_inlineCallbacksTracebacks(self):
        """
        L{defer.inlineCallbacks} that re-raise tracebacks into their deferred
        should not lose their tracebacks.
        """
        f = getDivisionFailure()
        d = defer.Deferred()
        try:
            f.raiseException()
        except:
            d.errback()

        def ic(d):
            yield d
        ic = defer.inlineCallbacks(ic)
        newFailure = self.failureResultOf(d)
        tb = traceback.extract_tb(newFailure.getTracebackObject())

        self.assertEqual(len(tb), 2)
        self.assertIn('test_defer', tb[0][0])
        self.assertEqual('test_inlineCallbacksTracebacks', tb[0][2])
        self.assertEqual('f.raiseException()', tb[0][3])
        self.assertIn('test_defer', tb[1][0])
        self.assertEqual('getDivisionFailure', tb[1][2])
        self.assertEqual('1/0', tb[1][3]) 
Example #17
Source File: test_inlinecb.py    From Safejumper-for-Desktop with GNU General Public License v2.0 6 votes vote down vote up
def assertMistakenMethodWarning(self, resultList):
        """
        Flush the current warnings and assert that we have been told that
        C{mistakenMethod} was invoked, and that the result from the Deferred
        that was fired (appended to the given list) is C{mistakenMethod}'s
        result.  The warning should indicate that an inlineCallbacks function
        called 'inline' was made to exit.
        """
        self.assertEqual(resultList, [1])
        warnings = self.flushWarnings(offendingFunctions=[self.mistakenMethod])
        self.assertEqual(len(warnings), 1)
        self.assertEqual(warnings[0]['category'], DeprecationWarning)
        self.assertEqual(
            warnings[0]['message'],
            "returnValue() in 'mistakenMethod' causing 'inline' to exit: "
            "returnValue should only be invoked by functions decorated with "
            "inlineCallbacks") 
Example #18
Source File: maintenance.py    From pixelated-user-agent with GNU Affero General Public License v3.0 6 votes vote down vote up
def initialize():
    args = arguments.parse_maintenance_args()

    logger.init(debug=args.debug)

    @defer.inlineCallbacks
    def _run():
        leap_session = yield initialize_leap_single_user(
            args.leap_provider_cert,
            args.leap_provider_cert_fingerprint,
            args.credentials_file,
            leap_home=args.leap_home)

        execute_command(args, leap_session)

    reactor.callWhenRunning(_run)
    reactor.run() 
Example #19
Source File: test_twisted.py    From python-consul2 with MIT License 6 votes vote down vote up
def test_catalog(self, consul_port):
        c = consul.twisted.Consul(port=consul_port)

        @defer.inlineCallbacks
        def register():
            response = yield c.catalog.register('n1', '10.1.10.11')
            assert response is True
            yield sleep(50 / 1000.0)
            response = yield c.catalog.deregister('n1')
            assert response is True

        reactor.callLater(1.0 / 100, register)

        index, nodes = yield c.catalog.nodes()
        assert len(nodes) == 1
        current = nodes[0]

        index, nodes = yield c.catalog.nodes(index=index)
        nodes.remove(current)
        assert [x['Node'] for x in nodes] == ['n1']

        index, nodes = yield c.catalog.nodes(index=index)
        nodes.remove(current)
        assert [x['Node'] for x in nodes] == [] 
Example #20
Source File: core.py    From python-wpa-supplicant with Mozilla Public License 2.0 6 votes vote down vote up
def connect(self):
        """Connect to wpa_supplicant over D-Bus

        :returns: Remote D-Bus proxy object of the root wpa_supplicant interface
        :rtype: :class:`~WpaSupplicant`
        """

        if not self._reactor.running:
            raise ReactorNotRunning('Twisted Reactor must be started (call .run())')

        @defer.inlineCallbacks
        def get_conn():
            self._reactor.thread_name = threading.currentThread().getName()
            conn = yield client.connect(self._reactor, busAddress='system')
            defer.returnValue(conn)

        conn = threads.blockingCallFromThread(self._reactor, get_conn)
        return WpaSupplicant('/fi/w1/wpa_supplicant1', conn, self._reactor, ) 
Example #21
Source File: test_returnvalue_in_inlinecallbacks.py    From dlint with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def test_returnvalue_statement(self):
        python_node = self.get_ast_node(
            """
            from twisted.internet import defer

            @defer.inlineCallbacks
            def func(arg):
                yield 'foo'
                defer.returnValue('foo')
            """
        )

        linter = dlint.linters.ReturnValueInInlineCallbacksLinter()
        linter.visit(python_node)

        result = linter.get_results()
        expected = []

        assert result == expected 
Example #22
Source File: test_returnvalue_in_inlinecallbacks.py    From dlint with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def test_returnvalue_name_statement(self):
        python_node = self.get_ast_node(
            """
            from twisted.internet.defer import inlineCallbacks, returnValue

            @inlineCallbacks
            def func(arg):
                yield 'foo'
                returnValue('foo')
            """
        )

        linter = dlint.linters.ReturnValueInInlineCallbacksLinter()
        linter.visit(python_node)

        result = linter.get_results()
        expected = []

        assert result == expected 
Example #23
Source File: test_returnvalue_in_inlinecallbacks.py    From dlint with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def test_nested_yield_statement(self):
        python_node = self.get_ast_node(
            """
            from twisted.internet import defer

            @defer.inlineCallbacks
            def func(arg):
                if True:
                    for _ in range(10):
                        defer.returnValue('foo')
            """
        )

        linter = dlint.linters.ReturnValueInInlineCallbacksLinter()
        linter.visit(python_node)

        result = linter.get_results()
        expected = []

        assert result == expected 
Example #24
Source File: test_inlinecallbacks_yield_statement.py    From dlint with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def test_nested_yield_statement(self):
        python_node = self.get_ast_node(
            """
            from twisted.internet import defer

            @defer.inlineCallbacks
            def func(arg):
                if True:
                    for _ in range(10):
                        yield 'foo'
                return
            """
        )

        linter = dlint.linters.InlineCallbacksYieldStatementLinter()
        linter.visit(python_node)

        result = linter.get_results()
        expected = []

        assert result == expected 
Example #25
Source File: test_inlinecallbacks_yield_statement.py    From dlint with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def test_assign_yield_statement(self):
        python_node = self.get_ast_node(
            """
            from twisted.internet import defer

            @defer.inlineCallbacks
            def func(arg):
                bar = yield foo()
                return
            """
        )

        linter = dlint.linters.InlineCallbacksYieldStatementLinter()
        linter.visit(python_node)

        result = linter.get_results()
        expected = []

        assert result == expected 
Example #26
Source File: test_inlinecallbacks_yield_statement.py    From dlint with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def test_nested_assign_yield_statement(self):
        python_node = self.get_ast_node(
            """
            from twisted.internet import defer

            @defer.inlineCallbacks
            def func(arg):
                if True:
                    for _ in range(10):
                        bar = yield foo()
                return
            """
        )

        linter = dlint.linters.InlineCallbacksYieldStatementLinter()
        linter.visit(python_node)

        result = linter.get_results()
        expected = []

        assert result == expected 
Example #27
Source File: test_inlinecallbacks_yield_statement.py    From dlint with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def test_only_raise_statement(self):
        python_node = self.get_ast_node(
            """
            from twisted.internet import defer

            @defer.inlineCallbacks
            def func(arg):
                raise NotImplementedError()
            """
        )

        linter = dlint.linters.InlineCallbacksYieldStatementLinter()
        linter.visit(python_node)

        result = linter.get_results()
        expected = []

        assert result == expected 
Example #28
Source File: test_inlinecallbacks_yield_statement.py    From dlint with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def test_only_pass_statement(self):
        python_node = self.get_ast_node(
            """
            from twisted.internet import defer

            @defer.inlineCallbacks
            def func(arg):
                pass
            """
        )

        linter = dlint.linters.InlineCallbacksYieldStatementLinter()
        linter.visit(python_node)

        result = linter.get_results()
        expected = []

        assert result == expected 
Example #29
Source File: test_inlinecallbacks_yield_statement.py    From dlint with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def test_raise_with_yield(self):
        python_node = self.get_ast_node(
            """
            from twisted.internet import defer

            @defer.inlineCallbacks
            def func(arg):
                if True:
                    raise NotImplementedError()
                yield 'foo'
                return
            """
        )

        linter = dlint.linters.InlineCallbacksYieldStatementLinter()
        linter.visit(python_node)

        result = linter.get_results()
        expected = []

        assert result == expected 
Example #30
Source File: test_yield_return_statement.py    From dlint with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def test_none_return_statement(self):
        python_node = self.get_ast_node(
            """
            from twisted.internet import defer

            @defer.inlineCallbacks
            def func(arg):
                return None
            """
        )

        linter = dlint.linters.YieldReturnStatementLinter()
        linter.visit(python_node)

        result = linter.get_results()
        expected = [] if IS_PYTHON_3_3 else [
            dlint.linters.base.Flake8Result(
                lineno=6,
                col_offset=4,
                message=dlint.linters.YieldReturnStatementLinter._error_tmpl
            )
        ]

        assert result == expected