1 #define KEYMACROS_H "$Id$\n"
2 #define KEY_TYPE PyObject *
3 #define KEY_TYPE_IS_PYOBJECT
4 
5 #include "Python.h"
6 #include "_compat.h"
7 
8 static PyObject *object_; /* initialized in BTreeModuleTemplate init */
9 
10 static int
check_argument_cmp(PyObject * arg)11 check_argument_cmp(PyObject *arg)
12 {
13     /* printf("check cmp %p %p %p %p\n",  */
14     /*        arg->ob_type->tp_richcompare, */
15     /*        ((PyTypeObject *)object_)->ob_type->tp_richcompare, */
16     /*        arg->ob_type->tp_compare, */
17     /*        ((PyTypeObject *)object_)->ob_type->tp_compare); */
18     if (arg == Py_None) {
19       return 1;
20     }
21 
22 
23 #ifdef PY3K
24     if (Py_TYPE(arg)->tp_richcompare == Py_TYPE(object_)->tp_richcompare)
25 #else
26     if ((Py_TYPE(arg)->tp_richcompare == NULL
27 	 && Py_TYPE(arg)->tp_compare == Py_TYPE(object_)->tp_compare)
28 	/* Also exclude new-style classes. On Python 2, they can be compared,
29 	   but order by address, making them not suitable for BTrees. */
30 	|| PyType_CheckExact(arg)
31 	/* But let classes with a meta class that implements comparison through. */
32 	|| (PyType_Check(arg) && Py_TYPE(arg)->tp_richcompare == PyType_Type.tp_richcompare)
33 	)
34 #endif
35     {
36         PyErr_Format(PyExc_TypeError,
37 		     "Object of class %s has default comparison",
38 		     Py_TYPE(arg)->tp_name);
39         return 0;
40     }
41     return 1;
42 }
43 
44 #define TEST_KEY_SET_OR(V, KEY, TARGET) \
45 if ( ( (V) = COMPARE((KEY),(TARGET)) ), PyErr_Occurred() )
46 #define INCREF_KEY(k) Py_INCREF(k)
47 #define DECREF_KEY(KEY) Py_DECREF(KEY)
48 #define COPY_KEY(KEY, E) KEY=(E)
49 #define COPY_KEY_TO_OBJECT(O, K) O=(K); Py_INCREF(O)
50 #define COPY_KEY_FROM_ARG(TARGET, ARG, S) \
51     TARGET=(ARG); \
52     (S) = 1;
53 #define KEY_CHECK_ON_SET check_argument_cmp
54