I'm using the code module to add an interactive interpreter to an object. This object has an interact method that when called creates an interactive interpreter with the object's dictionary as the namespace for the interpreter.
When done interacting with the object, I want the "exit" function to return execution back to the original thread of execution.
Here is the initial code.
from uuid import uuid4
import code
ANONYMOUS = None
UUID = 0
class Agent:
def __init__(self, name = UUID):
self.__uuid = uuid4()
if name == UUID:
name = self.__uuid
self.__name = name
@property
def uuid(self):
return self.__uuid
@property
def name(self):
return self.__name
@name.setter
def name(self, value):
self.__name = value
@name.deleter
def name(self):
self.__name = ANONYMOUS
# The interactive interpreter.
def interact(self):
code.interact(local = self.__dict__)
In the command prompt, calling the object's interact method and then calling the exit function results in a complete exit, but using ctrl+z and return returns the thread of execution to the original interpreter.
In the IDLE GUI interpreter, calling the object's interact method and then calling the exit function results in a complete exit, and ctrl+z and return only erases the last input or output.
So I'm open to suggestions. If possible I'd like to avoid redefining the exit function.
My first instinct was to catch the SystemExit exception in the interact function and return. This works well in the command prompt. However, it still results in a complete exit in the IDLE GUI interpreter.
def interact(self):
try:
code.interact(local = self.__dict__)
except SystemExit:
return
I have one tangent question about this. The object's attributes and methods not in the object's dictionary are obviously not accessible in the new interpreter. (In this case the property functions and the interact function.) This isn't a problem at the moment because I cannot think of any compelling reason why they should be accessible, and variables and functions defined in the new interpreter will be accessible from the object once the interpreter exits. If I did want these attributes and methods to be accessible though, what would be the best way to do it?
Thank you for reading this.