Interpreter

class pyto_runtime.Interpreter(version=None)

Interpreter holds configurations, context and debugging tools for scripts execution.

This is not the same as a Python subinterpreter because this would break compatibility with a lot of native extensions. Instead, PytoRuntime groups threads by interpreter to isolate modules in a single process environment. Standard library modules except sys and a few other ones are still shared.

property breakpoint_handler

Called when a breakpoint is hit and the REPL is entered.

Takes the triggered pyto_runtime.Breakpoint and a pyto_runtime.Traceback.

property breakpoints

Breakpoints to set before running a script with debug enabled.

Can also be updated while script is running.

property builtins

The builtins module.

clear_state()

Clears the state of the interpreter. That includes the sys module, all imported modules, the signal handlers and the tty state (termios, window size).

PytoRuntime implements a garbage collector. When the pyto_runtime.Interpreter object is deallocated, it gets scheduled for state clearing before the next script executes on any interpreter.

property display_hook

If set, expressions in the executed script will pass the string representation of their result to this block.

Takes a pyto_runtime.Frame and a string.

end_of_file()

Raises an EOFError if reading from standard input (simulates Ctrl+D).

As this is a feature not available in pipes but real terminals, it requires PytoRuntime’s patched CPython which uses a global __Py_send_eof flag alongside a SIGHUP signal handler that raises EOFError when reading from readline or when polling standard input witht the select module.

evaluate(expr: str)

Evaluates an expression and returns its result wrapped in a pyto_runtime.Object.

Can raise an pyto_runtime.IntepreterException.

Parameters:

expr – Expression to evaluate.

exec(code: str)

Executes code and returns a new global namespace in which it was executed, wrapped in a pyto_runtime.Object.

Can raise an pyto_runtime.IntepreterException.

Parameters:

code – Code to execute.

focus()

If a curses script is executing through curses.wrapper in this interpreter, this function sets the global terminal instance.

The Python curses module assumes a single global terminal per process. PytoRuntime internally calls the set_term C function to switch between terminals and receive size change notifications among other stuff. This function is intended to be called when the terminal is focused.

import(module: str)

Imports a module and returns it wrapped in a pyto_runtime.Object.

Can raise an pyto_runtime.IntepreterException.

Parameters:

module – Full module name to import.

kill(signal: int)

Sends a signal the interpreter’s main thread and to its pseudo subprocesses threads.

PytoRuntime’s patched CPython supports thread local signal handling.

Parameters:

signal – The signal to send.

property receive_image_output

Function responsible of showing images from PIL, Matplotlib or OpenCV.

Takes an image as bytes and a boolean indicating whether it is part of a video stream (from OpenCV).

property receive_view_controller

Function responsible of presenting an UI.

Takes a UIViewController and a boolean indicating whether it should be presented in a new window if possible.

run_code(code: str, args=[], debug: bool = False) → int

Runs code.

It returns the exit code or raises an pyto_runtime.IntepreterException.

Parameters:
  • code – Code to execute.

  • args – The command line arguments.

  • debug – Boolean indicating whether breakpoints should be enabled.

run_path(path: str, args=[], debug: bool = False, run_as_main: bool = True, package=None) → int

Runs a script.

It returns the exit code or raises an pyto_runtime.IntepreterException.

Parameters:
  • path – The path of the script to execute.

  • args – The command line arguments.

  • debug – Boolean indicating whether breakpoints should be enabled.

  • run_as_main – Boolean indicating whether the script should be run as __main__ or as its file name.

  • package – An optional parent package name. Used for relative imports so they work, whether running as __main__ or not.

run_repl(path=None, args=[], debug: bool = False, run_as_main: bool = True, package=None) → int

Runs the REPL.

It returns the exit code or raises an pyto_runtime.IntepreterException.

Parameters:
  • path – An optional path of a script to execute inside the REPL.

  • args – The command line arguments.

  • debug – Boolean indicating whether breakpoints should be enabled.

  • run_as_main – Boolean indicating whether the script should be run as __main__ or as its file name.

  • package – An optional parent package name. Used for relative imports so they work, whether running as __main__ or not.

run_shell(args=[]) → int

Runs the shell.

It returns the exit code or raises an pyto_runtime.IntepreterException.

Parameters:

args – The command line arguments, which can be used to execute a shell command. Exits inmediately after if it is set.

property running: bool

Indicates whether a script is running under this interpreter.

property standard_error

The standard error text stream.

property standard_input

The standard input text stream.

property standard_output

The standard output text stream.

property trace

if set, a tracer will call this block every time a line of code is about to be executed.

Takes the path of the script and a line number. But the tracer is only installed in the file directly executed (not imported modules). This can significantly slow down execution because of the use of sys.settrace if not already debugging.

property version

Embedded Python version to load.

property virtual_environment_home_path: str

The home URL of a virtual environment that is entered when running a script. The home is the path containing the binaries so the parent directory must be readable. Setting this does not affect currently running script automatically. Instead, if you’re running a shell you can call pyto_runtime.Interpreter.update_shell_virtual_environment().

property working_directory: str

The working directory. It takes effect before running a script and only affect the interpreter’s thread and subthreads.