Fixed dsn and closed attributes in failing connection subclasses.

From ticket #192 discussion.

Conflicts:

	NEWS
	tests/test_connection.py
This commit is contained in:
Daniele Varrazzo 2014-04-03 01:41:19 +01:00
parent 36a7deed52
commit eddb0e3d90
4 changed files with 27 additions and 4 deletions

2
NEWS
View File

@ -12,6 +12,8 @@ What's new in psycopg 2.4.7
(:ticket:`#187`). (:ticket:`#187`).
- 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)
{ {
if (async == 1) { int rv;
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

@ -1046,6 +1046,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) {
@ -1053,7 +1054,6 @@ connection_setup(connectionObject *self, const char *dsn, long int async)
*pos = 'x'; *pos = 'x';
} }
exit:
return res; return res;
} }

View File

@ -218,6 +218,18 @@ class ConnectionTests(unittest.TestCase):
self.assert_(not notices, "%d notices raised" % len(notices)) self.assert_(not notices, "%d notices raised" % len(notices))
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(unittest.TestCase): class IsolationLevelsTestCase(unittest.TestCase):