Idiom for error checking within a C extension

Discussion in 'Python' started by Jon Perez, Apr 7, 2004.

  1. Jon Perez

    Jon Perez Guest

    I want to retrieve a value from a tuple and convert it to a C
    type. Is the following idiom okay?

    if (!(
    ( tmp_pyobj=PyTuple_GetItem(tuple,1) ) &&
    ( c_int=PyInt_AsLong(tmp_pyobj) )
    ))
    {
    if (PyErr_ExceptionMatches(PyExc_TypeError))
    PyErr_SetString(PyExc_TypeError,"tuple's 1st member was not an integer");
    return NULL;
    }


    The PyErr_ExceptionMatches/PyErr_SetString combination in the if-block is
    where I'm a bit unsure. I want to check if the tuple element is of the
    correct type (a PyInt in this case) and if it isn't, I want to return my
    own customized error message instead of the default TypeError message.
    I'm kind of uncomfortable with the idea that I am checking for the kind
    of exception raised and then afterwards calling a function which can
    change it (even if I don't really end up doing that).
     
    Jon Perez, Apr 7, 2004
    #1
    1. Advertising

  2. At some point, Jon Perez <> wrote:

    > I want to retrieve a value from a tuple and convert it to a C
    > type. Is the following idiom okay?
    >
    > if (!(
    > ( tmp_pyobj=PyTuple_GetItem(tuple,1) ) &&


    You've got 1 here, but mention the first member below (that's number 0).

    > ( c_int=PyInt_AsLong(tmp_pyobj) )
    > ))
    > {
    > if (PyErr_ExceptionMatches(PyExc_TypeError))
    > PyErr_SetString(PyExc_TypeError,"tuple's 1st member was not an integer");
    > return NULL;
    > }
    >
    >
    > The PyErr_ExceptionMatches/PyErr_SetString combination in the if-block is
    > where I'm a bit unsure. I want to check if the tuple element is of the
    > correct type (a PyInt in this case) and if it isn't, I want to return my
    > own customized error message instead of the default TypeError message.
    > I'm kind of uncomfortable with the idea that I am checking for the kind
    > of exception raised and then afterwards calling a function which can
    > change it (even if I don't really end up doing that).


    That's no different than the python code

    try:
    p = tple[0]
    c = int(p)
    except TypeError:
    raise TypeError("tuple's 1st member was not an integer")

    You could use PyInt_Check(), and throw your own error, but if you want
    to handle longs or things that can be coerced to ints, you end up
    rewriting PyInt_AsLong().

    --
    |>|\/|<
    /--------------------------------------------------------------------------\
    |David M. Cooke
    |cookedm(at)physics(dot)mcmaster(dot)ca
     
    David M. Cooke, Apr 7, 2004
    #2
    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. Anand
    Replies:
    3
    Views:
    919
    Tim Daneliuk
    Nov 8, 2003
  2. Christian Seberino
    Replies:
    3
    Views:
    1,220
    Christian Seberino
    Feb 5, 2004
  3. Jon Perez

    Type checking inside a C extension

    Jon Perez, Apr 5, 2004, in forum: Python
    Replies:
    9
    Views:
    1,488
    Stephen Horne
    Apr 7, 2004
  4. Steve
    Replies:
    3
    Views:
    27,104
    Steve
    Aug 25, 2006
  5. bwv549
    Replies:
    8
    Views:
    140
    -lim-
    Feb 24, 2009
Loading...

Share This Page