C-API PyObject_Call

Discussion in 'Python' started by moerchendiser2k3, Mar 16, 2010.

  1. 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
     
    moerchendiser2k3, Mar 16, 2010
    #1
    1. Advertisements

  2. moerchendiser2k3

    Steve Holden Guest

    You'll need to use a debugger like gdb that gives you access to the C
    stack, I believe.

    regards
    Steve
     
    Steve Holden, Mar 16, 2010
    #2
    1. Advertisements

  3. But the stack is empty after PyObject_Call, isnt it?
     
    moerchendiser2k3, Mar 16, 2010
    #3
  4. moerchendiser2k3, 16.03.2010 17:08:
    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, Mar 16, 2010
    #4
  5. moerchendiser2k3, 16.03.2010 12:52:
    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
     
    Stefan Behnel, Mar 16, 2010
    #5
  6. 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...
     
    moerchendiser2k3, Mar 16, 2010
    #6
  7. moerchendiser2k3

    Steve Holden Guest

    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, Mar 16, 2010
    #7
  8. 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
     
    moerchendiser2k3, Mar 16, 2010
    #8
  9. moerchendiser2k3

    Carl Banks Guest

    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
     
    Carl Banks, Mar 16, 2010
    #9
  10. At first thanks for your answers!!!

    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
     
    moerchendiser2k3, Mar 17, 2010
    #10
  11. moerchendiser2k3, 16.03.2010 19:25:
    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
     
    Stefan Behnel, Mar 17, 2010
    #11
  12. 1) put the line number information into the message string when you raise

    you mean the line and file information of the C code, right?
     
    moerchendiser2k3, Mar 17, 2010
    #12
  13. moerchendiser2k3, 17.03.2010 23:35:
    Funny that you (being the OP) ask *me* what kind of line information *you*
    want.

    Stefan
     
    Stefan Behnel, Mar 18, 2010
    #13
  14. Funny that you (being the OP) ask *me* what kind of line information *you*
    Well, I need the line/file information of Python :
     
    moerchendiser2k3, Mar 18, 2010
    #14
  15. moerchendiser2k3, 18.03.2010 14:58:
    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
     
    Stefan Behnel, Mar 18, 2010
    #15
  16. 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.
     
    moerchendiser2k3, Mar 19, 2010
    #16
  17. moerchendiser2k3, 19.03.2010 14:12:
    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
     
    Stefan Behnel, Mar 19, 2010
    #17
  18. 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
     
    moerchendiser2k3, Mar 20, 2010
    #18
  19. moerchendiser2k3, 20.03.2010 03:01:
    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

    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
     
    Stefan Behnel, Mar 20, 2010
    #19
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.