interpreter frame

L

Leo

Why is it not possible to get the frame from the interpreter using the
inspect library? IOW, why does this code:

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.
 
P

Peter Hansen

Leo said:
Why is it not possible to get the frame from the interpreter using the
inspect library?

Because sys._getframe() does the job instead?

-Peter
 
L

Leo

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.('<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.
 
B

Bengt Richter

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.('<stdin>', 1, '?', None, None)

Is it different in 2.4? Maybe there is something else in sys.* that I
am having trouble finding?
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
 
P

Peter Hansen

Leo said:
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.

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
 
F

Fernando Perez

Peter said:
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...

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,262
Messages
2,571,311
Members
47,985
Latest member
kazewi

Latest Threads

Top