C-API PyObject_Call

M

moerchendiser2k3

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
 
S

Steve Holden

moerchendiser2k3 said:
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
 
S

Stefan Behnel

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
 
S

Stefan Behnel

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
 
M

moerchendiser2k3

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

Steve Holden

moerchendiser2k3 said:
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
 
M

moerchendiser2k3

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
 
C

Carl Banks

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
 
M

moerchendiser2k3

At first thanks for your answers!!!

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
 
S

Stefan Behnel

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
 
S

Stefan Behnel

moerchendiser2k3, 17.03.2010 23:35:
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
 
S

Stefan Behnel

moerchendiser2k3, 18.03.2010 14:58:
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
 
M

moerchendiser2k3

In my case I call a funcion and I would like to get the line
where the function returned.

for instance:


def my_function():
return 3

So I would like to get line 2(in C) somehow.
 
S

Stefan Behnel

moerchendiser2k3, 19.03.2010 14:12:
In my case I call a funcion and I would like to get the line
where the function returned.

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
 
M

moerchendiser2k3

Yes, the user is able to set a file which contains a function that
does what the user wants.
But in a case, I expect a special return value of this function.

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
 
S

Stefan Behnel

moerchendiser2k3, 20.03.2010 03:01:
Yes, the user is able to set a file which contains a function that
does what the user wants.
> But in a case, I expect a special return value of this function.

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
 

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
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top