Check connection type in lobject init

Fixes ticket #187.
This commit is contained in:
Daniele Varrazzo 2013-11-27 12:42:57 +00:00
parent 6ab1ec1c37
commit e11d0d39ec
3 changed files with 16 additions and 7 deletions

2
NEWS
View File

@ -8,6 +8,8 @@ What's new in psycopg 2.5.2
(:ticket:`#170`).
- Meaningful connection errors report a meaningful message, thanks to
Alexey Borzenkov (:ticket:`#173`).
- Manually creating `lobject` with the wrong parameter doesn't segfault
(:ticket:`#187`).
What's new in psycopg 2.5.1

View File

@ -355,10 +355,12 @@ lobject_dealloc(PyObject* obj)
{
lobjectObject *self = (lobjectObject *)obj;
if (self->conn) { /* if not, init failed */
if (lobject_close(self) < 0)
PyErr_Print();
Py_XDECREF((PyObject*)self->conn);
PyMem_Free(self->smode);
}
Dprintf("lobject_dealloc: deleted lobject object at %p, refcnt = "
FORMAT_CODE_PY_SSIZE_T, obj, Py_REFCNT(obj));
@ -372,10 +374,11 @@ lobject_init(PyObject *obj, PyObject *args, PyObject *kwds)
int oid = (int)InvalidOid, new_oid = (int)InvalidOid;
const char *smode = "";
const char *new_file = NULL;
PyObject *conn;
PyObject *conn = NULL;
if (!PyArg_ParseTuple(args, "O|iziz",
&conn, &oid, &smode, &new_oid, &new_file))
if (!PyArg_ParseTuple(args, "O!|iziz",
&connectionType, &conn,
&oid, &smode, &new_oid, &new_file))
return -1;
return lobject_setup((lobjectObject *)obj,

View File

@ -77,6 +77,10 @@ class LargeObjectTests(LargeObjectTestCase):
self.assertNotEqual(lo, None)
self.assertEqual(lo.mode[0], "w")
def test_connection_needed(self):
self.assertRaises(TypeError,
psycopg2.extensions.lobject, [])
def test_open_non_existent(self):
# By creating then removing a large object, we get an Oid that
# should be unused.