1From eee0beef88d135640871050b40844272a3aee790 Mon Sep 17 00:00:00 2001
2From: Tres Seaver <tseaver@palladion.com>
3Date: Tue, 15 Sep 2015 17:20:18 -0400
4Subject: [PATCH 1/2] Ensure that we don't overlook errors in first
5 PyObject_RichCompareBool call.
6
7Python 3.5 turns such cases into SystemErrors.
8
9See: https://bugs.python.org/issue23571
10
11Fixes #15.
12---
13 BTrees/_compat.h | 22 +++++++++++++++++++---
14 1 file changed, 19 insertions(+), 3 deletions(-)
15
16diff --git a/BTrees/_compat.h b/BTrees/_compat.h
17index e004d54..19dd377 100644
18--- a/BTrees/_compat.h
19+++ b/BTrees/_compat.h
20@@ -27,9 +27,25 @@
21 #define TEXT_FROM_STRING PyUnicode_FromString
22 #define TEXT_FORMAT PyUnicode_Format
23
24-#define COMPARE(lhs, rhs) \
25- PyObject_RichCompareBool((lhs), (rhs), Py_LT) > 0 ? -1 : \
26- (PyObject_RichCompareBool((lhs), (rhs), Py_EQ) > 0 ? 0 : 1)
27+/* Emulate Python2's __cmp__, wrapping PyObject_RichCompareBool(),
28+ * Return -2/-3 for errors, -1 for lhs<rhs, 0 for lhs==rhs, 1 for lhs>rhs.
29+ */
30+static inline
31+int __compare(PyObject *lhs, PyObject *rhs) {
32+ int less, equal;
33+
34+ less = PyObject_RichCompareBool(lhs, rhs, Py_LT);
35+ if ( less == -1 ) {
36+ return -2;
37+ }
38+ equal = PyObject_RichCompareBool(lhs, rhs, Py_EQ);
39+ if ( equal == -1 ) {
40+ return -3;
41+ }
42+ return less ? -1 : (equal ? 0 : 1);
43+}
44+
45+#define COMPARE(lhs, rhs) __compare((lhs), (rhs))
46
47
48 #else
49
50From ff4c3309fe471f2b9bdd642b8f7d1c2fe0f5e458 Mon Sep 17 00:00:00 2001
51From: Tres Seaver <tseaver@palladion.com>
52Date: Sun, 20 Sep 2015 11:07:10 -0400
53Subject: [PATCH 2/2] Avoid unnecessary comparison for 'Py_EQ' if 'Py_LT'
54 returned True.
55
56---
57 BTrees/_compat.h | 5 ++++-
58 1 file changed, 4 insertions(+), 1 deletion(-)
59
60diff --git a/BTrees/_compat.h b/BTrees/_compat.h
61index 19dd377..ece2bf9 100644
62--- a/BTrees/_compat.h
63+++ b/BTrees/_compat.h
64@@ -38,11 +38,14 @@ int __compare(PyObject *lhs, PyObject *rhs) {
65 if ( less == -1 ) {
66 return -2;
67 }
68+ if (less) {
69+ return -1;
70+ }
71 equal = PyObject_RichCompareBool(lhs, rhs, Py_EQ);
72 if ( equal == -1 ) {
73 return -3;
74 }
75- return less ? -1 : (equal ? 0 : 1);
76+ return equal ? 0 : 1;
77 }
78
79 #define COMPARE(lhs, rhs) __compare((lhs), (rhs))