Tcl 8.6.7 and Expect 5.45.3 on FreeBSD 11.1-RELEASE-p6
Regarding trap, the Expect book indicates that "if Expect is evaluating a Tcl or Expect procedure or command when a signal occurs, it is possible to change the return code that would otherwise be returned. Given a -codeflag, the trap command substitutes the return code of the trap handler for the return code that would have been returned."
Likewise, the manual page says that "the -code flag uses the return code of the command in place of whatever code Tcl was about to return when the command originally started running."
I have noted that trap appears to be doing this regardless of whether the -code flag is specified. My understanding is that the usage of trap without -code shouldn't affect the return code of the proc or command being evaluated when a signal occurs.
I am able to reproduce this behaviour with the following code:
package require Expect 5.45.3
trap {
puts "Hello, World"
} SIGINT
proc random_int {} {
return [expr {int( rand() * 1000 )}]
}
while {1} {
puts [expr {[random_int] * [random_int]}]
}
When run, this code simply spits out the product of two randomly selected integers. When ^C is supplied via the terminal, one would expect that "Hello, World" would be printed and the infinate loop continue. However, hold down ^C for a few seconds and the following error occurs:
...
...
...
150102
10108
851720
^CHello, World
can't use empty string as operand of "*"
while executing
"expr {[random_int] * [random_int]}"
("while" body line 2)
invoked from within
"while {1} {
puts [expr {[random_int] * [random_int]}]
}"
(file "./expect_trap_bug.tcl" line 17)
Clearly this usage appears to go against the book and manual page. Would someone be able to confirm whether or not this is a bug? Reimplemented signal handling using TclX does not exhibit this behaviour (signal is interrupted without affecting return value of proc being evaluated).
Thanks,
Matt
Hi Matt,
I'll try to have a look at this over the coming days.
Cheers,
Nils