Пример #1
0
 def make_wrapped_function():
     """Wraps a function in three stack contexts, and returns
     the function along with the deactivation functions.
     """
     # Remove the test's stack context to make sure we can cover
     # the case where the last context is deactivated.
     with NullContext():
         partial = functools.partial
         with StackContext(partial(self.context, 'c0')) as c0:
             with StackContext(partial(self.context, 'c1')) as c1:
                 with StackContext(partial(self.context, 'c2')) as c2:
                     return (wrap(check_contexts), [c0, c1, c2])
Пример #2
0
    def test_pre_wrap(self):
        # A pre-wrapped callback is run in the context in which it was
        # wrapped, not when it was added to the IOLoop.
        def f1():
            self.assertIn('c1', self.active_contexts)
            self.assertNotIn('c2', self.active_contexts)
            self.stop()

        with StackContext(functools.partial(self.context, 'c1')):
            wrapped = wrap(f1)

        with StackContext(functools.partial(self.context, 'c2')):
            self.add_callback(wrapped)

        self.wait()
Пример #3
0
 def f():
     self.callback = yield gen.Callback('a')
     with StackContext(functools.partial(self.context, 'c1')):
         # This yield is a problem: the generator will be suspended
         # and the StackContext's __exit__ is not called yet, so
         # the context will be left on _state.contexts for anything
         # that runs before the yield resolves.
         yield gen.Wait('a')
Пример #4
0
    def test_pre_wrap_with_args(self):
        # Same as test_pre_wrap, but the function takes arguments.
        # Implementation note: The function must not be wrapped in a
        # functools.partial until after it has been passed through
        # stack_context.wrap
        def f1(foo, bar):
            self.assertIn('c1', self.active_contexts)
            self.assertNotIn('c2', self.active_contexts)
            self.stop((foo, bar))

        with StackContext(functools.partial(self.context, 'c1')):
            wrapped = wrap(f1)

        with StackContext(functools.partial(self.context, 'c2')):
            self.add_callback(wrapped, 1, bar=2)

        result = self.wait()
        self.assertEqual(result, (1, 2))
Пример #5
0
    def test_run_with_stack_context(self):
        @gen.coroutine
        def f1():
            self.assertEqual(self.active_contexts, ['c1'])
            yield run_with_stack_context(
                StackContext(functools.partial(self.context, 'c2')),
                f2)
            self.assertEqual(self.active_contexts, ['c1'])

        @gen.coroutine
        def f2():
            self.assertEqual(self.active_contexts, ['c1', 'c2'])
            yield gen.Task(self.io_loop.add_callback)
            self.assertEqual(self.active_contexts, ['c1', 'c2'])

        self.assertEqual(self.active_contexts, [])
        yield run_with_stack_context(
            StackContext(functools.partial(self.context, 'c1')),
            f1)
        self.assertEqual(self.active_contexts, [])
Пример #6
0
    def test_exit_library_context(self):
        def library_function(callback):
            # capture the caller's context before introducing our own
            callback = wrap(callback)
            with StackContext(functools.partial(self.context, 'library')):
                self.io_loop.add_callback(
                    functools.partial(library_inner_callback, callback))

        def library_inner_callback(callback):
            self.assertEqual(self.active_contexts[-2:],
                             ['application', 'library'])
            callback()

        def final_callback():
            # implementation detail:  the full context stack at this point
            # is ['application', 'library', 'application'].  The 'library'
            # context was not removed, but is no longer innermost so
            # the application context takes precedence.
            self.assertEqual(self.active_contexts[-1], 'application')
            self.stop()
        with StackContext(functools.partial(self.context, 'application')):
            library_function(final_callback)
        self.wait()
Пример #7
0
 def library_function(callback):
     # capture the caller's context before introducing our own
     callback = wrap(callback)
     with StackContext(functools.partial(self.context, 'library')):
         self.io_loop.add_callback(
             functools.partial(library_inner_callback, callback))
Пример #8
0
 def f1():
     self.assertEqual(self.active_contexts, ['c1'])
     yield run_with_stack_context(
         StackContext(functools.partial(self.context, 'c2')),
         f2)
     self.assertEqual(self.active_contexts, ['c1'])
Пример #9
0
 def test_yield_outside_with(self):
     # This pattern avoids the problem in the previous test.
     cb = yield gen.Callback('k1')
     with StackContext(functools.partial(self.context, 'c1')):
         self.io_loop.add_callback(cb)
     yield gen.Wait('k1')
Пример #10
0
 def f1():
     with NullContext():
         wrapped = wrap(f2)
     with StackContext(functools.partial(self.context, 'c2')):
         wrapped()
Пример #11
0
 def f3():
     with StackContext(functools.partial(self.context, 'c3')) as c3:
         deactivate_callbacks.append(c3)
         self.io_loop.add_callback(f4)
Пример #12
0
 def f2():
     with StackContext(functools.partial(self.context, 'c2')) as c2:
         deactivate_callbacks.append(c2)
         self.io_loop.add_callback(f3)
Пример #13
0
 def f1():
     with StackContext(functools.partial(self.context, 'c1')) as c1:
         deactivate_callbacks.append(c1)
         self.io_loop.add_callback(f2)