Fixed explicit connection.cursor(cursor_factory=None)

Fixes issue #210.
This commit is contained in:
Daniele Varrazzo 2014-04-30 17:56:09 +01:00
parent 03b8b1e97a
commit 69605e54ec
3 changed files with 22 additions and 5 deletions

2
NEWS
View File

@ -32,6 +32,8 @@ What's new in psycopg 2.5.3
happens for TCP connections instead (:ticket:`#196`).
- Fixed overflow opening a lobject with an oid not fitting in a signed int
(:ticket:`#203`).
- Fixed handling of explicit default ``cursor_factory=None`` in
`connection.cursor()` (:ticket:`#210`).
- Fixed possible segfault in named cursors creation.
- Fixed debug build on Windows, thanks to James Emerton.

View File

@ -55,7 +55,7 @@ psyco_conn_cursor(connectionObject *self, PyObject *args, PyObject *kwargs)
PyObject *obj = NULL;
PyObject *rv = NULL;
PyObject *name = Py_None;
PyObject *factory = (PyObject *)&cursorType;
PyObject *factory = Py_None;
PyObject *withhold = Py_False;
PyObject *scrollable = Py_None;
@ -64,16 +64,21 @@ psyco_conn_cursor(connectionObject *self, PyObject *args, PyObject *kwargs)
EXC_IF_CONN_CLOSED(self);
if (self->cursor_factory && self->cursor_factory != Py_None) {
factory = self->cursor_factory;
}
if (!PyArg_ParseTupleAndKeywords(
args, kwargs, "|OOOO", kwlist,
&name, &factory, &withhold, &scrollable)) {
goto exit;
}
if (factory == Py_None) {
if (self->cursor_factory && self->cursor_factory != Py_None) {
factory = self->cursor_factory;
}
else {
factory = (PyObject *)&cursorType;
}
}
if (self->status != CONN_STATUS_READY &&
self->status != CONN_STATUS_BEGIN &&
self->status != CONN_STATUS_PREPARED) {

View File

@ -249,6 +249,16 @@ class ConnectionTests(ConnectingTestCase):
cur.execute("select 1 as a")
self.assertRaises(TypeError, (lambda r: r['a']), cur.fetchone())
def test_cursor_factory_none(self):
# issue #210
conn = self.connect()
cur = conn.cursor(cursor_factory=None)
self.assertEqual(type(cur), psycopg2.extensions.cursor)
conn = self.connect(cursor_factory=psycopg2.extras.DictCursor)
cur = conn.cursor(cursor_factory=None)
self.assertEqual(type(cur), psycopg2.extras.DictCursor)
def test_failed_init_status(self):
class SubConnection(psycopg2.extensions.connection):
def __init__(self, dsn):