RE: PyNumber_Int screwing Object.

Discussion in 'Python' started by Tim Peters, Oct 6, 2003.

  1. Tim Peters

    Tim Peters Guest

    [José Rui Faustino de Sousa]
    > This code snippet is broken in the case that the argument passed is a
    > complex number
    >
    > PyObject* tmp = Py_None;
    >
    > tmp=PyNumber_Int(object);
    > tmp=PyObject_GetAttrString(object,"real");


    That's incorrect use of the Python C API: the return value of every call
    made to a Python C API function must be checked for an error value, and if
    an error value is returned the caller must either explicitly clear the
    error, or return its own error value to its caller.

    > it wreaks havoc on Python making it fail in weird ways.


    So would dereferencing a NULL pointer <wink -- but you're coding in C here
    now, not Python, and any deviation from the rules can cause anything up to
    and including crashes -- C is utterly unforgiving>.

    > If I insert the line between the other two
    >
    > if(!tmp) PyErr_Clear();
    >
    > things seem to work.


    In that case you're doing the "must either explicitly clear the error"
    choice, and it will work fine. Note that you also need to check the result
    of PyObject_GetAttrString() for an error return.

    You can never skip these checks. Even if you don't think an error "should
    be" possible, an error is possible (for example, virtually any call can
    return an error code due to malloc() running out of memory 10 levels deep).
    Tim Peters, Oct 6, 2003
    #1
    1. Advertising

  2. On Sun, 5 Oct 2003 21:36:26 -0400, "Tim Peters" <> wrote:
    [...]
    >
    >You can never skip these checks. Even if you don't think an error "should
    >be" possible, an error is possible (for example, virtually any call can
    >return an error code due to malloc() running out of memory 10 levels deep).
    >

    A thought -- is there any guarantee that there will be enough memory to
    process an oom exception?

    Could there be an app-accessible s/w lever somewhere that one could kick over
    for reserve memory (assuming getting to the exception handler is guaranteed),
    like my old VW bug used to have?

    It would sputter and lunge when I "ran out" of gas on the freeway to work,
    and I'd just kick the little black lever on the firewall over 90 degrees,
    and sputter, lunge, continue (don't want to forget the state of that lever though ;-)

    Regards,
    Bengt Richter
    Bengt Richter, Oct 6, 2003
    #2
    1. Advertising

  3. "Tim Peters" <> wrote in message
    > You can never skip these checks.
    >


    I will keep that in mind!

    This is not my code and I am completely ignorant of the Python/C API I
    was just looking into the source of some really weird behaviour at a
    much higher level...

    That "PyNumber_Int" sure did look armless and it was some rough
    debugging session...

    By the way is there any nice tutorial on how to (properly) write this
    kind of stuff... I kinda got hooked... ;-)

    Thank you very much.

    Best regards
    José Rui


    --
    ========================================================================
    "Shrines! Shrines! Surely you don't believe in the gods. What's your
    argument? Where's your proof?." Aristophanes
    ========================================================================
    mailto:/// Crawler baith. UnCaps me to reply.
    Contact information: http://homepage.esoterica.pt/~jrfsousa/contact.html
    ========================================================================
    =?iso-8859-1?Q?Jos=E9_Rui_Faustino_de_Sousa?=, Oct 6, 2003
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Chris

    Screwing up my HTML

    Chris, Oct 22, 2003, in forum: ASP .Net
    Replies:
    3
    Views:
    374
    Chris
    Nov 5, 2003
  2. Darrel
    Replies:
    0
    Views:
    967
    Darrel
    Dec 28, 2004
  3. =?iso-8859-1?Q?Jos=E9_Rui_Faustino_de_Sousa?=

    PyNumber_Int screwing Object.

    =?iso-8859-1?Q?Jos=E9_Rui_Faustino_de_Sousa?=, Oct 6, 2003, in forum: Python
    Replies:
    0
    Views:
    412
    =?iso-8859-1?Q?Jos=E9_Rui_Faustino_de_Sousa?=
    Oct 6, 2003
  4. Tim Peters

    RE: PyNumber_Int screwing Object.

    Tim Peters, Oct 7, 2003, in forum: Python
    Replies:
    1
    Views:
    299
    Bengt Richter
    Oct 7, 2003
  5. Daniel Kramer

    string parsing screwing up on large files?

    Daniel Kramer, Dec 20, 2003, in forum: Python
    Replies:
    2
    Views:
    304
    Bengt Richter
    Dec 20, 2003
Loading...

Share This Page