Skip to content

Commit 10788e2

Browse files
authored
Merge pull request datacamp#377 from datacamp/jh/less-deepcopy
Improve detection for skipping deepcopy
2 parents d95fb22 + 1e314cf commit 10788e2

File tree

1 file changed

+8
-2
lines changed

1 file changed

+8
-2
lines changed

‎pythonwhat/tasks.py‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -368,7 +368,8 @@ def taskRunEval(
368368
mode = "exec"
369369
else:
370370
mode = "eval"
371-
tree = ast.Expression(tree)
371+
if not isinstance(tree, (ast.Module, ast.Expression, ast.Expr)):
372+
tree = ast.Expression(tree)
372373

373374
# Expression code takes precedence over tree code
374375
if expr_code:
@@ -378,9 +379,14 @@ def taskRunEval(
378379
code = compile(tree, "<script>", mode)
379380

380381
# Set up environment --------------------------------------------------
381-
# avoid deepy copy if specified, or just looking up variable by name
382+
# avoid deep copy if specified, or if just looking up variable by name
383+
# unpack 'container nodes' first
384+
if isinstance(tree, ast.Module):
385+
tree = tree.body
382386
if isinstance(tree, ast.Expression):
383387
tree = tree.body
388+
if isinstance(tree, ast.Expr):
389+
tree = tree.value
384390
if not copy or (
385391
isinstance(tree, (ast.Name, ast.Subscript))
386392
and isinstance(tree.ctx, ast.Load)

0 commit comments

Comments
 (0)