i have a serious problem and I am looking for a solution. I pass an
instance of a class from a file to PyObject_Call. When something
fails, I get a full traceback. If it succeeds, I get the return value.
Is it possible to get information from which line/file the return
value of PyObject_Call is?
Thanks!!
moerchendiser2k3
You'll need to use a debugger like gdb that gives you access to the C
stack, I believe.
regards
Steve
--
Steve Holden +1 571 484 6266 +1 800 494 3119
See PyCon Talks from Atlanta 2010 http://pycon.blip.tv/
Holden Web LLC http://www.holdenweb.com/
UPCOMING EVENTS: http://holdenweb.eventbrite.com/
I think Steve was expecting that you wanted to debug into your program,
step into the call, and find the line yourself.
Stefan
Could you explain what you want to do with this information and in what
cases you need it? Do you want to extract the information programmatically
at runtime?
Stefan
TypeError: Expected an instance of XYZ, no int.
instead of
Traceback (most called...)
TypeError: in line 3, file test.py: expected an instance of XYZ, no
int...
Could we perhaps see a little bit more of the code? Are you throwing the
exception from within your C code or from the Python calling environment
with a raise statement?
Looks like that
PyObject *result = PyObject_Call(my_isntance, "", NULL);
if(result==NULL)
{
PyErr_Print(); //when this happens, the traceback is correct with
information about the file/line
return;
}
if(!PyXYZ_Check(result))
{
PyErr_SetString(PyExc_TypeError, "Wrong type, ....");
PyErr_Print(); //missing information of the file/line.
return;
}
Well, I expect, that there are no information about the line/file, so
I know something is missing, but what is missing?
Bye, moerchendiser2k3
Python tracebacks only contain line/file information about Python
files, not C files. Here you raise an exception with a C statement,
and catch and print it in the very next line. The exception doesn't
exit from Python code so there are no lines to print.
What line/file data you do expect to see?
If you want to see C line/file informnation you have to use a debugger
like gdb, as Steve Holden said.
Carl Banks
On 16 Mrz., 21:16, Carl Banks <pavlovevide...@gmail.com> wrote:
> Here you raise an exception with a C statement,
> and catch and print it in the very next line. The exception doesn't
> exit from Python code so there are no lines to print.
Exactly, I dont expect any line/file information. I am just looking
forward for a solution how to solve that. I would like to avoid
the use of the debugger in this case.
My ideas:
1. I thought the return value might contain any information where it
is from.
2. Otherwise it would be cool if there are any information available
in which line, file
the last called function/method exited.
Bye, moerchendiser2k3
Two solutions here:
1) put the line number information into the message string when you raise
the exception
2) use Cython instead of plain C, as Cython will automatically generate
readable stack traces for you, also for non-Python functions.
Stefan
you mean the line and file information of the C code, right?
Funny that you (being the OP) ask *me* what kind of line information *you*
want.
Stefan
Well, I need the line/file information of Python :
Then please explain what kind of Python line number you expect to find in C
code.
Hint: providing details often results in getting helpful answers.
Stefan
for instance:
def my_function():
return 3
So I would like to get line 2(in C) somehow.
Ok, so you're looking for the C-level trace function in CPython, I guess.
Could you explain why you want to know the line number?
Stefan
So, I need to tell him, in which file/line the error occured,
otherwise he dont know
where to look.
Imagine he set 20 files, and all of them have implemented a special
function. So he
wouldnt know where to check for the error.
Bye, moerchendiser2k3
Ah, ok, that was the important piece of information that you omitted from
your previous posts. So what you actually do is call a user provided
function and you want to report errors back to the user if the function
does not behave as expected by the (plugin) interface, and not only when it
raises an exception itself.
You might want to read this:
http://catb.org/~esr/faqs/smart-questions.html
> So, I need to tell him, in which file/line the error occured,
> otherwise he dont know where to look.
>
> Imagine he set 20 files, and all of them have implemented a special
> function. So he wouldnt know where to check for the error.
Well, you can easily get at the module file path and the function name
through introspection of the function, just as you would in Python. That
should be enough information to present to the user. I don't see why you
would want to add the exact line number of the return statement. The inner
workings of the user function you call are up to the user, after all.
Again, take a look at Cython, where you can do the introspection in Python
code.
Stefan