Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Idiom for error checking within a C extension

Reply
Thread Tools

Idiom for error checking within a C extension

 
 
Jon Perez
Guest
Posts: n/a
 
      04-07-2004
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).
 
Reply With Quote
 
 
 
 
David M. Cooke
Guest
Posts: n/a
 
      04-07-2004
At some point, Jon Perez <(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
what is best idiom to remove file extension bwv549 Ruby 8 02-24-2009 03:06 PM
Ant idiom for signaling error on unset properties? nyenyec Java 0 01-26-2007 05:24 PM
New extension? saw extension .emf is it safe to open Jer Computer Support 5 10-08-2005 04:43 PM
Type checking inside a C extension Jon Perez Python 9 04-07-2004 09:35 PM
Unload extension modules when python22.dll unloads... [using C extension interpreter] Anand Python 3 11-08-2003 05:50 AM



Advertisments