[Numpy-discussion] puzzling compiler warning for numeric extension

Todd Miller jmiller at stsci.edu
Thu May 13 05:43:12 CDT 2004


On Wed, 2004-05-12 at 19:54, Russell E Owen wrote:
> I modified an existing numeric c extension today, adding a function 
> that returns a new array. This is the first time I've used 
> NA_NewArray. The code seems to run fine, but I get a disturbing 
> compiler warning:
> 
> [rowen:Shared Files/Guider/PyGuide] rowen% python setup.py build
> ...
> src/radProfModule.c: In function `Py_radSqByRadInd':
> src/radProfModule.c:378: warning: return from incompatible pointer type
> 
> The associated code can be summarized as follows (and it is simple 
> enough that I've also appended a copy). The error is thrown on the 
> last line:
> 
> static PyObject *Py_radSqByRadInd(PyObject *self, PyObject *args) {
> 	long nElt;
> 	Long *radSqByRadInd;
> 
> 	if (!PyArg_ParseTuple(args, "l", &nElt))
> 		return NULL;
> ...
> 	return NA_NewArray((void *)radSqByRadInd, tLong, 1, nElt);
> }
> 
> So...am I actually doing something wrong, or is this warning normal?

The problem is that your function returns a PyObject* but NA_NewArray
returns a PyArrayObject*.   This particular "error" is generally
harmless since all that is really in conflict is the compiler's view of
the same memory address.  You can eliminate the warning by sticking in a
cast to PyObject* like this:

	return (PyObject *) NA_NewArray((void *)radSqByRadInd, tLong, 1, nElt);

Regards,
Todd

> 

> 
> -- Russell
> 
> P.S. the full code:
> 
> static PyObject *Py_radSqByRadInd(PyObject *self, PyObject *args) {
> 	long nElt;
> 	Long radInd;
> 	char ModName[] = "radSqByRadInd";
> 	Long *radSqByRadInd;
> 
> 	if (!PyArg_ParseTuple(args, "l", &nElt))
> 		return NULL;
> 
> 	if (nElt < 0) {
> 		PyErr_Format(PyExc_ValueError, "%s: nPts < 0", ModName);
> 		return NULL;
> 	}
> 
> 	// allocate max(3, nElt) elements (simplifies the computation
> 	// and avoids calling calloc on 0 elements)
> 	radSqByRadInd = calloc(MAX(nElt, 3), sizeof *radSqByRadInd);
> 	if (radSqByRadInd == NULL) {
> 		PyErr_Format(PyExc_MemoryError, "%s: insufficient 
> memory", ModName);
> 		return NULL;
> 	}
> 
> 	for (radInd=0; radInd<3; radInd++) {
> 		radSqByRadInd[radInd] = radInd;
> 	}
> 	for (radInd=3; radInd<nElt; radInd++) {
> 		radSqByRadInd[radInd] = (radInd - 1) * (radInd - 1);
> 	}
> 
> 	return NA_NewArray((void *)radSqByRadInd, tLong, 1, nElt);
> }
> 
> 
> -------------------------------------------------------
> This SF.Net email is sponsored by: SourceForge.net Broadband
> Sign-up now for SourceForge Broadband and get the fastest
> 6.0/768 connection for only $19.95/mo for the first 3 months!
> http://ads.osdn.com/?ad_id=2562&alloc_id=6184&op=click
> _______________________________________________
> Numpy-discussion mailing list
> Numpy-discussion at lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/numpy-discussion
-- 
Todd Miller <jmiller at stsci.edu>





More information about the Numpy-discussion mailing list