]> granicus.if.org Git - python/commitdiff
memcmp() can return values other than -1, 0, and +1 but tp_compare
authorThomas Heller <theller@ctypes.org>
Tue, 8 Aug 2006 17:37:00 +0000 (17:37 +0000)
committerThomas Heller <theller@ctypes.org>
Tue, 8 Aug 2006 17:37:00 +0000 (17:37 +0000)
must not.

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

index f0bdfdebf7291c88284b061cb0678bcfdd4d0975..83a01aa8e5d1209adad8332e58874088f31d5dfa 100644 (file)
@@ -233,6 +233,9 @@ 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
+
+cmp(buffer('abc'), buffer('def'))
 
 try: buffer(None)
 except TypeError: pass
index 04549b919558186507848b306b914faf32ae184a..24b16453d177cae2441fc0d48c1ebd12d7f4a845 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -12,6 +12,8 @@ What's New in Python 2.5 release candidate 1?
 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 588f7e24eaf9509c3682e97504658c8c80763405..5f3c7a9eeb4affb891a8e5179aaaf535374b4bf1 100644 (file)
@@ -272,7 +272,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;
 }