Skip to content

gh-162537: Add tkinter support for Tcl channels - #162538

Open
serhiy-storchaka wants to merge 4 commits into
python:mainfrom
serhiy-storchaka:tkinter-channel
Open

serhiy-storchaka wants to merge 4 commits into
python:mainfrom
serhiy-storchaka:tkinter-channel

Conversation

@serhiy-storchaka

@serhiy-storchaka serhiy-storchaka commented Nov 24, 2026 •

Copy link
Copy Markdown
Member

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 tkchannel object supports read(), write(), flush(), eof() and close(), a createhandler() / deletehandler() pair that calls func(channel, mask) when the channel becomes readable or writable, and the name and closed attributes. It is also a context manager that closes the channel on exit. Its name can be passed to Tcl commands and options that take a channel, such as the channel option of Canvas.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.

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.
@bedevere-app bedevere-app Bot added the type-feature A feature request or enhancement label Nov 24, 2026
@asvetlov

Copy link
Copy Markdown
Contributor

I'm curious, could Tcl channels be potentially used to build Tcl-based asyncio proactor event loop?

@serhiy-storchaka

Copy link
Copy Markdown
Member Author

Probably. This is what I want to try next.

Comment thread Modules/_tkinter.c
Tcl_RegisterChannel(NULL, channel);
r = Tcl_SetChannelOption(NULL, channel, "-translation", "binary");
if (r == TCL_OK) {
r = Tcl_SetChannelOption(NULL, channel, "-blocking", "0");

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does setting the non-blocking flag affect the original socket?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread Modules/_tkinter.c Outdated
{
(void)Tkch_Close(self);
}
PyErr_Clear();

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If del channel was called in an exception handler, it seems that deallocator swallows the original exception.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch. Fixed, and added a test.

Comment thread Modules/_tkinter.c
return NULL;
}
ENTER_TCL
channel = Tcl_MakeTcpClientChannel((ClientData)(intptr_t)sock);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we raise an exception if UDP or UNIX sock was passed?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

serhiy-storchaka and others added 3 commits November 26, 2026 16:41
# 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>
@read-the-docs-community

Copy link
Copy Markdown

Documentation build overview

📚 cpython-previews | 🛠️ Build #36192214 | 📁 Comparing 6e06be4 against main (62a7400)

  🔍 Preview build  

3 files changed
± library/tkinter.html
± whatsnew/3.16.html
± whatsnew/changelog.html

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

awaiting core review type-feature A feature request or enhancement

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants