pyglove 0.4.5.dev202501060809__py3-none-any.whl → 0.4.5.dev202501110808__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
pyglove/core/__init__.py CHANGED
@@ -302,6 +302,9 @@ docstr = utils.docstr
302
302
  catch_errors = utils.catch_errors
303
303
  timeit = utils.timeit
304
304
 
305
+ colored = utils.colored
306
+ decolor = utils.decolor
307
+
305
308
  # Symbols from 'views' sub-module.
306
309
 
307
310
  from pyglove.core import views
@@ -18,6 +18,19 @@
18
18
  # pylint: disable=g-bad-import-order
19
19
  # pylint: disable=g-importing-member
20
20
 
21
+ from pyglove.core.coding.errors import CodeError
22
+ from pyglove.core.coding.errors import SerializationError
23
+
24
+ from pyglove.core.coding.permissions import CodePermission
25
+
26
+ from pyglove.core.coding.parsing import parse
27
+
28
+ from pyglove.core.coding.execution import context
29
+ from pyglove.core.coding.execution import get_context
30
+ from pyglove.core.coding.execution import evaluate
31
+ from pyglove.core.coding.execution import sandbox_call
32
+ from pyglove.core.coding.execution import run
33
+
21
34
  from pyglove.core.coding.function_generation import NO_TYPE_ANNOTATION
22
35
  from pyglove.core.coding.function_generation import make_function
23
36
 
@@ -0,0 +1,111 @@
1
+ # Copyright 2025 The PyGlove Authors
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+ """Python code errors."""
15
+
16
+ import io
17
+ import sys
18
+ import textwrap
19
+ import traceback
20
+ from typing import Optional
21
+
22
+ from pyglove.core import utils
23
+
24
+
25
+ class CodeError(RuntimeError):
26
+ """Python code error."""
27
+
28
+ def __init__(
29
+ self,
30
+ code: str,
31
+ cause: Exception,
32
+ ):
33
+ self.code = code
34
+ self.cause = cause
35
+
36
+ # Figure out the starting and ending line numbers of the erratic code.
37
+ lineno = None
38
+ end_lineno = None
39
+ if isinstance(cause, SyntaxError):
40
+ lineno = cause.lineno
41
+ # For Python 3.9 and below, `end_lineno` is not available.
42
+ end_lineno = getattr(cause, 'end_lineno', lineno)
43
+ elif not isinstance(cause, TimeoutError):
44
+ tb = sys.exc_info()[2]
45
+ frames = traceback.extract_tb(tb, limit=5)
46
+ for f in frames:
47
+ if not f.filename or f.filename == '<string>':
48
+ lineno = f.lineno
49
+ end_lineno = lineno
50
+ break
51
+ self.lineno = lineno
52
+ self.end_lineno = end_lineno
53
+
54
+ def __str__(self):
55
+ return self.format(include_complete_code=True)
56
+
57
+ def code_lines(self, start_line: int, end_line: int):
58
+ """Returns code lines ."""
59
+ return '\n'.join(self.code.split('\n')[start_line:end_line])
60
+
61
+ def format(self, include_complete_code: bool = True):
62
+ """Formats the code error."""
63
+ r = io.StringIO()
64
+ error_message = str(self.cause).rstrip()
65
+ if 'line' not in error_message and self.lineno is not None:
66
+ error_message += f' (<unknown>, line {self.lineno})'
67
+ r.write(
68
+ utils.colored(
69
+ f'{self.cause.__class__.__name__}: {error_message}', 'magenta'))
70
+
71
+ if self.lineno is not None:
72
+ r.write('\n\n')
73
+ r.write(textwrap.indent(
74
+ utils.colored(
75
+ self.code_lines(self.lineno - 1, self.end_lineno), 'magenta'),
76
+ ' ' * 2
77
+ ))
78
+ r.write('\n')
79
+
80
+ if include_complete_code:
81
+ r.write('\n')
82
+ r.write(utils.colored('[Code]', 'green', styles=['bold']))
83
+ r.write('\n\n')
84
+ r.write(utils.colored(' ```python\n', 'green'))
85
+ r.write(textwrap.indent(
86
+ utils.colored(self.code, 'green'),
87
+ ' ' * 2
88
+ ))
89
+ r.write(utils.colored('\n ```\n', 'green'))
90
+ return r.getvalue()
91
+
92
+
93
+ class SerializationError(RuntimeError):
94
+ """Object serialization error."""
95
+
96
+ def __init__(self, message: Optional[str], cause: Exception):
97
+ self.message = message
98
+ self.cause = cause
99
+
100
+ def __str__(self):
101
+ r = io.StringIO()
102
+ cause_message = str(self.cause).rstrip()
103
+ if self.message:
104
+ r.write(utils.colored(self.message, 'magenta'))
105
+ r.write('\n\n')
106
+ r.write(
107
+ utils.colored(
108
+ f'{self.cause.__class__.__name__}: {cause_message}', 'magenta'
109
+ )
110
+ )
111
+ return r.getvalue()
@@ -0,0 +1,98 @@
1
+ # Copyright 2025 The PyGlove Authors
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+ import inspect
15
+ import unittest
16
+
17
+ from pyglove.core.coding import errors
18
+ from pyglove.core.coding import execution
19
+
20
+
21
+ def code_error(code: str) -> errors.CodeError:
22
+ try:
23
+ execution.run(inspect.cleandoc(code), timeout=2)
24
+ assert False, 'should not reach here'
25
+ except errors.CodeError as e:
26
+ return e
27
+
28
+
29
+ class CodeErrorsTest(unittest.TestCase):
30
+
31
+ def test_format(self):
32
+ e = code_error(
33
+ """
34
+ x = y + 1
35
+ """
36
+ )
37
+ self.assertIn('[Code]', str(e))
38
+ self.assertNotIn(
39
+ '[Code]', e.format(include_complete_code=False))
40
+
41
+ def test_lineno(self):
42
+ self.assertEqual(
43
+ code_error(
44
+ """
45
+ x = y + 1
46
+ """
47
+ ).lineno, 1)
48
+ self.assertEqual(
49
+ code_error(
50
+ """
51
+ x = 1
52
+ for i of x:
53
+ y = i
54
+ """
55
+ ).lineno, 2)
56
+ self.assertEqual(
57
+ code_error(
58
+ """
59
+ x = 1
60
+ y = 2
61
+ raise ValueError
62
+ """
63
+ ).lineno, 3)
64
+
65
+ def test_lineno_in_error_message(self):
66
+ def assert_lineno(code):
67
+ e = code_error(code)
68
+ self.assertIn('line', e.format(include_complete_code=False))
69
+
70
+ assert_lineno(
71
+ """
72
+ x = y + 1
73
+ """
74
+ )
75
+ assert_lineno(
76
+ """
77
+ x = 1
78
+ y = 2
79
+ """
80
+ )
81
+ assert_lineno(
82
+ """
83
+ raise ValueError()
84
+ """
85
+ )
86
+
87
+
88
+ class SerializationErrorTest(unittest.TestCase):
89
+
90
+ def test_str(self):
91
+ e = errors.SerializationError(
92
+ 'Output cannot be serialized.', ValueError('abc'))
93
+ self.assertIn('Output cannot be serialized', str(e))
94
+ self.assertIn('ValueError: abc', str(e))
95
+
96
+
97
+ if __name__ == '__main__':
98
+ unittest.main()
@@ -0,0 +1,309 @@
1
+ # Copyright 2025 The PyGlove Authors
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+ """Python code execution."""
15
+
16
+ import ast
17
+ import contextlib
18
+ import io
19
+ import multiprocessing
20
+ import pickle
21
+ from typing import Any, Callable, Dict, Optional, Union
22
+
23
+ from pyglove.core import utils
24
+ from pyglove.core.coding import errors
25
+ from pyglove.core.coding import parsing
26
+ from pyglove.core.coding import permissions
27
+
28
+
29
+ # Key in returned dict that captures stdout.
30
+ STDOUT_KEY = '__stdout__'
31
+
32
+ # Key in the returned dict that represents the final result.
33
+ RESULT_KEY = '__result__'
34
+ _TLS_CODE_RUN_CONTEXT = '__code_run_context__'
35
+
36
+
37
+ @contextlib.contextmanager
38
+ def context(**kwargs):
39
+ """Context manager to inject symbols for code execution."""
40
+ ctx = get_context()
41
+ ctx.update(kwargs)
42
+ utils.thread_local_push(_TLS_CODE_RUN_CONTEXT, ctx)
43
+
44
+ try:
45
+ yield ctx
46
+ finally:
47
+ utils.thread_local_pop(_TLS_CODE_RUN_CONTEXT)
48
+
49
+
50
+ def get_context() -> Dict[str, Any]:
51
+ """Gets the current context for code execution."""
52
+ context_stack = utils.thread_local_get(_TLS_CODE_RUN_CONTEXT, None)
53
+ return dict(context_stack[-1]) if context_stack else {}
54
+
55
+
56
+ def evaluate(
57
+ code: str,
58
+ *,
59
+ global_vars: Optional[Dict[str, Any]] = None,
60
+ permission: Optional[permissions.CodePermission] = None,
61
+ returns_stdout: bool = False,
62
+ outputs_intermediate: bool = False,
63
+ ) -> Union[Any, Dict[str, Any]]:
64
+ """Executes Python code.
65
+
66
+ Features:
67
+ * Fine-grained execution policy for limiting what APIs could be executed.
68
+ This eliminates the need for sandboxing.
69
+ * It exposes both the final results and intermediate results (variables).
70
+
71
+ Args:
72
+ code: Python code to run.
73
+ global_vars: An optional dict as the globals that could be referenced by the
74
+ code.
75
+ permission: Permission for the Python code to run.
76
+ returns_stdout: If True, the stdout (a str) will be returned.
77
+ outputs_intermediate: Applicable when returns_stdout is False. If True,
78
+ intermediate output will be outputted as a dict, with the last line's
79
+ value accessible by key '__result__' and the std output accessible by
80
+ key '__stdout__'. Otherwise the value of the last line will be returned.
81
+
82
+ Returns:
83
+ The value of the last line of the code block. Or a dict of variable
84
+ names of all locals to their evaluated values as the output of the code to
85
+ run. The value for the last line can be accessed by key '__result__'. Or the
86
+ stdout as a str.
87
+ """
88
+ # Set up the permission and context.
89
+ permission = permission or permissions.get_permission()
90
+ ctx = dict(get_context())
91
+ if global_vars:
92
+ ctx.update(global_vars)
93
+
94
+ # Parse the code str.
95
+ code_block = parsing.parse(code, permission)
96
+ global_vars, orig_global_vars = ctx, ctx.copy()
97
+
98
+ # No code.
99
+ if not code_block.body: # pytype: disable=attribute-error
100
+ return {} if outputs_intermediate else None
101
+
102
+ stdout = io.StringIO()
103
+ with contextlib.redirect_stdout(stdout):
104
+ if hasattr(code_block.body[-1], 'value'): # pytype: disable=attribute-error
105
+ last_expr = code_block.body.pop() # pytype: disable=attribute-error
106
+ result_vars = [RESULT_KEY]
107
+
108
+ if isinstance(last_expr, ast.Assign):
109
+ for name_node in last_expr.targets:
110
+ result_vars.append(name_node.id)
111
+
112
+ last_expr = ast.Expression(last_expr.value) # pytype: disable=attribute-error
113
+
114
+ try:
115
+ # Execute the lines before the last expression.
116
+ # NOTE(daiyip): Only a `globals` dict is specified here, which will also
117
+ # be used to output intermediate values by `exec`. We do not specify a
118
+ # separate `locals` dict here, for - "If exec gets two separate objects
119
+ # as globals and locals, the code will be executed as if it were
120
+ # embedded in a class definition." - as the Python document explains.
121
+ # The outcome is that new functions defined in the code block could not
122
+ # be called by other newly defined functions.
123
+ # Refer to https://stackoverflow.com/questions/
124
+ # 73940751/why-cant-i-call-a-function-from-another-function-using-exec
125
+ # for more details.
126
+ exec(compile(code_block, '', mode='exec'), global_vars) # pylint: disable=exec-used
127
+
128
+ # Evaluate the last expression.
129
+ result = eval( # pylint: disable=eval-used
130
+ compile(last_expr, '', mode='eval'), global_vars
131
+ )
132
+ except Exception as e:
133
+ raise errors.CodeError(code, e) from e
134
+
135
+ for result_var in result_vars:
136
+ global_vars[result_var] = result
137
+ else:
138
+ try:
139
+ exec(compile(code_block, '', mode='exec'), global_vars) # pylint: disable=exec-used
140
+ except Exception as e:
141
+ raise errors.CodeError(code, e) from e
142
+ global_vars[RESULT_KEY] = list(global_vars.values())[-1]
143
+
144
+ if returns_stdout:
145
+ return stdout.getvalue()
146
+ if outputs_intermediate:
147
+ outputs = {}
148
+ for k, v in global_vars.items():
149
+ if k == '__builtins__':
150
+ continue
151
+ if k not in orig_global_vars or v is not orig_global_vars[k]:
152
+ outputs[k] = v
153
+ # Add stdout to outputs.
154
+ outputs[STDOUT_KEY] = stdout.getvalue()
155
+ return outputs
156
+ return global_vars[RESULT_KEY]
157
+
158
+
159
+ def sandbox_call(
160
+ func: Callable[..., Any],
161
+ *args,
162
+ timeout: Optional[float] = None,
163
+ **kwargs) -> Any:
164
+ """Calls a function with sandboxing.
165
+
166
+ Args:
167
+ func: Function to call.
168
+ *args: Positional arguments for `func`
169
+ timeout: Execution timeout in seconds. If None, wait `func` to complete.
170
+ **kwargs: Keyword arguments for `func`.
171
+
172
+ Returns:
173
+ Return value from `func`.
174
+
175
+ Raises:
176
+ TimeoutError: If the execution time exceeds the timeout.
177
+ Exception: Exception raised from `func`.
178
+ """
179
+ def _call(q, *args, **kwargs):
180
+ # NOTE(daiyip): if `q` is closed by the main process when `q.put` is called
181
+ # on a subprocess, ValueError will be raised. This is okay since the main
182
+ # process is no longer waiting for the result, and the subprocess could
183
+ # recycled with non-zero error code, which does not affect the main
184
+ # process.
185
+ def _run():
186
+ r = func(*args, **kwargs)
187
+ try:
188
+ return pickle.dumps(r)
189
+ except Exception as e:
190
+ raise errors.SerializationError(
191
+ f'Cannot serialize sandbox result: {r}', e
192
+ ) from e
193
+
194
+ try:
195
+ q.put(_run())
196
+ except Exception as e: # pylint: disable=broad-exception-caught
197
+ q.put(e)
198
+
199
+ q = multiprocessing.Queue()
200
+ try:
201
+ p = multiprocessing.Process(
202
+ target=_call, args=tuple([q] + list(args)), kwargs=kwargs)
203
+ p.start()
204
+ p.join(timeout=timeout)
205
+ if p.is_alive():
206
+ # We use `kill` instead of `terminate` to release process resources
207
+ # right away.
208
+ p.kill()
209
+ raise TimeoutError(f'Execution time exceed {timeout} seconds.')
210
+ x = q.get()
211
+ if isinstance(x, Exception):
212
+ raise x
213
+ try:
214
+ return pickle.loads(x)
215
+ except Exception as e:
216
+ raise errors.SerializationError(
217
+ 'Cannot deserialize the output from sandbox.', e
218
+ ) from e
219
+ finally:
220
+ q.close()
221
+
222
+
223
+ def _maybe_call_in_sandbox(
224
+ func: Callable[..., Any],
225
+ *args,
226
+ sandbox: Optional[bool] = None,
227
+ timeout: Optional[float] = None,
228
+ **kwargs
229
+ ) -> Any:
230
+ """Calls a function with sandbox support.
231
+
232
+ Args:
233
+ func: Function to call.
234
+ *args: Postional args that will be passed to `func`.
235
+ sandbox: If True, run code in sandbox; If False, run code in current
236
+ process. If None, run in sandbox first, if the output could not be
237
+ serialized and pass to current process, run the code again in current
238
+ process.
239
+ timeout: Execution timeout in seconds. If None, wait the code the complete.
240
+ **kwargs: Keyword args that will be passed to `func`.
241
+
242
+ Returns:
243
+ The return value of `func`.
244
+
245
+ Raises:
246
+ TimeoutError: If the execution time exceeds the timeout.
247
+ Exception: Exception that are raised from `func`.
248
+ """
249
+ if sandbox is None:
250
+ try:
251
+ return sandbox_call(func, *args, timeout=timeout, **kwargs)
252
+ # NOTE(daiyip): output could be serialized across processes, giving it
253
+ # already finishes on sandbox, so it should be much safer to run under
254
+ # current process.
255
+ except errors.SerializationError:
256
+ return func(*args, **kwargs)
257
+ elif sandbox:
258
+ return sandbox_call(func, *args, timeout=timeout, **kwargs)
259
+ else:
260
+ return func(*args, **kwargs)
261
+
262
+
263
+ def run(
264
+ code: str,
265
+ *,
266
+ global_vars: Optional[Dict[str, Any]] = None,
267
+ permission: Optional[permissions.CodePermission] = None,
268
+ returns_stdout: bool = False,
269
+ outputs_intermediate: bool = False,
270
+ sandbox: Optional[bool] = None,
271
+ timeout: Optional[float] = None,
272
+ ) -> Union[Any, Dict[str, Any]]:
273
+ """Executes Python code.
274
+
275
+ Features:
276
+ * Fine-grained execution policy for limiting what APIs could be executed.
277
+ This eliminates the need for sandboxing.
278
+ * It exposes both the final results and intermediate results (variables).
279
+
280
+ Args:
281
+ code: Python code to run.
282
+ global_vars: An optional dict of
283
+ permission: Permission for the Python code to run.
284
+ returns_stdout: If True, the stdout (a str) will be returned.
285
+ outputs_intermediate: Applicable when returns_stdout is False. If True,
286
+ intermediate output will be outputted as a dict, with the last line's
287
+ value accessible by key '__result__' and the std output accessible by
288
+ key '__stdout__'. Otherwise the value of the last line will be returned.
289
+ sandbox: If True, run code in sandbox; If False, run code in current
290
+ process. If None, run in sandbox first, if the output could not be
291
+ serialized and pass to current process, run the code again in current
292
+ process.
293
+ timeout: Execution timeout in seconds. If None, wait the code the complete.
294
+
295
+ Returns:
296
+ The value of the last line of the code block. Or a dict of variable
297
+ names of all locals to their evaluated values as the output of the code to
298
+ run. The value for the last line can be accessed by key '__result__'. Or the
299
+ stdout as a str.
300
+
301
+ Raises:
302
+ TimeoutError: If the execution time exceeds the timeout.
303
+ Exception: Exception that are raised from the code.
304
+ """
305
+ return _maybe_call_in_sandbox(
306
+ evaluate, code=code, global_vars=global_vars, permission=permission,
307
+ returns_stdout=returns_stdout, outputs_intermediate=outputs_intermediate,
308
+ sandbox=sandbox, timeout=timeout
309
+ )