Repository navigation
gh-162537: Add tkinter support for Tcl channels - #162538
serhiy-storchaka wants to merge 4 commits into
Conversation
Add the tk.createchannel() method, which creates a Tcl channel for a file, a pipe or a socket. The returned object supports reading, writing, flushing, EOF detection and registering a handler called from the Tk mainloop when the channel becomes readable or writable. Unlike file handlers, channels also work on Windows.
|
I'm curious, could Tcl channels be potentially used to build Tcl-based asyncio proactor event loop? |
|
Probably. This is what I want to try next. |
| Tcl_RegisterChannel(NULL, channel); | ||
| r = Tcl_SetChannelOption(NULL, channel, "-translation", "binary"); | ||
| if (r == TCL_OK) { | ||
| r = Tcl_SetChannelOption(NULL, channel, "-blocking", "0"); |
There was a problem hiding this comment.
Does setting the non-blocking flag affect the original socket?
There was a problem hiding this comment.
Yes. The duplicate shares the blocking mode with the original socket, so it is switched to non-blocking mode and stays in it after the channel is closed; sock.setblocking(True) restores it. Also, closing the channel shuts down reading from the socket, since Tcl closes the read side first. Both are now documented.
| { | ||
| (void)Tkch_Close(self); | ||
| } | ||
| PyErr_Clear(); |
There was a problem hiding this comment.
If del channel was called in an exception handler, it seems that deallocator swallows the original exception.
There was a problem hiding this comment.
Good catch. Fixed, and added a test.
| return NULL; | ||
| } | ||
| ENTER_TCL | ||
| channel = Tcl_MakeTcpClientChannel((ClientData)(intptr_t)sock); |
There was a problem hiding this comment.
Should we raise an exception if UDP or UNIX sock was passed?
There was a problem hiding this comment.
UNIX stream sockets work, and are used in tests. Datagram sockets are not supported, since message boundaries would be lost. It is now documented, but it is up to the user to pass a stream socket.
# Conflicts: # Doc/whatsnew/3.16.rst # Lib/test/test_tcl.py # Modules/clinic/_tkinter.c.h
Also document that the original file switches to non-blocking mode, and add Tkch_Type to globals-to-fix.tsv. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Documentation build overview
|
Add the
tk.createchannel()method, which creates a Tcl channel for a file, a pipe or a socket, using Tcl's channel API (Tcl_MakeFileChannel,Tcl_CreateChannelHandler).The returned
tkchannelobject supportsread(),write(),flush(),eof()andclose(), acreatehandler()/deletehandler()pair that callsfunc(channel, mask)when the channel becomes readable or writable, and thenameandclosedattributes. It is also a context manager that closes the channel on exit. Itsnamecan be passed to Tcl commands and options that take a channel, such as the channel option ofCanvas.postscript().The channel duplicates the file descriptor, handle or socket, so closing the channel does not close the original, and vice versa.
Unlike the Unix-only file handlers (
createfilehandler()/deletefilehandler()), channels work on all platforms, including Windows.