pygx.coding¶
Code generation utilities.
coding
¶
Code generation utilities.
pygx.coding provides a small toolkit for compiling and executing Python
source code at runtime under controlled conditions. It is used internally by
PyGX to materialize generated programs, but is exposed as a public sub-package
because the building blocks are useful on their own:
parse/evaluate/runfor compiling and executing Python source.make_functionfor synthesizing a callable from a source string.permission/get_permissionfor declaring which language features (assignment, function definition, import) the executed source may use.sandbox_call/maybe_sandbox_callfor executing a callable in a child process with a timeout.CodeError/SerializationErrorfor errors raised by the above APIs.
CodeError
¶
Bases: RuntimeError
Python code error.
Source code in pygx/coding/_errors.py
code_lines
¶
format
¶
Formats the code error.
Source code in pygx/coding/_errors.py
SerializationError
¶
CodePermission
¶
Bases: Flag
Permissions for code execution.
context
¶
Context manager to inject symbols for code execution.
Source code in pygx/coding/_execution.py
evaluate
¶
evaluate(
code: str,
*,
global_vars: dict[str, Any] | None = None,
permission: CodePermission | None = None,
returns_stdout: bool = False,
outputs_intermediate: bool = False
) -> Any | dict[str, Any]
Executes Python code.
Features
- Fine-grained execution policy for limiting what APIs could be executed. This eliminates the need for sandboxing.
- It exposes both the final results and intermediate results (variables).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
code
|
str
|
Python code to run. |
required |
global_vars
|
dict[str, Any] | None
|
An optional dict as the globals that could be referenced by the code. |
None
|
permission
|
CodePermission | None
|
Permission for the Python code to run. |
None
|
returns_stdout
|
bool
|
If True, the stdout (a str) will be returned. |
False
|
outputs_intermediate
|
bool
|
Applicable when returns_stdout is False. If True, intermediate output will be outputted as a dict, with the last line's value accessible by key 'result' and the std output accessible by key 'stdout'. Otherwise the value of the last line will be returned. |
False
|
Returns:
| Type | Description |
|---|---|
Any | dict[str, Any]
|
The value of the last line of the code block. Or a dict of variable |
Any | dict[str, Any]
|
names of all locals to their evaluated values as the output of the code to |
Any | dict[str, Any]
|
run. The value for the last line can be accessed by key 'result'. Or the |
Any | dict[str, Any]
|
stdout as a str. |
Source code in pygx/coding/_execution.py
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 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 | |
get_context
¶
Gets the current context for code execution.
maybe_sandbox_call
¶
maybe_sandbox_call(
func: Callable[..., Any],
*args: Any,
sandbox: bool | None = None,
timeout: float | None = None,
**kwargs: Any
) -> Any
Maybe calls a function with sandboxing.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
func
|
Callable[..., Any]
|
Function to call. |
required |
*args
|
Any
|
Postional args that will be passed to |
()
|
sandbox
|
bool | None
|
If True, run code in sandbox; If False, run code in current process. If None, run in sandbox first, if the output could not be serialized and pass to current process, run the code again in current process. |
None
|
timeout
|
float | None
|
Execution timeout in seconds. If None, wait the code the complete. |
None
|
**kwargs
|
Any
|
Keyword args that will be passed to |
{}
|
Returns:
| Type | Description |
|---|---|
Any
|
The return value of |
Raises:
| Type | Description |
|---|---|
TimeoutError
|
If the execution time exceeds the timeout. |
Exception
|
Exception that are raised from |
Source code in pygx/coding/_execution.py
run
¶
run(
code: str,
*,
global_vars: dict[str, Any] | None = None,
permission: CodePermission | None = None,
returns_stdout: bool = False,
outputs_intermediate: bool = False,
sandbox: bool | None = None,
timeout: float | None = None
) -> Any | dict[str, Any]
Executes Python code.
Features
- Fine-grained execution policy for limiting what APIs could be executed. This eliminates the need for sandboxing.
- It exposes both the final results and intermediate results (variables).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
code
|
str
|
Python code to run. |
required |
global_vars
|
dict[str, Any] | None
|
An optional dict of |
None
|
permission
|
CodePermission | None
|
Permission for the Python code to run. |
None
|
returns_stdout
|
bool
|
If True, the stdout (a str) will be returned. |
False
|
outputs_intermediate
|
bool
|
Applicable when returns_stdout is False. If True, intermediate output will be outputted as a dict, with the last line's value accessible by key 'result' and the std output accessible by key 'stdout'. Otherwise the value of the last line will be returned. |
False
|
sandbox
|
bool | None
|
If True, run code in sandbox; If False, run code in current process. If None, run in sandbox first, if the output could not be serialized and pass to current process, run the code again in current process. |
None
|
timeout
|
float | None
|
Execution timeout in seconds. If None, wait the code the complete. |
None
|
Returns:
| Type | Description |
|---|---|
Any | dict[str, Any]
|
The value of the last line of the code block. Or a dict of variable |
Any | dict[str, Any]
|
names of all locals to their evaluated values as the output of the code to |
Any | dict[str, Any]
|
run. The value for the last line can be accessed by key 'result'. Or the |
Any | dict[str, Any]
|
stdout as a str. |
Raises:
| Type | Description |
|---|---|
TimeoutError
|
If the execution time exceeds the timeout. |
Exception
|
Exception that are raised from the code. |
Source code in pygx/coding/_execution.py
sandbox_call
¶
sandbox_call(
func: Callable[..., Any],
*args: Any,
timeout: float | None = None,
**kwargs: Any
) -> Any
Calls a function with sandboxing.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
func
|
Callable[..., Any]
|
Function to call. |
required |
*args
|
Any
|
Positional arguments for |
()
|
timeout
|
float | None
|
Execution timeout in seconds. If None, wait |
None
|
**kwargs
|
Any
|
Keyword arguments for |
{}
|
Returns:
| Type | Description |
|---|---|
Any
|
Return value from |
Raises:
| Type | Description |
|---|---|
TimeoutError
|
If the execution time exceeds the timeout. |
SerializationError
|
If |
Exception
|
Exception raised from |
Source code in pygx/coding/_execution.py
make_function
¶
make_function(
name: str,
args: list[str],
body: list[str],
*,
exec_globals: dict[str, Any] | None = None,
exec_locals: dict[str, Any] | None = None,
return_type: Any = NO_TYPE_ANNOTATION
)
Creates a function dynamically from source.
Source code in pygx/coding/_function_generation.py
get_permission
¶
get_permission() -> CodePermission | None
permission
¶
permission(perm: CodePermission) -> Iterator[CodePermission]
Context manager for controling the permission for code execution.
When the permission context manager is nested, the outtermost permission
will be used. This design allows users to control permission at the top level.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
perm
|
CodePermission
|
Code execution permission. |
required |
Yields:
| Type | Description |
|---|---|
CodePermission
|
Actual permission applied. |
Source code in pygx/coding/_permissions.py
options: show_root_heading: false show_root_toc_entry: false members_order: source heading_level: 2