summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDaniele Varrazzo <daniele.varrazzo@gmail.com>2010-04-21 18:40:05 +0100
committerDaniele Varrazzo <daniele.varrazzo@gmail.com>2010-04-23 13:24:35 +0100
commitdf959c20bee9e4f0e452731c9d79e0578f3f01b0 (patch)
tree688840f6b8288e8b54c36ff72361d9075fd77e5d
parentd915cb12a8541af9477dde7546f0c4a76156fe60 (diff)
downloadpsycopg2-df959c20bee9e4f0e452731c9d79e0578f3f01b0.tar.gz
Making sync and async connection setup somewhat more consistent.
-rw-r--r--psycopg/connection_int.c50
-rwxr-xr-xtests/test_async.py5
-rw-r--r--tests/test_connection.py15
3 files changed, 41 insertions, 29 deletions
diff --git a/psycopg/connection_int.c b/psycopg/connection_int.c
index bde9a2a..891d0bf 100644
--- a/psycopg/connection_int.c
+++ b/psycopg/connection_int.c
@@ -232,13 +232,25 @@ conn_get_isolation_level(PGresult *pgres)
int
conn_get_protocol_version(PGconn *pgconn)
{
+ int ret;
+
#ifdef HAVE_PQPROTOCOL3
- return PQprotocolVersion(pgconn);
+ ret = PQprotocolVersion(pgconn);
#else
- return 2;
+ ret = 2;
#endif
+
+ Dprintf("conn_connect: using protocol %d", ret);
+ return ret;
+}
+
+int
+conn_get_server_version(PGconn *pgconn)
+{
+ return (int)PQserverVersion(pgconn);
}
+
/* conn_setup - setup and read basic information about the connection */
int
@@ -250,12 +262,6 @@ conn_setup(connectionObject *self, PGconn *pgconn)
pthread_mutex_lock(&self->lock);
Py_BLOCK_THREADS;
- if (self->encoding) free(self->encoding);
- self->equote = 0;
- self->isolation_level = 0;
-
- self->equote = conn_get_standard_conforming_strings(pgconn);
-
if (!psyco_green()) {
Py_UNBLOCK_THREADS;
pgres = PQexec(pgconn, psyco_datestyle);
@@ -366,14 +372,6 @@ conn_sync_connect(connectionObject *self)
PQsetNoticeProcessor(pgconn, conn_notice_callback, (void*)self);
-#ifdef HAVE_PQPROTOCOL3
- self->protocol = PQprotocolVersion(pgconn);
-#else
- self->protocol = 2;
-#endif
-
- Dprintf("conn_connect: using protocol %d", self->protocol);
-
/* if the connection is green, wait to finish connection */
if (green) {
wait_rv = psyco_wait(self);
@@ -384,7 +382,9 @@ conn_sync_connect(connectionObject *self)
}
}
- self->server_version = (int)PQserverVersion(pgconn);
+ self->equote = conn_get_standard_conforming_strings(pgconn);
+ self->server_version = conn_get_server_version(pgconn);
+ self->protocol = conn_get_protocol_version(self->pgconn);
/* From here the connection is considered ready: with the new status,
* poll() will use PQisBusy instead of PQconnectPoll.
@@ -592,7 +592,7 @@ conn_poll_connect_fetch(connectionObject *self)
/* since this is the last step, set the other instance variables now */
self->equote = conn_get_standard_conforming_strings(self->pgconn);
self->protocol = conn_get_protocol_version(self->pgconn);
- self->server_version = (int) PQserverVersion(self->pgconn);
+ self->server_version = conn_get_server_version(self->pgconn);
/*
* asynchronous connections always use isolation level 0, the user is
* expected to manage the transactions himself, by sending
@@ -600,20 +600,12 @@ conn_poll_connect_fetch(connectionObject *self)
*/
self->isolation_level = 0;
- Py_BEGIN_ALLOW_THREADS;
- pthread_mutex_lock(&(self->lock));
-
- /* set the connection to nonblocking */
- if (PQsetnonblocking(self->pgconn, 1) != 0) {
- Dprintf("conn_async_connect: PQsetnonblocking() FAILED");
- Py_BLOCK_THREADS;
- PyErr_SetString(OperationalError, "PQsetnonblocking() failed");
+ /* FIXME: this is a bug: the above queries were sent to the server
+ with a blocking connection */
+ if (pq_set_non_blocking(self, 1, 1) != 0) {
return NULL;
}
- pthread_mutex_unlock(&(self->lock));
- Py_END_ALLOW_THREADS;
-
/* next status is going to READY */
next_status = CONN_STATUS_READY;
}
diff --git a/tests/test_async.py b/tests/test_async.py
index a4a11b9..9187cec 100755
--- a/tests/test_async.py
+++ b/tests/test_async.py
@@ -74,6 +74,11 @@ class AsyncTests(unittest.TestCase):
# the async connection should be in isolevel 0
self.assertEquals(self.conn.isolation_level, 0)
+ # check other properties to be found on the connection
+ self.assert_(self.conn.server_version)
+ self.assert_(self.conn.protocol_version in (2,3))
+ self.assert_(self.conn.encoding in psycopg2.extensions.encodings)
+
def test_async_named_cursor(self):
self.assertRaises(psycopg2.ProgrammingError,
self.conn.cursor, "name")
diff --git a/tests/test_connection.py b/tests/test_connection.py
index dbb80af..4eff867 100644
--- a/tests/test_connection.py
+++ b/tests/test_connection.py
@@ -2,6 +2,7 @@
import unittest
import psycopg2
+import psycopg2.extensions
import tests
class ConnectionTests(unittest.TestCase):
@@ -49,6 +50,20 @@ class ConnectionTests(unittest.TestCase):
conn = self.connect()
self.assert_(conn.server_version)
+ def test_protocol_version(self):
+ conn = self.connect()
+ self.assert_(conn.protocol_version in (2,3), conn.protocol_version)
+
+ def test_isolation_level(self):
+ conn = self.connect()
+ self.assertEqual(
+ conn.isolation_level,
+ psycopg2.extensions.ISOLATION_LEVEL_READ_COMMITTED)
+
+ def test_encoding(self):
+ conn = self.connect()
+ self.assert_(conn.encoding in psycopg2.extensions.encodings)
+
def test_suite():
return unittest.TestLoader().loadTestsFromName(__name__)