pyglove 0.4.5.dev202411132359__py3-none-any.whl → 0.4.5.dev202501250807__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.
Files changed (118) hide show
  1. pyglove/core/__init__.py +40 -21
  2. pyglove/core/coding/__init__.py +42 -0
  3. pyglove/core/coding/errors.py +111 -0
  4. pyglove/core/coding/errors_test.py +98 -0
  5. pyglove/core/coding/execution.py +312 -0
  6. pyglove/core/coding/execution_test.py +333 -0
  7. pyglove/core/{object_utils/codegen.py → coding/function_generation.py} +10 -4
  8. pyglove/core/{object_utils/codegen_test.py → coding/function_generation_test.py} +5 -7
  9. pyglove/core/coding/parsing.py +153 -0
  10. pyglove/core/coding/parsing_test.py +150 -0
  11. pyglove/core/coding/permissions.py +100 -0
  12. pyglove/core/coding/permissions_test.py +93 -0
  13. pyglove/core/geno/base.py +53 -38
  14. pyglove/core/geno/base_test.py +2 -4
  15. pyglove/core/geno/categorical.py +36 -27
  16. pyglove/core/geno/custom.py +18 -15
  17. pyglove/core/geno/numerical.py +19 -16
  18. pyglove/core/geno/space.py +3 -4
  19. pyglove/core/hyper/base.py +6 -6
  20. pyglove/core/hyper/categorical.py +91 -52
  21. pyglove/core/hyper/custom.py +7 -7
  22. pyglove/core/hyper/custom_test.py +9 -10
  23. pyglove/core/hyper/derived.py +30 -22
  24. pyglove/core/hyper/derived_test.py +3 -5
  25. pyglove/core/hyper/dynamic_evaluation.py +3 -4
  26. pyglove/core/hyper/evolvable.py +57 -46
  27. pyglove/core/hyper/numerical.py +48 -24
  28. pyglove/core/hyper/numerical_test.py +9 -9
  29. pyglove/core/hyper/object_template.py +58 -46
  30. pyglove/core/logging_test.py +0 -2
  31. pyglove/core/patching/object_factory.py +4 -4
  32. pyglove/core/patching/pattern_based.py +4 -4
  33. pyglove/core/patching/rule_based.py +4 -3
  34. pyglove/core/symbolic/__init__.py +4 -0
  35. pyglove/core/symbolic/base.py +200 -136
  36. pyglove/core/symbolic/base_test.py +17 -19
  37. pyglove/core/symbolic/boilerplate.py +4 -5
  38. pyglove/core/symbolic/class_wrapper.py +10 -14
  39. pyglove/core/symbolic/class_wrapper_test.py +2 -2
  40. pyglove/core/symbolic/compounding.py +2 -2
  41. pyglove/core/symbolic/compounding_test.py +2 -4
  42. pyglove/core/symbolic/contextual_object.py +288 -0
  43. pyglove/core/symbolic/contextual_object_test.py +327 -0
  44. pyglove/core/symbolic/dict.py +115 -87
  45. pyglove/core/symbolic/dict_test.py +188 -131
  46. pyglove/core/symbolic/diff.py +12 -12
  47. pyglove/core/symbolic/flags.py +1 -1
  48. pyglove/core/symbolic/functor.py +16 -15
  49. pyglove/core/symbolic/functor_test.py +2 -4
  50. pyglove/core/symbolic/inferred.py +2 -2
  51. pyglove/core/symbolic/list.py +70 -47
  52. pyglove/core/symbolic/list_test.py +117 -98
  53. pyglove/core/symbolic/object.py +59 -58
  54. pyglove/core/symbolic/object_test.py +143 -90
  55. pyglove/core/symbolic/origin.py +5 -7
  56. pyglove/core/symbolic/pure_symbolic.py +4 -3
  57. pyglove/core/symbolic/ref.py +33 -16
  58. pyglove/core/symbolic/ref_test.py +17 -0
  59. pyglove/core/tuning/local_backend.py +2 -2
  60. pyglove/core/tuning/protocols.py +3 -3
  61. pyglove/core/typing/annotation_conversion.py +8 -3
  62. pyglove/core/typing/annotation_conversion_test.py +8 -0
  63. pyglove/core/typing/callable_ext.py +11 -13
  64. pyglove/core/typing/callable_signature.py +22 -19
  65. pyglove/core/typing/callable_signature_test.py +3 -5
  66. pyglove/core/typing/class_schema.py +93 -54
  67. pyglove/core/typing/class_schema_test.py +4 -5
  68. pyglove/core/typing/custom_typing.py +5 -4
  69. pyglove/core/typing/key_specs.py +5 -7
  70. pyglove/core/typing/key_specs_test.py +4 -4
  71. pyglove/core/typing/type_conversion.py +4 -5
  72. pyglove/core/typing/type_conversion_test.py +12 -12
  73. pyglove/core/typing/typed_missing.py +6 -7
  74. pyglove/core/typing/typed_missing_test.py +7 -8
  75. pyglove/core/typing/value_specs.py +287 -144
  76. pyglove/core/typing/value_specs_test.py +148 -25
  77. pyglove/core/utils/__init__.py +172 -0
  78. pyglove/core/{object_utils → utils}/common_traits.py +2 -2
  79. pyglove/core/{object_utils → utils}/common_traits_test.py +1 -3
  80. pyglove/core/utils/contextual.py +147 -0
  81. pyglove/core/utils/contextual_test.py +88 -0
  82. pyglove/core/{object_utils → utils}/docstr_utils_test.py +1 -3
  83. pyglove/core/{object_utils → utils}/error_utils.py +3 -3
  84. pyglove/core/{object_utils → utils}/error_utils_test.py +1 -1
  85. pyglove/core/{object_utils → utils}/formatting.py +1 -1
  86. pyglove/core/{object_utils → utils}/formatting_test.py +1 -2
  87. pyglove/core/{object_utils → utils}/hierarchical.py +23 -25
  88. pyglove/core/{object_utils → utils}/hierarchical_test.py +3 -5
  89. pyglove/core/{object_utils → utils}/json_conversion.py +1 -1
  90. pyglove/core/{object_utils → utils}/json_conversion_test.py +1 -3
  91. pyglove/core/{object_utils → utils}/missing.py +2 -2
  92. pyglove/core/{object_utils → utils}/missing_test.py +2 -4
  93. pyglove/core/utils/text_color.py +128 -0
  94. pyglove/core/utils/text_color_test.py +94 -0
  95. pyglove/core/{object_utils → utils}/thread_local_test.py +1 -3
  96. pyglove/core/{object_utils → utils}/timing.py +21 -10
  97. pyglove/core/{object_utils → utils}/timing_test.py +14 -12
  98. pyglove/core/{object_utils → utils}/value_location.py +2 -2
  99. pyglove/core/{object_utils → utils}/value_location_test.py +2 -4
  100. pyglove/core/views/base.py +25 -29
  101. pyglove/core/views/html/base.py +15 -16
  102. pyglove/core/views/html/controls/base.py +46 -9
  103. pyglove/core/views/html/controls/label.py +13 -2
  104. pyglove/core/views/html/controls/label_test.py +27 -8
  105. pyglove/core/views/html/controls/progress_bar.py +3 -5
  106. pyglove/core/views/html/controls/progress_bar_test.py +2 -2
  107. pyglove/core/views/html/controls/tab.py +217 -66
  108. pyglove/core/views/html/controls/tab_test.py +46 -15
  109. pyglove/core/views/html/tree_view.py +39 -37
  110. {pyglove-0.4.5.dev202411132359.dist-info → pyglove-0.4.5.dev202501250807.dist-info}/METADATA +17 -3
  111. pyglove-0.4.5.dev202501250807.dist-info/RECORD +218 -0
  112. {pyglove-0.4.5.dev202411132359.dist-info → pyglove-0.4.5.dev202501250807.dist-info}/WHEEL +1 -1
  113. pyglove/core/object_utils/__init__.py +0 -164
  114. pyglove-0.4.5.dev202411132359.dist-info/RECORD +0 -203
  115. /pyglove/core/{object_utils → utils}/docstr_utils.py +0 -0
  116. /pyglove/core/{object_utils → utils}/thread_local.py +0 -0
  117. {pyglove-0.4.5.dev202411132359.dist-info → pyglove-0.4.5.dev202501250807.dist-info}/LICENSE +0 -0
  118. {pyglove-0.4.5.dev202411132359.dist-info → pyglove-0.4.5.dev202501250807.dist-info}/top_level.txt +0 -0
pyglove/core/__init__.py CHANGED
@@ -37,8 +37,7 @@ Here lists the sub-modules included in the core PyGlove library:
37
37
  |__ tuning : Interface for program tuning with a local backend.
38
38
  |__ detouring : Detouring classes creation without symbolic types.
39
39
  |__ patching : Patching a program with URL-like strings.
40
- |__ object_utils : Utility libary on operating with Python objects.
41
-
40
+ |__ utils : Utility libary on operating with Python objects.
42
41
  """
43
42
 
44
43
  # NOTE(daiyip): We disable bad-import-order to preserve the relation of
@@ -126,6 +125,9 @@ compound_class = symbolic.compound_class
126
125
  # Method for declaring a boilerplated class from a symbolic instance.
127
126
  boilerplate_class = symbolic.boilerplate_class
128
127
 
128
+ # Methods for contextual objects.
129
+ ContextualObject = symbolic.ContextualObject
130
+ contextual_attribute = symbolic.contextual_attribute
129
131
 
130
132
  #
131
133
  # Context manager for swapping wrapped class with their wrappers.
@@ -273,31 +275,42 @@ ObjectFactory = patching.ObjectFactory
273
275
 
274
276
 
275
277
  #
276
- # Symbols from 'object_utils' sub-module.
278
+ # Symbols from 'utils' sub-module.
277
279
  #
278
280
 
279
- from pyglove.core import object_utils
280
- KeyPath = object_utils.KeyPath
281
- KeyPathSet = object_utils.KeyPathSet
282
- MISSING_VALUE = object_utils.MISSING_VALUE
281
+ from pyglove.core import utils
282
+
283
+ # For backward compatibility.
284
+ object_utils = utils
285
+
286
+ KeyPath = utils.KeyPath
287
+ KeyPathSet = utils.KeyPathSet
288
+ MISSING_VALUE = utils.MISSING_VALUE
289
+
290
+ Formattable = utils.Formattable
291
+ repr_format = utils.repr_format
292
+ str_format = utils.str_format
293
+
294
+ MaybePartial = utils.MaybePartial
295
+ JSONConvertible = utils.JSONConvertible
296
+ DocStr = utils.DocStr
283
297
 
284
- Formattable = object_utils.Formattable
285
- repr_format = object_utils.repr_format
286
- str_format = object_utils.str_format
298
+ registered_types = utils.registered_types
299
+ explicit_method_override = utils.explicit_method_override
287
300
 
288
- MaybePartial = object_utils.MaybePartial
289
- JSONConvertible = object_utils.JSONConvertible
290
- DocStr = object_utils.DocStr
301
+ is_partial = utils.is_partial
302
+ format = utils.format # pylint: disable=redefined-builtin
303
+ print = utils.print # pylint: disable=redefined-builtin
304
+ docstr = utils.docstr
305
+ catch_errors = utils.catch_errors
306
+ timeit = utils.timeit
291
307
 
292
- registered_types = object_utils.registered_types
293
- explicit_method_override = object_utils.explicit_method_override
308
+ contextual_override = utils.contextual_override
309
+ with_contextual_override = utils.with_contextual_override
310
+ contextual_value = utils.contextual_value
294
311
 
295
- is_partial = object_utils.is_partial
296
- format = object_utils.format # pylint: disable=redefined-builtin
297
- print = object_utils.print # pylint: disable=redefined-builtin
298
- docstr = object_utils.docstr
299
- catch_errors = object_utils.catch_errors
300
- timeit = object_utils.timeit
312
+ colored = utils.colored
313
+ decolor = utils.decolor
301
314
 
302
315
  # Symbols from 'views' sub-module.
303
316
 
@@ -323,6 +336,12 @@ views.html.controls = controls
323
336
 
324
337
  from pyglove.core import io
325
338
 
339
+ #
340
+ # Symbols from `coding` sub-module.
341
+ #
342
+ #
343
+
344
+ from pyglove.core import coding
326
345
 
327
346
  #
328
347
  # Symbols from `logging.py`.
@@ -0,0 +1,42 @@
1
+ # Copyright 2024 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
+ # pylint: disable=line-too-long
15
+ """Code generation utilities."""
16
+
17
+ # pylint: enable=line-too-long
18
+ # pylint: disable=g-bad-import-order
19
+ # pylint: disable=g-importing-member
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
+ from pyglove.core.coding.permissions import permission
26
+ from pyglove.core.coding.permissions import get_permission
27
+
28
+ from pyglove.core.coding.parsing import parse
29
+
30
+ from pyglove.core.coding.execution import context
31
+ from pyglove.core.coding.execution import get_context
32
+ from pyglove.core.coding.execution import evaluate
33
+ from pyglove.core.coding.execution import sandbox_call
34
+ from pyglove.core.coding.execution import maybe_sandbox_call
35
+ from pyglove.core.coding.execution import run
36
+
37
+ from pyglove.core.coding.function_generation import NO_TYPE_ANNOTATION
38
+ from pyglove.core.coding.function_generation import make_function
39
+
40
+ # pylint: disable=line-too-long
41
+ # pylint: enable=g-bad-import-order
42
+ # pylint: enable=g-importing-member
@@ -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,312 @@
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
+ import queue
22
+ from typing import Any, Callable, Dict, Optional, Union
23
+
24
+ from pyglove.core import utils
25
+ from pyglove.core.coding import errors
26
+ from pyglove.core.coding import parsing
27
+ from pyglove.core.coding import permissions
28
+
29
+
30
+ # Key in returned dict that captures stdout.
31
+ STDOUT_KEY = '__stdout__'
32
+
33
+ # Key in the returned dict that represents the final result.
34
+ RESULT_KEY = '__result__'
35
+ _TLS_CODE_RUN_CONTEXT = '__code_run_context__'
36
+
37
+
38
+ @contextlib.contextmanager
39
+ def context(**kwargs):
40
+ """Context manager to inject symbols for code execution."""
41
+ ctx = get_context()
42
+ ctx.update(kwargs)
43
+ utils.thread_local_push(_TLS_CODE_RUN_CONTEXT, ctx)
44
+
45
+ try:
46
+ yield ctx
47
+ finally:
48
+ utils.thread_local_pop(_TLS_CODE_RUN_CONTEXT)
49
+
50
+
51
+ def get_context() -> Dict[str, Any]:
52
+ """Gets the current context for code execution."""
53
+ context_stack = utils.thread_local_get(_TLS_CODE_RUN_CONTEXT, None)
54
+ return dict(context_stack[-1]) if context_stack else {}
55
+
56
+
57
+ def evaluate(
58
+ code: str,
59
+ *,
60
+ global_vars: Optional[Dict[str, Any]] = None,
61
+ permission: Optional[permissions.CodePermission] = None,
62
+ returns_stdout: bool = False,
63
+ outputs_intermediate: bool = False,
64
+ ) -> Union[Any, Dict[str, Any]]:
65
+ """Executes Python code.
66
+
67
+ Features:
68
+ * Fine-grained execution policy for limiting what APIs could be executed.
69
+ This eliminates the need for sandboxing.
70
+ * It exposes both the final results and intermediate results (variables).
71
+
72
+ Args:
73
+ code: Python code to run.
74
+ global_vars: An optional dict as the globals that could be referenced by the
75
+ code.
76
+ permission: Permission for the Python code to run.
77
+ returns_stdout: If True, the stdout (a str) will be returned.
78
+ outputs_intermediate: Applicable when returns_stdout is False. If True,
79
+ intermediate output will be outputted as a dict, with the last line's
80
+ value accessible by key '__result__' and the std output accessible by
81
+ key '__stdout__'. Otherwise the value of the last line will be returned.
82
+
83
+ Returns:
84
+ The value of the last line of the code block. Or a dict of variable
85
+ names of all locals to their evaluated values as the output of the code to
86
+ run. The value for the last line can be accessed by key '__result__'. Or the
87
+ stdout as a str.
88
+ """
89
+ # Set up the permission and context.
90
+ permission = permission or permissions.get_permission()
91
+ ctx = dict(get_context())
92
+ if global_vars:
93
+ ctx.update(global_vars)
94
+
95
+ # Parse the code str.
96
+ code_block = parsing.parse(code, permission)
97
+ global_vars, orig_global_vars = ctx, ctx.copy()
98
+
99
+ # No code.
100
+ if not code_block.body: # pytype: disable=attribute-error
101
+ return {} if outputs_intermediate else None
102
+
103
+ stdout = io.StringIO()
104
+ with contextlib.redirect_stdout(stdout):
105
+ if hasattr(code_block.body[-1], 'value'): # pytype: disable=attribute-error
106
+ last_expr = code_block.body.pop() # pytype: disable=attribute-error
107
+ result_vars = [RESULT_KEY]
108
+
109
+ if isinstance(last_expr, ast.Assign):
110
+ for name_node in last_expr.targets:
111
+ result_vars.append(name_node.id)
112
+
113
+ last_expr = ast.Expression(last_expr.value) # pytype: disable=attribute-error
114
+
115
+ try:
116
+ # Execute the lines before the last expression.
117
+ # NOTE(daiyip): Only a `globals` dict is specified here, which will also
118
+ # be used to output intermediate values by `exec`. We do not specify a
119
+ # separate `locals` dict here, for - "If exec gets two separate objects
120
+ # as globals and locals, the code will be executed as if it were
121
+ # embedded in a class definition." - as the Python document explains.
122
+ # The outcome is that new functions defined in the code block could not
123
+ # be called by other newly defined functions.
124
+ # Refer to https://stackoverflow.com/questions/
125
+ # 73940751/why-cant-i-call-a-function-from-another-function-using-exec
126
+ # for more details.
127
+ exec(compile(code_block, '', mode='exec'), global_vars) # pylint: disable=exec-used
128
+
129
+ # Evaluate the last expression.
130
+ result = eval( # pylint: disable=eval-used
131
+ compile(last_expr, '', mode='eval'), global_vars
132
+ )
133
+ except Exception as e:
134
+ raise errors.CodeError(code, e) from e
135
+
136
+ for result_var in result_vars:
137
+ global_vars[result_var] = result
138
+ else:
139
+ try:
140
+ exec(compile(code_block, '', mode='exec'), global_vars) # pylint: disable=exec-used
141
+ except Exception as e:
142
+ raise errors.CodeError(code, e) from e
143
+ global_vars[RESULT_KEY] = list(global_vars.values())[-1]
144
+
145
+ if returns_stdout:
146
+ return stdout.getvalue()
147
+ if outputs_intermediate:
148
+ outputs = {}
149
+ for k, v in global_vars.items():
150
+ if k == '__builtins__':
151
+ continue
152
+ if k not in orig_global_vars or v is not orig_global_vars[k]:
153
+ outputs[k] = v
154
+ # Add stdout to outputs.
155
+ outputs[STDOUT_KEY] = stdout.getvalue()
156
+ return outputs
157
+ return global_vars[RESULT_KEY]
158
+
159
+
160
+ def sandbox_call(
161
+ func: Callable[..., Any],
162
+ *args,
163
+ timeout: Optional[float] = None,
164
+ **kwargs) -> Any:
165
+ """Calls a function with sandboxing.
166
+
167
+ Args:
168
+ func: Function to call.
169
+ *args: Positional arguments for `func`
170
+ timeout: Execution timeout in seconds. If None, wait `func` to complete.
171
+ **kwargs: Keyword arguments for `func`.
172
+
173
+ Returns:
174
+ Return value from `func`.
175
+
176
+ Raises:
177
+ TimeoutError: If the execution time exceeds the timeout.
178
+ Exception: Exception raised from `func`.
179
+ """
180
+ def _call(q, *args, **kwargs):
181
+ # NOTE(daiyip): if `q` is closed by the main process when `q.put` is called
182
+ # on a subprocess, ValueError will be raised. This is okay since the main
183
+ # process is no longer waiting for the result, and the subprocess could
184
+ # recycled with non-zero error code, which does not affect the main
185
+ # process.
186
+ def _run():
187
+ r = func(*args, **kwargs)
188
+ try:
189
+ return pickle.dumps(r)
190
+ except Exception as e:
191
+ raise errors.SerializationError(
192
+ f'Cannot serialize sandbox result: {r}', e
193
+ ) from e
194
+
195
+ try:
196
+ q.put(_run())
197
+ except Exception as e: # pylint: disable=broad-exception-caught
198
+ q.put(e)
199
+
200
+ q = multiprocessing.Queue()
201
+ p = multiprocessing.Process(
202
+ target=_call, args=tuple([q] + list(args)), kwargs=kwargs
203
+ )
204
+ try:
205
+ p.start()
206
+ x = q.get(timeout=timeout)
207
+ except queue.Empty as e:
208
+ if p.is_alive():
209
+ # We use `kill` instead of `terminate` to release process resources
210
+ # right away.
211
+ p.kill()
212
+ raise TimeoutError(f'Execution time exceed {timeout} seconds.') from e
213
+ finally:
214
+ q.close()
215
+
216
+ if isinstance(x, Exception):
217
+ raise x
218
+ try:
219
+ return pickle.loads(x)
220
+ except Exception as e:
221
+ raise errors.SerializationError(
222
+ 'Cannot deserialize the output from sandbox.', e
223
+ ) from e
224
+
225
+
226
+ def maybe_sandbox_call(
227
+ func: Callable[..., Any],
228
+ *args,
229
+ sandbox: Optional[bool] = None,
230
+ timeout: Optional[float] = None,
231
+ **kwargs
232
+ ) -> Any:
233
+ """Maybe calls a function with sandboxing.
234
+
235
+ Args:
236
+ func: Function to call.
237
+ *args: Postional args that will be passed to `func`.
238
+ sandbox: If True, run code in sandbox; If False, run code in current
239
+ process. If None, run in sandbox first, if the output could not be
240
+ serialized and pass to current process, run the code again in current
241
+ process.
242
+ timeout: Execution timeout in seconds. If None, wait the code the complete.
243
+ **kwargs: Keyword args that will be passed to `func`.
244
+
245
+ Returns:
246
+ The return value of `func`.
247
+
248
+ Raises:
249
+ TimeoutError: If the execution time exceeds the timeout.
250
+ Exception: Exception that are raised from `func`.
251
+ """
252
+ if sandbox is None:
253
+ try:
254
+ return sandbox_call(func, *args, timeout=timeout, **kwargs)
255
+ # NOTE(daiyip): output could be serialized across processes, giving it
256
+ # already finishes on sandbox, so it should be much safer to run under
257
+ # current process.
258
+ except errors.SerializationError:
259
+ return func(*args, **kwargs)
260
+ elif sandbox:
261
+ return sandbox_call(func, *args, timeout=timeout, **kwargs)
262
+ else:
263
+ return func(*args, **kwargs)
264
+
265
+
266
+ def run(
267
+ code: str,
268
+ *,
269
+ global_vars: Optional[Dict[str, Any]] = None,
270
+ permission: Optional[permissions.CodePermission] = None,
271
+ returns_stdout: bool = False,
272
+ outputs_intermediate: bool = False,
273
+ sandbox: Optional[bool] = None,
274
+ timeout: Optional[float] = None,
275
+ ) -> Union[Any, Dict[str, Any]]:
276
+ """Executes Python code.
277
+
278
+ Features:
279
+ * Fine-grained execution policy for limiting what APIs could be executed.
280
+ This eliminates the need for sandboxing.
281
+ * It exposes both the final results and intermediate results (variables).
282
+
283
+ Args:
284
+ code: Python code to run.
285
+ global_vars: An optional dict of
286
+ permission: Permission for the Python code to run.
287
+ returns_stdout: If True, the stdout (a str) will be returned.
288
+ outputs_intermediate: Applicable when returns_stdout is False. If True,
289
+ intermediate output will be outputted as a dict, with the last line's
290
+ value accessible by key '__result__' and the std output accessible by
291
+ key '__stdout__'. Otherwise the value of the last line will be returned.
292
+ sandbox: If True, run code in sandbox; If False, run code in current
293
+ process. If None, run in sandbox first, if the output could not be
294
+ serialized and pass to current process, run the code again in current
295
+ process.
296
+ timeout: Execution timeout in seconds. If None, wait the code the complete.
297
+
298
+ Returns:
299
+ The value of the last line of the code block. Or a dict of variable
300
+ names of all locals to their evaluated values as the output of the code to
301
+ run. The value for the last line can be accessed by key '__result__'. Or the
302
+ stdout as a str.
303
+
304
+ Raises:
305
+ TimeoutError: If the execution time exceeds the timeout.
306
+ Exception: Exception that are raised from the code.
307
+ """
308
+ return maybe_sandbox_call(
309
+ evaluate, code=code, global_vars=global_vars, permission=permission,
310
+ returns_stdout=returns_stdout, outputs_intermediate=outputs_intermediate,
311
+ sandbox=sandbox, timeout=timeout
312
+ )