Tk Source Code

View Ticket
Login
Ticket UUID: 237971ceff64add7a1567a5bf0df58abc7c406c5
Title: [$canvas insert] changed behaviour
Type: Bug Version: 8.7a4
Submitter: emiliano Created on: 2021-01-28 00:22:28
Subsystem: 05. Canvas Items Assigned To: jan.nijtmans
Priority: 7 High Severity: Severe
Status: Closed Last Modified: 2022-10-28 22:41:10
Resolution: Fixed Closed By: jan.nijtmans
    Closed on: 2022-10-28 22:41:10
Description:
Sample script (canvas_bug.tcl)

package require Tk
pack [canvas .c]
set line [.c create line {0 0 50 50 100 50}]
.c insert $line end {200 200}
puts [package require Tk]
puts [.c coords $line]
after 2000 exit

emiliano@emiliano-netbook:~/src$ /home/emiliano/soft/bin/tclsh8.6 canvas_bug.tcl 
8.6.11
0.0 0.0 50.0 50.0 100.0 50.0 200.0 200.0
emiliano@emiliano-netbook:~/src$ tclsh8.7 canvas_bug.tcl 
8.7a4
0.0 0.0 50.0 50.0 200.0 200.0 100.0 50.0

As shown in the code, the behaviour of [$canvas insert] changed between 8.6 and 8.7

While one might argue that 8.7 behaviour is more "correct" (after all, [canvas insert] documentation describes that it will insert *before* "beforethis" element) the old behaviour is consistent with [linsert] in which [linsert $list $index] treats "end" as a special case of $index and inserts *after* the end and not *before*, as shown in this example:

% set list {a b c d}
a b c d
% set last [expr {[llength $list] - 1}]
3
% lindex $list end
d
% lindex $list $last
d
% linsert $list end foo
a b c d foo
% linsert $list $last foo
a b c foo d
User Comments: jan.nijtmans added on 2021-01-28 10:27:08:

Fix merged to 8.7 now.


jan.nijtmans added on 2021-01-28 08:07:05:

Proposed fix here: [079e0a0f88a4624b]. Thanks for your report and your testcase.


emiliano added on 2021-01-28 01:02:46:
Misread the docs. canvas(n) reads (emphasis mine)

end       Refers to the character or coordinate just *after* the last one
          in  the item (same as the number of characters or coordinates
          in the item).

more evidence that this is a bug.

Also, tested both text and polygon items.
Text items are right:

emiliano@emiliano-netbook:~/src$ cat canvas_bug_text.tcl 
package require Tk
pack [canvas .c]
set text [.c create text {100 100} -text "hello world"]
.c insert $text end "foo bar baz"
puts [package require Tk]
puts [.c itemcget $text -text]
emiliano@emiliano-netbook:~/src$ /home/emiliano/soft/bin/tclsh8.6 canvas_bug_text.tcl 
8.6.11
hello worldfoo bar baz
emiliano@emiliano-netbook:~/src$ tclsh8.7 canvas_bug_text.tcl 
8.7a4
hello worldfoo bar baz

Polygon items shows the same bug as line items:

emiliano@emiliano-netbook:~/src$ cat canvas_bug_polygon.tcl 
package require Tk
pack [canvas .c]
set poly [.c create polygon {0 0 50 50 100 50}]
.c insert $poly end {200 200}
puts [package require Tk]
puts [.c coords $poly]
emiliano@emiliano-netbook:~/src$ /home/emiliano/soft/bin/tclsh8.6 canvas_bug_polygon.tcl 
8.6.11
0.0 0.0 50.0 50.0 100.0 50.0 200.0 200.0
emiliano@emiliano-netbook:~/src$ tclsh8.7 canvas_bug_polygon.tcl 
8.7a4
200.0 200.0 0.0 0.0 50.0 50.0 100.0 50.0