From: Matthias Kievernagel on
From: Matthias Kievernagel <mkiever(a)Pirx.sirius.org>
Subject: py3 tkinter acceps bytes. why?
Newsgroups: comp.lang.python
Summary:
Keywords:

In a recent thread named "py3 tkinter Text accepts what bytes?"
(google groups link:
http://groups.google.com/group/comp.lang.python/browse_thread/thread/b75ed69f4e81b202/e2aff9ddd62d210c?lnk=raot)
I asked what kinds of bytes are accepted as tkinter parameters.
I still wonder why they are accepted at all.
Does anyone know a reason for this
or has a link to some relevant discussion?

Thanks for any hint,
Matthias Kievernagel


From: Terry Reedy on
On 5/4/2010 10:17 AM, Matthias Kievernagel wrote:
> From: Matthias Kievernagel<mkiever(a)Pirx.sirius.org>
> Subject: py3 tkinter acceps bytes. why?
> Newsgroups: comp.lang.python
> Summary:
> Keywords:
>
> In a recent thread named "py3 tkinter Text accepts what bytes?"
> (google groups link:
> http://groups.google.com/group/comp.lang.python/browse_thread/thread/b75ed69f4e81b202/e2aff9ddd62d210c?lnk=raot)
> I asked what kinds of bytes are accepted as tkinter parameters.
> I still wonder why they are accepted at all.
> Does anyone know a reason for this
> or has a link to some relevant discussion?

I do not remember any particular public discussion of tkinter on the dev
lists. I suspect the reason is a) avoid breaking old code and b) tkinter
(tk inter-face) passes params on to tk which expects byte strings. I
would not be surprised if tkinter has to encode py3 (unicode) strings
before passing them on.

The Py3 changes were greatly complicated by the need to interface with
the older bytes world.

Terry Jan Reedy

From: Martin v. Loewis on
> In a recent thread named "py3 tkinter Text accepts what bytes?"
> (google groups link:
> http://groups.google.com/group/comp.lang.python/browse_thread/thread/b75ed69f4e81b202/e2aff9ddd62d210c?lnk=raot)
> I asked what kinds of bytes are accepted as tkinter parameters.
> I still wonder why they are accepted at all.

That's basically because the port from 2.x was shallow: it supports the
exact same feature set that 2.x supports, with just the type names swapped.

Tkinter in 2.x will already use Unicode strings in most situations.
However, 2.x code would often also provide byte strings for text
properties, and Tcl supports that "nicely" (*)

Regards,
Martin

(*) I think you also asked how Tcl interprets things. IIUC, it first
assumes that a byte string is UTF-8 (starting from 8.1 or so); if it's
not, it then assumes that it's in the locale's encoding. If that turns
out incorrect also, it just tries to pass it as-is to the operating
system GUI API. Or something like that.
From: Matthias Kievernagel on

>>Me:
>> I asked what kinds of bytes are accepted as tkinter parameters.
>> I still wonder why they are accepted at all.
>> Does anyone know a reason for this
>> or has a link to some relevant discussion?
>Terry Reedy:
> I do not remember any particular public discussion of tkinter on the dev
> lists. I suspect the reason is a) avoid breaking old code and b) tkinter
> (tk inter-face) passes params on to tk which expects byte strings. I
> would not be surprised if tkinter has to encode py3 (unicode) strings
> before passing them on.

If I interpret the code in _tkinter.c correctly
Python bytes are converted to a String Tcl_Obj
and Python str to a Unicode Tcl_Obj:

AsObj(PyObject *value)
{
....
if (PyBytes_Check(value))
return Tcl_NewStringObj(PyBytes_AS_STRING(value),
PyBytes_GET_SIZE(value));
....
else if (PyUnicode_Check(value)) {
....
return Tcl_NewUnicodeObj(inbuf, size);
....
}

And as Martin pointed out:
>That's basically because the port from 2.x was shallow: it supports the
>exact same feature set that 2.x supports, with just the type names
>swapped.

If I don't want bytes to get passed to tkinter
I just have to raise an exception in AsObj, no?
Or is it even sufficient to just remove the bytes case?

Regards and thanks for your answers.
Matthias Kievernagel.

From: Martin v. Loewis on
> If I don't want bytes to get passed to tkinter
> I just have to raise an exception in AsObj, no?
> Or is it even sufficient to just remove the bytes case?

But why would you want that? There are commands which legitimately
return bytes, e.g. the file and network io libraries of Tcl (not that
you would usually want to use them in Python, but Tkinter is actually
Tclinter, and should support all Tcl commands).

Regards,
Martin