TIP 140: Tracing Namespace Modifications

Login
Bounty program for improvements to Tcl and certain Tcl packages.
State:		Deferred
Type:		Project
Tcl-Version:	8.5
Vote:		Pending
Post-History:	
Author:		Donal K. Fellows <donal.k.fellows@man.ac.uk>
Created:	04-Jun-2003

Abstract

This TIP allows scripts to register callbacks to monitor alterations to namespaces, such as their deletion, the creation, renaming and deletion of commands within the namespace and modifications to the exports list of the namespace.

Rationale

As part of my work on an experimental [112] implementation, I have discovered that computing the list of public (i.e. exported) commands from a namespace is a comparatively expensive operation. This means that it is necessary (well, highly desirable) to cache the list of exports from a namespace. But then I thought that it might be nice to expose that interface to use to better enable namespaces to support other interesting uses, and that it should be possible to implement the default behaviour in a Tcl script. It is in support of this that I present this TIP; I also add the ability to trace deletion of the namespace as a fairly obvious extension.

Proposed Change at Tcl Script Level

I propose that the [trace] command be extended with a new type of thing to trace: a namespace. The operations that would trigger a trace are:

  • Namespace deletion (subject to the same sorts of restrictions in relation to deleted interpreters that are present for traces of command deletion) with the ops value delete.

  • Command list modification (being creation, renaming or deletion of any command within that namespace) with the ops value command. Modification due to deletion of the namespace will not trigger traces with this operation, but instead the delete operation outlined above (subject to the caveats relating to interpreter deletion.)

  • Exports list modification (i.e. updates with [namespace export]) with the ops value export. Modification due to the deletion of the namespace will not trigger this traces with this operation, but instead the delete operation outlined above (subject to the caveats relating to interpreter deletion.)

In each case, the trace callback script will have the fully qualified name of the namespace and the name of the triggering operation appended to it before execution. In the case of command list modification by the creation of a command (which might also be through import of commands from another namespace, for example) there will be an extra parameter being the local name of the command (without namespace separators.)

These changes will be propagated to each of the [trace add], [trace remove] and [trace info] subcommands, of course.

Proposed Change at C API Level

Yet to be designed, but will be based as closely as possible on the existing Tcl trace APIs.

Copyright

This document has been placed in the public domain.

History