C API String Parsing/Returning

k3xji sumerc at gmail.com
Mon Apr 6 08:18:10 EDT 2009


Sorry, Here is the correct output:
>>> ss= esauth.penc('s')
>>> print ss
╣
>>> esauth.pdec(ss)
'\xb9'
>>> print ss
s --> Works fine!!!
>>> ss= esauth.penc('s')
>>> print ss
s
>>> ss = esauth.pdec(ss)
>>> print ss
╣  --> how did this happen if the param and return values are same? I
cannot understand this. Something has todo with ref counts but I don't
understand the problem.
>>>



On Apr 6, 3:13 pm, k3xji <sum... at gmail.com> wrote:
> Hi all,
>
> This might be a newbie question. I am trying to implement a simple
> string decoder/encoder algorithm. Just suppose I am substrcating some
> values from the string passed as a parameter to the function and I
> want the function to return encoded/decoded version of the string.
>
> Here is the call:
> ss= esauth.penc('s')
> st = esauth.pdec(ss)
>
> static PyObject *
> pdec(PyObject *self, PyObject *args)
> {
>         unsigned char *s= NULL;
>
>         unsigned int v,len,i = 0;
>
>         if (!PyArg_ParseTuple(args, "s", &s))
>         return NULL;
>         if (!s)
>                 return NULL;
>
>         len = strlen(s);
>
>         for(i=0;i<len;i++) {
>                 if (s[i] > 10)
>                     s[i] = s[i] - 10;
>         }
>
>         return Py_BuildValue("s",s);
>
> }
>
> This is returning the original string. I mean the parameter is changed
> but the Py_BuildValue is returning the original string passed in as
> param.
>
>  have dealt with another nmore complex extension and because of the
> same string handling problems, I just stop implementing it. Can
> somebody please briefly explain the gotchas in Python's string
> handling and returning values, cause I am having real trouble with
> them.
>
> Thanks,




More information about the Python-list mailing list