Add strip_fences option and fix exec() scoping for nested functions
- Add strip_fences field to PromptStep for removing markdown code fences - Fix exec() to use same dict for globals and locals, allowing nested functions to access module-level imports - Add GUI checkbox for strip_fences option - Update CLAUDE.md with improved documentation Co-Authored-By: Claude Opus 4.5 <[email protected]>
This commit is contained in:
@@ -296,6 +296,54 @@ class TestExecuteCodeStep:
|
||||
assert success is True
|
||||
assert outputs2["total"] == 15
|
||||
|
||||
def test_nested_function_can_access_imports(self):
|
||||
"""Nested functions should be able to access module-level imports.
|
||||
|
||||
This tests a fix for a scoping bug where exec() with separate
|
||||
globals/locals dicts caused nested functions to fail when accessing
|
||||
imported modules. The fix uses the same dict for both globals and
|
||||
locals so imports are visible to nested function closures.
|
||||
"""
|
||||
step = CodeStep(
|
||||
code="""
|
||||
import re
|
||||
|
||||
def parse_numbers(text):
|
||||
# This should be able to access 're' from the outer scope
|
||||
matches = re.findall(r'\\d+', text)
|
||||
return [int(m) for m in matches]
|
||||
|
||||
result = parse_numbers(input)
|
||||
""",
|
||||
output_var="result"
|
||||
)
|
||||
variables = {"input": "abc 123 def 456 ghi"}
|
||||
|
||||
outputs, success = execute_code_step(step, variables)
|
||||
|
||||
assert success is True
|
||||
assert outputs["result"] == [123, 456]
|
||||
|
||||
def test_nested_function_can_access_outer_variables(self):
|
||||
"""Nested functions should access variables defined in outer scope."""
|
||||
step = CodeStep(
|
||||
code="""
|
||||
multiplier = 10
|
||||
|
||||
def multiply_all(numbers):
|
||||
return [n * multiplier for n in numbers]
|
||||
|
||||
result = multiply_all([1, 2, 3])
|
||||
""",
|
||||
output_var="result"
|
||||
)
|
||||
variables = {"input": ""}
|
||||
|
||||
outputs, success = execute_code_step(step, variables)
|
||||
|
||||
assert success is True
|
||||
assert outputs["result"] == [10, 20, 30]
|
||||
|
||||
|
||||
class TestRunTool:
|
||||
"""Tests for run_tool function."""
|
||||
|
||||
Reference in New Issue
Block a user