MCPcopy Index your code
hub / github.com/datacamp/pythonwhat / with_context

Function with_context

pythonwhat/checks/check_funcs.py:147–194  ·  view source on GitHub ↗
(state, *args, child=None)

Source from the content-addressed store, hash-verified

145# context functions -----------------------------------------------------------
146# TODO: check if still useful
147def with_context(state, *args, child=None):
148
149 # set up context in processes
150 solution_res = setUpNewEnvInProcess(
151 process=state.solution_process, context=state.solution_parts["with_items"]
152 )
153 if isinstance(solution_res, Exception):
154 with debugger(state):
155 state.report(
156 "error in the solution, running test_with(): %s" % str(solution_res)
157 )
158
159 student_res = setUpNewEnvInProcess(
160 process=state.student_process, context=state.student_parts["with_items"]
161 )
162 if isinstance(student_res, AttributeError):
163 child.report(
164 "In your `with` statement, you're not using a correct context manager."
165 )
166
167 if isinstance(student_res, (AssertionError, ValueError, TypeError)):
168 child.report(
169 "In your `with` statement, the number of values in your context manager "
170 "doesn't correspond to the number of variables you're trying to assign it to."
171 )
172
173 # run subtests
174 try:
175 multi(state, *args)
176 finally:
177 # exit context
178 close_solution_context = breakDownNewEnvInProcess(
179 process=state.solution_process
180 )
181 if isinstance(close_solution_context, Exception):
182 with debugger(state):
183 state.report(
184 "error in the solution, closing the `with` fails with: %s"
185 % close_solution_context
186 )
187
188 close_student_context = breakDownNewEnvInProcess(process=state.student_process)
189 if isinstance(close_student_context, Exception):
190 state.report(
191 "Your `with` statement can not be closed off correctly, you're "
192 "not using the context manager correctly."
193 )
194 return state
195
196
197def check_args(state, name, missing_msg=None):

Callers 1

test_withFunction · 0.90

Calls 2

setUpNewEnvInProcessFunction · 0.90
breakDownNewEnvInProcessFunction · 0.90

Tested by 1

test_withFunction · 0.72