Skip to content
Tcl Source Code View Ticket
Login

View Ticket

2016-07-09
13:27 • Closed ticket [1493a43044]: namespace upvar / TclOO problem plus 6 other cha... artifact: 47ca2a414a user: dkf
13:26
[1493a43044] Make [namespace upvar] ignore variable resolvers; the previous situation was completely... check-in: 3749c1071e user: dkf tags: core-8-6-branch
13:24 • Ticket [1493a43044] namespace upvar / TclOO problem status still Open with 4... artifact: 941152f68f user: dkf
2016-07-07
10:14 • Ticket [1493a43044]: 4 changes artifact: 5c36277369 user: dkf
2016-07-05
22:13 • Ticket [1493a43044]: 4 changes artifact: 9c407e65ec user: dkf
2016-07-04
22:59 • Ticket [1493a43044]: 3 changes artifact: 8812d40008 user: aspect
15:19 • Ticket [1493a43044]: 3 changes artifact: 0519bf3669 user: aspect
09:28 • Ticket [1493a43044]: 4 changes artifact: e567ef7f18 user: dkf
09:23 • Ticket [1493a43044]: 4 changes artifact: d95edc63b6 user: dkf
08:20 • Ticket [1493a43044]: 4 changes artifact: fd2a1176ea user: dkf
08:18
Add ability to disassemble TclOO constructors and destructors ([1493a43044] motivates) check-in: 5fc1ac9c4f user: dkf tags: core-8-6-branch
2016-07-03
11:50 • Ticket [1493a43044] namespace upvar / TclOO problem status still Open with 4... artifact: 222d9cf09e user: dkf
2016-05-29
09:30 • Ticket [1493a43044]: 3 changes artifact: f5722099d7 user: beric
07:45 • Ticket [1493a43044]: 3 changes artifact: f47c83d0e3 user: beric
2016-05-28
16:57 • Ticket [1493a43044]: 3 changes artifact: dc57338a40 user: beric
2016-05-17
15:01 • Ticket [1493a43044]: 3 changes artifact: 70e2c0dca5 user: dgp
14:46 • New ticket [1493a43044]. artifact: 463b7f8d02 user: beric

namespace upvar / TclOO problem

Closed / Fixed1493a4304444fcfbe6ed72638f404431e03a39d7Created 11 years agoUpdated 10 years ago
Descriptionplain2016-05-17 14:46:07
There is a problem using [namespace upvar] when - I guess - the target variable is also a variable of the class.

Here is a contrived test which shows the problem:

package require TclOO

oo::class create Master {
    variable member1 member2

    constructor {} {
        set member1 "m1"
        set member2 "m2"
    }
    method getChild {} {
        Child new [self]
    }
}

oo::class create Child {
    variable member1

    constructor {m} {
        set ns [info object namespace $m]
        namespace upvar $ns \
            member1 local_member1 \
            member2 local_member2

        foreach {vref vloc} {
            member1 local_member1
            member2 local_member2
        } {
            puts "$vref = [catch {set $vloc} err; set err]"
        }
    }
}

Master create m
m getChild

Outputs:

member1 = can't read "local_member1": no such variable
member2 = m2

Looks like "local_member1" refers to Child member1 variable instead of Master member1 variable.
Referenced by2
  • 3749c1071e[1493a43044] Make [namespace upvar] ignore variable resolvers; the previous situation was completely unintuitive. check-in2016-07-09 13:26:46
  • 5fc1ac9c4fAdd ability to disassemble TclOO constructors and destructors ([1493a43044] motivates) check-in2016-07-04 08:18:53
dgp Ticket [1493a43044]: 3 changes [History diff]2016-05-17 15:01:29
bericplain2016-05-28 16:57:55
Tying to find the problem...
Using [namespace upvar] through bytecode or via NamespaceUpvarObjCmd gives the same error.
By the way, tcl::unsupported::disassemble has no way to get bytecode from constructor or destructor.
bericplain2016-05-29 07:45:58
Debugging the non-bytecoded path, here is the stack of [namespace upvar]:

  ProcedureMethodCompiledVarConnect
  ProcedureMethodVarResolver
  TclLookupSimpleVar
  TclObjLookupVarEx
  NamespaceUpvarCmd

ProcedureMethodCompiledVarConnect returns the Var bound to "member1" of Child class, while [namespace upvar] is asking for a specific context (Master namespace).

Note that the bug is not present in 8.5.18 / TclOO 1.0.2.
bericplain2016-05-29 09:30:17
Similar to [631741]? Should AVOID_RESOLVERS flag be used?
dkfhtml2016-07-03 11:50:51

By the way, tcl::unsupported::disassemble has no way to get bytecode from constructor or destructor.
Hmm, that's correct. Missed it.

dkfhtml2016-07-04 08:20:44

The disassembler's now been enhanced on the 8.6 branch; it'll hit trunk and novem in due course.

dkfhtml2016-07-04 09:23:48

I can reproduce the problem, but don't really understand what is failing.

dkfhtml2016-07-04 09:28:31

And disassembling the constructor really doesn't help very much.

aspectplain2016-07-04 15:19:31
Just noting that it's hard to reproduce with a variation.  Moving the constructor body into a method has no effect, but:

Changing the source or destination to be a pure namespace (rather than an object) - no error.

Moving [variable member1] into the constructor, with or without [my] - no error.

Replacing [namespace upvar] with [upvar #0] - no error.


Minimal test case seems to be:

test .. .. -body {
  oo::class create M {
    variable v
    constructor {} {
      set v Mval
    }
  }

  oo::class create C {
    variable v
    method x {ns} {
      set v Cval
      namespace upvar $ns v l
      return $l
    }
  }

  M create m
  C create c
  c x [info object namespace m]
} -cleanup {
  M destroy
  C destroy
} -result Mval
aspecthtml2016-07-04 22:59:17

Also, yuck. Just eyeballing that code without knowing much about it, the proc-local variable is what I expect it to look up.

dkfhtml2016-07-05 22:13:30

The code is quite possibly imperfect. I've added a test case on the [bug-1493a43044] branch. The test currently fails, so I'm on a branch for now.

dkfhtml2016-07-07 10:14:57

Thank you for the hint about AVOID_RESOLVERS. It does appear to be exactly the missing ingredient precisely because it was previously internal to tclVar.c while namespace upvar is implemented in tclNamesp.c and tclExecute.c. Exposing (as the imaginatively named TCL_AVOID_RESOLVERS in tclInt.h) seems reasonable, and doesn't break any existing tests (while fixing this).

My main worry now is that this will cause problems with Itcl. Need to check before committing the fix back to a main branch…

dkfhtml2016-07-09 13:24:57

I've been thinking about this a bit more. When someone uses namespace upvar, they really do mean that they want the variable in that namespace, not to perform a name lookup according to that namespace's rules or the current namespace's rules either. As such, preventing the use of resolvers is exactly the right approach even in [incr Tcl] classes. If they wanted resolution, they wouldn't use namespace upvar (which isn't really itcl style anyway).

dkfplain2016-07-09 13:27:41
Fixed in 8.6 branch. Merge to trunk will happen at next inter-branch sync.
Log in (anonymous is fine) to add a comment.