From 269156d9bfef52ad5f2dd865fe4a69c34574939b Mon Sep 17 00:00:00 2001 From: Federico Di Gregorio Date: Sat, 2 Sep 2006 05:33:03 +0000 Subject: [PATCH] Added some file-like attributes to lobject. --- lib/extensions.py | 3 ++- psycopg/connection_type.c | 2 +- psycopg/lobject.h | 6 ++++-- psycopg/lobject_int.c | 12 ++++++++++++ psycopg/lobject_type.c | 18 ++++++++++-------- 5 files changed, 29 insertions(+), 12 deletions(-) diff --git a/lib/extensions.py b/lib/extensions.py index 9233d1d7..5fedbd23 100644 --- a/lib/extensions.py +++ b/lib/extensions.py @@ -4,6 +4,7 @@ This module holds all the extensions to the DBAPI-2.0 provided by psycopg. - `connection` -- the new-type inheritable connection class - `cursor` -- the new-type inheritable cursor class +- `lobject` -- the new-type inheritable large object class - `adapt()` -- exposes the PEP-246_ compatible adapting mechanism used by psycopg to adapt Python types to PostgreSQL ones @@ -38,7 +39,7 @@ try: except: pass -from _psycopg import adapt, adapters, encodings, connection, cursor +from _psycopg import adapt, adapters, encodings, connection, cursor, lobject from _psycopg import string_types, binary_types, new_type, register_type from _psycopg import ISQLQuote diff --git a/psycopg/connection_type.c b/psycopg/connection_type.c index 009955a1..32ce8618 100644 --- a/psycopg/connection_type.c +++ b/psycopg/connection_type.c @@ -217,7 +217,7 @@ psyco_conn_lobject(connectionObject *self, PyObject *args, PyObject *keywds) static char *kwlist[] = {"oid", "mode", "new_oid", "new_file", "cursor_factory", NULL}; - if (!PyArg_ParseTupleAndKeywords(args, keywds, "|izisO", kwlist, + if (!PyArg_ParseTupleAndKeywords(args, keywds, "|izizO", kwlist, &oid, &smode, &new_oid, &new_file, &factory)) { return NULL; diff --git a/psycopg/lobject.h b/psycopg/lobject.h index 76c5cfb8..4b84021b 100644 --- a/psycopg/lobject.h +++ b/psycopg/lobject.h @@ -39,8 +39,10 @@ typedef struct { connectionObject *conn; /* connection owning the lobject */ - int closed:1; /* 1 if the lobject is closed */ - + int closed; /* 1 if the lobject is closed */ + int mode; /* numeric mode, tells if lobject was opened */ + char *smode; /* string mode if lobject was opened */ + long int mark; /* copied from conn->mark */ Oid oid; /* the oid for this lobject */ diff --git a/psycopg/lobject_int.c b/psycopg/lobject_int.c index 9b40608e..d4cc38f2 100644 --- a/psycopg/lobject_int.c +++ b/psycopg/lobject_int.c @@ -86,6 +86,18 @@ lobject_open(lobjectObject *self, connectionObject *conn, return -1; } else { + /* set the mode for future reference and return */ + self->mode = mode; + switch (mode) { + case -1: + self->smode = "n"; break; + case INV_READ: + self->smode = "r"; break; + case INV_WRITE: + self->smode = "w"; break; + case INV_READ+INV_WRITE: + self->smode = "rw"; break; + } return 0; } } diff --git a/psycopg/lobject_type.c b/psycopg/lobject_type.c index 69036091..8d299e94 100644 --- a/psycopg/lobject_type.c +++ b/psycopg/lobject_type.c @@ -230,9 +230,12 @@ static struct PyMethodDef lobjectObject_methods[] = { /* object member list */ static struct PyMemberDef lobjectObject_members[] = { - {"oid", T_LONG, - offsetof(lobjectObject, oid), RO, + {"oid", T_LONG, offsetof(lobjectObject, oid), RO, "The backend OID associated to this lobject."}, + {"closed", T_LONG, offsetof(lobjectObject, closed), RO, + "The if the large object is closed (no file-like methods)."}, + {"mode", T_STRING, offsetof(lobjectObject, smode), RO, + "Open mode ('r', 'w', 'rw' or 'n')."}, {NULL} }; @@ -261,12 +264,11 @@ lobject_setup(lobjectObject *self, connectionObject *conn, if (lobject_open(self, conn, oid, mode, new_oid, new_file) == -1) return -1; - else { - Dprintf("lobject_setup: good lobject object at %p, refcnt = %d", - self, ((PyObject *)self)->ob_refcnt); - Dprintf("lobject_setup: oid = %d, fd = %d", self->oid, self->fd); - return 0; - } + + Dprintf("lobject_setup: good lobject object at %p, refcnt = %d", + self, ((PyObject *)self)->ob_refcnt); + Dprintf("lobject_setup: oid = %d, fd = %d", self->oid, self->fd); + return 0; } static void