[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.python

C-API PyObject_Call

moerchendiser2k3

3/16/2010 11:53:00 AM

Hi,

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
18 Answers

Steve Holden

3/16/2010 2:25:00 PM

0

moerchendiser2k3 wrote:
> Hi,
>
> 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://pyco...
Holden Web LLC http://www.hold...
UPCOMING EVENTS: http://holdenweb.event...

moerchendiser2k3

3/16/2010 4:08:00 PM

0

But the stack is empty after PyObject_Call, isnt it?

Stefan Behnel

3/16/2010 4:17:00 PM

0

moerchendiser2k3, 16.03.2010 17:08:
> But the stack is empty after PyObject_Call, isnt it?

I think Steve was expecting that you wanted to debug into your program,
step into the call, and find the line yourself.

Stefan

Stefan Behnel

3/16/2010 4:18:00 PM

0

moerchendiser2k3, 16.03.2010 12:52:
> 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?

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

moerchendiser2k3

3/16/2010 4:41:00 PM

0

In one case I have to check the return value of PyObject_Call, and if
its not of the correct return value,
I throw an exception, but I just get a simple output:

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

Steve Holden

3/16/2010 5:14:00 PM

0

moerchendiser2k3 wrote:
> In one case I have to check the return value of PyObject_Call, and if
> its not of the correct return value,
> I throw an exception, but I just get a simple output:
>
> 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?

regards
Steve
--
Steve Holden +1 571 484 6266 +1 800 494 3119
See PyCon Talks from Atlanta 2010 http://pyco...
Holden Web LLC http://www.hold...
UPCOMING EVENTS: http://holdenweb.event...

moerchendiser2k3

3/16/2010 6:25:00 PM

0

Hi, currently I am not at home, I will post some stuff when I am back.
Just the note: I throw an exception with the C API.

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

Carl Banks

3/16/2010 8:17:00 PM

0

On Mar 16, 11:25 am, moerchendiser2k3 <googler.
1.webmas...@spamgourmet.com> wrote:
> Hi, currently I am not at home, I will post some stuff when I am back.
> Just the note: I throw an exception with the C API.
>
> 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?

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

moerchendiser2k3

3/17/2010 2:46:00 AM

0

At first thanks for your answers!!!

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

Stefan Behnel

3/17/2010 6:46:00 AM

0

moerchendiser2k3, 16.03.2010 19:25:
> Hi, currently I am not at home, I will post some stuff when I am back.
> Just the note: I throw an exception with the C API.
>
> 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?

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