Commit 60289a91 authored by Kevin Modzelewski's avatar Kevin Modzelewski

Support tp_str

parent dd12bff0
......@@ -364,6 +364,14 @@ PyObject* slot_tp_repr(PyObject* self) noexcept {
}
}
PyObject* slot_tp_str(PyObject* self) noexcept {
try {
return str(self);
} catch (Box* e) {
abort();
}
}
static long slot_tp_hash(PyObject* self) noexcept {
PyObject* func;
static PyObject* hash_str, *eq_str, *cmp_str;
......@@ -734,6 +742,7 @@ static slotdef slotdefs[] = {
TPSLOT("__hash__", tp_hash, slot_tp_hash, wrap_hashfunc, "x.__hash__() <==> hash(x)"),
FLSLOT("__call__", tp_call, slot_tp_call, (wrapperfunc)wrap_call, "x.__call__(...) <==> x(...)",
PyWrapperFlag_KEYWORDS),
TPSLOT("__str__", tp_str, slot_tp_str, wrap_unaryfunc, "x.__str__() <==> str(x)"),
TPSLOT("__lt__", tp_richcompare, slot_tp_richcompare, richcmp_lt, "x.__lt__(y) <==> x<y"),
TPSLOT("__le__", tp_richcompare, slot_tp_richcompare, richcmp_le, "x.__le__(y) <==> x<=y"),
TPSLOT("__eq__", tp_richcompare, slot_tp_richcompare, richcmp_eq, "x.__eq__(y) <==> x==y"),
......@@ -914,7 +923,6 @@ extern "C" int PyType_Ready(PyTypeObject* cls) {
cls->tp_as_number->nb_nonzero = nb_nonzero;
}
RELEASE_ASSERT(cls->tp_str == NULL, "");
RELEASE_ASSERT(cls->tp_getattro == NULL || cls->tp_getattro == PyObject_GenericGetAttr, "");
RELEASE_ASSERT(cls->tp_setattro == NULL || cls->tp_setattro == PyObject_GenericSetAttr, "");
RELEASE_ASSERT(cls->tp_as_buffer == NULL, "");
......
......@@ -61,6 +61,14 @@ slots_tester_seq_repr(slots_tester_object *obj)
return PyString_FromString(buf);
}
static PyObject *
slots_tester_seq_str(slots_tester_object *obj)
{
char buf[80];
snprintf(buf, sizeof(buf), "<my custom str: %d>", obj->n);
return PyString_FromString(buf);
}
static PyObject *
slots_tester_seq_call(slots_tester_object *obj, PyObject *args, PyObject *kw)
{
......@@ -111,7 +119,7 @@ static PyTypeObject slots_tester_seq = {
0, /* tp_as_mapping */
(hashfunc)slots_tester_seq_hash, /* tp_hash */
(ternaryfunc)slots_tester_seq_call, /* tp_call */
0, /* tp_str */
(reprfunc)slots_tester_seq_str, /* tp_str */
0, /* tp_getattro */
0, /* tp_setattro */
0, /* tp_as_buffer */
......@@ -170,7 +178,7 @@ static PyTypeObject slots_tester_map= {
0, /* tp_getattr */
0, /* tp_setattr */
0, /* tp_compare */
0, /* tp_repr */
(reprfunc)slots_tester_seq_repr, /* tp_repr */
0, /* tp_as_number */
0, /* tp_as_sequence */
&slots_tester_map_asmapping, /* tp_as_mapping */
......
......@@ -2,7 +2,7 @@ import slots_test
for i in xrange(3):
t = slots_test.SlotsTesterSeq(i + 5)
print t, repr(t), t(), t[2]
print t, repr(t), str(t), t(), t[2]
print hash(t), t < 1, t > 2, t != 3, bool(t)
# print slots_test.SlotsTesterSeq.__doc__
......@@ -10,7 +10,7 @@ print slots_test.SlotsTesterSeq.set_through_tpdict, slots_test.SlotsTesterSeq(5)
for i in xrange(3):
t = slots_test.SlotsTesterMap(i + 5)
print len(t), t[2]
print len(t), t[2], repr(t), str(t)
t[1] = 5
del t[2]
......
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