From: Adapted C. <a.c...@gm...> - 2007-09-08 04:29:26
|
Hi, I'm trying to wrap some GMP code, and I have the following typemaps defined: ---------------------- %typemap(arginit) mpz_t { mpz_init($1); } %typemap(in) mpz_t INPUT { //Convert Int/Long to %x string, and then mpz_set_str... } %typemap(in) mpz_t OUTPUT { //empty but necessary to avoid generic code insert } %typemap(argout) mpz_t OUTPUT { //Use mpz_get_str and then PyLong_FromString... } %typemap(freearg) mpz_t { mpz_clear($1); } ---------------------- Suppose my function is declared as follows: void mpz_func(mpz_t INPUT, mpz_t OUTPUT); By the way, I'm using SWIG 1.3.31. All seems to go well at the SWIG stage - the first argument is initialized and then set to the value from the first Python argument; the second argument is initialized but then ignored until after the function has been called, at which point its value is copied into $result. Both arguments are then freed. However, the argument parsing in the Python function uses: if (!PyArg_ParseTuple(args,(char *)"OO:mpz_func",&obj0,&obj1)) SWIG_fail; This works if I do result = mpz_func(value, None) but complains if I do result = mpz_func(value), which is what I really want to do. Now one way to avoid this issue is to ensure that the second argument has a default value of None, but why should I even need to bother here? If I have an argument labeled as an INPUT I don't see its value being copied automatically to $result, do I? So why does an OUTPUT value get parsed as an input? If I didn't have the empty %typemap(in) mpz_t OUTPUT then the output variable would get a generic pointer-based code block on input (which doesn't compile by the way), so either way I am forced to treat an OUTPUT variable as an input. Note that this is not an INOUT argument - it is purely an OUTPUT. So why is it also treated as an input? Perhaps there is another, better, way to do this without using typemaps. If so, please enlighten me! Finally, I've noticed that in the generated wrapper code, obj* variables are numberd 0-up, while arg* and res* variables are numbered 1-up. This is most confusing. Many thanks in advance. |