TIP 207: Add a -namespace Option to [interp invokehidden]

Login
Bounty program for improvements to Tcl and certain Tcl packages.
Author:         Joe Mistachkin <joe@mistachkin.com>
Author:         Don Porter <dgp@users.sf.net>
State:          Final
Type:           Project
Vote:           Done
Created:        29-Jun-2004
Post-History:   
Tcl-Version:    8.5

Abstract

This TIP proposes a -namespace option for the interp invokehidden subcommand to allow hidden commands to be invoked in the specified namespace context in the slave interpreter.

Rationale

Safe interpreters and namespaces have been useful and important features of Tcl since version 8.0. However, it can sometimes be difficult to use them together effectively. For example, while you can invoke a hidden command in a safe interpreter with interp invokehidden, such as source, you cannot specify the target namespace context where the command should be invoked. Using the interp eval subcommand is not an option because that, by design, does not allow hidden commands to be invoked. In addition, since the interp invokehidden subcommand expects a command name, namespace eval and similar constructs that would typically be used to change the target namespace context cannot be used.

Specification

interp invokehidden path ?-namespace namespace? ?-global? ?--? hiddenCmdName ?arg ...?

The interp invokehidden subcommand would continue to function as the current documentation dictates with the following notable exceptions:

  • The -namespace specifies the target namespace context in the slave interpreter where the command should be invoked starting from the global namespace in the slave interpreter.

  • The target namespace context specified by the -namespace option is created in the slave interpreter if it does not already exist.

  • If both the -global and -namespace options are present, the -namespace option is ignored.

A -- option (with the standard meaning) will be added as well.

The slave-interpreter command version of invokehidden will be updated in an equivalent way.

Example Usage

The following example scripts illustrates how this feature might be used.

################################################
# file inside.tcl
################################################

set x "this is a test from inside [namespace current]."

################################################
# file invoke.tcl
################################################

set i [interp create -safe]
set file [file join [file dirname [info script]] inside.tcl]

interp invokehidden $i -namespace ::test::foo::bar source $file

set result [interp eval $i [list set ::test::foo::bar::x]]

# should be: "this is a test from inside ::test::foo::bar."
puts stdout $result

Proposed Internal C API Changes

To be determined.

Reference Implementation

A reference implementation of this TIP is availble http://sf.net/tracker/?func=detail&aid=981841&group_id=10894&atid=310894 .

Comments

DGP: Note that the interp command (including the interp invokehidden subcommand) was added for Tcl 7.5. The namespace command arrived later in Tcl 8.0. This proposal observes and proposes to correct that the interp invokehidden command was not updated to account for the arrival of namespaces. Clearly the -global option indicates that control over evaluation context is something the caller needs. This command simply failed to evolve in a timely manner. This proposal is an overdue correction.

Looking ahead, I'd be happy if the -global option was dropped from the documentation, so that only the new -namespace option is favored, and so that the recommended way one forces evaluation in the global namespace is with -namespace ::. The -global option would still work through the remaining Tcl 8 releases, but no longer be recommended for new code.

Copyright

This document has been placed in the public domain.

History