• Python

    From saito@[email protected] to comp.lang.tcl on Wed Oct 14 09:07:56 2026
    From Newsgroup: comp.lang.tcl

    I would like to start a python session, send it some simple commands and
    get the results back. From Tcl, I have tried opening a pipe with RDWR
    and r+ and various fconfigure/filevenets and none seem to work.

    AI bot solutions end up hanging on the reading part as well.

    Is this use case possible?

    --- Synchronet 3.22a-Linux NewsLink 1.2
  • From Arjen@[email protected] to comp.lang.tcl on Wed Oct 14 13:13:51 2026
    From Newsgroup: comp.lang.tcl


    saito <[email protected]> posted:

    I would like to start a python session, send it some simple commands and
    get the results back. From Tcl, I have tried opening a pipe with RDWR
    and r+ and various fconfigure/filevenets and none seem to work.

    AI bot solutions end up hanging on the reading part as well.

    Is this use case possible?


    You should probably have a look at Tohil - this makes the interconnection between
    Tcl and Python quite easy. See https://wiki.tcl-lang.org/page/Accessing+Tcl+and+Python+from+one+another
    --- Synchronet 3.22a-Linux NewsLink 1.2
  • From saito@[email protected] to comp.lang.tcl on Wed Oct 14 09:26:24 2026
    From Newsgroup: comp.lang.tcl

    On 10/14/2026 9:13 AM, Arjen wrote:

    You should probably have a look at Tohil - this makes the interconnection between
    Tcl and Python quite easy. See https://wiki.tcl-lang.org/page/Accessing+Tcl+and+Python+from+one+another

    I'd try it but it looks like it doesn't run on Windows yet. I would like
    to avoid binary extensions if possible.

    I am not sure how the file open modes RDWR and r+ are supposed to work. Perhaps my challenge is isolating the two ends of the pipe. I am pretty
    sure I have seen this being done but I am not sure where to start looking.

    --- Synchronet 3.22a-Linux NewsLink 1.2
  • From Rich@[email protected] to comp.lang.tcl on Wed Oct 14 13:53:14 2026
    From Newsgroup: comp.lang.tcl

    saito <[email protected]> wrote:
    On 10/14/2026 9:13 AM, Arjen wrote:

    You should probably have a look at Tohil - this makes the interconnection between
    Tcl and Python quite easy. See https://wiki.tcl-lang.org/page/Accessing+Tcl+and+Python+from+one+another

    I'd try it but it looks like it doesn't run on Windows yet. I would like
    to avoid binary extensions if possible.

    I am not sure how the file open modes RDWR and r+ are supposed to work. Perhaps my challenge is isolating the two ends of the pipe. I am pretty
    sure I have seen this being done but I am not sure where to start looking.

    Perhaps some of the discussion here:

    https://wiki.tcl-lang.org/page/chan+pipe

    will be helpful?
    --- Synchronet 3.22a-Linux NewsLink 1.2
  • From Arjen@[email protected] to comp.lang.tcl on Wed Oct 14 13:56:09 2026
    From Newsgroup: comp.lang.tcl


    saito <[email protected]> posted:

    On 10/14/2026 9:13 AM, Arjen wrote:

    You should probably have a look at Tohil - this makes the interconnection between
    Tcl and Python quite easy. See https://wiki.tcl-lang.org/page/Accessing+Tcl+and+Python+from+one+another

    I'd try it but it looks like it doesn't run on Windows yet. I would like
    to avoid binary extensions if possible.

    I am not sure how the file open modes RDWR and r+ are supposed to work. Perhaps my challenge is isolating the two ends of the pipe. I am pretty
    sure I have seen this being done but I am not sure where to start looking.

    I have used it on Windows (not extensively I admit), but your method with pipes
    should work as well. Have you checked the Wiki for examples? (I see Rich has made a similar remark)
    --- Synchronet 3.22a-Linux NewsLink 1.2
  • From saito@[email protected] to comp.lang.tcl on Wed Oct 14 09:57:34 2026
    From Newsgroup: comp.lang.tcl

    On 10/14/2026 9:53 AM, Rich wrote:

    Perhaps some of the discussion here:

    https://wiki.tcl-lang.org/page/chan+pipe

    will be helpful?

    Thank you very much! This is the command I was thinking of.
    Now I will see if it works the way I need it.

    --- Synchronet 3.22a-Linux NewsLink 1.2
  • From saito@[email protected] to comp.lang.tcl on Wed Oct 14 10:03:09 2026
    From Newsgroup: comp.lang.tcl

    On 10/14/2026 9:56 AM, Arjen wrote:

    I have used it on Windows (not extensively I admit),

    Interesting! I wonder if their info is simply out of date. Under their
    "To Do", it says: "We'd like to have Tohil work on Windows if someone
    would be willing to take it on."


    but your method with pipes
    should work as well. Have you checked the Wiki for examples? (I see Rich has made a similar remark)

    I hope so too. That was indeed the command that I was trying to find. Sometimes you just don't know what magic words to use.

    Thank you!

    --- Synchronet 3.22a-Linux NewsLink 1.2
  • From Arjen@[email protected] to comp.lang.tcl on Wed Oct 14 14:30:34 2026
    From Newsgroup: comp.lang.tcl


    saito <[email protected]> posted:

    On 10/14/2026 9:56 AM, Arjen wrote:

    I have used it on Windows (not extensively I admit),

    Interesting! I wonder if their info is simply out of date. Under their
    "To Do", it says: "We'd like to have Tohil work on Windows if someone
    would be willing to take it on."


    but your method with pipes
    should work as well. Have you checked the Wiki for examples? (I see Rich has
    made a similar remark)

    I hope so too. That was indeed the command that I was trying to find. Sometimes you just don't know what magic words to use.

    Thank you!


    If it does not work, can you show us via a simple/minimal example
    how you are trying to do it?
    --- Synchronet 3.22a-Linux NewsLink 1.2
  • From Ralf Fassel@[email protected] to comp.lang.tcl on Wed Oct 14 18:04:17 2026
    From Newsgroup: comp.lang.tcl

    * saito <[email protected]>
    | I would like to start a python session, send it some simple commands
    | and get the results back. From Tcl, I have tried opening a pipe with
    | RDWR and r+ and various fconfigure/filevenets and none seem to work.

    | AI bot solutions end up hanging on the reading part as well.

    | Is this use case possible?

    Why not write a temp file with the simple commands and call python with
    that?

    set fd [file tempfile tmpfile]
    puts $fd $simple_commands
    close $fd
    set result [exec python $tmpfile]

    R'
    --- Synchronet 3.22a-Linux NewsLink 1.2
  • From saito@[email protected] to comp.lang.tcl on Wed Oct 14 15:44:42 2026
    From Newsgroup: comp.lang.tcl

    On 10/14/2026 10:30 AM, Arjen wrote:

    If it does not work, can you show us via a simple/minimal example
    how you are trying to do it?

    I am getting back to it. I read thru the docs on [chan pipe]. It seems a powerful and flexible construct. Unfortunately no detailed explanation
    or examples exist beyond redirecting or controlling the output part.


    This has syntax errors:

    =======================================
    lassign [chan pipe] toPy fromPy

    chan configure $toPy -buffering line
    chan configure $fromPy -buffering line

    set fileID [open "|python.exe <$toPy 2>@ $fromPy" r] =======================================

    % couldn't read file "file52551d0": no such file or directory

    where that handle is the value of "toPy"


    --- Synchronet 3.22a-Linux NewsLink 1.2
  • From saito@[email protected] to comp.lang.tcl on Wed Oct 14 16:08:21 2026
    From Newsgroup: comp.lang.tcl

    On 10/14/2026 12:04 PM, Ralf Fassel wrote:

    Why not write a temp file with the simple commands and call python with
    that?

    set fd [file tempfile tmpfile]
    puts $fd $simple_commands
    close $fd
    set result [exec python $tmpfile]


    Commands are read at a time from a text widget when a button is clicked.
    So this would require idle file reads and waits at both Tcl and python,
    if I am not mistaken.







    --- Synchronet 3.22a-Linux NewsLink 1.2
  • From Rich@[email protected] to comp.lang.tcl on Thu Oct 15 02:45:17 2026
    From Newsgroup: comp.lang.tcl

    saito <[email protected]> wrote:
    On 10/14/2026 10:30 AM, Arjen wrote:

    If it does not work, can you show us via a simple/minimal example
    how you are trying to do it?

    I am getting back to it. I read thru the docs on [chan pipe]. It seems a powerful and flexible construct. Unfortunately no detailed explanation
    or examples exist beyond redirecting or controlling the output part.


    This has syntax errors:

    =======================================
    lassign [chan pipe] toPy fromPy

    chan configure $toPy -buffering line
    chan configure $fromPy -buffering line

    set fileID [open "|python.exe <$toPy 2>@ $fromPy" r] =======================================

    % couldn't read file "file52551d0": no such file or directory

    where that handle is the value of "toPy"

    From the 'exec' man page, '<' is defined as working with "files" (i.e., filenames).

    chan pipe gives you file id's, not names.

    You want the <@ version:

    <@ fileId FileId must be the identifier for an open file, such as
    the return value from a previous call to open. It is
    used as the standard input for the first command in the
    pipeline. FileId must have been opened for reading.

    --- Synchronet 3.22a-Linux NewsLink 1.2
  • From Christian Gollwitzer@[email protected] to comp.lang.tcl on Thu Oct 15 10:37:13 2026
    From Newsgroup: comp.lang.tcl

    Am 14.10.26 um 15:07 schrieb saito:
    I would like to start a python session, send it some simple commands and
    get the results back. From Tcl, I have tried opening a pipe with RDWR
    and r+ and various fconfigure/filevenets and none seem to work.

    AI bot solutions end up hanging on the reading part as well.

    Is this use case possible?

    Maybe it's failing because python uses GNU readline and expects a real terminal, and behaves differently when you redirect stdin to a pipe.

    YOu could write a Python script that reads from stdin and executes the commands. I'm using a scheme like this in a larger Python application
    that is remotely controlled from a Tcl GUI.

    Python code is like this:

    def slave_loop(self):
    tkroot.createfilehandler(sys.stdin, tkinter.READABLE, self.stdin_handler)

    def stdin_handler(self, file, mask):
    cmd = file.readline()

    if not cmd:
    exit()
    try:
    print(eval(cmd))
    except:
    print('ERROR')
    traceback.print_exc()

    self.slave_loop()



    The Python program in this case is itself a Tkinter app, but it should
    work similarly well without an event loop like this

    while True:
    cmd=sys.stdin.readline()
    eval(cmd)

    Obviously needs some error handling etc.

    Christian

    --- Synchronet 3.22a-Linux NewsLink 1.2
  • From saito@[email protected] to comp.lang.tcl on Thu Oct 15 08:52:56 2026
    From Newsgroup: comp.lang.tcl

    On 10/15/2026 4:37 AM, Christian Gollwitzer wrote:

    YOu could write a Python script that reads from stdin and executes the commands. I'm using a scheme like this in a larger Python application
    that is remotely controlled from a Tcl GUI.


    I would prefer the Tcl way but I can't make it work. So I ended up doing something similar yesterday. Through a few iterations of the idea, this
    is what it looks like now:

    ===== save to driver.py ====================

    import sys

    errCounter = 0

    while True:
    try:
    line = input()
    except EOFError:
    break

    try:
    try:
    # simple expression?
    result = eval(line)
    if result is not None:
    print(result)

    except SyntaxError:
    # code block?
    exec(line)

    except Exception as e:
    errCounter=errCounter+1
    print(f"ERROR #{errCounter}: ", e)

    ============================================


    This is how to use it:

    ============================================
    set fid [open |[list python.exe -u driver.py 2>@1] r+]

    fconfigure $fid -buffering line -blocking 0

    puts $dif {X = 10}
    gets $fid
    puts $fid {X}
    gets $fid

    ============================================


    It works quite well. Things to note:

    1) Currently both input and output is line oriented.

    2) If the result from python has multiple lines, you need to go multiple
    gets to consume all.

    Both of these could be improved upon but it seems to work quite well at
    the moment for the task it was meant for.





    --- Synchronet 3.22a-Linux NewsLink 1.2
  • From Ralf Fassel@[email protected] to comp.lang.tcl on Thu Oct 15 18:31:43 2026
    From Newsgroup: comp.lang.tcl

    * saito <[email protected]>
    | It works quite well. Things to note:

    | 1) Currently both input and output is line oriented.

    | 2) If the result from python has multiple lines, you need to go
    | multiple gets to consume all.

    I have similar inter-process-pipes, though between two TCL processes.

    For line-oriented communication, I simply base64-encode the whole
    content and send one line, base64-decode on the other side.
    That way each message (regardless how long within sane limits :-)
    is one line only.

    Sender:
    package require base64
    proc send_base64 {chan txt} {
    set line [base64::encode -maxlen 0 [encoding convertto utf-8 $txt]]
    # assumes line-buffering
    puts $chan $line
    }

    Receiver:
    package require base64
    proc recv_base64 {chan} {
    # error and eof handling omitted
    gets $chan line
    return [encoding convertfrom utf-8 [base64::decode $line]]
    }

    HTH
    R'
    --- Synchronet 3.22a-Linux NewsLink 1.2
  • From saito@[email protected] to comp.lang.tcl on Thu Oct 15 13:04:13 2026
    From Newsgroup: comp.lang.tcl

    On 10/15/2026 12:31 PM, Ralf Fassel wrote:

    I have similar inter-process-pipes, though between two TCL processes.

    For line-oriented communication, I simply base64-encode the whole
    content and send one line, base64-decode on the other side.
    That way each message (regardless how long within sane limits :-)
    is one line only.


    Awesome! This will work well.

    --- Synchronet 3.22a-Linux NewsLink 1.2