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

Function taskRunEval

pythonwhat/tasks.py:352–472  ·  view source on GitHub ↗

Eval an expression tree (with setting envs, pre_code and/or expr_code) Utility function later wrapped to extract either result (end state of a variable), output (stdout) or error Args: tree (ast): current focused ast, used to get code to execute process: manages shell (

(
    tree,
    process,
    shell,
    env=None,
    extra_env=None,
    context=None,
    context_vals=None,
    pre_code="",
    expr_code="",
    name="",
    copy=True,
    tempname="_evaluation_object_",
    call=None,
)

Source from the content-addressed store, hash-verified

350
351@process_task
352def taskRunEval(
353 tree,
354 process,
355 shell,
356 env=None,
357 extra_env=None,
358 context=None,
359 context_vals=None,
360 pre_code="",
361 expr_code="",
362 name="",
363 copy=True,
364 tempname="_evaluation_object_",
365 call=None,
366):
367 """
368 Eval an expression tree (with setting envs, pre_code and/or expr_code)
369 Utility function later wrapped to extract either result (end state of a variable), output (stdout) or error
370
371 Args:
372 tree (ast): current focused ast, used to get code to execute
373 process: manages shell (see local.py)
374 shell: link to get process namespace from execution up until now
375 env: update value in focused code by name
376 extra_env: variables to be replaced in focused code by name from extra_env in has_expr
377 context: sum of set_context in sct chain
378 context_vals: extra context argument in has_expr
379 pre_code: argument in has_expr to execute code before evaluating, for example to set a seed
380 expr_code: code to execute instead of focused code
381 name: extract value after executing focused expr_code (~post_code)
382 copy: copy entire env because our expr_code could have side effects
383 tempname: key for the result when it is added to context, only for v1 sct's
384 call: only used in v1 sct's
385
386 Returns:
387 str: output of the executed code
388 """
389 try:
390 # Prepare code and mode -----------------------------------------------
391 # Verify if expr_code is expression code (returning a value) or just runnable code.
392 if ( # expr_code returns nothing and then we will extract a value
393 expr_code and name
394 ) or ( # No expr_code and the tree is of a node type that does not evaluate to have output
395 not expr_code and isinstance(tree, ast.Module)
396 ):
397 # We are not focused on an expression (no output)
398 mode = "exec"
399 else:
400 mode = "eval"
401 # Wrap the focused node in the tree so it can be run with eval()
402 if not isinstance(tree, (ast.Module, ast.Expression, ast.Expr)):
403 tree = ast.Expression(tree)
404
405 # Expression code takes precedence over tree code
406 if expr_code:
407 code = expr_code
408 tree = ast.parse(code, mode=mode)
409 else: # Compile the tree to Python code

Callers

nothing calls this directly

Calls 5

set_context_valsFunction · 0.90
get_envFunction · 0.85
UndefinedValueClass · 0.85
updateMethod · 0.80
parseMethod · 0.45

Tested by

no test coverage detected