55import pythonwhat
66import ast
77import inspect
8- import copy
8+ from copy import deepcopy
99from pickle import PicklingError
1010from pythonwhat .utils_env import set_context_vals , assign_from_ast
1111from contextlib import contextmanager
@@ -330,17 +330,10 @@ def get_error(f, *args, **kwargs):
330330def taskRunEval (tree ,
331331 process , shell ,
332332 keep_objs_in_env = None , extra_env = None , context = None , context_vals = None ,
333- pre_code = "" , expr_code = "" , name = "" , tempname = '_evaluation_object_' , do_exec = False ,
334- call = None ):
335- new_env = utils .copy_env (get_env (shell .user_ns ), keep_objs_in_env )
336- if extra_env is not None :
337- new_env .update (copy .deepcopy (extra_env ))
338- if context is not None :
339- set_context_vals (new_env , context , context_vals )
333+ pre_code = "" , expr_code = "" , name = "" , copy = True , tempname = '_evaluation_object_' ,
334+ do_exec = False , call = None ):
340335 try :
341- # Execute pre_code if specified
342- if pre_code : exec (pre_code , new_env )
343-
336+ # Prepare code --------------------------------------------------------
344337 # If no name given, the object of interest is the output of eval
345338 # otherwise, we'll use name to get the object from the environment
346339 if not (name or do_exec ):
@@ -353,6 +346,22 @@ def taskRunEval(tree,
353346 if expr_code : code = expr_code
354347 else : code = compile (tree , "<script>" , mode )
355348
349+ # Set up environment --------------------------------------------------
350+ # avoid deepy copy if specified, or just looking up variable by name
351+ if not copy or (isinstance (tree , ast .Name ) and isinstance (tree .ctx , ast .Load )):
352+ new_env = dict (get_env (shell .user_ns ))
353+ else :
354+ new_env = utils .copy_env (get_env (shell .user_ns ), keep_objs_in_env )
355+
356+ if extra_env is not None :
357+ new_env .update (deepcopy (extra_env ))
358+ if context is not None :
359+ set_context_vals (new_env , context , context_vals )
360+
361+ # Execute code --------------------------------------------------------
362+ # Run pre_code if specified
363+ if pre_code : exec (pre_code , new_env )
364+
356365 if mode == 'eval' :
357366 obj = eval (code , new_env )
358367 else :
0 commit comments