psycopg is enlarging..

This commit is contained in:
Federico Di Gregorio 2006-09-01 16:27:02 +00:00
parent 64933f2004
commit b8f3cef62f
11 changed files with 486 additions and 9 deletions

View File

@ -1,3 +1,11 @@
2006-09-01 Federico Di Gregorio <fog@initd.org>
* psycopg/connection_int.c: removed increment of self->mark,
now it is done directly in pqpath.c to make sure even the
large object support gets it.
* Starting 2.1 development.
2006-09-01 Federico Di Gregorio <fog@initd.org> 2006-09-01 Federico Di Gregorio <fog@initd.org>
* Release 2.0.5. * Release 2.0.5.

40
examples/lobject.py Normal file
View File

@ -0,0 +1,40 @@
# lobject.py - lobject example
#
# Copyright (C) 2001-2006 Federico Di Gregorio <fog@debian.org>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by the
# Free Software Foundation; either version 2, or (at your option) any later
# version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTIBILITY
# or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
# for more details.
## put in DSN your DSN string
DSN = 'dbname=test'
## don't modify anything below this line (except for experimenting)
import sys
import psycopg2
if len(sys.argv) > 1:
DSN = sys.argv[1]
print "Opening connection using dns:", DSN
conn = psycopg2.connect(DSN)
print "Encoding for this connection is", conn.encoding
# this will create a large object with a new random oid
lobj = conn.lobject()
print "lobject oid =", lobj.oid
# this will create a large object with the given oid
lobj = conn.lobject(0, 0, 666)
print "lobject oid =", lobj.oid
lobj = conn.lobject(0, 0, 666)

View File

@ -202,7 +202,6 @@ conn_commit(connectionObject *self)
pthread_mutex_lock(&self->lock); pthread_mutex_lock(&self->lock);
res = pq_commit(self); res = pq_commit(self);
self->mark++;
pthread_mutex_unlock(&self->lock); pthread_mutex_unlock(&self->lock);
Py_END_ALLOW_THREADS; Py_END_ALLOW_THREADS;
@ -221,7 +220,6 @@ conn_rollback(connectionObject *self)
pthread_mutex_lock(&self->lock); pthread_mutex_lock(&self->lock);
res = pq_abort(self); res = pq_abort(self);
self->mark++;
pthread_mutex_unlock(&self->lock); pthread_mutex_unlock(&self->lock);
Py_END_ALLOW_THREADS; Py_END_ALLOW_THREADS;
@ -245,7 +243,6 @@ conn_switch_isolation_level(connectionObject *self, int level)
res = pq_abort(self); res = pq_abort(self);
} }
self->isolation_level = level; self->isolation_level = level;
self->mark++;
Dprintf("conn_switch_isolation_level: switched to level %d", level); Dprintf("conn_switch_isolation_level: switched to level %d", level);

View File

@ -31,6 +31,7 @@
#include "psycopg/psycopg.h" #include "psycopg/psycopg.h"
#include "psycopg/connection.h" #include "psycopg/connection.h"
#include "psycopg/cursor.h" #include "psycopg/cursor.h"
#include "psycopg/lobject.h"
/** DBAPI methods **/ /** DBAPI methods **/
@ -169,9 +170,8 @@ psyco_conn_set_isolation_level(connectionObject *self, PyObject *args)
return Py_None; return Py_None;
} }
/* set_isolation_level method - switch connection isolation level */ /* set_client_encoding method - set client encoding */
#define psyco_conn_set_client_encoding_doc \ #define psyco_conn_set_client_encoding_doc \
"set_client_encoding(encoding) -- Set client encoding to ``encoding``." "set_client_encoding(encoding) -- Set client encoding to ``encoding``."
@ -193,6 +193,66 @@ psyco_conn_set_client_encoding(connectionObject *self, PyObject *args)
return NULL; return NULL;
} }
} }
/* lobject method - allocate a new lobject */
#define psyco_conn_lobject_doc \
"cursor(oid=0, mode=0, new_oid=0, new_file=None,\n" \
" lobject_factory=extensions.lobject) -- new lobject\n\n" \
"Return a new lobject.\n\nThe ``lobject_factory`` argument can be used\n" \
"to create non-standard lobjects by passing a class different from the\n" \
"default. Note that the new class *should* be a sub-class of\n" \
"`extensions.lobject`.\n\n" \
":rtype: `extensions.lobject`"
static PyObject *
psyco_conn_lobject(connectionObject *self, PyObject *args, PyObject *keywds)
{
int oid=0, new_oid=0, mode=0;
char *new_file = NULL;
PyObject *obj, *factory = NULL;
static char *kwlist[] = {"oid", "mode", "new_oid", "new_file",
"cursor_factory", NULL};
if (!PyArg_ParseTupleAndKeywords(args, keywds, "|iiisO", kwlist,
&oid, &mode, &new_oid, &new_file,
&factory)) {
return NULL;
}
EXC_IF_CONN_CLOSED(self);
Dprintf("psyco_conn_lobject: new lobject for connection at %p", self);
Dprintf("psyco_conn_lobject: parameters: oid = %d, mode = %d",
oid, mode);
Dprintf("psyco_conn_lobject: parameters: new_oid = %d, new_file = %s",
new_oid, new_file);
if (factory == NULL) factory = (PyObject *)&lobjectType;
if (new_file)
obj = PyObject_CallFunction(factory, "Oiiis",
self, oid, mode, new_oid, new_file);
else
obj = PyObject_CallFunction(factory, "Oiii",
self, oid, mode, new_oid);
if (obj == NULL) return NULL;
if (PyObject_IsInstance(obj, (PyObject *)&lobjectType) == 0) {
PyErr_SetString(PyExc_TypeError,
"lobject factory must be subclass of psycopg2._psycopg.lobject");
Py_DECREF(obj);
return NULL;
}
Dprintf("psyco_conn_lobject: new lobject at %p: refcnt = %d",
obj, obj->ob_refcnt);
return obj;
}
#endif #endif
@ -214,7 +274,9 @@ static struct PyMethodDef connectionObject_methods[] = {
{"set_isolation_level", (PyCFunction)psyco_conn_set_isolation_level, {"set_isolation_level", (PyCFunction)psyco_conn_set_isolation_level,
METH_VARARGS, psyco_conn_set_isolation_level_doc}, METH_VARARGS, psyco_conn_set_isolation_level_doc},
{"set_client_encoding", (PyCFunction)psyco_conn_set_client_encoding, {"set_client_encoding", (PyCFunction)psyco_conn_set_client_encoding,
METH_VARARGS, psyco_conn_set_client_encoding_doc}, METH_VARARGS, psyco_conn_set_client_encoding_doc},
{"lobject", (PyCFunction)psyco_conn_lobject,
METH_VARARGS|METH_KEYWORDS, psyco_conn_lobject_doc},
#endif #endif
{NULL} {NULL}
}; };

74
psycopg/lobject.h Normal file
View File

@ -0,0 +1,74 @@
/* lobject.h - definition for the psycopg lobject type
*
* Copyright (C) 2006 Federico Di Gregorio <fog@debian.org>
*
* This file is part of psycopg.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2,
* or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#ifndef PSYCOPG_LOBJECT_H
#define PSYCOPG_LOBJECT_H 1
#include <Python.h>
#include <libpq-fe.h>
#include <libpq/libpq-fs.h>
#include "psycopg/connection.h"
#ifdef __cplusplus
extern "C" {
#endif
extern PyTypeObject lobjectType;
typedef struct {
PyObject HEAD;
connectionObject *conn; /* connection owning the lobject */
int closed:1; /* 1 if the lobject is closed */
long int mark; /* copied from conn->mark */
Oid oid; /* the oid for this lobject */
int fd; /* the file descriptor for file-like ops */
} lobjectObject;
/* exception-raising macros */
#define EXC_IF_LOBJ_CLOSED(self) \
if ((self)->closed || ((self)->conn && (self)->conn->closed)) { \
PyErr_SetString(InterfaceError, "lobject already closed"); \
return NULL; }
#define EXC_IF_LOBJ_LEVEL0(self) \
if (self->conn->isolation_level == 0) { \
psyco_set_error(ProgrammingError, (PyObject*)self, \
"can't use a lobject outside of transactions", NULL, NULL); \
return NULL; \
}
#define EXC_IF_LOBJ_UNMARKED(self) \
if (self->conn->mark != self->mark) { \
psyco_set_error(ProgrammingError, (PyObject*)self, \
"lobject isn't valid anymore", NULL, NULL); \
return NULL; \
}
#ifdef __cplusplus
}
#endif
#endif /* !defined(PSYCOPG_LOBJECT_H) */

276
psycopg/lobject_type.c Normal file
View File

@ -0,0 +1,276 @@
/* lobject_type.c - python interface to lobject objects
*
* Copyright (C) 2003-2006 Federico Di Gregorio <fog@debian.org>
*
* This file is part of psycopg.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2,
* or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public Likcense
* along with this program; if not, write to the Free Software
* Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include <Python.h>
#include <structmember.h>
#include <string.h>
#define PSYCOPG_MODULE
#include "psycopg/config.h"
#include "psycopg/python.h"
#include "psycopg/psycopg.h"
#include "psycopg/lobject.h"
#include "psycopg/connection.h"
#include "psycopg/microprotocols.h"
#include "psycopg/microprotocols_proto.h"
#include "psycopg/pqpath.h"
#include "pgversion.h"
#ifdef PSYCOPG_EXTENSIONS
/** public methods **/
/* close method - close the lobject */
#define psyco_lobj_close_doc \
"close() -- Close the lobject."
static PyObject *
psyco_lobj_close(lobjectObject *self, PyObject *args)
{
if (!PyArg_ParseTuple(args, "")) return NULL;
EXC_IF_LOBJ_CLOSED(self);
EXC_IF_LOBJ_LEVEL0(self);
EXC_IF_LOBJ_UNMARKED(self);
self->closed = 1;
if (self->fd != -1)
lo_close(self->conn->pgconn, self->fd);
self->fd = -1;
Dprintf("psyco_lobj_close: lobject at %p closed", self);
Py_INCREF(Py_None);
return Py_None;
}
/** the lobject object **/
/* object method list */
static struct PyMethodDef lobjectObject_methods[] = {
{"close", (PyCFunction)psyco_lobj_close,
METH_VARARGS, psyco_lobj_close_doc},
{NULL}
};
/* object member list */
static struct PyMemberDef lobjectObject_members[] = {
{"oid", T_LONG,
offsetof(lobjectObject, oid), RO,
"The backend OID associated to this lobject."},
{NULL}
};
/* initialization and finalization methods */
static int
lobject_setup(lobjectObject *self, connectionObject *conn,
Oid oid, int mode, Oid new_oid, char *new_file)
{
Dprintf("lobject_setup: init lobject object at %p", self);
if (conn->isolation_level == 0) {
psyco_set_error(ProgrammingError, (PyObject*)self,
"can't use a lobject outside of transactions", NULL, NULL);
return -1;
}
self->conn = conn;
self->mark = conn->mark;
Py_INCREF((PyObject*)self->conn);
self->closed = 0;
self->oid = InvalidOid;
self->fd = -1;
Py_BEGIN_ALLOW_THREADS;
pthread_mutex_lock(&(self->conn->lock));
pq_begin(self->conn);
/* if the oid is InvalidOid we create a new lob before opening it
or we import a file from the FS, depending on the value of
new_name */
if (oid == InvalidOid) {
if (new_file)
self->oid = lo_import(self->conn->pgconn, new_file);
else
self->oid = lo_create(self->conn->pgconn, new_oid);
Dprintf("lobject_setup: large object created with oid = %d",
self->oid);
if (self->oid == InvalidOid) goto end;
mode = INV_WRITE;
}
else {
self->oid = oid;
if (mode == 0) mode = INV_READ;
}
/* if the oid is a real one we try to open with the given mode */
Dprintf("oid = %d, mode = %d", self->oid, mode);
self->fd = lo_open(self->conn->pgconn, self->oid, mode);
end:
pthread_mutex_unlock(&(self->conn->lock));
Py_END_ALLOW_THREADS;
/* here we check for errors before returning 0 */
if (self->fd == -1 || self->oid == InvalidOid) {
pq_raise(conn, NULL, NULL, NULL);
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;
}
}
static void
lobject_dealloc(PyObject* obj)
{
lobjectObject *self = (lobjectObject *)obj;
if (self->conn->isolation_level > 0
&& self->conn->mark == self->mark) {
if (self->fd != -1)
lo_close(self->conn->pgconn, self->fd);
}
Py_XDECREF((PyObject*)self->conn);
Dprintf("lobject_dealloc: deleted lobject object at %p, refcnt = %d",
obj, obj->ob_refcnt);
obj->ob_type->tp_free(obj);
}
static int
lobject_init(PyObject *obj, PyObject *args, PyObject *kwds)
{
Oid oid=InvalidOid, new_oid=InvalidOid;
int mode=0;
char *new_file = NULL;
PyObject *conn;
if (!PyArg_ParseTuple(args, "O|iiis",
&conn, &oid, &mode, &new_oid, &new_file))
return -1;
return lobject_setup((lobjectObject *)obj,
(connectionObject *)conn, oid, mode, new_oid, new_file);
}
static PyObject *
lobject_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
{
return type->tp_alloc(type, 0);
}
static void
lobject_del(PyObject* self)
{
PyObject_Del(self);
}
static PyObject *
lobject_repr(lobjectObject *self)
{
return PyString_FromFormat(
"<lobject object at %p; closed: %d>", self, self->closed);
}
/* object type */
#define lobjectType_doc \
"A database large object."
PyTypeObject lobjectType = {
PyObject_HEAD_INIT(NULL)
0,
"psycopg2._psycopg.lobject",
sizeof(lobjectObject),
0,
lobject_dealloc, /*tp_dealloc*/
0, /*tp_print*/
0, /*tp_getattr*/
0, /*tp_setattr*/
0, /*tp_compare*/
(reprfunc)lobject_repr, /*tp_repr*/
0, /*tp_as_number*/
0, /*tp_as_sequence*/
0, /*tp_as_mapping*/
0, /*tp_hash */
0, /*tp_call*/
(reprfunc)lobject_repr, /*tp_str*/
0, /*tp_getattro*/
0, /*tp_setattro*/
0, /*tp_as_buffer*/
Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE|Py_TPFLAGS_HAVE_ITER, /*tp_flags*/
lobjectType_doc, /*tp_doc*/
0, /*tp_traverse*/
0, /*tp_clear*/
0, /*tp_richcompare*/
0, /*tp_weaklistoffset*/
0, /*tp_iter*/
0, /*tp_iternext*/
/* Attribute descriptor and subclassing stuff */
lobjectObject_methods, /*tp_methods*/
lobjectObject_members, /*tp_members*/
0, /*tp_getset*/
0, /*tp_base*/
0, /*tp_dict*/
0, /*tp_descr_get*/
0, /*tp_descr_set*/
0, /*tp_dictoffset*/
lobject_init, /*tp_init*/
0, /*tp_alloc Will be set to PyType_GenericAlloc in module init*/
lobject_new, /*tp_new*/
(freefunc)lobject_del, /*tp_free Low-level free-memory routine */
0, /*tp_is_gc For PyObject_IS_GC */
0, /*tp_bases*/
0, /*tp_mro method resolution order */
0, /*tp_cache*/
0, /*tp_subclasses*/
0 /*tp_weaklist*/
};
#endif

View File

@ -256,6 +256,8 @@ pq_commit(connectionObject *conn)
return 0; return 0;
} }
conn->mark += 1;
pq_clear_async(conn); pq_clear_async(conn);
pgres = PQexec(conn->pgconn, query); pgres = PQexec(conn->pgconn, query);
if (pgres == NULL) { if (pgres == NULL) {
@ -299,6 +301,8 @@ pq_abort(connectionObject *conn)
return 0; return 0;
} }
conn->mark += 1;
pq_clear_async(conn); pq_clear_async(conn);
pgres = PQexec(conn->pgconn, query); pgres = PQexec(conn->pgconn, query);
if (pgres == NULL) { if (pgres == NULL) {

View File

@ -37,5 +37,7 @@ extern int pq_commit(connectionObject *conn);
extern int pq_abort(connectionObject *conn); extern int pq_abort(connectionObject *conn);
extern int pq_is_busy(connectionObject *conn); extern int pq_is_busy(connectionObject *conn);
extern void pq_set_critical(connectionObject *conn, const char *msg); extern void pq_set_critical(connectionObject *conn, const char *msg);
extern void pq_raise(connectionObject *conn, cursorObject *curs,
PyObject *exc, char *msg);
#endif /* !defined(PSYCOPG_PQPATH_H) */ #endif /* !defined(PSYCOPG_PQPATH_H) */

View File

@ -27,6 +27,7 @@
#include "psycopg/psycopg.h" #include "psycopg/psycopg.h"
#include "psycopg/connection.h" #include "psycopg/connection.h"
#include "psycopg/cursor.h" #include "psycopg/cursor.h"
#include "psycopg/lobject.h"
#include "psycopg/typecast.h" #include "psycopg/typecast.h"
#include "psycopg/microprotocols.h" #include "psycopg/microprotocols.h"
#include "psycopg/microprotocols_proto.h" #include "psycopg/microprotocols_proto.h"
@ -551,7 +552,7 @@ init_psycopg(void)
asisType.ob_type = &PyType_Type; asisType.ob_type = &PyType_Type;
listType.ob_type = &PyType_Type; listType.ob_type = &PyType_Type;
chunkType.ob_type = &PyType_Type; chunkType.ob_type = &PyType_Type;
if (PyType_Ready(&connectionType) == -1) return; if (PyType_Ready(&connectionType) == -1) return;
if (PyType_Ready(&cursorType) == -1) return; if (PyType_Ready(&cursorType) == -1) return;
if (PyType_Ready(&typecastType) == -1) return; if (PyType_Ready(&typecastType) == -1) return;
@ -562,6 +563,11 @@ init_psycopg(void)
if (PyType_Ready(&listType) == -1) return; if (PyType_Ready(&listType) == -1) return;
if (PyType_Ready(&chunkType) == -1) return; if (PyType_Ready(&chunkType) == -1) return;
#ifdef PSYCOPG_EXTENSIONS
lobjectType.ob_type = &PyType_Type;
if (PyType_Ready(&lobjectType) == -1) return;
#endif
#ifdef HAVE_PYBOOL #ifdef HAVE_PYBOOL
pbooleanType.ob_type = &PyType_Type; pbooleanType.ob_type = &PyType_Type;
if (PyType_Ready(&pbooleanType) == -1) return; if (PyType_Ready(&pbooleanType) == -1) return;
@ -638,7 +644,10 @@ init_psycopg(void)
PyModule_AddObject(module, "connection", (PyObject*)&connectionType); PyModule_AddObject(module, "connection", (PyObject*)&connectionType);
PyModule_AddObject(module, "cursor", (PyObject*)&cursorType); PyModule_AddObject(module, "cursor", (PyObject*)&cursorType);
PyModule_AddObject(module, "ISQLQuote", (PyObject*)&isqlquoteType); PyModule_AddObject(module, "ISQLQuote", (PyObject*)&isqlquoteType);
#ifdef PSYCOPG_EXTENSIONS
PyModule_AddObject(module, "lobject", (PyObject*)&lobjectType);
#endif
/* encodings dictionary in module dictionary */ /* encodings dictionary in module dictionary */
PyModule_AddObject(module, "encodings", psycoEncodings); PyModule_AddObject(module, "encodings", psycoEncodings);
@ -664,6 +673,10 @@ init_psycopg(void)
listType.tp_alloc = PyType_GenericAlloc; listType.tp_alloc = PyType_GenericAlloc;
chunkType.tp_alloc = PyType_GenericAlloc; chunkType.tp_alloc = PyType_GenericAlloc;
#ifdef PSYCOPG_EXTENSIONS
lobjectType.tp_alloc = PyType_GenericAlloc;
#endif
#ifdef HAVE_PYDATETIME #ifdef HAVE_PYDATETIME
pydatetimeType.tp_alloc = PyType_GenericAlloc; pydatetimeType.tp_alloc = PyType_GenericAlloc;
#endif #endif

View File

@ -1,5 +1,5 @@
[build_ext] [build_ext]
define=PSYCOPG_EXTENSIONS,PSYCOPG_DISPLAY_SIZE,PSYCOPG_NEW_BOOLEAN,HAVE_PQFREEMEM,HAVE_PQPROTOCOL3 define=PSYCOPG_EXTENSIONS,PSYCOPG_DISPLAY_SIZE,PSYCOPG_NEW_BOOLEAN,HAVE_PQFREEMEM,HAVE_PQPROTOCOL3,PSYCOPG_DEBUG
# PSYCOPG_EXTENSIONS enables extensions to PEP-249 (you really want this) # PSYCOPG_EXTENSIONS enables extensions to PEP-249 (you really want this)
# PSYCOPG_DISPLAY_SIZE enable display size calculation (a little slower) # PSYCOPG_DISPLAY_SIZE enable display size calculation (a little slower)
# HAVE_PQFREEMEM should be defined on PostgreSQL >= 7.3 # HAVE_PQFREEMEM should be defined on PostgreSQL >= 7.3

View File

@ -193,6 +193,7 @@ sources = [
'psycopgmodule.c', 'pqpath.c', 'typecast.c', 'psycopgmodule.c', 'pqpath.c', 'typecast.c',
'microprotocols.c', 'microprotocols_proto.c', 'microprotocols.c', 'microprotocols_proto.c',
'connection_type.c', 'connection_int.c', 'cursor_type.c', 'cursor_int.c', 'connection_type.c', 'connection_int.c', 'cursor_type.c', 'cursor_int.c',
'lobject_type.c',
'adapter_qstring.c', 'adapter_pboolean.c', 'adapter_binary.c', 'adapter_qstring.c', 'adapter_pboolean.c', 'adapter_binary.c',
'adapter_asis.c', 'adapter_list.c'] 'adapter_asis.c', 'adapter_list.c']