Tk Source Code

View Ticket
Login
Ticket UUID: 3e33602eaebbcf043b6ebdbcd8c4a29813cb23d5
Title: Inconsistent bind substitution of %S
Type: Bug Version: 8.6.9
Submitter: anonymous Created on: 2021-05-16 05:38:00
Subsystem: 01. Bindings Assigned To: jan.nijtmans
Priority: 5 Medium Severity: Minor
Status: Closed Last Modified: 2021-05-22 16:14:04
Resolution: Fixed Closed By: jan.nijtmans
    Closed on: 2021-05-22 16:14:04
Description:
While playing around with bind and events, I made a small process that simply logs events and every %-substitution--even for events which do not use them all. For easier peeking inside events, I formatted all the info in to fixed size columns, which is when I noticed this issue.

What I noticed was the following for %S (uppercase S):
-- As expected, if %S has no meaning for a specified event, it is simply replaced with "??".
-- As expected, when the code does have meaning and a value not equal to 0, it is replaced by an "0xNNNNNNNN" hexadecimal code, as documented.
-- *NOT* as expected, when the value is 0, it is replaced by "00000000", and not "0x00000000". I.E., the leading "0x" prefix is not present.

Given the Tcl mantra, this is a problem because it gave incorrect results in my initial logging code:
    ... elseif { [ regexp -nocase {^0x[0-9a-f]{8}$} $subwin ] == 1 } { ...

Obviously, I changed my code to work around this (by making it match with or without the "0x"). But I can imagine some poor soul at 2:00 AM pulling out their hair trying to figure out why their 2-million-lines-of-code flagship application is behaving mysteriously when every line of code seems completely correct.

I have not fully checked everywhere for similar problems, but the documentation for %a also states "... formatted as a hexadecimal number ..."--In this case, all my samples logged only have "??" or "00000000", and not any other value(s).
User Comments: jan.nijtmans added on 2021-05-22 16:14:04:

Thanks, all, for your comments.

Fixed now in core-8-6-branch and trunk. Since extending to 8 hex digits is only done on X11, I now changed that in 8.7 to match MacOS and Windows behavior, so all 3 platforms use the same format.


anonymous added on 2021-05-22 10:25:20:
My assumption was such--it was a NULL value and likely had no useful meaning for most purposes. (Part of my thinking on this, however, is mostly aimed at debugging Tk apps where it should *not* be NULL...)

Just a thought... Since the specific problem is a documented and known issue with the specifier and only on Linux, maybe it is possible to detect when it is NULL/zero and handle that (with a ?: trinary operator)? Other than that, I leave it to you experts! (I already refactored my own Tcl/Tk code to handle both missing '0x' and more/less than 8 hexadecimal digits.) :-)

fvogel added on 2021-05-22 07:23:14:

This explains how events with NULL -subwindow show up: they are generated by the OS, not by Tk. When the -subwindow field has no meaning for the event, it is left to the initialized NULL value.

Now when it comes to printing this value, TkpPrintWindowId has a different implementation for each platform. The format specifier on Windows is

   "0x%" TCL_Z_MODIFIER "x"
while macOs uses
   "0x%lx"
and Linux uses
   "%#08lx"
That last one creates the problem because, as Jan already spotted it, the (documented) behaviour of the "#" format character only causes a "0x" prefix if the value is not 0.

Could we use "0x%lx" on Linux as we already do on macOS? Actually we used "0x%x" until [d188a1554439cf51] changed it 20 years ago. But I think we could do this change. Jan, may I leave this to your expertise?


anonymous added on 2021-05-22 02:13:28:
My OS is:
$ uname --all
Linux earth 5.4.0-0.bpo.4-amd64 #1 SMP Debian 5.4.19-1~bpo10+1 (2020-03-09) x86_64 GNU/Linux

The script I am using simply enumerates all the (documented) bind %-substitutions in to a dict-style string ("... -subwiin \{%S\} ...", all the key names have a - prefix). then enumerates all of the (documented) bind events (<Enter>, <Leave>, et cetera), and saves them to a log file. It is not doing anything else as I am only trying to learn and explore how all this works: Looking at the log file to see what events are being generated/thrown in response to user actions along with he parameters available. (The actual script code is a little more complex because I use my own private library/package of commonly used utilities, such as one to backslash escape characters special to Tcl, and so on, so I can "replay" the log file back to my Tcl/Tk program(s) using the same event sequences.)

Here are some examples from my log file:
*** 1 {::fnEvent "Enter"                                  -serial    {398}        -above     {}               -button    {}        -count     {}        -detail    {NotifyVirtual}                -focus     {1}       -height    {}           -window    {0xc40000c}       -keycode   {}         -mode      {NotifyNormal}       -override  {}        -place     {}       -state     {16}                                -time      {1266701329}       -width     {}           -x         {41}         -y         {2}         -unicode   {}        -border    {}        -delta     {}       -synevt    {0}       -keysym    {}                -keysymhex {}            -property  {}                                     -rootwin   {0x0006b1}       -subwin    {0xc40000d}          -type      {7}        -path      {.}                        -xroot     {1869}       -yroot     {964}        -matched   {0}      }
*** 1 {::fnEvent "Enter"                                  -serial    {398}        -above     {}               -button    {}        -count     {}        -detail    {NotifyVirtual}                -focus     {1}       -height    {}           -window    {0xc40000d}       -keycode   {}         -mode      {NotifyNormal}       -override  {}        -place     {}       -state     {16}                                -time      {1266701329}       -width     {}           -x         {41}         -y         {2}         -unicode   {}        -border    {}        -delta     {}       -synevt    {0}       -keysym    {}                -keysymhex {}            -property  {}                                     -rootwin   {0x0006b1}       -subwin    {0xc40000e}          -type      {7}        -path      {.frmMain}                 -xroot     {1869}       -yroot     {964}        -matched   {0}      }
*** 1 {::fnEvent "Enter"                                  -serial    {398}        -above     {}               -button    {}        -count     {}        -detail    {NotifyAncestor}               -focus     {1}       -height    {}           -window    {0xc40000e}       -keycode   {}         -mode      {NotifyNormal}       -override  {}        -place     {}       -state     {16}                                -time      {1266701329}       -width     {}           -x         {41}         -y         {2}         -unicode   {}        -border    {}        -delta     {}       -synevt    {0}       -keysym    {}                -keysymhex {}            -property  {}                                     -rootwin   {0x0006b1}       -subwin    {00000000}           -type      {7}        -path      {.frmMain.btnStatus}       -xroot     {1869}       -yroot     {964}        -matched   {1}      }
*** 1 {::fnEvent "Motion"                                 -serial    {398}        -above     {}               -button    {}        -count     {}        -detail    {}                             -focus     {}        -height    {}           -window    {0xc40000e}       -keycode   {}         -mode      {}                   -override  {}        -place     {}       -state     {16}                                -time      {1266701329}       -width     {}           -x         {41}         -y         {2}         -unicode   {}        -border    {}        -delta     {}       -synevt    {0}       -keysym    {}                -keysymhex {}            -property  {}                                     -rootwin   {0x0006b1}       -subwin    {00000000}           -type      {6}        -path      {.frmMain.btnStatus}       -xroot     {1869}       -yroot     {964}        -matched   {0}      }
*** 1 {::fnEvent "Motion"                                 -serial    {421}        -above     {}               -button    {}        -count     {}        -detail    {}                             -focus     {}        -height    {}           -window    {0xc40000e}       -keycode   {}         -mode      {}                   -override  {}        -place     {}       -state     {16}                                -time      {1266701337}       -width     {}           -x         {59}         -y         {8}         -unicode   {}        -border    {}        -delta     {}       -synevt    {0}       -keysym    {}                -keysymhex {}            -property  {}                                     -rootwin   {0x0006b1}       -subwin    {00000000}           -type      {6}        -path      {.frmMain.btnStatus}       -xroot     {1887}       -yroot     {970}        -matched   {0}      }


If needed, I can supply the script as-is, or show some more examples from the log file that was generated.

As a side note: I personally view this is a very minor "bug" since I was able to adjust my code to recognize and handle the situation, and I cannot conceive of a common real use case where it will cause problems--only edge cases and possibly trying to debug production code problems (without being aware of the issue). Thus, it may be better to simply document it in 8.6.* Tk documentation, and possibly fix it 8.7.* if it is easy to do so.

jan.nijtmans added on 2021-05-17 13:33:50:

For UNIX, this can be found here: https://core.tcl-lang.org/tk/file?udc=1&ln=191&ci=3e1f2d5e2f134261&name=unix%2FtkUnixPort.h

Indeed, "#" format character only causes a "0x" prefix if the value is not 0.

MacOS and Windows don't use "08", there the printed value is not '0'-extended until a length of 8. Most logical would be to adapt this to work on UNIX the same as on MacOS and Windows. But then your regexp will fail too, since the number might have less than 8 digits. Anyway it is dangerous to change this in Tk 8.6, but we could do it in Tk 8.7.

Thanks!


fvogel added on 2021-05-16 08:50:03:
On what platform are you experiencing this please? The code formatting the %S substitution output in Tk is platform-dependent (even if perhaps the bug is not).

Also, can you show a script demonstrating a %S (subwindow) substitution being zero? I couldn't [event generate] one with '-subwindow 0'. Such a script would help for the debug and could later be turned into a non-regression test. Thanks!