Skip to content
Tcl Source Code View Ticket
Login

View Ticket

auto_mkindex produces bad tclIndex file in some situations w

Deleted219265ffffffffffffffffffffffffffffffffffCreated 26 years agoUpdated 26 years ago
Descriptionplain2000-10-26 05:09:37
OriginalBugID: 1115 Bug
Version: 8.0.4
SubmitDate: '1999-01-26'
LastModified: '2000-02-02'
Severity: MED
Status: UnAssn
Submitter: welch
ChangedBy: ericm
RelatedBugIDs: 1187 1559 1558 1549 1481
OS: Solaris
Machine: Other
FixedDate: '2000-10-25'
ClosedDate: '2000-10-25'


Name:
Jim Inham

Extensions:
Itcl3.0.1

CustomShell:
This is vanilla Tcl + Itcl3.0.1 unmodified.

ReproducibleScript:
Make two files:

main.tcl:
namespace import itcl::*

class.itcl:
class foo {}

Then do:

itclsh3.0
% auto_mkindex [pwd] main.tcl class.itcl



ObservedBehavior:
The tclIndex file will be empty, except for the header comments.



DesiredBehavior:
There should be an entry for foo in the tclIndex



Patch:
*** init.tcl.old        Wed Jan 20 18:25:34 1999
--- init.tcl.new        Wed Jan 20 18:25:52 1999
***************
*** 717,724 ****
        if {$args == ""} {
            set args *.tcl
        }
-       auto_mkindex_parser::init
        foreach file [eval glob $args] {
            if {[catch {auto_mkindex_parser::mkindex $file} msg] == 0} {
                append index $msg
            } else {
--- 717,724 ----
        if {$args == ""} {
            set args *.tcl
        }
        foreach file [eval glob $args] {
+         auto_mkindex_parser::init
            if {[catch {auto_mkindex_parser::mkindex $file} msg] == 0} {
                append index $msg
            } else {
***************
*** 727,734 ****
                cd $oldDir
                error $msg $info $code
            }
        }
-       auto_mkindex_parser::cleanup
  
        set fid [open "tclIndex" w]
        puts $fid $index nonewline
--- 727,734 ----
                cd $oldDir
                error $msg $info $code
            }
+         auto_mkindex_parser::cleanup
        }
  
        set fid [open "tclIndex" w]
        puts $fid $index nonewline




PatchFiles:
library/init.tcl





The original bug is kind of bogus, since the namespace import statement should never be in a file that goes into the auto_mkindex line.  I fixed this, by the way, in our code...

However, it reveals what to me seems a weakness in the auto_mkindex 
architecture.  auto_mkindex is trying to treat each file on the auto_mkindex 
line independently, which is why it tracks namespace imports in the 
auto_mkindex_parser::parser, and tries to remove them after each file is 
processed.  However, this will eventually lose against another package that has "command producing commands" which live in a namespace, like itcl::class.  

That package needs to import these by hand so that files that are meant to be run in an environment where the user has already done

namespace import itcl::*

will get properly processed.  Itcl does this by adding the import using auto_mkindex_slavehook in itcl/unix/tclAppInit.c.  But this means that
any time you import itcl::* in ANY other code, the imports from the environment will get blown away...

My solution is to REALLY clean out the auto_mkindex_parser::parser, by deleting and recreating it between each file.  This is a little slower in theory, though not detectably so.  However, it is MUCH cleaner, and more robust.
---------------

I don't understand the problem here.  Can you provide further explainantion?

- eric

-- 02/01/2000 ericm

From Jim:

Anyway, back to this case.  The behavior that I reported here is
straightforward, and wrong.  I am not sure where the confusion lies.
If I do a "namespace import" in a file that I include in the
auto_mkindex list before the files that define the namespace, then the
commands defined in that namespace don't show up in the tclIndex file.
This is just a bug, it is pretty simple.

Moreover, it has bitten a number of people on the Itcl mailing list at
least, and I have sent them the fix I gave below, which solved the
problem.  It comes up, for instance, if you have a fairly
straightforward Itcl application, with a few application tcl files,
and a main.tcl file that does the imports, all in the same directory.
It is quite common in that case to have a make rule like:

TCL := $(*.tcl)

ITCL_SH = itclsh3.0

tclIndex: $(TCL) Makefile
    echo "auto_mkindex 'pwd' $(TCL)" | $(ITCL_SH)

This is a very simple way to generate the tclIndex, and means you
don't have to mess with adding files to the Make line every time you
add a new one.  However, in this setup, you will hit the bug I reported
if the ordering happens to be wrong.  For no obvious reason, you will
end up with an incomplete tclIndex file.  This situation does not seem 
so contrived to me.

When I brought this up with Brent, he agreed that this was a real
concern, and that the patch I suggested was an acceptable solution.  I
really can't figure out how to explain the problem more clearly than I
did originally.  You just need to read the auto.tcl file to see how it
works, and have a look at how itcl adds elements to the
auto_mkindex_parser namespace to generate class command references for
Itcl classes.  This little bit of code is somewhat narly, but not that
bad.  If you are still confused, you should ask Brent, he can probably
do a better job of explaining what is going on in person than I can by
mail.

-- 02/02/2000 ericm

More from Brent/Jim:

From [email protected] Wed Feb  2 14:06:05 2000
Date: Wed, 2 Feb 2000 12:04:49 -0800 (PST)
From: James Ingham <[email protected]>
To: Brent Welch <[email protected]>
Cc: Eric Melski <[email protected]>, [email protected]
Subject: Re: Scriptics Bug Database--report 1187 (fwd) 

Brent, and Eric...

Here is the contents of Brent and my previous discussion (at least
your side of it, I didn't go dig my replies out of my sent box, I
don't think they are necessary...  Almost at the bottom are the bits
of code that Brent hacked out...

Jim

From: Brent Welch <[email protected]>
To: James Ingham <[email protected]>
Subject: Re: [Bug Tcl 8.0.4] auto_mkindex produces bad tclIndex file in some 
 situations when used with Itcl3.0
Date: Tue, 26 Jan 1999 13:35:17 -0800

I understood you moving the parser_init stuff inside the per-file loop.

I haven't understood your weirdo namespace import bug :-)
Can you spoon feed the scenario to me again?

>>>James Ingham said:
 > Brent,
 > 
 > Thanks...  It took a little while to fathom this one.  The mkindex
 > code is pretty cool, once you wrap your head around it, however...
 > 
 > Have you had a chance to think about the wierdo namespace import bug I 
 > mentioned?  Michael is right to think that it is kind of bogus that a
 > namespace import should force the whole set of commands in a namespace 
 > to be loaded, but I don't see a good way around the export list
 > issue.  Perhaps there needs to be a namespace preexport or something
 > gross like that that gets written into the tclIndex file and populates 
 > the namespace export list in a way that will get blown away when a
 > real namespace export command is called for the namespace...
 > 
 > It would be neat to get Michael's stuff working, but it may be too
 > much trouble...
 > 
 > Jim
 > 
 > -- 
 > ++==++==++==++==++==++==++==++==++==++==++==++==++==++==++==++==++==++==++
 > Jim Ingham                                              [email protected]
 > Cygnus Solutions Inc.

From: Brent Welch <[email protected]>
To: James Ingham <[email protected]>
cc: [email protected]
Subject: Re: [Bug Tcl 8.0.4] auto_mkindex produces bad tclIndex file in some 
 situations when used with Itcl3.0
Date: Tue, 26 Jan 1999 15:50:54 -0800

OK, so I'm aware of Michaels scheme to first create stubs and
then have them load their real implementation.  Are you saying
it is broken?  entirely, or just in some cases?

OK - I believe I understand.  Listen, I prototyped some code
that tries to remember what export commands to do.
Can you take a peek at the attached file and see if it
is plausible?  Look at the very end for a tweak to
auto_import and another utility called 'addIndex',
which logically belongs in init.tcl

>>>James Ingham said:

 > So what he does is to write an auto_import proc that looks at the
 > auto_index array, gets all the matches with the import pattern, and
 > make STUB commands for them in the source namespace.  The stub
 > commands are then the ones that get imported into the target
 > namespace.  Then, when the imported command is actually used, the
 > stub command deletes itself, and sources in the real command instead.
 > 
 > 
 > Making the stub commands is pretty fast, so this is a big win in
 > startup time, if there are a lot of packages in namespaces.
 > 
 > The problem with this is that the tclIndex file knows nothing about
 > the export list for the source namespace, and so NO COMMANDS are
 > exported into the target namespace in the actual body of the namespace 
 > import C command.  

I can't make the leap to this conclusion without more details.

 > The only way I can think of to fix this problem is to put something in 
 > the tclIndex file that records what namespace export lists were
 > registered as the code was analysed by the auto_mkindex process.
 > However, this is not entirely trivial...
 > 
 > Anyway, as it stands, Itcl is pretty broken.  You basically can't do
 > namespace import using Itcl, and then have the proc be autoloaded
 > using the imported version of the proc.  This at least needs to be
 > fixed (by whacking Michael's version of the auto_import command.)

So - the .tcl file has "namespace exports" that trigger the
creation of stub procedures.  The caller does "import ns::*"
and this creates ns::* procedures that are stubs (in the Itcl case).
And what is missing is the "namespace export" command has not
been executed.  Right?  (I think so).
So, the fix is to have the code that generates the stubs also
generate the "namespace export" command.

 > Working to fix up Michael's stuff would be cool, but would require
 > some more careful engineering to get it right.  Think for instance, of 
 > the horrors you could get into if a namespace was broken up into
 > several files, and "namespace -clear" was EVER used...  Yecch!

I'm inclined to to worry about the weird cases so much...

--Brent Welch<[email protected]>
http://www.scriptics.com
Scriptics: The Tcl Platform Company

#
# itcl.tcl
# ----------------------------------------------------------------------
# Invoked automatically upon startup to customize the interpreter
# for [incr Tcl].
# ----------------------------------------------------------------------
#   AUTHOR:  Michael J. McLennan
#            Bell Labs Innovations for Lucent Technologies
#            [email protected]
#            http://www.tcltk.com/itcl
#
#      RCS:  $Id: itcl.tcl,v 1.2 1998/08/11 14:40:44 welch Exp $
# ----------------------------------------------------------------------
#            Copyright (c) 1993-1998  Lucent Technologies, Inc.
# ======================================================================
# See the file "license.terms" for information on usage and
# redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES.

# ----------------------------------------------------------------------
#  USAGE:  local <className> <objName> ?<arg> <arg>...?
#
#  Creates a new object called <objName> in class <className>, passing
#  the remaining <arg>'s to the constructor.  Unlike the usual
#  [incr Tcl] objects, however, an object created by this procedure
#  will be automatically deleted when the local call frame is destroyed.
#  This command is useful for creating objects that should only remain
#  alive until a procedure exits.
# ----------------------------------------------------------------------
proc ::itcl::local {class name args} {
    set ptr [uplevel eval [list $class $name] $args]
    uplevel [list set itcl-local-$ptr $ptr]
    set cmd [uplevel namespace which -command $ptr]
    uplevel [list trace variable itcl-local-$ptr u  "itcl::delete object $cmd; list"]
    return $ptr
}

# ----------------------------------------------------------------------
# auto_mkindex
# ----------------------------------------------------------------------
# Define Itcl commands that will be recognized by the auto_mkindex
# parser in Tcl...
#

#
# USAGE:  itcl::class name body
# Adds an entry for the given class declaration.
#
foreach cmd {itcl::class itcl_class} {
    auto_mkindex_parser::command $cmd {name body} {
addIndex $name

        variable parser
        variable contextStack
        set contextStack [linsert $contextStack 0 $name]
        $parser eval $body
        set contextStack [lrange $contextStack 1 end]
    }
}

#
# USAGE:  itcl::body name arglist body
# Adds an entry for the given method/proc body.
#
auto_mkindex_parser::command itcl::body {name arglist body} {
    addIndex $name
}

#
# USAGE:  itcl::configbody name arglist body
# Adds an entry for the given method/proc body.
#
auto_mkindex_parser::command itcl::configbody {name body} {
    addIndex $name
}

#
# USAGE:  ensemble name ?body?
# Adds an entry to the auto index list for the given ensemble name.
#
auto_mkindex_parser::command itcl::ensemble {name {body ""}} {
    addIndex $name
}

#
# USAGE:  public arg ?arg arg...?
#         protected arg ?arg arg...?
#         private arg ?arg arg...?
#
# Evaluates the arguments as commands, so we can recognize proc
# declarations within classes.
#
foreach cmd {public protected private} {
    auto_mkindex_parser::command $cmd {args} {
        variable parser
        $parser eval $args
    }
}

# ----------------------------------------------------------------------
# auto_import
# ----------------------------------------------------------------------
# This procedure overrides the usual "auto_import" function in the
# Tcl library.  It is invoked during "namespace import" to make see
# if the imported commands reside in an autoloaded library.  If so,
# stubs are created to represent the commands.  Executing a stub
# later on causes the real implementation to be autoloaded.
#
# Arguments -
# patternThe pattern of commands being imported (like "foo::*")
#               a canonical namespace as returned by [namespace current]

proc auto_import {pattern} {
    global auto_index
    global auto_import

    set ns [uplevel namespace current]
    set patternList [auto_qualify $pattern $ns]

    auto_load_index

    foreach pattern $patternList {
        foreach name [array names auto_index $pattern] {
            if {"" == [info commands $name]} {
                ::itcl::import::stub create $name
if {[info exists auto_import($name)]} {
    set ns [namespace qualifiers $name]
    namespace eval $ns [list namespace export $auto_import($name)]
}
            }
        }
    }
}

proc auto_mkindex_parser::addIndex {name} {
    variable index
    variable scriptFile

    # Add the usual auto_index element for the fully-qualified name

    set full [fullname $name]
    append index "set [list auto_index($full)]"
    append index " \[list source \[file join \$dir [list $scriptFile]\]\]\n"

    # See if the name is exported, and if so, note that in auto_import

    set ns [namespace qualifiers $full]
    set tail [namespace tail $full]
    foreach pat [namespace eval $ns { namespace export }] {
if {[string match $pat $tail]} {
    append index "[list set auto_import($full) $pat]\n"
    break
}
    }
}

--==_Exmh_7642084930--

From: Brent Welch <[email protected]>
To: James Ingham <[email protected]>
cc: [email protected]
Subject: Re: [Bug Tcl 8.0.4] auto_mkindex produces bad tclIndex file in some 
 situations when used with Itcl3.0
Date: Wed, 27 Jan 1999 15:15:30 -0800

OK - groan - I re-wrapping my mind around the mkindex parser.
The namespace wrapper is maintaining a "contextStack" variable,
which I think has our namespace information back on the master side.

Hmm - the namespace wrapper doesn't even create the namespaces
in the slave interpreter - so maybe we don't have to execute
namespace export commands either.  We just save their export
commands on the master and do all introspection through those 
variables. So, something like:

auto_mkindex_parser::command namespace {op args} {
    variable $parser
    switch -- $op {
        eval {
            variable parser
            variable contextStack
            variable exports

            set name [lindex $args 0]
            set args [lrange $args 1 end]

            if {![info exist exports($name)]} {
                 set exports($name) {}
            }
            set contextStack [linsert $contextStack 0 $name]
            if {[llength $args] == 1} {
                $parser eval [lindex $args 0]
            } else {
                eval $parser eval $args
            }
            set contextStack [lrange $contextStack 1 end]
        }
        import {
            variable parser
            variable imports
            foreach pattern $args {
                if {$pattern != "-force"} {
                    lappend imports $pattern
                }
            }
            catch {$parser eval "_%@namespace import $args"}
        }
        export {
            variable parser
            variable exports
            variable contextStack
            set current [lindex $contextStack 0]
            foreach a $args {
                lappend exports($current) $a
            }
        }   
    }
}

(The export list needs to get cleaned up)
and

proc auto_mkindex_parser::addIndex {name} {
    variable index
    variable scriptFile
    variable parser
    variable contextStack
    variable exports

    # Add the usual auto_index element for the fully-qualified name

    set full [fullname $name]
    append index "set [list auto_index($full)]"
    append index " \[list source \[file join \$dir [list $scriptFile]\]\]\n"

    # See if the name is exported, and if so, note that in auto_import

    set ns [namespace qualifiers $full]
    set tail [namespace tail $full]
    set current [lindex $contextStack 0]
    foreach pat $exports($current) {
        if {[string match $pat $tail]} {
            append index "[list set auto_import($full) $pat]\n"
            break
        }
    }
}

From: Brent Welch <[email protected]>
To: James Ingham <[email protected]>
Subject: Re: [Bug Tcl 8.0.4] auto_mkindex produces bad tclIndex file in some 
 situations when used with Itcl3.0
Date: Wed, 27 Jan 1999 18:18:03 -0800

OK - unfortunately this is a change in both Tcl and Itcl,
so I'll have to set time aside soon to do some testing myself.

>>>James Ingham said:
 > Brent,
 > 
 > That looks pretty groovy.  You have found the missing piece that I
 > could not get right...  I am kind of pressed right now, so I don't
 > have time to try it out yet, but this looks right.
 > 
 > Do you need to do something about the use of "namespace export
 > -clear"?  I think if someone does this, you pretty much lose.  There
 > are two possible cases:
 > 
 > 1) They do namespace export & namespace export -clear in the same
 > file.  You could handle this, but it is such a stupid thing to do that 
 > I don't know why you would want to.
 > 
 > 2) If there is a namespace export in one file, and a namespace export
 > -clear in another, for the same namespace.  There is no good way to
 > handle this, since you can't guarantee that the order in which you
 > process the files in the auto_mkindex process, and the order that they
 > are sourced in when you actually load stuff will be the same (or even
 > THAT both files will get sourced in when you load a given proc, for
 > that matter)
 > 
 > In any case, namespace export -clear seems to me to be more of a
 > development process command, and not a release command, so I wouldn't
 > worry too much about it unless you can think of some really clever way 
 > to handle it.
 > 
 > Jim.

--Brent Welch<[email protected]>
http://www.scriptics.com
Scriptics: The Tcl Platform Company

This is the end of what I have got...  Hope this helps.

Jim

-- 02/02/2000 ericm
dgp Ticket [219265ffff]: 2 changes [History diff]2001-02-08 13:37:03
Log in (anonymous is fine) to add a comment.