|
2026-05-22
| ||
| 10:53 | • Closed ticket [2969488fff]: trace variable doesn't fire if you upvar to an a... artifact: 683a9ba72c user: apnadkarni | |
|
2022-08-26
| ||
| 18:59 | • Ticket [2969488fff]: 3 changes artifact: 888b111058 user: chw | |
| 18:42 | • Ticket [2969488fff]: 3 changes artifact: 473e42ef2c user: chw | |
|
2022-08-25
| ||
| 08:13 | • Ticket [2969488fff]: 3 changes artifact: ef31021d18 user: sbron | |
| 07:53 | • Ticket [2969488fff]: 3 changes artifact: e86a330989 user: chw | |
|
2022-08-24
| ||
| 12:23 | • Ticket [2969488fff]: 4 changes artifact: 1073b4edc3 user: oehhar | |
| 12:17 | • Ticket [2969488fff]: 3 changes artifact: 291b9ab90e user: sbron | |
| 12:11 | • Add attachment tip634.patch to ticket [2969488fff] artifact: 7b63536073 user: sbron | |
|
2022-08-18
| ||
| 07:55 | • Ticket [2969488fff] trace variable doesn't fire if you upvar to an array ele... artifact: f91294df1b user: oehhar | |
|
2022-08-17
| ||
| 11:06 | • Ticket [2969488fff]: 6 changes artifact: 667ffd5e7d user: sbron | |
|
2011-04-12
| ||
| 09:42 | • Ticket [2969488fff]: 1 change artifact: 7ce4fb7f5e user: msofer | |
|
2010-03-13
| ||
| 07:18 | • Ticket [2969488fff]: 4 changes artifact: bc75d9345f user: ferrieux | |
| 07:18 | • Add attachment upvartraces.patch to ticket [2969488fffffffff|2969488fff... artifact: 5fc483ac40 user: ferrieux | |
| 07:15 | • Ticket [2969488fff] trace variable doesn't fire if you upvar to an array ele... artifact: b785af33c1 user: ferrieux | |
| 01:08 | • Ticket [2969488fff]: 4 changes artifact: f2de07bc9b user: dgp | |
| 00:22 | • Ticket [2969488fff]: 4 changes artifact: 1e20552e8e user: ferrieux | |
|
2010-03-12
| ||
| 23:46 | • Ticket [2969488fff]: 4 changes artifact: 193cf2f6f0 user: msofer | |
| 23:35 | • Ticket [2969488fff]: 4 changes artifact: bef42a2a34 user: twylite | |
| 23:20 | • Ticket [2969488fff]: 4 changes artifact: cd0e336e5a user: dkf | |
| 16:04 | • New ticket [2969488fff]. artifact: f41113ff11 user: twylite | |
trace variable doesn't fire if you upvar to an array element
(This is FR 572889 from June 2002 converted to a bug)
Tcl Versions: all 8.4 and onwards; confirmed on 8.4.11 and 8.6b1.1
OS platform and version: probably all, confirmed on Windows XP + SP3
Problem behaviour:
If you set a trace on an array, then link to a specific element of that array using [upvar], any changes made via the linked variable will not cause traces to fire.
Expected behaviour:
If a trace has been placed on a variable, any access to that variable (even indirectly) should invoke the trace.
Additional detail / Use case
I have a "fileentry" megawidget that combines an entry, a button with an icon, and a tk_getOpenFile / tk_getSaveFile. If the button is clicked the appropriate file dialog is displayed, and the selected filename written back into the -textvariable. The obvious implementation is to link to the -textvariable using [upvar]. If the -textvariable is an array element then traces on the element will not fire. In this case I am using variable traces to maintain consistency between various UI fields, so I lose consistency if the trace doesn't fire.
Workarounds
(1) Change the UI to use a scalar rather than an array element for this field
(2) set and dereference the varname given by -textvariable directly rather than using [upvar]
(3) have the megawidget use the entry's get/insert/delete methods rather than accessing -textvariable.
Code sample
---
proc tracer {args} { puts "<trace> $args" }
set ui(field1) "alpha"
trace add variable ui write "tracer write"
trace add variable ui array "tracer array"
set ui(field1) "beta" ;# trace write fires
array set ui {field1 "gamma"} ;# trace write & array both fire
proc alter {varname} {
upvar #0 $varname v
set v "delta"
}
alter ui(field1) ;# trace doesn't fire
---
This has an impact on ::env traces, though things are even worse there.
I don't know what the impact is on ::env. IMHO ::env should be changed to a command [env] or similar and not managed as a variable, but that's a different discussion. If there are conditions under which a traced variable can change without the trace being invoked the whole value of trace is undermined. I can't rely on it, and must change the application's design not use this behaviour. I can use a number of useful tricks if my UI widgets link to elements in an array (e.g. setting up defaults, loading/saving records, reverting changes all become trivial). With certain UIs I may have to rethink that strategy as I cannot use [trace] to protect the consistency of the variables or the UI as a whole.
IIRC, the "unsolvable problem" (without some redesign) is that an array element does not know to which array it belongs. If it is accessed as a scalar, that info is just not available. Will look again.
Hmm, I think it's not as unsolvable as it seems.
The test TclIsVarDirectWritable(varPtr) is currently:
!((varPtr)->flags & (VAR_ARRAY|VAR_LINK|VAR_TRACED_WRITE|VAR_DEAD_HASH))
and in this case it returns 'true' because the linked array element's flags are only
VAR_ARRAY_ELEMENT | VAR_IN_HASHTABLE
Now, I believe we can still reach the containing array with moderate additional complexity , because the VAR_IN_HASHTABLE shows that the (Var *) is in fact a (VarInHash *), whose entry->tablePtr is the array's backing hashtable. Dunno exactly how easily we could climb back from hashtable to array, but we could even add a field in the hashtable struct for that. The important thing is that we don't need a per-element field.
Miguel, do you agree with this view ?
In the realm of workarounds, see also what the tcltest package does to work around the problem (search for 572889).
Attaching a patch doing 99% of the job :/ (The 1% is failing tests in trace-16.* by giving a misleading error message "no such variable" vs "no such element in array". Will look into this later.) Method: (1) Enrich TclVarHashTable with an arrayPtr field, backlinking to the parent array when it exists, NULL otherwise. (2) Enrich the various TclIsVarDirectXXXable with proper tests on the parent array (3) At the beginning of the TclPtr(Set,Get,Incr,Unset) functions, when the arrayPtr passed is NULL, check again, and overwrite it with the parent array from the table. As a result, array elements now know their parent, *and* the superficial arrayPtr estimated from the var's syntax (an upvar may look like a scalar) is no longer enough to hide the truth. Doc update is just the removal of that ugly paragraph describing the old behavior. Tests need a specific case checking for this (apparently no one bothered to test that ugly documented behavior !)
File Added - 366551: upvartraces.patch
I just ran into this problem again today. Considering the multiple bug reports of similar issues over the last 20 years, I'm clearly not the only one.
Tcl 9.0 seems like the perfect opportunity to rectify the situation. Can the fix attached to this ticket please make it into 9.0?
Schelte,
thanks for the great effort !
Maybe, you may drive the process:
- Provide the patch in a branch and add tests
- If it is a general discussion, perhaps write a TIP for 9.0
Take care, Harald
I attached an updated version of ferrieux' patch that will apply against Tcl 9.0a4. This also addresses the failing tests in trace-16.* and ::env traces.
Tests for the new functionality are included.
Schelte, perhaps, you may ask for commit right to TCL core & friends and provide it in a branch ?
Thanks, Harald
Schelte, since your patch can be applied to core-8-branch as well, why not target TIP#634 for Tcl 8.7? Yet should it be feasible to backport it for 8.6.
The fact that an [upvar] to an array element won't trigger a trace on the entire array is a documented feature (in the upvar manual page). So, in theory, people may use it on purpose to change an array without firing the trace. This means that this is a breaking change. Those can only go into 9.0.
Schelte, please consider the changes in https://www.androwish.org/home/info/1982e49611f2a9f3 which try to fix the case of an unknown array element tracked by a trace on an entire array. In the current implementation, this does not fire the trace, which makes ::env inconsistent.
Additionally to TIP#634/635 I propose to make a function similar to
Var *TclObjLookupVar(Tcl_Interp *interp,
Tcl_Obj *part1Ptr, const char *part2,
int flags, const char *msg,
const int createPart1, const int createPart2,
Var **arrayPtrPtr);
a public (aka tclDecls.h) function in order to enable extension
writers to deal with upvar'ed array elements as you did in tclEnv.c.
The sole objective is to be able to find out the real names of the
variable referenced in the trace callback.
Presumably fixed as part of TIP 634/635
```
c:\src\tcltk\main\tcl\win>tclsh z.tcl
<trace> write ui field1 write
<trace> array ui {} array
<trace> write ui field1 write
<trace> write v field1 write
```
Attachments:
- upvartraces.patch [download] added by ferrieux on 2010-03-13 07:18:30. [details]