How to I restart an interactive session?

M

markscottwright

I'm trying to cobble together an IDLE equivalent using pyshell and VIM
(My idea is just to pipe exec file commands from VIM to pyshell via a
socket or something). The one feature that IDLE has that I would
really like but can't seem to duplicate is the "Restart Shell" command.
Delving through the IDLE code, it looks like IDLE kills and restarts
its InteractiveInterpreter-derived class to do this. Is this really
necessary? If I just take __main__.__dict__ and strip out everything
since a start point, haven't I restored the interpreter to a virgin
state?

(Of course, assuming that there are no threads running, no
c-code-invoked junk lying around, etc).
 
D

Diez B. Roggisch

markscottwright said:
I'm trying to cobble together an IDLE equivalent using pyshell and VIM
(My idea is just to pipe exec file commands from VIM to pyshell via a
socket or something). The one feature that IDLE has that I would
really like but can't seem to duplicate is the "Restart Shell" command.
Delving through the IDLE code, it looks like IDLE kills and restarts
its InteractiveInterpreter-derived class to do this. Is this really
necessary? If I just take __main__.__dict__ and strip out everything
since a start point, haven't I restored the interpreter to a virgin
state?

You can't unload modules, or at least reload is not always working properly.
So - you'd gotta go the road IDLE has gone before I assume.
 
M

markscottwright

But, by deleting their namespace entries haven't I effectively unloaded
them? In other words, from the point of the interpreter, isn't the
state at point A and point B the same?

--- point A:
import os
del __main__.__dict__['os']
--- point B

I guess my question boils down to, is the state of the python
interpreter kept anywhere other than the __main__ namespace? Obviously
the answer is yes - there's the current working directory and any
running threads. I think I'm willing to live with those (yeah, I know,
those are the words of a man who is going to spend time chasing obscure
side-effects...)

But are there other things I'm missing? Is my whole plan misguided
from the beginning?
 
M

Mike Meyer

markscottwright said:
But, by deleting their namespace entries haven't I effectively unloaded
them? In other words, from the point of the interpreter, isn't the
state at point A and point B the same?

--- point A:
import os
del __main__.__dict__['os']
--- point B

That depends on the module you import. At point B, sys.modules *will*
contain an entry for the imported module. That won't be true at point
A unless the module was a builtin one.
I guess my question boils down to, is the state of the python
interpreter kept anywhere other than the __main__ namespace? Obviously
the answer is yes - there's the current working directory and any
running threads. I think I'm willing to live with those (yeah, I know,
those are the words of a man who is going to spend time chasing obscure
side-effects...)

Yes. Various things in sys record information about the state of the
interpreter.
But are there other things I'm missing? Is my whole plan misguided
from the beginning?

It's not clear that misguided is the correct term. You're trying to do
something with the interpreter that it wasn't designed for. I'd say
that was unwise. Whether or not it *should* have been designed for
what you want to do is another problem.

<mike
 
D

Diez B. Roggisch

But, by deleting their namespace entries haven't I effectively unloaded
them? In other words, from the point of the interpreter, isn't the

No. You haven't I'm not entirely sure why - that's deep in the internals of
python - but I know for sure that reload() is not working fully as expected
in some cases.
state at point A and point B the same?

--- point A:
import os
del __main__.__dict__['os']
--- point B

I guess my question boils down to, is the state of the python
interpreter kept anywhere other than the __main__ namespace? Obviously
the answer is yes - there's the current working directory and any
running threads. I think I'm willing to live with those (yeah, I know,
those are the words of a man who is going to spend time chasing obscure
side-effects...)

But are there other things I'm missing? Is my whole plan misguided
from the beginning?

Sort of, as for your comparably little saving of effort you put the burden
of unknown side-effects from your doings on your user. Certainly not the
right thing to do for an IDE, people prefer to trust these :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top