Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old August 10th, 2005, 05:15 PM
Leo
Guest
 
Posts: n/a
Default 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.

  #2  
Old August 10th, 2005, 08:45 PM
Peter Hansen
Guest
 
Posts: n/a
Default 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
  #3  
Old August 10th, 2005, 11:15 PM
Leo
Guest
 
Posts: n/a
Default 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(sys._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.

  #4  
Old August 11th, 2005, 12:25 AM
Bengt Richter
Guest
 
Posts: n/a
Default 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(sys._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
  #5  
Old August 11th, 2005, 01:35 PM
Peter Hansen
Guest
 
Posts: n/a
Default 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
"interpreter" is sometimes applied to the virtual machine, so I thought
you just wanted the current frame inside an application.

For the "interactive 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
  #6  
Old August 11th, 2005, 08:05 PM
Fernando Perez
Guest
 
Posts: n/a
Default 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
> "interpreter" is sometimes applied to the virtual machine, so I thought
> you just wanted the current frame inside an application.
>
> For the "interactive 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

 

Bookmarks

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles