Attaching to a Python Interpreter a la Tcl

D

DE

Hello,

Some long time ago, I used to use Tcl/Tk. I had an tcl embedded into my
app.

The coolest thing was however, I was able to attach to the interpreter
(built in to my app) via a tcl shell in which I could type in regular
tcl code which would be interpreted by the interpreter of my
application. Naturally, it was possible to call tcl functions of my
applications.

Some kind of rapid RPC.

Is this also possible with python ?

Thanks,
 
R

Robin Becker

DE said:
Hello,

Some long time ago, I used to use Tcl/Tk. I had an tcl embedded into my
app.

The coolest thing was however, I was able to attach to the interpreter
(built in to my app) via a tcl shell in which I could type in regular
tcl code which would be interpreted by the interpreter of my
application. Naturally, it was possible to call tcl functions of my
applications.

Some kind of rapid RPC.

Is this also possible with python ?

Thanks,
I think you are talking about the tk send command which allows for
communicating with a known app.

I don't believe python comes with such a facility without some coding,
but it has been implemented in various ways using sockets etc etc.

I seem to remember that modern idle uses an rpc technique for debugging.

There are several python projects which address interprocess
communication pyro http://pyro.sourceforge.net/ is a good example.
 
V

Ville Vainio

fuzzyman> Do you mean making the interpreter available from within
fuzzyman> a Python app ? There are various ways of doing that -
fuzzyman> you can see the SPE editor which uses pycrust as one
fuzzyman> example. http://spe.pycs.net

I believe he means embedding he interpreter in his app, then accessing
the interpreter from another process - so you could change and view
global vars of the running process from the interpreter, for
example. This basically means redirecting i/o of the interpreter to a
socket to which you connect via, say, telnet. There are libs that do
such a thing, I even remember trying one out myself, but I couldn't
find it quickly enough from google.
 
C

Cameron Laird

I think you are talking about the tk send command which allows for
communicating with a known app.

I don't believe python comes with such a facility without some coding,
but it has been implemented in various ways using sockets etc etc.

I seem to remember that modern idle uses an rpc technique for debugging.

There are several python projects which address interprocess
communication pyro http://pyro.sourceforge.net/ is a good example.
.
.
.
Agreed: Python makes it easy to do this. Tk *does* this,
out-of-the-box.

But note! Tkinter can access Tk's [send], so, if the appli-
cation happened to be built with Tkinter, and one were fluent
enough at translating between Tcl and Python to reach the
latter by way of the former, then, yes, you would find that
your Python application already has this.
 
S

Stephen Thorne

Hello,

Some long time ago, I used to use Tcl/Tk. I had an tcl embedded into my
app.

The coolest thing was however, I was able to attach to the interpreter
(built in to my app) via a tcl shell in which I could type in regular
tcl code which would be interpreted by the interpreter of my
application. Naturally, it was possible to call tcl functions of my
applications.

Some kind of rapid RPC.

Is this also possible with python ?

Yes, using something like twisted's 'Manhole', which allows you to
execute code in the server process.

It may require twisted-ising your application however.

Stephen
 
J

Jeff Epler

Cameron Laird mentioned Tk's send working with Python; if you are writing your
app with Tkinter, here is some code to let you use tcl commands like
send <appname> python <expression or statement>
for remote control. You could build a more sophisticated front-end for this,
and you'll probably also want to add stuff like sending the text of an
exception as the result of the 'send' command.

Jeff

#------------------------------------------------------------------------
import Tkinter

__all__ = 'python', 'setup_send'
def makecommand(master, name, func, subst=None, needcleanup=0):
f = Tkinter.CallWrapper(func, subst, master).__call__
master.tk.createcommand(name, f)
if needcleanup:
if master._tclCommands is None:
master._tclCommands = []
master._tclCommands.append(name)
return name


def setup_send(app, ns, name="python"):
def python(*args):
s = " ".join(args)
print args
try:
code = compile(s, '<send>', 'eval')
return eval(code, ns)
except SyntaxError:
code = compile(s, '<send>', 'exec')
exec code in ns
makecommand(app, name, python)

if __name__ == '__main__':
app = Tkinter.Tk()
setup_send(app, {})
app.mainloop()
#------------------------------------------------------------------------

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.6 (GNU/Linux)

iD8DBQFCHdt7Jd01MZaTXX0RAp0dAKCbVF88J7nEo4Nyg+F06xD5uZhrPgCeIwyz
HepI83ocgSrW+EoRD/BQ8Vc=
=vBV2
-----END PGP SIGNATURE-----
 

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

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top