Script filename introspection

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Script filename introspection

Jean-Claude Wippler
Is there a (preferred) way to determine inside a script what file it was
loaded from?  I'm trying to make scripts load what they need themselves,
so they become self-contained.  Python has __file__, while Tcl has [info
source] for this.  With that, one can do the equivalent of "dofile('../a-
file-next-to-this-one')", regardless of how the first script was loaded.

Somewhat related, is there a way to mimic Python's "if name=='__main__'"?
 This makes it easy to include tests and sample code in any source file,
yet also use the same file for loading common scripts needed in other
contexts.  One solution I can think of, is that if the first issue is
solved, then scripts can achieve this effect by having code of the form:
    if the_name_of_this_script==the_name_of_the_main_script then
      ... tests, demos, etc ...
    end

-jcw


Reply | Threaded
Open this post in threaded view
|

Re: Script filename introspection

Luiz Henrique de Figueiredo
>Is there a (preferred) way to determine inside a script what file it was
>loaded from? 

Yes, use "getinfo(1)". This returns a table.  The "source" field contains
the file name (after a '@'). See below:

$ cat i
t=getinfo(1)
foreach(t,print)

$ lua i
what    main
namewhat
linedefined     0
nups    0
currentline     1
source  @i
short_src       file `i'
func    function: 0x80604c8

>Somewhat related, is there a way to mimic Python's "if name=='__main__'"?

Same answer. Check whether the field "what" is "main", as above.

The manual says that "getinfo" is part of the debug API and that you should
avoid it. I think that using it for information purposes is ok (ie., avoid
"setlocal"!). Perhaps we can change the wording in the manual.
--lhf

Reply | Threaded
Open this post in threaded view
|

Re: Script filename introspection

Jean-Claude Wippler
Luiz Henrique de Figueiredo <[hidden email]> wrote:

>>Is there a (preferred) way to determine inside a script what file it was
>>loaded from? 
>
>Yes, use "getinfo(1)". This returns a table.  The "source" field contains
>the file name (after a '@'). See below:
[...]
>The manual says that "getinfo" is part of the debug API and that you should
>avoid it. I think that using it for information purposes is ok (ie., avoid
>"setlocal"!). Perhaps we can change the wording in the manual.

Thanks, this is exactly the answer I was hoping for.  I have not looked
much at getinfo, because I did not want to rely on a function which is
part of a library one is not supposed to load in normal use.  You could
consider splitting of those parts of getinfo which you consider really
safe and useful in general, and put it in a separate function in the
baselib some day (or keep the name, and move the "bad" parts to another one).

-jcw


Reply | Threaded
Open this post in threaded view
|

Re: Script filename introspection

Luiz Henrique de Figueiredo
In reply to this post by Jean-Claude Wippler
>Thanks, this is exactly the answer I was hoping for.  I have not looked
>much at getinfo, because I did not want to rely on a function which is
>part of a library one is not supposed to load in normal use.

The main point about the the debug API, on top of which the Reflexive Debug
Interface (ldblib) is built, is that its functions are likely to be slow,
because their main purpose is to generate good error messages.
For instance, to get the "name" of the current function, getinfo must traverse
all global variables. Calling getinfo(1,"S") might be slightly faster if you
only want to know the source.

It is in this sense that this library is not supposed to be used as a normal
programming tool. But it's very useful nonetheless.
--lhf