Title: qsort doesn't work for double arrays
Type: behavior Stage:
Components: ctypes Versions: Python 3.3, Python 2.7, Python 2.6
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: amaury.forgeotdarc, belopolsky, mark.dickinson, meador.inge, stephane.poirier
Priority: normal Keywords:

Created on 2013-01-11 14:13 by stephane.poirier, last changed 2013-01-11 14:57 by mark.dickinson. This issue is now closed.

File name Uploaded Description Edit stephane.poirier, 2013-01-11 14:13
Messages (3)
msg179680 - (view) Author: Stephane Poirier (stephane.poirier) Date: 2013-01-11 14:13
qsort doesn't sort arrays that are defined with double elements.
You can find attached a script that shows this function working well for int (as described in the ctypes documentation). If int array is replaced by double array qsort doesn't sort any more.
Even worse, with python 2.6, the difference between a[0] and b[0] may be nan.
The script can be launch by 
msg179683 - (view) Author: Mark Dickinson (mark.dickinson) * (Python committer) Date: 2013-01-11 14:53
What happens if you replace the line:

    CMPFUNC = CFUNCTYPE(c_double, POINTER(c_double), POINTER(c_double))

in your example code with

    CMPFUNC = CFUNCTYPE(c_int, POINTER(c_double), POINTER(c_double))

msg179685 - (view) Author: Mark Dickinson (mark.dickinson) * (Python committer) Date: 2013-01-11 14:57
Closing as invalid:  the issue is not in Python but with the CMPFUNC line.  Your py_cmp_func should return an integer rather than a float to be compatible with libc.qsort.
Date User Action Args
2013-01-11 14:57:43mark.dickinsonsetstatus: open -> closed
resolution: not a bug
messages: + msg179685
2013-01-11 14:53:53mark.dickinsonsetnosy: + mark.dickinson
messages: + msg179683
2013-01-11 14:24:18serhiy.storchakasetnosy: + amaury.forgeotdarc, belopolsky, meador.inge
2013-01-11 14:13:56stephane.poiriercreate