Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > C API String Parsing/Returning

Reply
Thread Tools

C API String Parsing/Returning

 
 
k3xji
Guest
Posts: n/a
 
      04-06-2009
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,
 
Reply With Quote
 
 
 
 
k3xji
Guest
Posts: n/a
 
      04-06-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...@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,


 
Reply With Quote
 
 
 
 
Gerhard Hδring
Guest
Posts: n/a
 
      04-06-2009
k3xji 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;


These two lines are superfluous. s now points to the contents of the
Python string (which must not contain any 0 characters, else a TypeError
is raised instead). Python strings are immutable, so you should *not
modify this C string*.

> 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. [...]


Yes, that's because you're returning a Python string from the string
passed in

You should do something else instead:

char* buf = strdup(s);
if (!buf) {
PyErr_SetString(PyExc_MemoryError, "Out of memory: strdup failed");
return NULL;
}

/* TODO: your string manipulation */

return PyString_FromString(buf); /* return Py_BuildValue("s", buf); */

If you want to cope with Python strings that may contain 0 bytes, parse
them with "s#" instead. This should normally be better because you avoid
the strlen() this way.

HTH

-- Gerhard

 
Reply With Quote
 
k3xji
Guest
Posts: n/a
 
      04-07-2009
Whan I run the following function, I see a mem leak, a 20 mb of memory
is allocated and is not freed. Here is the code I run:

>>> import esauth
>>> for i in range(1000000):

.... ss = esauth.penc('sumer')
....
>>> for i in range(1000000):

.... ss = esauth.penc('sumer')
....

And here is the penc() function.


static PyObject *
penc(PyObject *self, PyObject *args)
{
unsigned char *s= NULL;
unsigned char *buf = NULL;
PyObject * result = NULL;
unsigned int v,len,i = 0;

if (!PyArg_ParseTuple(args, "s#", &s, &len))
return NULL;

buf = strdup(s);
if (!buf) {
PyErr_SetString(PyExc_MemoryError,
"Out of memory: strdup failed");
return NULL;
}


/*string manipulation*/


result = PyString_FromString(buf);
free(buf);
return result;
}


Am I doing something wrong?

Thanks,
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
platform specific API or C standard API George2 C Programming 13 11-13-2007 06:29 PM
.Net Profiler API in 64 bit windows -FunctionMapper callback API =?Utf-8?B?TGVv?= Windows 64bit 0 09-05-2007 06:10 PM
Profiling API or Membership API John123 ASP .Net 0 10-20-2006 03:18 PM
Calling the C API from Python and Python program from same C API -bidirectional Praveen, Tayal (IE10) Python 0 03-17-2005 06:33 AM
What API replaces the unlock API that existed in gcc 2.9.3? Shlomo Anglister C++ 1 08-02-2004 06:50 PM



Advertisments