0

Is there a way in Python to launch a Tcl shell or a unix shell and take control of it to perform operations? I understand that the subprocess, TkInter can be used to execute shell/Tcl scripts and get the output.

Specifically this will be a wrapper on the Tcl shell/tool that will control its stdin/out. I have tried establishing a client-server connection but in order for python to control tcl shell, the connection needs to be established manually. What should be approach here?

asked Oct 18, 2021 at 18:19

1 Answer 1

1

With Tkinter, you have a Tcl interpreter inside the same process as Python. That's fine for doing conventional stuff (or Tk) but if you've got some sort of custom Tcl interpreter to control, you'll probably want to launch that as a subprocess connected by pipes to the parent Python process.

The subprocess module in Python has most of what you'll need.

import subprocess
p = subprocess.Popen(["tclimplementation"], stdin=subprocess.PIPE, stdout=subprocess.PIPE)
# Turn off buffering on the Tcl side
print("fconfigure stdout -buffering none", file=p.stdin)
# Also should read from p.stdout; here's how
line = p.stdout.readline().strip()

That telling of the Tcl side to fconfigure stdout -buffering none is important, as Tcl (in common with most processes) defaults to buffering multiple kilobytes when writing to anything other than a terminal (great for performance, not great for interactive use).

Now that you can send messages back and forth, you need to think more about how to actually control the other side. That's rather application specific.

answered Oct 18, 2021 at 20:19
Sign up to request clarification or add additional context in comments.

5 Comments

A key point is the length of messages sent back and forth; multiline messages require more work than if you can guarantee they're single lines.
hmm. It would be nice if you could provide channels to use for stdin/stdout/stderr when creating a new tcl interp or thread.
Thanks @DonalFellows. Can you also explain what "tclimplementation" means? Is that a placeholder for a tcl bin path?
@almostacoder Exactly that. If it was a standard tclsh then you'd be able to use Tkinter's internal stuff (which is definitely more efficient).
Okay, just to add by default the stream is chose as byte so the print statement string needs to converted to byte first.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.