From 3312897e5dc17f9b49c4eebdd4a87c9ac3ab12dd Mon Sep 17 00:00:00 2001 From: Daniele Varrazzo Date: Tue, 12 Oct 2010 01:02:13 +0100 Subject: [PATCH] Added str() and repr() for Xid objects. --- psycopg/xid_type.c | 47 ++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 45 insertions(+), 2 deletions(-) diff --git a/psycopg/xid_type.c b/psycopg/xid_type.c index fbc49662..39cbed8b 100644 --- a/psycopg/xid_type.c +++ b/psycopg/xid_type.c @@ -188,6 +188,49 @@ xid_getitem(XidObject *self, Py_ssize_t item) } } +static PyObject * +xid_str(XidObject *self) +{ + return xid_get_tid(self); +} + +static PyObject * +xid_repr(XidObject *self) +{ + PyObject *rv = NULL; + PyObject *format = NULL; + PyObject *args = NULL; + + if (Py_None == self->format_id) { + if (!(format = PyString_FromString(""))) { + goto exit; + } + if (!(args = PyTuple_New(1))) { goto exit; } + Py_INCREF(self->gtrid); + PyTuple_SET_ITEM(args, 0, self->gtrid); + } + else { + if (!(format = PyString_FromString(""))) { + goto exit; + } + if (!(args = PyTuple_New(3))) { goto exit; } + Py_INCREF(self->format_id); + PyTuple_SET_ITEM(args, 0, self->format_id); + Py_INCREF(self->gtrid); + PyTuple_SET_ITEM(args, 1, self->gtrid); + Py_INCREF(self->bqual); + PyTuple_SET_ITEM(args, 2, self->bqual); + } + + rv = PyString_Format(format, args); + +exit: + Py_XDECREF(args); + Py_XDECREF(format); + + return rv; +} + static PySequenceMethods xid_sequence = { (lenfunc)xid_len, /* sq_length */ 0, /* sq_concat */ @@ -218,14 +261,14 @@ PyTypeObject XidType = { 0, /*tp_compare*/ - 0, /*tp_repr*/ + (reprfunc)xid_repr, /*tp_repr*/ 0, /*tp_as_number*/ &xid_sequence, /*tp_as_sequence*/ 0, /*tp_as_mapping*/ 0, /*tp_hash */ 0, /*tp_call*/ - 0, /*tp_str*/ + (reprfunc)xid_str, /*tp_str*/ 0, /*tp_getattro*/ 0, /*tp_setattro*/