Calling a C function that alters the passed argument?

Donn Cave donn at u.washington.edu
Wed Jun 6 14:06:07 EDT 2001


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