Skip to content
Tcl Source Code Artifact [44f8d3532e]
Login

Artifact [44f8d3532e]

Artifact 44f8d3532e234503b9778f55a6f6389490a7e5a22eb2863ff22c26f31270f107:



CommandName: info ManualSection: n Version: 8.4 TclPart: Tcl TclDescription: Tcl Built-In Commands Links:

Name

info - Information about the state of the Tcl interpreter

Synopsis

::: {.synopsis} ::: [info]{.cmd} [option]{.arg} [arg arg]{.optdot} :::

Description

Available commands:

info args fIprocnamefR : Returns the names of the parameters to the procedure named fIprocnamefR.

info body fIprocnamefR : Returns the body of the procedure named fIprocnamefR.

info class fIsubcommand classfR ?fIarg ...fR : Returns information about the class named fIclassfR. See fBCLASS INTROSPECTIONfR below.

info cmdcount : Returns the total number of commands evaluated in this interpreter.

info cmdtype fIcommandNamefR : Returns a the type of the command named fIcommandNamefR. Built-in types are:

alias : commandName was created by fBinterp aliasfR. In a safe interpreter an alias is only visible if both the alias and the target are visible.

coroutine : commandName was created by fBcoroutinefR.

ensemble : commandName was created by fBnamespace ensemblefR.

import : commandName was created by fBnamespace importfR.

native : commandName was created by the fBTcl_CreateObjCommandfR interface directly without further registration of the type of command.

object : commandName is the public command that represents an instance of fBoo::objectfR or one of its subclasses.

privateObject : commandName is the private command, fBmyfR by default, that represents an instance of fBoo::objectfR or one of its subclasses.

proc : commandName was created by fBprocfR.

interp : commandName was created by fBinterp createfR.

zlibStream : commandName was created by fBzlib streamfR.

info commands ?fIpatternfR? : Returns the names of all commands visible in the current namespace. If fIpatternfR is given, returns only those names that match according to fBstring matchfR. Only the last component of fIpatternfR is a pattern. Other components identify a namespace. See fBNAMESPACE RESOLUTIONfR in the fBnamespacefR(n) documentation.

info complete fIcommandfR : Returns 1 if fIcommandfR is a complete command, and fB0fR otherwise. Typically used in line-oriented input environments to allow users to type in commands that span multiple lines.

info constant fIvarNamefR : Returns 1 if fIvarNamefR is a constant variable (see fBconstfR) and 0 otherwise.

info consts ?fIpatternfR? : Returns the list of constant variables (see fBconstfR) in the current scope, or the list of constant variables matching fIpatternfR (if that is provided) in a manner similar to fBinfo varsfR.

info coroutine : Returns the name of the current fBcoroutinefR, or the empty string if there is no current coroutine or the current coroutine has been deleted.

info default fIprocname parameter varnamefR : If the parameter fIparameterfR for the procedure named fIprocnamefR has a default value, stores that value in fIvarnamefR and returns fB1fR. Otherwise, returns fB0fR.

info errorstack ?fIinterpfR? : Returns a description of the active command at each level for the last error in the current interpreter, or in the interpreter named fIinterpfR if given. The description is a dictionary of tokens and parameters. Tokens are currently either fBCALLfR, fBUPfR, or fBINNERfR, but other values may be introduced in the future. fBCALLfR indicates a command call, and its parameter is the corresponding fBinfo levelfR fB0fR. fBUPfR indicates a shift in variable frames generated by fBuplevelfR or similar, and applies to the previous fBCALLfR item. Its parameter is the level offset. fBINNERfR identifies the "inner context", which is the innermost atomic command or bytecode instruction that raised the error, along with its arguments when available. While fBCALLfR and fBUPfR provide a trail of the call path, fBINNERfR provides details of the offending operation in the innermost procedure call, even to sub-expression granularity. This information is also present in the fB-errorstackfR entry of the options dictionary returned by 3-argument fBcatchfR; fBinfo errorstackfR is a convenient way of retrieving it for uncaught errors at top-level in an interactive fBinterpreterfR.

info exists fIvarNamefR : Returns fB1fR if a variable named fIvarNamefR is visible and has been defined, and fB0fR otherwise.

info frame ?fIdepthfR? : Returns the depth of the call to fBinfo framefR itself. Otherwise, returns a dictionary describing the active command at the fIdepthfR, which counts all commands visible to fBinfo levelfR, plus commands that don't create a new level, such as fBevalfR, fBsourcefR, or fIuplevelfR. The frame depth is always greater than the current level. If fIdepthfR is greater than fB0fR it is the frame at that depth. Otherwise it is the number of frames up from the current frame. As with fBinfo levelfR and error traces, for nested commands like "foo [bar [x]]", only "x" is seen by fBinfo framefR invoked within "x". The dictionary may contain the following keys:

type : Always present. Possible values are fBsourcefR, fBprocfR, fBevalfR, and fBprecompiledfR.

source : A script loaded via the fBsourcefR command.

proc : The body of a procedure that could not be traced back to a line in a particular script.

eval : The body of a script provided to fBevalfR or fBuplevelfR.

precompiled : A precompiled script (loadable by the package fBtbcloadfR), and no further information is available.

line : The line number of of the command inside its script. Not available for fBprecompiledfR commands. When the type is fBsourcefR, the line number is relative to the beginning of the file, whereas for the last two types it is relative to the start of the script.

file : For type fBsourcefR, provides the normalized path of the file that contains the command.

cmd : The command before substitutions were performed.

proc : For type fBprocfR, the name of the procedure containing the command.

lambda : For a command in a script evaluated as the body of an unnamed routine via the fBapplyfR command, the definition of that routine.

level : For a frame that corresponds to a level, (to be determined).

When a command can be traced to its literal definition in some script, e.g. procedures nested in statically defined procedures, and literal eval scripts in files or statically defined procedures, its type is fBsourcefR and its location is the absolute line number in the script. Otherwise, its type is fBprocfR and its location is its line number within the body of the procedure.

In contrast, procedure definitions and fBevalfR within a dynamically fBevalfRuated environment count line numbers relative to the start of their script, even if they would be able to count relative to the start of the outer dynamic script. That type of number usually makes more sense.

A different way of describing this behaviour is that file-based locations are tracked as deeply as possible, and where this is not possible the lines are counted based on the smallest possible fBevalfR or procedure body, as that scope is usually easier to find than any dynamic outer scope.

The syntactic form fB{*}fR is handled like fBevalfR. I.e. if it is given a literal list argument the system tracks the line number within the list words as well, and otherwise all line numbers are counted relative to the start of each word (smallest scope)

info functions ?fIpatternfR? : If fIpatternfR is not given, returns a list of all the math functions currently defined. If fIpatternfR is given, returns only those names that match fIpatternfR according to fBstring matchfR.

info globals ?fIpatternfR? : If fIpatternfR is not given, returns a list of all the names of currently-defined global variables. Global variables are variables in the global namespace. If fIpatternfR is given, only those names matching fIpatternfR are returned. Matching is determined using the same rules as for fBstring matchfR.

info hostname : Returns the name of the current host. This name is not guaranteed to be the fully-qualified domain name of the host. Where machines have several different names, as is common on systems with both TCP/IP (DNS) and NetBIOS-based networking installed, it is the name that is suitable for TCP/IP networking that is returned.

info level ?fIlevelfR? : If fInumberfR is not given, the level this routine was called from. Otherwise returns the complete command active at the given level. If fInumberfR is greater than fB0fR, it is the desired level. Otherwise, it is fInumberfR levels up from the current level. A complete command is the words in the command, with all substitutions performed, meaning that it is a list. See fBuplevelfR for more information on levels.

info library : Returns the value of fBtcl_libraryfR, which is the name of the library directory in which the scripts distributed with Tcl scripts are stored.

info loaded ?fIinterpfR? ?fIprefixfR? : Returns the name of each file loaded in fIinterpfR by the fBloadfR command with prefix fIprefixfR . If fIprefixfR is not given, returns a list where each item is the name of the loaded file and the prefix for which the file was loaded. For a statically-loaded package the name of the file is the empty string. For fIinterpfR, the empty string is the current interpreter.

info locals ?fIpatternfR? : If fIpatternfR is given, returns the name of each local variable matching fIpatternfR according to fBstring matchfR. Otherwise, returns the name of each local variable. A variables defined with the fBglobalfR, fBupvarfR or fBvariablefR is not local.

info nameofexecutable : Returns the absolute pathname of the program for the current interpreter. If such a file can not be identified an empty string is returned.

info object fIsubcommand objectfR ?fIarg ...fR : Returns information about the object named fIobjectfR. fIsubcommandfR is described fBOBJECT INTROSPECTIONfR below.

info patchlevel : Returns the value of the global variable fBtcl_patchLevelfR, in which the exact version of the Tcl library initially stored.

info procs ?fIpatternfR? : Returns the names of all visible procedures. If fIpatternfR is given, returns only those names that match according to fBstring matchfR. Only the final component in fIpatternfR is actually considered a pattern. Any qualifying components simply select a namespace. See fBNAMESPACE RESOLUTIONfR in the fBnamespacefR(n) documentation.

info script ?fIfilenamefR? : Returns the pathname of the innermost script currently being evaluated, or the empty string if no pathname can be determined. If fIfilenamefR is given, sets the return value of any future calls to fBinfo scriptfR for the duration of the innermost active script. This is useful in virtual file system applications.

info sharedlibextension : Returns the extension used on this platform for names of shared libraries, e.g. fB.sofR under Solaris. Returns the empty string if shared libraries are not supported on this platform.

info tclversion : Returns the value of the global variable fBtcl_versionfR, in which the major and minor version of the Tcl library are stored.

info vars ?fIpatternfR? : If fIpatternfR is not given, returns the names of all visible variables. If fIpatternfR is given, returns only those names that match according to fBstring matchfR. Only the last component of fIpatternfR is a pattern. Other components identify a namespace. See fBNAMESPACE RESOLUTIONfR in the fBnamespacefR(n) documentation. When fIpatternfR is a qualified name, results are fully qualified. A variable that has been declared but not yet given a value will be included in the results.

Class introspection

The following fIsubcommandfR values are supported by fBinfo classfR:

info class call fIclass methodfR : Returns a description of the method implementations that are used to provide a stereotypical instance of fIclassfR's implementation of fImethodfR (stereotypical instances being objects instantiated by a class without having any object-specific definitions added). This consists of a list of lists of four elements, where each sublist consists of a word that describes the general type of method implementation (being one of fBmethodfR for an ordinary method, fBfilterfR for an applied filter, fBprivatefR for a private method, and fBunknownfR for a method that is invoked as part of unknown method handling), a word giving the name of the particular method invoked (which is always the same as fImethodfR for the fBmethodfR type, and "unknown" for the fBunknownfR type), a word giving the fully qualified name of the class that defined the method, and a word describing the type of method implementation (see fBinfo class methodtypefR). Note that there is no inspection of whether the method implementations actually use fBnextfR to transfer control along the call chain, and the call chains that this command files do not actually contain private methods.

info class constructor fIclassfR : This subcommand returns a description of the definition of the constructor of class fIclassfR. The definition is described as a two element list; the first element is the list of arguments to the constructor in a form suitable for passing to another call to fBprocfR or a method definition, and the second element is the body of the constructor. If no constructor is present, this returns the empty list.

info class definition fIclass methodfR : This subcommand returns a description of the definition of the method named fImethodfR of class fIclassfR. The definition is described as a two element list; the first element is the list of arguments to the method in a form suitable for passing to another call to fBprocfR or a method definition, and the second element is the body of the method.

info class definitionnamespace fIclassfR ?fIkindfR? : This subcommand returns the definition namespace for fIkindfR definitions of the class fIclassfR; the definition namespace only affects the instances of fIclassfR, not fIclassfR itself. The fIkindfR can be either fB-classfR to return the definition namespace used for fBoo::definefR, or fB-instancefR to return the definition namespace used for fBoo::objdefinefR; the fB-classfR kind is default (though this is only actually useful on classes that are subclasses of fBoo::classfR). If fIclassfR does not provide a definition namespace of the given kind, this command returns the empty string. In those circumstances, the fBoo::definefR and fBoo::objdefinefR commands look up which definition namespace to use using the class inheritance hierarchy.

info class destructor fIclassfR : This subcommand returns the body of the destructor of class fIclassfR. If no destructor is present, this returns the empty string.

info class filters fIclassfR : This subcommand returns the list of filter methods set on the class.

info class forward fIclass methodfR : This subcommand returns the argument list for the method forwarding called fImethodfR that is set on the class called fIclassfR.

info class instances fIclassfR ?fIpatternfR? : This subcommand returns a list of instances of class fIclassfR. If the optional fIpatternfR argument is present, it constrains the list of returned instances to those that match it according to the rules of fBstring matchfR.

info class methods fIclassfR ?fIoptions...fR? : This subcommand returns a list of all public (i.e. exported) methods of the class called fIclassfR. Any of the following fIoptionfRs may be given, controlling exactly which method names are returned:

-all : If the fB-allfR flag is given, and the fB-scopefR flag is not given, the list of methods will include those methods defined not just by the class, but also by the class's superclasses and mixins.

-private : If the fB-privatefR flag is given, and the fB-scopefR flag is not given, the list of methods will also include the non-exported methods of the class (and superclasses and mixins, if fB-allfR is also given). Note that this naming is an unfortunate clash with true private methods; this option name is retained for backward compatibility.

-scope fIscopefR : Returns a list of all methods on fIclassfR that have the given visibility fIscopefR. When this option is supplied, both the fB-allfR and fB-privatefR options are ignored. The valid values for fIscopefR are:

public : Only methods with fIpublicfR scope (i.e., callable from anywhere by any instance of this class) are to be returned.

unexported : Only methods with fIunexportedfR scope (i.e., only callable via fBmyfR) are to be returned.

private : Only methods with fIprivatefR scope (i.e., only callable from within this class's methods) are to be returned.

info class methodtype fIclass methodfR : This subcommand returns a description of the type of implementation used for the method named fImethodfR of class fIclassfR. When the result is fBmethodfR, further information can be discovered with fBinfo class definitionfR, and when the result is fBforwardfR, further information can be discovered with fBinfo class forwardfR.

info class mixins fIclassfR : This subcommand returns a list of all classes that have been mixed into the class named fIclassfR.

info class properties fIclassfR ?fIoptions...fR : This subcommand returns a sorted list of properties defined on the class named fIclassfR. The fIoptionsfR define exactly which properties are returned:

-all : With this option, the properties from the superclasses and mixins of the class are also returned.

-readable : This option (the default behavior) asks for the readable properties to be returned. Only readable or writable properties are returned, not both.

-writable : This option asks for the writable properties to be returned. Only readable or writable properties are returned, not both.

info class subclasses fIclassfR ?fIpatternfR? : This subcommand returns a list of direct subclasses of class fIclassfR. If the optional fIpatternfR argument is present, it constrains the list of returned classes to those that match it according to the rules of fBstring matchfR.

info class superclasses fIclassfR : This subcommand returns a list of direct superclasses of class fIclassfR in inheritance precedence order.

info class variables fIclassfR ?fB-privatefR? : This subcommand returns a list of all variables that have been declared for the class named fIclassfR (i.e. that are automatically present in the class's methods, constructor and destructor). If the fB-privatefR option is given, this lists the private variables declared instead.

Object introspection

The following fIsubcommandfR values are supported by fBinfo objectfR:

info object call fIobject methodfR : Returns a description of the method implementations that are used to provide fIobjectfR's implementation of fImethodfR. This consists of a list of lists of four elements, where each sublist consists of a word that describes the general type of method implementation (being one of fBmethodfR for an ordinary method, fBfilterfR for an applied filter, fBprivatefR for a private method, and fBunknownfR for a method that is invoked as part of unknown method handling), a word giving the name of the particular method invoked (which is always the same as fImethodfR for the fBmethodfR type, and "unknown" for the fBunknownfR type), a word giving what defined the method (the fully qualified name of the class, or the literal string fBobjectfR if the method implementation is on an instance), and a word describing the type of method implementation (see fBinfo object methodtypefR). Note that there is no inspection of whether the method implementations actually use fBnextfR to transfer control along the call chain, and the call chains that this command files do not actually contain private methods.

info object class fIobjectfR ?fIclassNamefR? : If fIclassNamefR is not given, this subcommand returns class of the fIobjectfR object. If fIclassNamefR is present, this subcommand returns a boolean value indicating whether the fIobjectfR is of that class.

info object creationid fIobjectfR : Returns the unique creation identifier for the fIobjectfR object. This creation identifier is unique to the object (within a Tcl interpreter) and cannot be controlled at object creation time or altered afterwards. Implementation note: the creation identifier is used to generate unique identifiers associated with the object, especially for private variables.

info object definition fIobject methodfR : This subcommand returns a description of the definition of the method named fImethodfR of object fIobjectfR. The definition is described as a two element list; the first element is the list of arguments to the method in a form suitable for passing to another call to fBprocfR or a method definition, and the second element is the body of the method.

info object filters fIobjectfR : This subcommand returns the list of filter methods set on the object.

info object forward fIobject methodfR : This subcommand returns the argument list for the method forwarding called fImethodfR that is set on the object called fIobjectfR.

info object isa fIcategory objectfR ?fIargfR? : This subcommand tests whether an object belongs to a particular category, returning a boolean value that indicates whether the fIobjectfR argument meets the criteria for the category. The supported categories are:

info object isa class fIobjectfR : This returns whether fIobjectfR is a class (i.e. an instance of fBoo::classfR or one of its subclasses).

info object isa metaclass fIobjectfR : This returns whether fIobjectfR is a class that can manufacture classes (i.e. is fBoo::classfR or a subclass of it).

info object isa mixin fIobject classfR : This returns whether fIclassfR is directly mixed into fIobjectfR.

info object isa object fIobjectfR : This returns whether fIobjectfR really is an object.

info object isa typeof fIobject classfR : This returns whether fIclassfR is the type of fIobjectfR (i.e. whether fIobjectfR is an instance of fIclassfR or one of its subclasses, whether direct or indirect).

info object methods fIobjectfR ?fIoption...fR? : This subcommand returns a list of all public (i.e. exported) methods of the object called fIobjectfR. Any of the following fIoptionfRs may be given, controlling exactly which method names are returned:

-all : If the fB-allfR flag is given, and the fB-scopefR flag is not given, the list of methods will include those methods defined not just by the object, but also by the object's class and mixins, plus the superclasses of those classes.

-private : If the fB-privatefR flag is given, and the fB-scopefR flag is not given, the list of methods will also include the non-exported methods of the object (and classes, if fB-allfR is also given). Note that this naming is an unfortunate clash with true private methods; this option name is retained for backward compatibility.

-scope fIscopefR : Returns a list of all methods on fIobjectfR that have the given visibility fIscopefR. When this option is supplied, both the fB-allfR and fB-privatefR options are ignored. The valid values for fIscopefR are:

public : Only methods with fIpublicfR scope (i.e., callable from anywhere) are to be returned.

unexported : Only methods with fIunexportedfR scope (i.e., only callable via fBmyfR) are to be returned.

private : Only methods with fIprivatefR scope (i.e., only callable from within this object's instance methods) are to be returned.

info object methodtype fIobject methodfR : This subcommand returns a description of the type of implementation used for the method named fImethodfR of object fIobjectfR. When the result is fBmethodfR, further information can be discovered with fBinfo object definitionfR, and when the result is fBforwardfR, further information can be discovered with fBinfo object forwardfR.

info object mixins fIobjectfR : This subcommand returns a list of all classes that have been mixed into the object named fIobjectfR.

info object namespace fIobjectfR : This subcommand returns the name of the internal namespace of the object named fIobjectfR.

info object properties fIobjectfR ?fIoptions...fR : This subcommand returns a sorted list of properties defined on the object named fIobjectfR. The fIoptionsfR define exactly which properties are returned:

-all : With this option, the properties from the class, superclasses and mixins of the object are also returned.

-readable : This option (the default behavior) asks for the readable properties to be returned. Only readable or writable properties are returned, not both.

-writable : This option asks for the writable properties to be returned. Only readable or writable properties are returned, not both.

info object variables fIobjectfR ?fB-privatefR? : This subcommand returns a list of all variables that have been declared for the object named fIobjectfR (i.e. that are automatically present in the object's methods). If the fB-privatefR option is given, this lists the private variables declared instead.

info object vars fIobjectfR ?fIpatternfR? : This subcommand returns a list of all variables in the private namespace of the object named fIobjectfR. If the optional fIpatternfR argument is given, it is a filter (in the syntax of a fBstring matchfR glob pattern) that constrains the list of variables returned. Note that this is different from the list returned by fBinfo object variablesfR; that can include variables that are currently unset, whereas this can include variables that are not automatically included by any of fIobjectfR's methods (or those of its class, superclasses or mixins).

Examples

This command prints out a procedure suitable for saving in a Tcl script:

proc printProc {procName} {
    set result [list proc $procName]
    set formals {}
    foreach var [info args $procName] {
        if {[info default $procName $var def]} {
            lappend formals [list $var $def]
        } else {
            # Still need the list-quoting because variable
            # names may properly contain spaces.
            lappend formals [list $var]
        }
    }
    puts [lappend result $formals [info body $procName]]
}

Examples with objects

Every object necessarily knows what its class is; this information is trivially extractable through introspection:

oo::class create c
c create o
puts [info object class o]
                     \(-> prints "::c"
puts [info object class c]
                     \(-> prints "::oo::class"

The introspection capabilities can be used to discover what class implements a method and get how it is defined. This procedure illustrates how:

proc getDef {obj method} {
    foreach inf [info object call $obj $method] {
        lassign $inf calltype name locus methodtype

        # Assume no forwards or filters, and hence no $calltype
        # or $methodtype checks...

        if {$locus eq "object"} {
            return [info object definition $obj $name]
        } else {
            return [info class definition $locus $name]
        }
    }
    error "no definition for $method"
}

This is an alternate way of looking up the definition; it is implemented by manually scanning the list of methods up the inheritance tree. This code assumes that only single inheritance is in use, and that there is no complex use of mixed-in classes (in such cases, using fBinfo object callfR as above is the simplest way of doing this by far):

proc getDef {obj method} {
    if {$method in [info object methods $obj]} {
        # Assume no forwards
        return [info object definition $obj $method]
    }

    set cls [info object class $obj]

    while {$method ni [info class methods $cls]} {
        # Assume the simple case
        set cls [lindex [info class superclass $cls] 0]
        if {$cls eq ""} {
            error "no definition for $method"
        }
    }

    # Assume no forwards
    return [info class definition $cls $method]
}