py3 tkinter Text accepts what bytes?

Matthias Kievernagel mkiever at Pirx.sirius.org
Fri Apr 23 08:00:30 EDT 2010


Hello,

I stumbled upon this one while porting some of my programs
to Python 3.1. The program receives messages from a socket
and displays them in a tkinter Text. Works fine in Python 2
and Python 3.1. The problems arrived when I wanted to know
the details...

First surprise: Text.insert accepts not only str
but also bytes.

So I looked into the sources to see how it is done.
I found no magic in 'tkinter.__init__.py'. All python
objects seem to go unchanged to _tkinter.c.
There they are turned into Tcl objects using Tcl_NewUnicodeObj
(for str) and Tcl_NewStringObj (for bytes).
The man page for Tcl_NewStringObj says that it creates
a tcl string from utf-8 encoded bytes.
So I continued to test...

Second surprise: Text.insert also works for latin-1 encoded bytes.
It even works with mixed utf-8 and latin-1 encoded bytes.
At least it works for me.

Anyone can enlighten me, where this magic is done?
Is it tcl magic or did I miss something in the python sources?
Is this somewhere documented?

Thanks for any hints,
Matthias Kievernagel




More information about the Python-list mailing list