Пример #1
0
    def test_handles_exceptions_raised_in_python_code_passed_to_c_code(self):
        def fun():
            def rescue(x):
                return x - 1
            def after(x):
                return x + 1
            def bad(x):
                if x > 0:
                    raise ValueError
            def trymap():
                try:
                    map(bad, [0, 1, 2])
                except ValueError:
                    rescue(1)
                after(2)
            trymap()

        expected_call_graph = noindent("""
            trymap()
                map()
                    bad()
                    bad()
                rescue()
                after()
        """)

        callables, execution = inspect_returning_callables_and_execution(fun)
        assert_equal_strings(expected_call_graph,
            call_graph_as_string(execution.call_graph))

        assert_length(callables, 4)
        rescue = find_first_with_name("rescue", callables)
        after = find_first_with_name("after", callables)
        trymap = find_first_with_name("trymap", callables)
        bad = find_first_with_name("bad", callables)

        assert_single_call({'x': 1}, 0, rescue)
        assert_single_call({'x': 2}, 3, after)
        assert_single_call({}, None, trymap)
        assert_length(bad.calls, 2)
        assert_call({'x': 0}, None, bad.calls[0])
        assert_call_with_exception({'x': 1}, 'ValueError', bad.calls[1])
Пример #2
0
    def test_generator_can_be_invoked_in_multiple_places(self):
        def function():
            def generator():
                i = 1
                while True:
                    yield i
                    i += 1
            def first(g):
                return g.next()
            def second(g):
                return g.next() + g.next()
            g = generator()
            g.next()
            first(g)
            second(g)

        execution = TestingProject()\
            .with_all_catch_module()\
            .with_object(Function("generator", is_generator=True))\
            .with_object(Function("first"))\
            .with_object(Function("second"))\
            .make_new_execution()
        inspect_code_in_context(function, execution)

        expected_call_graph = noindent("""
            generator()
            first()
                next()
                    generator()
            second()
                next()
                    generator()
                next()
                    generator()
        """)
        assert_equal_strings(expected_call_graph,
            call_graph_as_string(execution.call_graph))