|
|
Re: member strings one character too long: msg#00125
programming.swig
|
Subject: |
Re: member strings one character too long |
The behavior has been changed back to what you expect.
The changes are in the CVS version.
Marcelo
digitalpenis@xxxxxxxxxxxxxx wrote:
hello,
i have encountered a problem with character array
members in classes in swig-1.3.22. here is an example:
the interface is for the following test case...
class zap {
public:
char str[4];
zap() {
strncpy(str, "FOO", 3);
str[3] = 0;
}
};
the wrapper code has
static PyObject *_wrap_zap_str_get(PyObject *, PyObject
*args) {
PyObject *resultobj;
zap *arg1 = (zap *) 0 ;
char *result;
PyObject * obj0 = 0 ;
if(!PyArg_ParseTuple(args,(char
*)"O:zap_str_get",&obj0)) goto fail;
if ((SWIG_ConvertPtr(obj0,(void
**)(&arg1),SWIGTYPE_p_zap,
SWIG_POINTER_EXCEPTION | 0)) == -1) SWIG_fail;
result = (char *)(char *) ((arg1)->str);
resultobj = SWIG_FromCharArray(result, 4);
return resultobj;
fail:
return NULL;
}
which appears to make the returned string one character
too long (ie. it includes the terminating NULL). the
outcome is that, if you do something like:
import example
z = example.zap()
print z.str
and redirect the output to a file (you don't see the
NULL in a terminal!), the it looks like:
FOO^@
i trust that i am not doing something stupid here
(although that is always a possibility!).
i have investigated this a little further. the same
example code with swig-1.3.17, gives:
static PyObject *_wrap_zap_str_get(PyObject *self, PyObject
*args) {
PyObject *resultobj;
zap *arg1 = (zap *) 0 ;
char *result;
PyObject * obj0 = 0 ;
if(!PyArg_ParseTuple(args,(char
*)"O:zap_str_get",&obj0)) goto fail;
if ((SWIG_ConvertPtr(obj0,(void **) &arg1,
SWIGTYPE_p_zap,SWIG_POINTER_EXCEPTION | 0 )) == -1) SWIG_fail;
result = (char *)(char *) ((arg1)->str);
resultobj = PyString_FromString(result);
return resultobj;
fail:
return NULL;
}
this works perfectly, as i would expect.
does this mean that something unintended has been introduced
between versions 1.3.17 and 1.3.22?
cheers,
andrew.
_______________________________________________
Swig maillist - Swig@xxxxxxxxxxxxxxx
http://mailman.cs.uchicago.edu/mailman/listinfo/swig
|
|