Fixed dsn and closed attributes in failing connection subclasses.

From ticket #192 discussion.
This commit is contained in:
Daniele Varrazzo 2014-04-03 01:41:19 +01:00
parent e2bb7ff8da
commit 65fbe9159a
4 changed files with 27 additions and 4 deletions

2
NEWS
View File

@ -8,6 +8,8 @@ What's new in psycopg 2.5.3
Chris Withers (:ticket:`#193`). Chris Withers (:ticket:`#193`).
- Avoid blocking async connections on connect (:ticket:`#194`). Thanks to - Avoid blocking async connections on connect (:ticket:`#194`). Thanks to
Adam Petrovich for the bug report and diagnosis. Adam Petrovich for the bug report and diagnosis.
- Fixed handling of dsn and closed attributes in connection subclasses
failing to connect (from :ticket:`#192` discussion).
- Don't segfault using poorly defined cursor subclasses which forgot to call - Don't segfault using poorly defined cursor subclasses which forgot to call
the superclass init (:ticket:`#195`). the superclass init (:ticket:`#195`).
- Fixed debug build on Windows, thanks to James Emerton. - Fixed debug build on Windows, thanks to James Emerton.

View File

@ -629,14 +629,23 @@ _conn_async_connect(connectionObject *self)
int int
conn_connect(connectionObject *self, long int async) conn_connect(connectionObject *self, long int async)
{ {
int rv;
if (async == 1) { if (async == 1) {
Dprintf("con_connect: connecting in ASYNC mode"); Dprintf("con_connect: connecting in ASYNC mode");
return _conn_async_connect(self); rv = _conn_async_connect(self);
} }
else { else {
Dprintf("con_connect: connecting in SYNC mode"); Dprintf("con_connect: connecting in SYNC mode");
return _conn_sync_connect(self); rv = _conn_sync_connect(self);
} }
if (rv != 0) {
/* connection failed, so let's close ourselves */
self->closed = 2;
}
return rv;
} }

View File

@ -1099,6 +1099,7 @@ connection_setup(connectionObject *self, const char *dsn, long int async)
res = 0; res = 0;
} }
exit:
/* here we obfuscate the password even if there was a connection error */ /* here we obfuscate the password even if there was a connection error */
pos = strstr(self->dsn, "password"); pos = strstr(self->dsn, "password");
if (pos != NULL) { if (pos != NULL) {
@ -1106,7 +1107,6 @@ connection_setup(connectionObject *self, const char *dsn, long int async)
*pos = 'x'; *pos = 'x';
} }
exit:
return res; return res;
} }

View File

@ -249,6 +249,18 @@ class ConnectionTests(ConnectingTestCase):
cur.execute("select 1 as a") cur.execute("select 1 as a")
self.assertRaises(TypeError, (lambda r: r['a']), cur.fetchone()) self.assertRaises(TypeError, (lambda r: r['a']), cur.fetchone())
def test_failed_init_status(self):
class SubConnection(psycopg2.extensions.connection):
def __init__(self, dsn):
try:
super(SubConnection, self).__init__(dsn)
except Exception:
pass
c = SubConnection("dbname=thereisnosuchdatabasemate password=foobar")
self.assert_(c.closed, "connection failed so it must be closed")
self.assert_('foobar' not in c.dsn, "password was not obscured")
pass
class IsolationLevelsTestCase(ConnectingTestCase): class IsolationLevelsTestCase(ConnectingTestCase):