interpreter frame

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Leo

    #1

    interpreter frame

    Why is it not possible to get the frame from the interpreter using the
    inspect library? IOW, why does this code:
    [color=blue][color=green][color=darkred]
    >>> from inspect import *
    >>> stack()[/color][/color][/color]

    produce:

    [(<frame object at 0x81b6d94>, '<stdin>', 1, '?', None, None)]

    instead of:

    [(<frame object at 0x81b6d94>, '<stdin>', 1, '?', '\tstack()', 0)]

    ?

    I must be missing something. The motivating question is:

    How can I get the interpreter line that triggered the current actions?

    TIA,
    Leo.

  • Peter Hansen

    #2
    Re: interpreter frame

    Leo wrote:[color=blue]
    > Why is it not possible to get the frame from the interpreter using the
    > inspect library?[/color]

    Because sys._getframe() does the job instead?

    -Peter

    Comment

    • Leo

      #3
      Re: interpreter frame

      Good try, but that doesn't seem to work either. Maybe I should have
      emphasized that what I really want is the line of code, as opposed to
      the entire frame. Here is the output of sys._getframe() on my system:

      Python 2.3.4 (#1, Feb 2 2005, 12:11:53)
      [GCC 3.4.2 20041017 (Red Hat 3.4.2-6.fc3)] on linux2
      Type "help", "copyright" , "credits" or "license" for more information.[color=blue][color=green][color=darkred]
      >>> import sys
      >>> sys._getframe()[/color][/color][/color]
      <frame object at 0x9fbce0c>[color=blue][color=green][color=darkred]
      >>> from inspect import *
      >>> getframeinfo(sy s._getframe())[/color][/color][/color]
      ('<stdin>', 1, '?', None, None)

      Is it different in 2.4? Maybe there is something else in sys.* that I
      am having trouble finding?

      TIA,
      Leo.

      Comment

      • Bengt Richter

        #4
        Re: interpreter frame

        On 10 Aug 2005 15:08:21 -0700, "Leo" <leo.uaz@gmail. com> wrote:
        [color=blue]
        >Good try, but that doesn't seem to work either. Maybe I should have
        >emphasized that what I really want is the line of code, as opposed to
        >the entire frame. Here is the output of sys._getframe() on my system:
        >
        >Python 2.3.4 (#1, Feb 2 2005, 12:11:53)
        >[GCC 3.4.2 20041017 (Red Hat 3.4.2-6.fc3)] on linux2
        >Type "help", "copyright" , "credits" or "license" for more information.[color=green][color=darkred]
        >>>> import sys
        >>>> sys._getframe()[/color][/color]
        ><frame object at 0x9fbce0c>[color=green][color=darkred]
        >>>> from inspect import *
        >>>> getframeinfo(sy s._getframe())[/color][/color]
        >('<stdin>', 1, '?', None, None)
        >
        >Is it different in 2.4? Maybe there is something else in sys.* that I
        >am having trouble finding?
        >[/color]
        Isn't there some requirement of having a source file in order to get
        the line of code, which an interactive session does not satisfy?
        (Maybe a strategically located StringIO instance encapsulating the latest
        interactive chunk as "source file" could solve it?)

        Regards,
        Bengt Richter

        Comment

        • Peter Hansen

          #5
          Re: interpreter frame

          Leo wrote:[color=blue]
          > Good try, but that doesn't seem to work either. Maybe I should have
          > emphasized that what I really want is the line of code, as opposed to
          > the entire frame.[/color]

          Ah, it wasn't clear from your first post that you were specifically
          interested in a line you entered at the *interactive prompt*. The word
          "interprete r" is sometimes applied to the virtual machine, so I thought
          you just wanted the current frame inside an application.

          For the "interactiv e interpreter", I doubt the line of code that you are
          executing is preserved anywhere (at least not in a supported, documented
          fashion) as source, so I don't think there's a simple way to get at it.
          Certainly not (I believe) through the frame or code object. Maybe
          checking the source will lead to a hack solution...

          -Peter

          Comment

          • Fernando Perez

            #6
            Re: interpreter frame

            Peter Hansen wrote:
            [color=blue]
            > Leo wrote:[color=green]
            >> Good try, but that doesn't seem to work either. Maybe I should have
            >> emphasized that what I really want is the line of code, as opposed to
            >> the entire frame.[/color]
            >
            > Ah, it wasn't clear from your first post that you were specifically
            > interested in a line you entered at the *interactive prompt*. The word
            > "interprete r" is sometimes applied to the virtual machine, so I thought
            > you just wanted the current frame inside an application.
            >
            > For the "interactiv e interpreter", I doubt the line of code that you are
            > executing is preserved anywhere (at least not in a supported, documented
            > fashion) as source, so I don't think there's a simple way to get at it.
            > Certainly not (I believe) through the frame or code object. Maybe
            > checking the source will lead to a hack solution...[/color]

            If using the mock interpreter in code.py (in the stdlib), the object's .buffer
            attribute holds that info as a list of lines. IPython exposes it publicly via
            its custom exception handlers mechanism (some details here:
            http://www.scipy.org/wikis/featurerequests/IPython).

            Such a buffer must also exist in the CPython interactive interpreter, but I
            don't think it's accessible in any way via Python-level functionality (it's
            most likely an internal C variable). But some perusing of the C sources could
            indicate a way to get to it, I'm just not familiar with that particular code.

            Cheers,

            f

            Comment

            Working...