]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
memcmp() can return values other than -1, 0, and +1 but tp_compare
authorThomas Heller <theller@ctypes.org>
Tue, 8 Aug 2006 17:42:30 +0000 (17:42 +0000)
committerThomas Heller <theller@ctypes.org>
Tue, 8 Aug 2006 17:42:30 +0000 (17:42 +0000)
must not.

Lib/test/test_types.py
Misc/NEWS
Objects/bufferobject.c

index 4abc7caafb3473d93d3607e21f90c9e0411b650e..38cf2189081c74b44a49e5ca0a7074b1030f716e 100644 (file)
@@ -229,6 +229,7 @@ print 'Buffers'
 try: buffer('asdf', -1)
 except ValueError: pass
 else: raise TestFailed, "buffer('asdf', -1) should raise ValueError"
+cmp(buffer("abc"), buffer("def")) # used to raise a warning: tp_compare didn't return -1, 0, or 1
 
 try: buffer(None)
 except TypeError: pass
index c15925720c5e0ca95f1f490a49da695cf1c1c59a..ad1bb0702c765fd6ed4a05cfcb206ba9ea9a115e 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -12,6 +12,8 @@ What's New in Python 2.4.4c1?
 Core and builtins
 -----------------
 
+- Bug #1536786: buffer comparison could emit a RuntimeWarning.
+
 - Bug #1535165: fixed a segfault in input() and raw_input() when
   sys.stdin is closed.
 
index da8d9fc6a25767eb5962e0d48948fc93c16597d6..a677d0cda34892d2a974514dd14ba0d73795098c 100644 (file)
@@ -230,7 +230,7 @@ buffer_compare(PyBufferObject *self, PyBufferObject *other)
        if (min_len > 0) {
                cmp = memcmp(p1, p2, min_len);
                if (cmp != 0)
-                       return cmp;
+                       return cmp < 0 ? -1 : 1;
        }
        return (len_self < len_other) ? -1 : (len_self > len_other) ? 1 : 0;
 }