Tcl Source Code

View Ticket
Login
Ticket UUID: 572889
Title: upvar links do not fire array read trace
Type: RFE Created on: 2002-06-23 21:43:28
Submitter: dgp Assigned to: dgp
Subsystem: 46. Traces Severity: Minor
Priority: 5 Medium Last modified: 2026-11-15 14:14:38
Status: Closed Closed by: jan.nijtmans
Resolution: Fixed Closed on: 2026-11-15 14:14:38
Version: None
Description:
% set a(foo) 1
1
% trace variable a r {puts a ;#}
%  set a(foo)
a
1
% upvar 0 a(foo) baz
%  set baz
1

I would like to see the trace fire when
I read from baz.
User Comments:
msofer added on 2002-06-24 23:56:38:
Logged In: YES 
user_id=148712

upvar.n dixit: "If otherVar refers to an element of an
array, then variable traces set for the entire array will
not be invoked when myVar is accessed (but traces on the
particular element will still be invoked). In particular, if
the array is env, then changes made to myVar will not be
passed to subprocesses correctly."

So, is this a bug or a FR? Does it need a TIP?

dgp added on 2002-06-25 00:09:53:
Logged In: YES 
user_id=80530

Thanks.  If this is to change, it would be a FR/TIP.
Don't feel strong enough about it to do that now,
but I'll shift this over the FR tracker in case
anyone else wants to pick it up.

Note to self: read *all* relevant man pages before
filing bugs...

Workaround seems to be to set traces on both
the source and target of the [upvar].  Works fine,
then, if annoying.

dgp added on 2003-04-18 13:28:07:
Logged In: YES 
user_id=80530


Joe, you were asking about this on the chat.

serhiy.storchaka added on 2026-11-13 11:18:45:

Implemented by TIP 634 in Tcl 9.0: traces on the whole array now fire when the array element is accessed through an upvar link, and the upvar manual page was updated accordingly.


dgp added on 2026-11-13 16:15:31:
Please note there is code in the tcltest package 
that is a workaround of the (mis)feature reported
in this ticket.  Someone with the time might examine
whether this workaround can be phased out now.

serhiy.storchaka added on 2026-11-13 17:51:03:

The workaround is the list of option variables in tcltest, which each get their own auto-configure read trace because a trace on the Option array did not fire through their upvar links. On Tcl 9.0 and later the trace on Option is enough, but tcltest still runs on Tcl 8.6 (and 8.5), where the workaround is still needed.

Proposed change in branch tcltest-array-trace-upvar: the per-variable traces are only set before Tcl 9.0.


jan.nijtmans added on 2026-11-15 14:14:38:

Branch branch tcltest-array-trace-upvar is now merged to trunk, core-9-0-branch and core-8-6-branch. Good idea (without any ill-effects)