943,074 Members | Top Members by Rank

Ad:
  • Python Discussion Thread
  • Unsolved
  • Views: 607
  • Python RSS
Jan 18th, 2010
0

Problems with the interpreter's exit() function.

Expand Post »
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.

Python Syntax (Toggle Plain Text)
  1. from uuid import uuid4
  2. import code
  3.  
  4. ANONYMOUS = None
  5. UUID = 0
  6.  
  7. class Agent:
  8.  
  9. def __init__(self, name = UUID):
  10. self.__uuid = uuid4()
  11. if name == UUID:
  12. name = self.__uuid
  13. self.__name = name
  14.  
  15.  
  16. @property
  17. def uuid(self):
  18. return self.__uuid
  19.  
  20.  
  21. @property
  22. def name(self):
  23. return self.__name
  24.  
  25. @name.setter
  26. def name(self, value):
  27. self.__name = value
  28.  
  29. @name.deleter
  30. def name(self):
  31. self.__name = ANONYMOUS
  32.  
  33. # The interactive interpreter.
  34. def interact(self):
  35. 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.

Python Syntax (Toggle Plain Text)
  1. def interact(self):
  2. try:
  3. code.interact(local = self.__dict__)
  4. except SystemExit:
  5. 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.
Similar Threads
Reputation Points: 106
Solved Threads: 35
Posting Whiz in Training
lrh9 is offline Offline
238 posts
since Oct 2009
Jan 18th, 2010
0
Re: Problems with the interpreter's exit() function.
I was able to answer my last question.

In order to access all of the attributes of the object simply pass locals() as the local, and then refer to the object as "self".
Last edited by lrh9; Jan 18th, 2010 at 2:32 pm.
Reputation Points: 106
Solved Threads: 35
Posting Whiz in Training
lrh9 is offline Offline
238 posts
since Oct 2009

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Python Forum Timeline: Deceptively simple syntax error
Next Thread in Python Forum Timeline: search directory for config files, search and delete item





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC