cillow.Interpreter
Create an interpreter from a given python environment.
Examples:
>>> import cillow
>>>
>>> interpreter = cillow.Interpreter()
>>>
>>> interpreter.run_code("x = 1; x + 1")
Result(value=2)
>>>
>>> interpreter.install_requirements(["package-name"])
Don't trust LLMS? Concerned about arbitrary code execution? Take full control by limiting functionalities using patches.
To add patches, use the add_patches()
function. To clear patches, use clear_patches()
.
Examples:
>>> import cillow
>>>
>>> import os
>>> from contextlib import contextmanager
>>>
>>> os_system_switchable = cillow.Switchable(os.system)
>>>
>>> @contextmanager
... def patch_os_system():
... def disabled_os_system(command: str):
... return "os.system has been disabled."
...
... with os_system_switchable.switch_to(disabled_os_system):
... yield
...
>>> cillow.add_patches(patch_os_system) # Disable os.system
>>>
>>> interpreter = cillow.Interpreter()
>>> interpreter.run_code("import os;os.system('echo Hi')")
Result(value='os.system has been disabled.')
Source code in cillow/interpreter.py
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 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 170 171 172 173 174 |
|
environment
property
¶
The current Python environment
install_requirements
¶
Install the given requirements.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
requirements
|
str
|
The requirements to install |
()
|
on_stream
|
Callable[[Stream], None] | None
|
The callback to capture streaming output. (defaults to print) |
None
|
Source code in cillow/interpreter.py
run_code
¶
Run the given code.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
code
|
str
|
The code to run |
required |
on_stream
|
Callable[[Stream | ByteStream], None] | None
|
The callback to capture streaming output. |
None
|
Returns:
Type | Description |
---|---|
Result | ExceptionInfo
|
Result or ExceptionInfo dataclass instance |
Source code in cillow/interpreter.py
run_command
¶
Run the given command.
⚠️ WARNING: This class allows execution of system commands and should be used with EXTREME CAUTION.
- Never run commands with user-supplied or untrusted input
- Always validate and sanitize any command arguments
- Be aware of potential security risks, especially with privilege escalation
Parameters:
Name | Type | Description | Default |
---|---|---|---|
cmd
|
str
|
The command to run |
()
|
on_stream
|
Callable[[Stream], Any] | None
|
The callback to capture streaming output. |
None
|