summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDaniele Varrazzo <daniele.varrazzo@gmail.com>2014-04-03 01:41:19 +0100
committerDaniele Varrazzo <daniele.varrazzo@gmail.com>2014-04-03 01:42:35 +0100
commite7fc7f31b9c1c405b57c83b46475eb8618a66362 (patch)
treec16a2c048302d4af51670edaec59a47b239cd890
parent0258e90ef069161de5f6b2d02fa26f031d9674fc (diff)
downloadpsycopg2-e7fc7f31b9c1c405b57c83b46475eb8618a66362.tar.gz
Fixed dsn and closed attributes in failing connection subclasses.
From ticket #192 discussion.
-rw-r--r--NEWS2
-rw-r--r--psycopg/connection_int.c15
-rw-r--r--psycopg/connection_type.c2
-rwxr-xr-xtests/test_connection.py12
4 files changed, 27 insertions, 4 deletions
diff --git a/NEWS b/NEWS
index d5f1686..928eede 100644
--- a/NEWS
+++ b/NEWS
@@ -17,6 +17,8 @@ What's new in psycopg 2.5.3
Chris Withers (:ticket:`#193`).
- Avoid blocking async connections on connect (:ticket:`#194`). Thanks to
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
the superclass init (:ticket:`#195`).
- Fixed debug build on Windows, thanks to James Emerton.
diff --git a/psycopg/connection_int.c b/psycopg/connection_int.c
index c48dc81..aea2841 100644
--- a/psycopg/connection_int.c
+++ b/psycopg/connection_int.c
@@ -629,14 +629,23 @@ _conn_async_connect(connectionObject *self)
int
conn_connect(connectionObject *self, long int async)
{
- if (async == 1) {
+ int rv;
+
+ if (async == 1) {
Dprintf("con_connect: connecting in ASYNC mode");
- return _conn_async_connect(self);
+ rv = _conn_async_connect(self);
}
else {
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;
}
diff --git a/psycopg/connection_type.c b/psycopg/connection_type.c
index b3d4aa6..80ce7fe 100644
--- a/psycopg/connection_type.c
+++ b/psycopg/connection_type.c
@@ -1099,6 +1099,7 @@ connection_setup(connectionObject *self, const char *dsn, long int async)
res = 0;
}
+exit:
/* here we obfuscate the password even if there was a connection error */
pos = strstr(self->dsn, "password");
if (pos != NULL) {
@@ -1106,7 +1107,6 @@ connection_setup(connectionObject *self, const char *dsn, long int async)
*pos = 'x';
}
-exit:
return res;
}
diff --git a/tests/test_connection.py b/tests/test_connection.py
index 26ad932..b2c5197 100755
--- a/tests/test_connection.py
+++ b/tests/test_connection.py
@@ -249,6 +249,18 @@ class ConnectionTests(ConnectingTestCase):
cur.execute("select 1 as a")
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):