On this page
code.InteractiveConsole
Prev: code.interact | Up: code | Next: code.InteractiveInterpreter
Rel: code.compile_command, code.interact, code.InteractiveInterpreter
Ref: docs.python.org — code.InteractiveConsole
Tags: #public #python3 #stdlib #code
Import
import code
from code import InteractiveConsole
Signature & Overview
code.InteractiveConsole(locals=None, filename='<console>', *, local_exit=False)
Closely emulate the behavior of the interactive Python interpreter.
Methods & Attributes
InteractiveConsole.interact(self, banner=None, exitmsg=None)— Closely emulate the interactive Python console.InteractiveConsole.push(self, line, filename=None, _symbol='single')— Push a line to the interpreter.InteractiveConsole.raw_input(self, prompt='')— Write a prompt and read a line.InteractiveConsole.resetbuffer(self)— Reset the input buffer.InteractiveConsole.runcode(self, code)— Execute a code object.InteractiveConsole.runsource(self, source, filename='<input>', symbol='single')— Compile and run some source in the interpreter.InteractiveConsole.showsyntaxerror(self, filename=None, **kwargs)— Display the syntax error that just occurred.InteractiveConsole.showtraceback(self)— Display the exception that just occurred.InteractiveConsole.write(self, data)— Write a string.
Usage
import code
import contextlib
import io
buf = io.StringIO()
con = code.InteractiveConsole(locals={"x": 10})
with contextlib.redirect_stdout(buf):
assert con.push("print(x * 2)") is False
assert buf.getvalue().strip() == "20"
Gotchas
- Not a security sandbox: Executes arbitrary Python in the current interpreter.
REPL Inspect
from code import InteractiveConsole
help(InteractiveConsole)
Linked from
Related notes
- tuts/python3/stdlib_module/code/compile_command Linked here; links to this page; shared tags: #code, #stdlib
- tuts/python3/stdlib_module/code/interact Linked here; links to this page; shared tags: #code, #stdlib
- tuts/python3/stdlib_module/code/InteractiveInterpreter Linked here; links to this page; shared tags: #code, #stdlib
- tuts/python3/stdlib_module/code Linked here; links to this page; shared tags: #stdlib
- tuts/c/stdlib shared tags: #stdlib
- tuts/c/stdlib/assert shared tags: #stdlib
- tuts/c/stdlib/assert/assert shared tags: #stdlib
- tuts/c/stdlib/assert/static_assert shared tags: #stdlib
- tuts/c/stdlib/ctype shared tags: #stdlib
- tuts/c/stdlib/ctype/isalpha shared tags: #stdlib