summaryrefslogtreecommitdiff
path: root/psycopg
diff options
context:
space:
mode:
authorDaniele Varrazzo <daniele.varrazzo@gmail.com>2010-10-06 01:25:14 +0100
committerDaniele Varrazzo <daniele.varrazzo@gmail.com>2010-11-05 09:34:47 +0000
commit22aea9114b0e37716394f9de476d8f1a6720931e (patch)
treefc2aab3edcba8497bcd247af92ab2cb76849735d /psycopg
parent0021e56d800dffbb7fa30fbb3349a1016456343e (diff)
downloadpsycopg2-22aea9114b0e37716394f9de476d8f1a6720931e.tar.gz
implement sequence behaviour, as required for transaction IDs.
By James Henstridge on 2008-07-24. Merged from lp:~jamesh/psycopg/two-phase-commit/revision/357
Diffstat (limited to 'psycopg')
-rw-r--r--psycopg/python.h2
-rw-r--r--psycopg/xid_type.c43
2 files changed, 44 insertions, 1 deletions
diff --git a/psycopg/python.h b/psycopg/python.h
index 4d7333f..95debf3 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 963ce97..e4b8fb1 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 */