예제 #1
0
def with_context(*args, state=None):
    # set up context in processes
    solution_res = setUpNewEnvInProcess(process = state.solution_process,
                                        context = state.solution_parts['with_items'])
    if isinstance(solution_res, Exception):
        raise Exception("error in the solution, running test_with() on with %d: %s" % (index - 1, str(solution_res)))

    student_res = setUpNewEnvInProcess(process = state.student_process,
                                       context = state.student_parts['with_items'])
    if isinstance(student_res, AttributeError):
        rep.do_test(Test(Feedback("In your %s `with` statement, you're not using a correct context manager." % (get_ord(index)), child.highlight)))

    if isinstance(student_res, (AssertionError, ValueError, TypeError)):
        rep.do_test(Test(Feedback("In your %s `with` statement, the number of values in your context manager " + \
            "doesn't correspond to the number of variables you're trying to assign it to." % (get_ord(index)), child.highlight)))

    # run subtests
    try:
        multi(*args, state=state)
    finally:
        # exit context
        if breakDownNewEnvInProcess(process = state.solution_process):
            raise Exception("error in the solution, closing the %s with fails with: %s" %
                (get_ord(index), close_solution_context))

        if breakDownNewEnvInProcess(process = state.student_process):

            rep.do_test(Test(Feedback("Your %s `with` statement can not be closed off correctly, you're " + \
                            "not using the context manager correctly." % (get_ord(index)), state.highlight)),
                        fallback_ast = state.highlight)
    return state
예제 #2
0
def with_context(state, *args, child=None):

    # set up context in processes
    solution_res = setUpNewEnvInProcess(
        process=state.solution_process, context=state.solution_parts["with_items"]
    )
    if isinstance(solution_res, Exception):
        raise InstructorError(
            "error in the solution, running test_with(): %s" % str(solution_res)
        )

    student_res = setUpNewEnvInProcess(
        process=state.student_process, context=state.student_parts["with_items"]
    )
    if isinstance(student_res, AttributeError):
        state.report(
            Feedback(
                "In your `with` statement, you're not using a correct context manager.",
                child.highlight,  # TODO
            )
        )

    if isinstance(student_res, (AssertionError, ValueError, TypeError)):
        state.report(
            Feedback(
                "In your `with` statement, the number of values in your context manager "
                "doesn't correspond to the number of variables you're trying to assign it to.",
                child.highlight,
            )
        )

    # run subtests
    try:
        multi(state, *args)
    finally:
        # exit context
        close_solution_context = breakDownNewEnvInProcess(
            process=state.solution_process
        )
        if isinstance(close_solution_context, Exception):
            raise InstructorError(
                "error in the solution, closing the `with` fails with: %s"
                % close_solution_context
            )

        close_student_context = breakDownNewEnvInProcess(process=state.student_process)
        if isinstance(close_student_context, Exception):
            state.report(
                Feedback(
                    "Your `with` statement can not be closed off correctly, you're "
                    + "not using the context manager correctly.",
                    state,
                )
            )
    return state
예제 #3
0
def with_context(*args, state=None):

    rep = Reporter.active_reporter

    # set up context in processes
    solution_res = setUpNewEnvInProcess(
        process=state.solution_process,
        context=state.solution_parts['with_items'])
    if isinstance(solution_res, Exception):
        raise Exception("error in the solution, running test_with(): %s" %
                        str(solution_res))

    student_res = setUpNewEnvInProcess(
        process=state.student_process,
        context=state.student_parts['with_items'])
    if isinstance(student_res, AttributeError):
        rep.do_test(
            Test(
                Feedback(
                    "In your `with` statement, you're not using a correct context manager.",
                    child.highlight)))

    if isinstance(student_res, (AssertionError, ValueError, TypeError)):
        rep.do_test(
            Test(
                Feedback(
                    "In your `with` statement, the number of values in your context manager "
                    "doesn't correspond to the number of variables you're trying to assign it to.",
                    child.highlight)))

    # run subtests
    try:
        multi(*args, state=state)
    finally:
        # exit context
        if breakDownNewEnvInProcess(process=state.solution_process):
            raise Exception(
                "error in the solution, closing the `with` fails with: %s" %
                (close_solution_context))

        if breakDownNewEnvInProcess(process=state.student_process):

            rep.do_test(Test(Feedback("Your `with` statement can not be closed off correctly, you're " + \
                            "not using the context manager correctly.", state)))
    return state