-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode_test.py
More file actions
executable file
·105 lines (85 loc) · 2.95 KB
/
code_test.py
File metadata and controls
executable file
·105 lines (85 loc) · 2.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#!/usr/bin/env python3
"""
A simplified test script that directly tests the Python code execution functionality.
"""
import os
import sys
from datetime import date
def execute_python(code):
"""Execute Python code safely and return the results."""
import io
from contextlib import redirect_stdout, redirect_stderr
stdout_capture = io.StringIO()
stderr_capture = io.StringIO()
local_vars = {}
try:
with redirect_stdout(stdout_capture), redirect_stderr(stderr_capture):
exec(code, globals(), local_vars)
stdout = stdout_capture.getvalue()
stderr = stderr_capture.getvalue()
return {
"stdout": stdout,
"stderr": stderr,
"success": True
}
except Exception as e:
import traceback
return {
"error": str(e),
"traceback": traceback.format_exc(),
"success": False
}
def extract_python_code(text):
"""Extract Python code from text between <|python_start|> and <|python_end|> tags."""
import re
pattern = r'<\|python_start\|>(.*?)(?:<\|python_end\|>|<\|python_end)'
matches = re.findall(pattern, text, re.DOTALL)
return [match.strip() for match in matches]
def main():
"""Test Python code extraction and execution."""
# Example response with Python code
example_response = """
Here's Python code to print the current date:
<|python_start|>
from datetime import date
def get_current_date():
# Get today's date
today = date.today()
return today
current_date = get_current_date()
print("Current Date: ", current_date)
<|python_end|>
This code imports the date class from the datetime module, defines a function to get the current date, and then prints it.
"""
print("Extracting Python code blocks from example response...")
code_blocks = extract_python_code(example_response)
if not code_blocks:
print("❌ No Python code blocks found!")
return 1
print(f"✅ Found {len(code_blocks)} Python code block(s)")
for i, code in enumerate(code_blocks):
print(f"\nExecuting code block {i+1}:")
print("=" * 40)
print(code)
print("=" * 40)
# Execute the code
result = execute_python(code)
if result["success"]:
print("\nExecution successful!")
if result["stdout"]:
print("\nStandard output:")
print("-" * 40)
print(result["stdout"])
print("-" * 40)
else:
print("No output produced.")
if result["stderr"]:
print("\nStandard error:")
print(result["stderr"])
else:
print("\n❌ Execution failed!")
print(f"Error: {result['error']}")
print(result["traceback"])
return 0
if __name__ == "__main__":
sys.exit(main())