Added some file-like attributes to lobject.

This commit is contained in:
Federico Di Gregorio 2006-09-02 05:33:03 +00:00
parent 64bd7ae61c
commit 269156d9bf
5 changed files with 29 additions and 12 deletions

View File

@ -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 - `connection` -- the new-type inheritable connection class
- `cursor` -- the new-type inheritable cursor 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 - `adapt()` -- exposes the PEP-246_ compatible adapting mechanism used
by psycopg to adapt Python types to PostgreSQL ones by psycopg to adapt Python types to PostgreSQL ones
@ -38,7 +39,7 @@ try:
except: except:
pass 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 string_types, binary_types, new_type, register_type
from _psycopg import ISQLQuote from _psycopg import ISQLQuote

View File

@ -217,7 +217,7 @@ psyco_conn_lobject(connectionObject *self, PyObject *args, PyObject *keywds)
static char *kwlist[] = {"oid", "mode", "new_oid", "new_file", static char *kwlist[] = {"oid", "mode", "new_oid", "new_file",
"cursor_factory", NULL}; "cursor_factory", NULL};
if (!PyArg_ParseTupleAndKeywords(args, keywds, "|izisO", kwlist, if (!PyArg_ParseTupleAndKeywords(args, keywds, "|izizO", kwlist,
&oid, &smode, &new_oid, &new_file, &oid, &smode, &new_oid, &new_file,
&factory)) { &factory)) {
return NULL; return NULL;

View File

@ -39,8 +39,10 @@ typedef struct {
connectionObject *conn; /* connection owning the lobject */ 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 */ long int mark; /* copied from conn->mark */
Oid oid; /* the oid for this lobject */ Oid oid; /* the oid for this lobject */

View File

@ -86,6 +86,18 @@ lobject_open(lobjectObject *self, connectionObject *conn,
return -1; return -1;
} }
else { 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; return 0;
} }
} }

View File

@ -230,9 +230,12 @@ static struct PyMethodDef lobjectObject_methods[] = {
/* object member list */ /* object member list */
static struct PyMemberDef lobjectObject_members[] = { static struct PyMemberDef lobjectObject_members[] = {
{"oid", T_LONG, {"oid", T_LONG, offsetof(lobjectObject, oid), RO,
offsetof(lobjectObject, oid), RO,
"The backend OID associated to this lobject."}, "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} {NULL}
}; };
@ -261,12 +264,11 @@ lobject_setup(lobjectObject *self, connectionObject *conn,
if (lobject_open(self, conn, oid, mode, new_oid, new_file) == -1) if (lobject_open(self, conn, oid, mode, new_oid, new_file) == -1)
return -1; return -1;
else {
Dprintf("lobject_setup: good lobject object at %p, refcnt = %d", Dprintf("lobject_setup: good lobject object at %p, refcnt = %d",
self, ((PyObject *)self)->ob_refcnt); self, ((PyObject *)self)->ob_refcnt);
Dprintf("lobject_setup: oid = %d, fd = %d", self->oid, self->fd); Dprintf("lobject_setup: oid = %d, fd = %d", self->oid, self->fd);
return 0; return 0;
}
} }
static void static void