1
+ import os
1
2
import random
2
3
import shutil
3
- import os
4
+
4
5
import pytest
6
+
5
7
from dspy .primitives .python_interpreter import InterpreterError , PythonInterpreter
6
8
7
9
# This test suite requires deno to be installed. Please install deno following https://docs.deno.com/runtime/getting_started/installation/
@@ -60,7 +62,7 @@ def test_final_answer_trick():
60
62
61
63
# They should maintain the same order
62
64
assert result == ["The result is" , token ], "The returned results are differ, `final_answer` trick doesn't work"
63
-
65
+
64
66
def test_enable_env_vars_flag ():
65
67
os .environ ["FOO_TEST_ENV" ] = "test_value"
66
68
@@ -84,7 +86,7 @@ def test_read_file_access_control(tmp_path):
84
86
85
87
with PythonInterpreter (enable_read_paths = [str (testfile_path )]) as interpreter :
86
88
code = (
87
- f"with open({ repr ( virtual_path ) } , 'r') as f:\n "
89
+ f"with open({ virtual_path !r } , 'r') as f:\n "
88
90
f" data = f.read()\n "
89
91
f"data"
90
92
)
@@ -94,7 +96,7 @@ def test_read_file_access_control(tmp_path):
94
96
with PythonInterpreter (enable_read_paths = None ) as interpreter :
95
97
code = (
96
98
f"try:\n "
97
- f" with open({ repr ( virtual_path ) } , 'r') as f:\n "
99
+ f" with open({ virtual_path !r } , 'r') as f:\n "
98
100
f" data = f.read()\n "
99
101
f"except Exception as e:\n "
100
102
f" data = str(e)\n "
@@ -110,7 +112,7 @@ def test_enable_write_flag(tmp_path):
110
112
with PythonInterpreter (enable_write_paths = None ) as interpreter :
111
113
code = (
112
114
f"try:\n "
113
- f" with open({ repr ( virtual_path ) } , 'w') as f:\n "
115
+ f" with open({ virtual_path !r } , 'w') as f:\n "
114
116
f" f.write('blocked')\n "
115
117
f" result = 'wrote'\n "
116
118
f"except Exception as e:\n "
@@ -122,27 +124,27 @@ def test_enable_write_flag(tmp_path):
122
124
123
125
with PythonInterpreter (enable_write_paths = [str (testfile_path )]) as interpreter :
124
126
code = (
125
- f"with open({ repr ( virtual_path ) } , 'w') as f:\n "
127
+ f"with open({ virtual_path !r } , 'w') as f:\n "
126
128
f" f.write('allowed')\n "
127
129
f"'ok'"
128
130
)
129
131
result = interpreter .execute (code )
130
132
assert result == "ok" , "Test file should be writable with enable_write_paths"
131
133
assert testfile_path .exists ()
132
- with open (testfile_path , "r" ) as f :
134
+ with open (testfile_path ) as f :
133
135
assert f .read () == "allowed" , "Test file outputs should match content written during execution"
134
-
136
+
135
137
with open (testfile_path , "w" ) as f :
136
138
f .write ("original_content" )
137
139
with PythonInterpreter (enable_write_paths = [str (testfile_path )], sync_files = False ) as interpreter :
138
140
code = (
139
- f"with open({ repr ( virtual_path ) } , 'w') as f:\n "
141
+ f"with open({ virtual_path !r } , 'w') as f:\n "
140
142
f" f.write('should_not_sync')\n "
141
143
f"'done_no_sync'"
142
144
)
143
145
result = interpreter .execute (code )
144
146
assert result == "done_no_sync"
145
- with open (testfile_path , "r" ) as f :
147
+ with open (testfile_path ) as f :
146
148
assert f .read () == "original_content" , "File should not be changed when sync_files is False"
147
149
148
150
@@ -153,7 +155,7 @@ def test_enable_net_flag():
153
155
with PythonInterpreter (enable_network_access = None ) as interpreter :
154
156
code = (
155
157
"import js\n "
156
- f"resp = await js.fetch({ repr ( test_url ) } )\n "
158
+ f"resp = await js.fetch({ test_url !r } )\n "
157
159
"resp.status"
158
160
)
159
161
with pytest .raises (InterpreterError , match = "PythonError" ):
@@ -162,7 +164,7 @@ def test_enable_net_flag():
162
164
with PythonInterpreter (enable_network_access = ["example.com" ]) as interpreter :
163
165
code = (
164
166
"import js\n "
165
- f"resp = await js.fetch({ repr ( test_url ) } )\n "
167
+ f"resp = await js.fetch({ test_url !r } )\n "
166
168
"resp.status"
167
169
)
168
170
result = interpreter .execute (code )
0 commit comments