diff --git a/ChangeLog b/ChangeLog index 1b9d0d95..eb0caab3 100644 --- a/ChangeLog +++ b/ChangeLog @@ -12,6 +12,11 @@ * Merged James Henstridge work on two-phase commit support. + 2008-07-24 James Henstridge + + * psycopg/xid_type.c (xid_len, xid_getitem): implement sequence + behaviour, as required for transaction IDs. + 2008-07-23 James Henstridge * psycopg/connection_type.c (psyco_conn_xid): add a diff --git a/psycopg/python.h b/psycopg/python.h index 4d7333fa..95debf3d 100644 --- a/psycopg/python.h +++ b/psycopg/python.h @@ -41,6 +41,8 @@ #define PY_FORMAT_SIZE_T "" #define PyInt_FromSsize_t(x) PyInt_FromLong((x)) + #define lenfunc inquiry + #define ssizeargfunc intargfunc #define readbufferproc getreadbufferproc #define writebufferproc getwritebufferproc #define segcountproc getsegcountproc diff --git a/psycopg/xid_type.c b/psycopg/xid_type.c index 963ce972..e4b8fb1e 100644 --- a/psycopg/xid_type.c +++ b/psycopg/xid_type.c @@ -184,6 +184,47 @@ xid_repr(XidObject *self) self->pg_xact_id ? self->pg_xact_id : "(null)"); } +static Py_ssize_t +xid_len(XidObject *self) +{ + return 3; +} + +static PyObject * +xid_getitem(XidObject *self, Py_ssize_t item) +{ + if (item < 0) + item += 3; + + switch (item) { + case 0: + Py_INCREF(self->format_id); + return self->format_id; + case 1: + Py_INCREF(self->gtrid); + return self->gtrid; + case 2: + Py_INCREF(self->bqual); + return self->bqual; + default: + PyErr_SetString(PyExc_IndexError, "index out of range"); + return NULL; + } +} + +static PySequenceMethods xid_sequence = { + (lenfunc)xid_len, /* sq_length */ + 0, /* sq_concat */ + 0, /* sq_repeat */ + (ssizeargfunc)xid_getitem, /* sq_item */ + 0, /* sq_slice */ + 0, /* sq_ass_item */ + 0, /* sq_ass_slice */ + 0, /* sq_contains */ + 0, /* sq_inplace_concat */ + 0, /* sq_inplace_repeat */ +}; + static const char xid_doc[] = "A transaction identifier used for two phase commit."; @@ -203,7 +244,7 @@ PyTypeObject XidType = { (reprfunc)xid_repr, /*tp_repr*/ 0, /*tp_as_number*/ - 0, /*tp_as_sequence*/ + &xid_sequence, /*tp_as_sequence*/ 0, /*tp_as_mapping*/ 0, /*tp_hash */