C Extension - return an array of longs or pointer?

Java and Swing codecraig at gmail.com
Wed Oct 12 09:01:55 EDT 2005


Interesting...thanks.  Any good tutorials out there, other than the
python doc for ext?


thanks.

Brandon K wrote:
> All the veteran programmers out there can correct me, but the way I did
> it in my extension was this:
>
> static PyObject *wrap_doNumberStuff(PyObject* self, PyObject* args)
> {
> 	char* in = 0;
> 	char* x = 0;
> 	long* result = 0;
> 	int i = 0;
> 	PyObject* py = PyTuple_New()
> 	int ok = PyArg_ParseTuple(args,"ss",&in, &x);
> 	if(!ok) return NULL;
>
> 	result = doNumberStuff(in,x):
> 	len = sizeof(result)/sizeof(long)
> 	for(i;i < len; i++)
> 		PyTuple_SET_ITEM(py, i,Py_BuildValue("l",*result[i])
> }
>
> Simple enough idea...i'm not quite sure if I've done everything
> correctly with the pointers, but I'm sure you can figure that out, the
> algorithm is simple enough.
>
> > Hi,
> >    I have been posting about writing a C extension for Python...so far,
> > so good.  At least for the "simple" functions that I need to wrap.
> >
> > Ok, my c function looks like...
> >
> > MY_NUM *doNumberStuff(const char *in, const char *x) { ... }
> >
> > MY_NUM is defined as, typedef unsigned long MY_NUM;  (not sure if that
> > matters, or can i just create a wrapper which handles longs?)
> >
> > anyhow..for my wrapper I have this..
> >
> > static PyObject *wrap_doNumberStuff(PyObject *self, PyObject args) {
> >     char *in = 0;
> >     char *x = 0;
> >     long *result = 0;
> >     int ok = PyArg_ParseTuple(args, "ss", &in, &x);
> >     if (!ok) return 0;
> >
> >     result = doNumberStuff(in, x);
> >
> >     return Py_BuildValue("l", result);
> > }
> >
> > ...my question is...in the c code, result is a pointer to an array of
> > longs, how can I get the returned result to be a list or something
> > similar to an array in Python?
> >
> > ...I also have a function which returns a character array (denoted by a
> > char *)...would it work the same as the previous question?
> >
> > Thanks!!
> >
>
>
> ----== Posted via Newsgroups.com - Usenet Access to over 100,000 Newsgroups ==----
> Get Anonymous, Uncensored, Access to West and East Coast Server Farms!
> ----== Highest Retention and Completion Rates! HTTP://WWW.NEWSGROUPS.COM ==----




More information about the Python-list mailing list