C Extension - return an array of longs or pointer?

Brandon K prince_amir86 at yahoo.com
Wed Oct 12 08:56:12 EDT 2005


I'm sorry...I just woke up and forgot my C...must have left it in the 
Coffee...Anyway, i made a few mistakes (can't initialize blank 
tuple...function should return a value, lol).

static PyObject* wrap_doNumberStuff(PyObject* self, PyObject* args)
{
	char* in = 0;
	char* x = 0;
	long* result = 0;
	int i = 0;
	PyObject* py = NULL;
	if(!PyArg_ParseTuple(args,"ss",&in,&x) return NULL;
	
	result = doNumberStuff(in,x);
	len = sizeof(result)/sizeof(long);
	py = PyTuple_New(len);
	for(i; i < len; i++)
		PyTuple_SET_ITEM(py, i, Py_BuildValue("l",*result[i]);

	return py;
}

Additionally, the Python/C api in the docs tells you all of these nifty 
little abstract layer functions that you can call from your extension.

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


----== 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