Tcl Source Code

View Ticket
Login
Ticket UUID: 197e2bf85c08b9f468b764c30fab2f3c549592da
Title: info frame 'level' does not match documentation
Type: Bug Created on: 2013-07-14 07:53:48
Submitter: anonymous Assigned to: nobody
Subsystem: - New Builtin Commands Severity: Important
Priority: 5 Medium Last modified: 2013-07-14 07:53:48
Status: Open Closed by: nobody
Resolution: None Closed on:
Version: 8.6.0
Description:
This is probably an error in the documentation, not in the command.

"info frame" returns a dict with a 'level' entry, defined as

> level
>    This entry is present only if the queried frame has a corresponding frame returned by info level.
> It provides the index of this frame, relative to the current level (0 and negative numbers).

That isn't what actually happens. What happens appears to be:
1. The level is indeed a relative level, but uses the convention of 'uplevel', not 'info level', namely 0 or a positive number.
2. "corresponding" is rather vague, but it seems more accurate to say that 'level' is the caller's level rather than the level of the frame itself.
3. As far as I can tell, 'level' is always present.

Taken together, this is sufficiently different that the actual values are hard to interpret. Example:

set x 10

proc tst {x} {
    incr x
    eval eval tst2 $x
}

proc tst2 {x} {
    incr x
    set depth [info level]
    for {set i $depth} {$i > 0} {incr i -1} {
        set rel [expr $i - $depth]
        puts "level $i ($rel) = [info level $i]"
    }
    set depth [info frame]
    for {set i $depth} {$i > 0} {incr i -1} {
        set frame [info frame $i]
        set level [dict get $frame level]
        set rel [expr $i - $depth]
        puts "frame $i ($rel) -> level $level ([dict get $frame cmd])"
    }
}

When called as "tst $x" this prints

level 2 (0) = tst2 11
level 1 (-1) = tst 10
frame 5 (0) -> level 0 (info frame $i)
frame 4 (-1) -> level 1 (tst2 11)
frame 3 (-2) -> level 1 (eval tst2 11)
frame 2 (-3) -> level 1 (eval eval tst2 $x)
frame 1 (-4) -> level 2 (tst $x)

From the documentation, I expected
frame 5 -> (no level)
frame 4 -> level 0
frame 3 -> (no level)
frame 2 -> (no level)
frame 1 -> level -1

However, if you try to use 'uplevel' to determine what the values of $i and $x are in the commands, then it turns out the returned levels are exactly right: add
        puts "[uplevel $level subst [list [dict get $frame cmd]]]"
to the above loop.

-Marcel van der Goot