473,396 Members | 1,907 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Getting better traceback info on exec and execfile - introspection?

In doing the extension to the python debugger which I have here:
http://sourceforge.net/project/showf...kage_id=175827
I came across one little thing that it would be nice to get done better.

I notice on stack traces and tracebacks, an exec or execfile command
appears as a stack entry -- probably as it should since it creates a
new environment.

However the frame information for exec or execfile looks like this:
File "<string>", line 1, in ?

Okay, the "line 1" probably changes, but most of the time it's going
to be one. And in code that's in CVS (cvsview:
http://cvs.sourceforge.net/viewcvs.py/bashdb/pydb/) you'll see that
instead of this I now put some thing like:

## 1 in exec '<string>' at line 1

which is perhaps is a little more honest since one is not really in a
file called <string>. However the way the debugger gets this *is*
still a little hoaky in that it looks for something in the frame's
f_code.co_filename *called* <string>. And from that it *assumes* this
is an exec, so it can't really tell the difference between an exec
command an execfile command, or a file called <string>. But I suppose
a little more hoakiness *could* be added to look at the outer frame
and parse the source line for "exec" or "execfile".

And suppose instead of '<string>' I'd like to give the value or the
leading prefix of the value instead of the unhelpful word '<string>'?
How would one do that? Again, one way is to go into the outer frame
get the source line (if that exists), parse that and interpolate
argument to exec(file). Is there are better way?

Another and perhaps more direct way to distinguish exec/execfile might
be to some how look at the byte code of the frame entry and decode
that. It also could be used for example in another place in the
debugger where one wants to skip over executing "def" statements (not
the function invocation, but the statement which adds the
function/method so it can get called). But depending on how things are
done unless there are nice version-independent and
implementation-independent symbolic definitions for exec_opcode,
execfile_opcode and def_opcode, although straight-forward and reliable
for a given version/implementation it might not be a good thing to do.

Any help, thoughts or pointers?
Jan 14 '06 #1
8 3289
I suggested:
And suppose instead of '<string>' I'd like to give the value or the
leading prefix of the value instead of the unhelpful word '<string>'?
How would one do that? Again, one way is to go into the outer frame
get the source line (if that exists), parse that and interpolate
argument to exec(file). Is there are better way?
I've hacked this approach into the CVS for pydb, but it is not all
that accurate (although in practice I'm pleased with the result).

My current regular expression is (^|\s+)exec\s+(.*) which does match
simple things, but it would fail on say by *not* matching:
x=1;exec "y=2";exec "z=3"
and fail by *wrongly* matching on:
x=" exec meeting in 5 mins"

Even if I knew how to call the python parser on the line, that still
might not be good enough in the presence of continuation lines.

This probably should be addressed at a lower level in gathering
traceback data inside Python.
Another and perhaps more direct way to distinguish exec/execfile might

^^^^^^^^
execfile is not a problem, just exec.
Jan 15 '06 #2
R. Bernstein wrote:
In doing the extension to the python debugger which I have here:
http://sourceforge.net/project/showf...kage_id=175827 I came across one little thing that it would be nice to get done better.

I notice on stack traces and tracebacks, an exec or execfile command
appears as a stack entry -- probably as it should since it creates a
new environment.

However the frame information for exec or execfile looks like this:
File "<string>", line 1, in ?


That comes from how the code object was compiled:
code = compile('error','Anything you want goes here','single')
exec code

Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "Anything you want goes here", line 1, in ?
NameError: name 'error' is not defined
For example, in ipython:

In [1]: error
---------------------------------------------------------------------------
exceptions.NameError Traceback (most recent
call last)

/home/fperez/<ipython console>

NameError: name 'error' is not defined
So any regexp-matching based approach here is likely to be fairly brittle,
unless you restrict your tool to the standard python interpreter, and you
get some guarantee that it will always tag interactive code with
'<string>'.

Regards,

f

Jan 15 '06 #3
Fernando Perez <fp********@gmail.com> writes:
R. Bernstein wrote: ....
However the frame information for exec or execfile looks like this:
File "<string>", line 1, in ?


That comes from how the code object was compiled:

.... So any regexp-matching based approach here is likely to be fairly brittle,
unless you restrict your tool to the standard python interpreter, and you
get some guarantee that it will always tag interactive code with
'<string>'.
Thanks for the information! Alas, that's bad news. The question then
remains as to how to accurately determine if a frame is running an
exec operation and accurately get the string associated with the exec.

Given all the introspection about python's introspection, it should be
possible.

I also tried pydb.py using IPython 0.7.0 (using an automatically
updated Fedora Core 5 RPM) and although I was expecting pydb.py breakage
from what you report, I didn't find any difference.
% ipython
Python 2.4.2 (#1, Dec 17 2005, 13:02:22)
Type "copyright", "credits" or "license" for more information.

IPython 0.7.0 -- An enhanced Interactive Python.
? -> Introduction to IPython's features.
%magic -> Information about IPython's 'magic' % functions.
help -> Python's own help system.
object? -> Details about 'object'. ?object also works, ?? prints more.

In [1]: %run /usr/lib/python2.4/site-packages/pydb.py test1.py /home/rocky/python/test1.py(2)?()

-> import sys
(Pydb) where
-> 0 in file '/home/rocky/python/test1.py' at line 2
## 1 in exec cmd in globals, locals at line 1
## 2 run() called from file '/usr/lib/python2.4/bdb.py' at line 366
(Pydb)

Note entry ##1 is *not*
File "<string>", line 1, in ?

So somehow I guess ipython is not intercepting or changing how compile
was run here.
Jan 15 '06 #4
R. Bernstein wrote:
Fernando Perez <fp********@gmail.com> writes:
R. Bernstein wrote: ...
> However the frame information for exec or execfile looks like this:
> File "<string>", line 1, in ?


That comes from how the code object was compiled:

...
So any regexp-matching based approach here is likely to be fairly
brittle, unless you restrict your tool to the standard python
interpreter, and you get some guarantee that it will always tag
interactive code with '<string>'.


Thanks for the information! Alas, that's bad news. The question then
remains as to how to accurately determine if a frame is running an
exec operation and accurately get the string associated with the exec.

Given all the introspection about python's introspection, it should be
possible.


I thought a little about this. One possibility would be to check whether
the argument given in the string exists as a file; if not, assume it's a
string. I can't think of another way, since the string given is completely
arbitrary. But someone who knows more about the internal structure of code
objects may give you better tips.
I also tried pydb.py using IPython 0.7.0 (using an automatically
updated Fedora Core 5 RPM) and although I was expecting pydb.py breakage
from what you report, I didn't find any difference.
% ipython
Python 2.4.2 (#1, Dec 17 2005, 13:02:22)
Type "copyright", "credits" or "license" for more information.

IPython 0.7.0 -- An enhanced Interactive Python.
? -> Introduction to IPython's features.
%magic -> Information about IPython's 'magic' % functions.
help -> Python's own help system.
object? -> Details about 'object'. ?object also works, ?? prints more.

In [1]: %run /usr/lib/python2.4/site-packages/pydb.py test1.py
/home/rocky/python/test1.py(2)?()

-> import sys
(Pydb) where
-> 0 in file '/home/rocky/python/test1.py' at line 2
## 1 in exec cmd in globals, locals at line 1
## 2 run() called from file '/usr/lib/python2.4/bdb.py' at line 366
(Pydb)

Note entry ##1 is *not*
File "<string>", line 1, in ?

So somehow I guess ipython is not intercepting or changing how compile
was run here.


Oh, that's because you're using %run, so your code is in complete control.
What I meant about a restriction was thiking about using your pydb as the
debugger called by ipython when exceptions occur.

Try

%pdb

and then %run any script with an error in it, to be dropped in post-mortem
mode inside the stack. That uses an ipython-enhanced pdb, but not your
pydb. As your code matures and improves, it would be nice to, for example,
make it possible to plug it into ipython as a 'better debugger'. But
there, you'll see '<ipython console>' as the filename markers.

Anyway, good luck with this. If you are interested in ipython integration,
I suggest the ipython-dev list as a better place for discussion: I only
monitor c.l.py on occasion, so I may well miss things here.

Regards,

f

Jan 15 '06 #5
R. Bernstein wrote:
..
..
..
which is perhaps is a little more honest since one is not really in a
file called <string>. However the way the debugger gets this *is*
still a little hoaky in that it looks for something in the frame's
f_code.co_filename *called* <string>. And from that it *assumes* this
is an exec, so it can't really tell the difference between an exec
command an execfile command, or a file called <string>. But I suppose
a little more hoakiness *could* be added to look at the outer frame
and parse the source line for "exec" or "execfile".
You should check the getFrameInfo function in zope.interface package:
http://svn.zope.org/Zope3/trunk/src/...77&view=markup
And suppose instead of '<string>' I'd like to give the value or the
leading prefix of the value instead of the unhelpful word '<string>'?
How would one do that? Again, one way is to go into the outer frame
get the source line (if that exists), parse that and interpolate
argument to exec(file). Is there are better way?


Py library (http://codespeak.net/py/current/doc/index.html) has some
similar functionality in the code subpackage.

Jan 15 '06 #6
"Ziga Seilnacht" <zi************@gmail.com> writes:
You should check the getFrameInfo function in zope.interface package:
http://svn.zope.org/Zope3/trunk/src/...77&view=markup


Thanks! Just looked at that. The logic in the relevant part (if I've extracted
this correctly):

f_globals = frame.f_globals
hasName = '__name__' in f_globals
module = hasName and sys.modules.get(f_globals['__name__']) or None
namespaceIsModule = module and module.__dict__ is f_globals
if not namespaceIsModule:
# some kind of funky exec
kind = "exec"

is definitely not obvious to me. exec's don't have module namespace
(or something or other)? Okay. And that the way to determine the
module-namespace thingy is whatever that logic is? Are the assumptions
here be likely to be valid in the future?

Another problem I have with that code is that it uses the Zope Public
License. But the code is adapted from the Python Enterprise
Application Kit (PEAK) which doesn't seem to use the Zope Public
License. I'm not sure it's right to adopt a Zope Public License just
for this.

So instead, I followed the other avenue I suggested which is
dissembling the statement around the frame. Talk about the kettle
calling the pot black! Yes, I don't know if the assumptions in this
method are likely to be valid in the future either.

But I can use op-code examination to also help me determine if we are
stopped at a def statement which I want to skip over. So here's the
code I've currently settled on:

from opcode import opname
def op_at_frame(frame):
code = frame.f_code.co_code
pos = frame.f_lasti
op = ord(code[pos])
return opname[op]

def is_exec_stmt(frame):
"""Return True if we are looking at an exec statement"""
return frame.f_back is not None and op_at_frame(frame.f_back)=='EXEC_STMT'

re_def = re.compile(r'^\s*def\s+')
def is_def_stmt(line, frame):
"""Return True if we are looking at a def statement"""
# Should really also check that the operand is a code object
return re_def.match(line) and op_at_frame(frame)=='LOAD_CONST'

But it may just be because I wrote it that I find it easier to
understand and more straightforward to fathom.
And suppose instead of '<string>' I'd like to give the value or the
leading prefix of the value instead of the unhelpful word '<string>'?
How would one do that? Again, one way is to go into the outer frame
get the source line (if that exists), parse that and interpolate
argument to exec(file). Is there are better way?


Py library (http://codespeak.net/py/current/doc/index.html) has some
similar functionality in the code subpackage.


Again, many thanks. I'll have to study this further. It may be that
exec and so on are wrapped so that it's possible to squirrel away the
string before calling exec. Again, dunno. But thanks for the pointer.
Jan 16 '06 #7
Fernando Perez <fp********@gmail.com> writes:
I thought a little about this. One possibility ...
Thanks. A sibling thread has the code I'm currently using.
Oh, that's because you're using %run, so your code is in complete control.
What I meant about a restriction ... Okay.
If you are interested in ipython integration,
Yes I am.
I suggest the ipython-dev list as a better place for discussion: I only
monitor c.l.py on occasion, so I may well miss things here.


Okay now subscribed. But interestingly I looked in the IPython PDF and
didn't see mention of it when looking for contact info. I do however
see it (now) listed on http://ipython.scipy.org/. Thanks again.
Jan 16 '06 #8
Fernando Perez <fp********@gmail.com> writes:
So any regexp-matching based approach here is likely to be fairly brittle,
unless you restrict your tool to the standard python interpreter, and you
get some guarantee that it will always tag interactive code with
'<string>'.


Meant to mention for what it's worth. Looks like I'm not the first to use the
filename == '<string>' test. I note this in the stock pdb.py:

# To be overridden in derived debuggers
def defaultFile(self):
"""Produce a reasonable default."""
filename = self.curframe.f_code.co_filename
if filename == '<string>' and self.mainpyfile:
filename = self.mainpyfile
return filename

I'm not sure under what conditions this equality test normally occurs
though.
Jan 16 '06 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Jonathan | last post by:
I'm puzzled by Python's behavior when binding local variables which are introduced within exec() or execfile() statements. First, consider this simple Python program: # main.py def f() : x = 1...
3
by: | last post by:
Hi everybody, I'm sure there's a way to do this, but I can't find it. How can I execute another .py file from my first .py file without using an exec* command? They're both in the same...
2
by: Chris S. | last post by:
I'd like to dynamically execute multiple lines of indented code from within a script, but I can't seem to find a suitable function. Exec only works with unindented code, and execfile only works...
2
by: tedsuzman | last post by:
----- def f(): ret = 2 exec "ret += 10" return ret print f() ----- The above prints '12', as expected. However,
5
by: David Rasmussen | last post by:
If I have a string that contains the name of a function, can I call it? As in: def someFunction(): print "Hello" s = "someFunction" s() # I know this is wrong, but you get the idea... ...
1
by: tkpmep | last post by:
I installed SciPy and NumPy (0.9.5, because 0.9.6 does not work with the current version of SciPy), and had some teething troubles. I looked around for help and observed that the tutorial is dated...
5
by: Edward K Ream | last post by:
It looks like both exec and execfile are converting "\n" to an actual newline in docstrings! Start idle: Python 2.5 (r25:51908, Sep 19 2006, 09:52:17) on win32
1
by: Fernando Perez | last post by:
Hi all, I'm finding the following behavior truly puzzling, but before I post a bug report on the site, I'd rather be corrected if I'm just missing somethin obvious. Consider the following...
8
by: gregpinero | last post by:
I'm running code via the "exec in context" statement within a much larger program. What I would like to do is capture any possible errors and show a pretty traceback just like the Python...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.