Calling a C function that alters the passed argument?

Bob Greschke bob at passcal.nmt.edu
Wed Jun 6 14:57:35 EDT 2001


> The last is by far the best, in most cases.  The options do not
> include modifying an integer parameter, that isn't how Python works.

I sorta figured that when I couldn't find any examples of it.  I've
got a whole library of C functions to write wrappers for that are
basically like this, and they can't be changed.  OK, I'll TRY to
EXCEPT your example. :-)

Thanks!

Bob

"Donn Cave" <donn at u.washington.edu> wrote in message
news:9flref$ong$1 at nntp6.u.washington.edu...
> Quoth "Bob Greschke" <bob at passcal.nmt.edu>:
> | Here's the C function I want to call from Python:
> |
> | char *boo(ANumber)
> | int *ANumber;
> | {
> |     *ANumber = 42;
> |
> |     if (something went wrong)
> |         return "An error message";
> |
> |     return (char *)NULL;
> | }
> |
> |
> | How would I write the wrapper function for this?  All I can find
are
> | examples where the wrapper functions deal with the returned value
from
> | C functions.
>
> Trying to understand your question, the assumption I'm leaning
> towards is that you want the Python function to preserve this API.
> Don't even think about it.  Your choices are basically
>
>     error, number = boo(), or
>     number, error = boo(), or
>     try:
>        number = boo()
>     except SomeError, val:
>        print 'boo error', val
>
> The last is by far the best, in most cases.  The options do not
> include modifying an integer parameter, that isn't how Python works.
>
> You may want to look at some examples for the way to set an
exception,
> will probably look something like
>    PyErr_SetString(PyExc_ValueError, "error message shredded");
>
> Then return 0 to signal the exception, instead of
PyInt_FromLong(number).
>
> Donn Cave, donn at u.washington.edu





More information about the Python-list mailing list