When And Where Signals Are Evaluated

Signal handlers are evaluated in the global scope. Global variables are directly accessible. Local variables in current procedures are inaccessible.

Ideally, handlers are evaluated immediately after the signals are received. However, in reality there may be a delay before evaluating handlers in order to preserve the consistency of the internal data structures of Expect.

Generally, you can count on signal handlers being evaluated before each Tcl command. Consider the following command:

set a [expr $b*4]

A signal that arrives just prior to this line in a script has its handler evaluated immediately. If a signal arrives while expr is executing, expr completes, the signal handler is run, and then the set command is executed. If a signal arrives while set is executing, the handler is deferred until just before the next command in the script.

Signal handlers are also evaluated during most time-consuming operations such as I/O. For example, if an expect command is waiting for a process to produce output, signal handlers can be executed.

Because signals are handled between each command and in the middle of long-running commands, delays in handling signals should not be significant and you should not be able to notice them even when using Expect interactively. There is one exception however. If a signal handler is in the process of being evaluated, no other signal handlers can be evaluated. For example, the following fragment prints acb after a ^C is pressed. ...

Get Exploring Expect now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.