[Python-Dev] getting at the current frame

barry@wooz.org barry@wooz.org
Thu, 26 Oct 2000 11:36:37 -0400 (EDT)


>>>>> "KY" == Ka-Ping Yee <ping@lfw.org> writes:

    >>  Okay, we all know that's a pain, right?  Lots of people have
    >> proposed solutions.  I've looked briefly at !?ng's Itpl.py, but
    >> I think it probably does too much by adding evaluation.

    KY> Do you think a variant of Itpl that only looked up variable
    KY> names would solve your problem?

I think it would calm my objections, yes.  I'll observe that I think
there may be a middle ground between my approach and Itpl.  I've found
a couple of situations where I want a minimal form of evaluation,
e.g. I want attributes to be expanded (and methods called), but not a
general evaluation facility.  So for example, I'd like to be able to
say:

    print _("The name of the mailing list is %(mlist.listname())s")

What worries me about a general evaluation framework is that we then
have to be really really careful about security and origin of the
strings we're using here.  Maybe we'd need to adopt something like
Perl's taint strings.

    KY> How about this routine to get the current frame:
    KY> inspect.currentframe().

It uses the same intentional-exception trick to get at the current
frame.  It's about 8x faster to do it in C.  I'll note that your
version raises and catches a string exception, while mine uses an
exception instance.  I don't see much difference in speed between the
two.

    KY> If there are changes that need to be made before it can be
    KY> accepted, i'd be happy to make them.

Sent under a separate private response...

-Barry