[PYTHON MATRIX-SIG] More thoughts on comparsion operators.

tim@lassi.ece.uiuc.edu tim@lassi.ece.uiuc.edu
Thu, 24 Oct 1996 10:10:53 -0500


Konrad Hinsen writes:
>> I thought I might be able to use this to make ordered comparisons a
>> little safer. That is, to raise exceptions for ordered comparisons that
>> make no sense instead of returning an arbitrary value. In particular,
>> I was thinking of >, <, etc. on matrices. However this also applies to
>> other cases such as comparing strings to ints.
>
>I think we all agree that this is an important point and should be
>taken care of. As for the suitability of your solution, well, I don't
>particularly like messing around with things like INT_MIN.
>
>Would it be much more work to use a more clearly non-comparison return
>value for "not equal"? Perhaps None in Python code and a null pointer
>in C code?

None works for python code as is. The only way this implementation
detail creeps into Python itself, is that if you return INT_MIN from
__cmp__ your going to get an error. As I mentioned previously, this is
a bug anyway since no legitimate cmp function will return INT_MIN, so
an error here is the right thing. On the down side, it will be the
wrong error message (Hmmm - but I think I could fix that...).

As for c code, I think we're out of luck there. The comparison
function for c-objects returns an int, not a python object. INT_MIN is
the best I could come with. If you have any ideas though, let me know.


>Whatever a final solution will look like, your patches are interesting
>just for personal use as a debugging aid. Thanks!
_

Your welcome. Glad to be of service. Guido pointed out that I hadn't
fixed list.sort(). Here's a patch to fix that:

The way errors are handled could still be cleaned up, but not today... 

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++




*** Objects/listobject.c        Wed Oct 23 18:49:54 1996
--- Objects/listobject.c.orig   Wed Oct 23 18:39:31 1996
***************
*** 24,34 ****
  
  /* List object implementation */
  
- #include <limits.h>
- #ifdef roundup
- #undef roundup
- #endif
- 
  #include "allobjects.h"
  #include "modsupport.h"
  #include "ceval.h"
--- 24,29 ----
***************
*** 560,573 ****
        if (err_occurred())
                return 0;
  
!       if (comparefunc == NULL) {
!               int cmp_result = cmpobject(* (object **) v, * (object **) w);
!               if (cmp_result == INT_MIN) {
!                   err_setstr(TypeError, "Illegal types for ordered comparison i
n sort");
!                   return 0;
!               }
!               return cmp_result;
!       }
  
        /* Call the user-supplied comparison function */
        t = mkvalue("(OO)", * (object **) v, * (object **) w);
--- 555,562 ----
        if (err_occurred())
                return 0;
  
!       if (comparefunc == NULL)
!               return cmpobject(* (object **) v, * (object **) w);
  
        /* Call the user-supplied comparison function */
        t = mkvalue("(OO)", * (object **) v, * (object **) w);





++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

	-tim

+--------------------------------------------------------------------+
| Tim Hochberg               Ultrahigh Speed Digital Electronics Lab |
| tim@lassi.ece.uiuc.edu              University of Illinois         |
| http://dogbert.ece.uiuc.edu/~tim         (217) 333-6014            |
+--------------------------------------------------------------------+

=================
MATRIX-SIG  - SIG on Matrix Math for Python

send messages to: matrix-sig@python.org
administrivia to: matrix-sig-request@python.org
=================