Skip to content
Tcl Source Code Ticket Change Details
Login

Ticket Change Details

Overview

Artifact ID: c549d8dfcf27f77b81e200fdb0a553225c03b489
Ticket: 218607ffffffffffffffffffffffffffffffffff
array trace doesn't fire when an element is aliased with upv
User & Date: nobody on 2000-10-26 01:03:02
Changes

  1. assignee changed to: "nobody"
  2. cmimetype changed to: "text/plain"
  3. comment changed to:
    OriginalBugID: 1433 Bug
    Version: 8.0.4
    SubmitDate: '1999-03-04'
    LastModified: '2000-01-20'
    Severity: MED
    Status: Released
    Submitter: pat
    ChangedBy: ericm
    RelatedBugIDs: 3917
    OS: All
    FixedDate: '2000-01-20'
    FixedInVersion: 8.3
    ClosedDate: '2000-01-20'
    
    
    Name:
    Kevin B. Kenny
    
    Extensions:
    None relevant
    
    CustomShell:
    No relevant modifications
    
    ReproducibleScript:
    # When a procedure upvar's an array element to a scalar variable, and
    # then set's the scalar variable, write traces on the array do not fire.
    # This allows the array to change "behind the back" of the trace.
    
    # The following script exhibits the problem:
    
    proc callback1 { n1 n2 op } {
        variable callback1_data
        set callback1_data [list $n1 $n2 $op]
        return
    }
    
    proc callback2 { n1 n2 op } {
        variable callback2_data
        set callback2_data [list $n1 $n2 $op]
        return
    }
    
    proc set_by_alias { name value } {
        upvar 1 $name variable
        set variable $value
    }
    
    set data(test) {}
    set callback1_data {}
    set callback2_data {}
    trace variable data w callback1
    trace variable data(test) w callback2
    
    set_by_alias data(test) test
    
    puts [list $callback1_data $callback2_data]
    
    
    
    
    ObservedBehavior:
    The array trace does not fire.  The callback1 procedure is never invoked,
    and callback1_data continues to contain the null string.
    
    The script therefore prints
    
    {} {variable {} w}
    
    
    
    DesiredBehavior:
    This is rather a peculiar case.  Since the 'data' array is not present
    in the calling namespace *as an array*, but only via the alias, the trace
    procedure can't get a name2 argument.  It might make sense to call it
    with name1 equal to the upvar-alias and name2 equal to the null string, which
    would at least let it find the content.  Unfortunately, this solution
    gives the trace procedure no way to determine which array element is being set!
    
    An alternative might be to call the trace procedure uplevel'ed into the
    scope in which the array is defined.  In this case, name1 and name2 could
    be the original array name and subscript.  In addition to a bunch of changes
    in the trace logic, this alternative would require some changes in the
    Var structure in tclInt.h, so that an array element can find its array
    and an upvar alias can find the defining stack frame.
    
    A third alternative would be just to document the existing behavior, and
    warn that array traces will not work in this case.  In the program that I
    was working with, this alternative would be acceptable, since I was able
    to replace the trace on the array with traces on the relevant elements without
    any real trouble.
    
    
    This problem appears to affect all extant versions of Tcl from the
    8.0 general release through the latest 8.1 sources.
    
    When the developers decide what to do about this, I'd appreciate a note
    via e-mail.  The chosen solution affects some documentation that I'm writing.
    
    
    
    This is actually the designed behavior.  The problem is that as further levels of indirection (more upvars) and more traces are added, it becomes less and less clear what the behavior should be; in order to avoid that "slippery slope", the behavior was defined this way.  Consider:
    
    proc update {name ind op} {
            puts "name $name index $index"
    }
    
    proc getUpdate {inVar} {
            upvar $inVar localVar
            trace variable localVar w update
            getUpdate2 localVar
    }
    
    proc getUpdate2 {inVar} {
            upvar $inVar localVar2
            set localVar2 100
    }
    
    set somevar(1) test
    trace variable somevar w update
    getUpdate somevar(1)
    
    In this case, one could persuasively make arguments for many different ideas of what the behavior should be.  It could fire both traces with the name somevar and the index 1; it could fire one trace with the name somevar and the index 1 and the other with the name localVar and no index; it could fire both traces with the name localVar2; it could fire neither trace.  I'm sure there are other possibilities.
    
    Of the choices, one stands out as being the easiest to implement and the least confusing:  fire both traces with the name localVar2.  I consider this the least confusing because all of the variables refer to the same object, and the trace is on that object, so changing the object's value should fire all the traces.  And calling one trace with one name and another trace with another name when setting the same object would be confusing.  Thus the behaviour is defined as it is.  This is now more clearly documented in the upvar man page in 8.3.
    
    - eric
    
    -- 01/20/2000 ericm
    
    
    
  4. foundin changed to: "obsolete: 8.0.4"
  5. priority changed to: "5 Medium"
  6. resolution changed to: "None"
  7. status changed to: "Open"
  8. submitter changed to: "nobody"
  9. subsystem changed to: "07. Variables"
  10. title changed to:
    array trace doesn't fire when an element is aliased with upv
    
  11. type changed to: "Bug"