Commit 217654ec authored by Kevin Modzelewski's avatar Kevin Modzelewski

Add test for tp_hash and tp_richcompare slots

Somehow had gotten the C API definitions of True and False backwards...
parent c7819eea
...@@ -28,8 +28,8 @@ Don't forget to apply Py_INCREF() when returning either!!! */ ...@@ -28,8 +28,8 @@ Don't forget to apply Py_INCREF() when returning either!!! */
//PyAPI_DATA(PyIntObject) _Py_ZeroStruct, _Py_TrueStruct; //PyAPI_DATA(PyIntObject) _Py_ZeroStruct, _Py_TrueStruct;
PyAPI_DATA(PyObject) *True, *False; PyAPI_DATA(PyObject) *True, *False;
/* Use these macros */ /* Use these macros */
#define Py_False ((PyObject *) True) #define Py_False ((PyObject *) False)
#define Py_True ((PyObject *) False) #define Py_True ((PyObject *) True)
/* Macros for returning Py_True or Py_False, respectively */ /* Macros for returning Py_True or Py_False, respectively */
#define Py_RETURN_TRUE return Py_INCREF(Py_True), Py_True #define Py_RETURN_TRUE return Py_INCREF(Py_True), Py_True
......
...@@ -28,6 +28,21 @@ slots_tester_new(PyTypeObject *type, PyObject *args, PyObject *kwds) ...@@ -28,6 +28,21 @@ slots_tester_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
return (PyObject *)obj; return (PyObject *)obj;
} }
static long slots_tester_seq_hash(slots_tester_object* obj) {
printf("slots_tester_seq.__hash__\n");
return obj->n ^ 1;
}
static PyObject* slots_tester_seq_richcmp(slots_tester_object* lhs, PyObject* rhs, int op) {
printf("slots_tester_seq.richcmp(%d, %d)\n", lhs->n, op);
Py_RETURN_TRUE;
if (op % 2)
Py_RETURN_TRUE;
else
Py_RETURN_FALSE;
}
static PyObject * static PyObject *
slots_tester_seq_repr(slots_tester_object *obj) slots_tester_seq_repr(slots_tester_object *obj)
{ {
...@@ -84,7 +99,7 @@ static PyTypeObject slots_tester_seq = { ...@@ -84,7 +99,7 @@ static PyTypeObject slots_tester_seq = {
0, /* tp_as_number */ 0, /* tp_as_number */
&slots_tester_seq_as_sequence, /* tp_as_sequence */ &slots_tester_seq_as_sequence, /* tp_as_sequence */
0, /* tp_as_mapping */ 0, /* tp_as_mapping */
0, /* tp_hash */ (hashfunc)slots_tester_seq_hash, /* tp_hash */
(ternaryfunc)slots_tester_seq_call, /* tp_call */ (ternaryfunc)slots_tester_seq_call, /* tp_call */
0, /* tp_str */ 0, /* tp_str */
0, /* tp_getattro */ 0, /* tp_getattro */
...@@ -94,7 +109,7 @@ static PyTypeObject slots_tester_seq = { ...@@ -94,7 +109,7 @@ static PyTypeObject slots_tester_seq = {
slots_tester_seq_doc, /* tp_doc */ slots_tester_seq_doc, /* tp_doc */
0, /* tp_traverse */ 0, /* tp_traverse */
0, /* tp_clear */ 0, /* tp_clear */
0, /* tp_richcompare */ (richcmpfunc)slots_tester_seq_richcmp, /* tp_richcompare */
0, /* tp_weaklistoffset */ 0, /* tp_weaklistoffset */
0, /* tp_iter */ 0, /* tp_iter */
0, /* tp_iternext */ 0, /* tp_iternext */
......
...@@ -3,6 +3,7 @@ import slots_test ...@@ -3,6 +3,7 @@ import slots_test
for i in xrange(3): for i in xrange(3):
t = slots_test.SlotsTesterSeq(i + 5) t = slots_test.SlotsTesterSeq(i + 5)
print t, repr(t), t(), t[2] print t, repr(t), t(), t[2]
print hash(t), t < 1, t > 2, t != 3
# print slots_test.SlotsTesterSeq.__doc__ # print slots_test.SlotsTesterSeq.__doc__
print slots_test.SlotsTesterSeq.set_through_tpdict, slots_test.SlotsTesterSeq(5).set_through_tpdict print slots_test.SlotsTesterSeq.set_through_tpdict, slots_test.SlotsTesterSeq(5).set_through_tpdict
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment