* psycopg/lobject_*.c: replace uses of the closed struct member,

and change the Python level attribute to a getset.

	* psycopg/lobject.h (lobjectObject): remove the closed member,
	since "fd < 0" gives us the same information.  Reorder the struct
	members for better packing.
This commit is contained in:
James Henstridge 2008-05-06 18:07:54 +08:00
parent 2046ae34fb
commit 9d20f5c09c
4 changed files with 40 additions and 17 deletions

View File

@ -1,5 +1,12 @@
2008-05-06 James Henstridge <james@jamesh.id.au>
* psycopg/lobject_*.c: replace uses of the closed struct member,
and change the Python level attribute to a getset.
* psycopg/lobject.h (lobjectObject): remove the closed member,
since "fd < 0" gives us the same information. Reorder the struct
members for better packing.
* psycopg/lobject*: const'ify the code.
* tests/test_lobject.py (LargeObjectTests): add more tests,

View File

@ -39,15 +39,14 @@ typedef struct {
PyObject HEAD;
connectionObject *conn; /* connection owning the lobject */
int closed; /* 1 if the lobject is closed */
int mode; /* numeric mode, tells if lobject was opened */
const char *smode; /* string mode if lobject was opened */
long int mark; /* copied from conn->mark */
Oid oid; /* the oid for this lobject */
const char *smode; /* string mode if lobject was opened */
int mode; /* numeric mode, tells if lobject was opened */
int fd; /* the file descriptor for file-like ops */
Oid oid; /* the oid for this lobject */
} lobjectObject;
/* functions exported from lobject_int.c */
@ -65,10 +64,13 @@ HIDDEN int lobject_seek(lobjectObject *self, int pos, int whence);
HIDDEN int lobject_tell(lobjectObject *self);
HIDDEN int lobject_close(lobjectObject *self);
#define lobject_is_closed(self) \
((self)->fd < 0 || !(self)->conn || (self)->conn->closed)
/* exception-raising macros */
#define EXC_IF_LOBJ_CLOSED(self) \
if ((self)->closed || ((self)->conn && (self)->conn->closed)) { \
if (lobject_is_closed(self)) { \
PyErr_SetString(InterfaceError, "lobject already closed"); \
return NULL; }

View File

@ -95,7 +95,6 @@ lobject_open(lobjectObject *self, connectionObject *conn,
retvalue = -1;
goto end;
}
self->closed = 0;
}
/* set the mode for future reference */
self->mode = mode;
@ -132,7 +131,6 @@ lobject_close_locked(lobjectObject *self, char **error)
self->fd == -1)
return 0;
self->closed = 1;
retvalue = lo_close(self->conn->pgconn, self->fd);
self->fd = -1;
if (retvalue < 0)

View File

@ -52,7 +52,7 @@ psyco_lobj_close(lobjectObject *self, PyObject *args)
/* file-like objects can be closed multiple times and remember that
closing the current transaction is equivalent to close all the
opened large objects */
if (!self->closed
if (!lobject_is_closed(self)
&& self->conn->isolation_level > 0
&& self->conn->mark == self->mark)
{
@ -206,6 +206,17 @@ psyco_lobj_export(lobjectObject *self, PyObject *args)
}
static PyObject *
psyco_lobj_get_closed(lobjectObject *self, void *closure)
{
PyObject *closed;
closed = lobject_is_closed(self) ? Py_True : Py_False;
Py_INCREF(closed);
return closed;
}
/** the lobject object **/
/* object method list */
@ -233,13 +244,19 @@ static struct PyMethodDef lobjectObject_methods[] = {
static struct PyMemberDef lobjectObject_members[] = {
{"oid", T_UINT, offsetof(lobjectObject, oid), RO,
"The backend OID associated to this lobject."},
{"closed", T_INT, 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}
};
/* object getset list */
static struct PyGetSetDef lobjectObject_getsets[] = {
{"closed", (getter)psyco_lobj_get_closed, NULL,
"The if the large object is closed (no file-like methods)."},
{NULL}
};
/* initialization and finalization methods */
static int
@ -259,9 +276,8 @@ lobject_setup(lobjectObject *self, connectionObject *conn,
Py_INCREF((PyObject*)self->conn);
self->closed = 1;
self->oid = InvalidOid;
self->fd = -1;
self->oid = InvalidOid;
if (lobject_open(self, conn, oid, mode, new_oid, new_file) == -1)
return -1;
@ -319,7 +335,7 @@ static PyObject *
lobject_repr(lobjectObject *self)
{
return PyString_FromFormat(
"<lobject object at %p; closed: %d>", self, self->closed);
"<lobject object at %p; closed: %d>", self, lobject_is_closed(self));
}
@ -367,7 +383,7 @@ PyTypeObject lobjectType = {
lobjectObject_methods, /*tp_methods*/
lobjectObject_members, /*tp_members*/
0, /*tp_getset*/
lobjectObject_getsets, /*tp_getset*/
0, /*tp_base*/
0, /*tp_dict*/