Skip to main content

A Python interpreter with built-in safeguards for executing untrusted code, like LLM-generated scripts.

Project description

🐍 pytherpreter 🐍

A Python interpreter with built-in safeguards for executing untrusted code, like LLM-generated scripts.

This repository contains the Python interpreter tool extracted from HuggingFace’s smolagents project. Big hug to the HuggingFace team for their initial implementation! 🤗

Some improvements over the smolagents tool:

  • Supports async code execution using the async_evaluate function and AsyncPythonInterpreter class.
  • Improved function call resolution.
  • Supports custom subscriptable objects.
  • No external dependencies.
  • More flexible print handling.

Installation

pip install pytherpreter

Latest development version:

pip install git+ssh://git@github.com/aremeis/pytherpreter.git

or

pip install git+https://github.com/aremeis/pytherpreter.git

Usage

Using evaluate

This function evaluates Python code and returns the result.

from pytherpreter import evaluate

result = evaluate("""
from math import sqrt
sqrt(4)
""")
print(result)

# Output:
# 2.0

The evaluate function returns the result of the last expression in the code.

Using PythonInterpreter

This class is a wrapper around the evaluate function that keeps the state of the interpreter between calls. Variables and functions defined by the code will be be available in subsequent calls.

from pytherpreter import PythonInterpreter

interpreter = PythonInterpreter()
result = interpreter("x = 3")
print(result)

# Output:
# 3

result = interpreter("x += 1")
print(result)

# Output:
# 4

Printing

You may provide a stdout argument to capture the output of print statements in the code.

from pytherpreter import evaluate
import io
stdout = io.StringIO()
result = evaluate("print('Hello, World!')", stdout=stdout)
print(stdout.getvalue())

# Output:
# Hello, World!

Variables

You may provide a variables argument to preset variables and capture changes to them.

from pytherpreter import evaluate
variables = {"x": 3}
result = evaluate("x += 1", variables=variables)
print(variables["x"])

# Output:
# 4

Safeguards

Built-in functions

You may provide a builtin_functions argument containing a dictionary of built-in functions the code is allowed to call. If you don't provide this argument, the code will only be able to call the built-in functions in BASE_BUILTIN_FUNCTIONS.

The code will not be able to modify the provided built-in functions.

Modules

By default, the code will only be able to import the modules in BASE_BUILTIN_MODULES. You may provide an authorized_imports argument to allow the code to import additional modules.

Documentation

For more details, the reference documentation is available here.

License

This project is licensed under the Apache 2.0 License - see the LICENSE file for details.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pytherpreter-0.2.1.tar.gz (46.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pytherpreter-0.2.1-py3-none-any.whl (27.8 kB view details)

Uploaded Python 3

File details

Details for the file pytherpreter-0.2.1.tar.gz.

File metadata

  • Download URL: pytherpreter-0.2.1.tar.gz
  • Upload date:
  • Size: 46.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.11.3

File hashes

Hashes for pytherpreter-0.2.1.tar.gz
Algorithm Hash digest
SHA256 fca8edef5fe4535d386cb2f1db29a82c52aef20f11c87bc7b32fc3e4cfb30299
MD5 985d694e90c58ca192f9a5cdf6f525e3
BLAKE2b-256 7e2df54edd20b7fb4522c55d12b69a7f54ca6a75f9da130a3a9f1ae260b9a293

See more details on using hashes here.

File details

Details for the file pytherpreter-0.2.1-py3-none-any.whl.

File metadata

  • Download URL: pytherpreter-0.2.1-py3-none-any.whl
  • Upload date:
  • Size: 27.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.11.3

File hashes

Hashes for pytherpreter-0.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 a4cb8b6f48da1e92bc16ab1a6a3a07f7dffe596edf2bc6240f49fc46e91f6e01
MD5 a5d838c6a514a4da7f83fce7bd064fbf
BLAKE2b-256 8214e27cb655a37c41860e23b29af844e4b63348429ade78b054b7157042716b

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page