[PyGreSQL] [CVS] Change to pygresql: pgmodule.c, test_pg.py

Christoph Zwerschke cito at druid.net
Sun Nov 23 09:07:35 EST 2008


Update of /usr/cvs/Public/pygresql/module
In directory druid.net:/tmp/cvs-serv16578/module

Modified Files:
	pgmodule.c test_pg.py 
Log Message:
Added test for getnotify() and improved this method by calling PQconsumeInput() instead of submitting an empty command.
To see the diffs for this commit:
   http://www.druid.net/pygresql/viewcvs.cgi/cvs/pygresql/module/pgmodule.c.diff?r1=1.87&r2=1.88

Index: pgmodule.c
===================================================================
RCS file: /usr/cvs/Public/pygresql/module/pgmodule.c,v
retrieving revision 1.87
retrieving revision 1.88
diff -u -r1.87 -r1.88
--- pgmodule.c	21 Nov 2008 23:24:38 -0000	1.87
+++ pgmodule.c	23 Nov 2008 14:07:35 -0000	1.88
@@ -1,5 +1,5 @@
 /*
- * $Id: pgmodule.c,v 1.87 2008/11/21 23:24:38 cito Exp $
+ * $Id: pgmodule.c,v 1.88 2008/11/23 14:07:35 cito Exp $
  * PyGres, version 2.2 A Python interface for PostgreSQL database. Written by
  * D'Arcy J.M. Cain, (darcy at druid.net).  Based heavily on code written by
  * Pascal Andre, andre at chimay.via.ecp.fr. Copyright (c) 1995, Pascal Andre
@@ -2142,9 +2142,6 @@
 pg_getnotify(pgobject * self, PyObject * args)
 {
 	PGnotify   *notify;
-	PGresult   *result;
-	PyObject   *notify_result,
-			   *temp;
 
 	if (!self->cnx)
 	{
@@ -2160,27 +2157,22 @@
 		return NULL;
 	}
 
-	/* gets notify and builds result */
-
-	/*
-	 * notifies only come back as result of a query, so I send an empty
-	 * query
-	 */
-	Py_BEGIN_ALLOW_THREADS
-	result = PQexec(self->cnx, " ");
-	Py_END_ALLOW_THREADS
+	/* checks for NOTIFY messages */
+	PQconsumeInput(self->cnx);
 
-		if ((notify = PQnotifies(self->cnx)) != NULL)
+	if ((notify = PQnotifies(self->cnx)) == NULL)
 	{
-		if ((notify_result = PyTuple_New(2)) == NULL)
-		{
-			PQclear(result);
-			return NULL;
-		}
+		Py_INCREF(Py_None);
+		return Py_None;
+	}
+	else
+	{
+		PyObject   *notify_result,
+				   *temp;
 
-		if ((temp = PyString_FromString(notify->relname)) == NULL)
+		if ((notify_result = PyTuple_New(2)) == NULL ||
+			(temp = PyString_FromString(notify->relname)) == NULL)
 		{
-			PQclear(result);
 			return NULL;
 		}
 
@@ -2188,24 +2180,14 @@
 
 		if ((temp = PyInt_FromLong(notify->be_pid)) == NULL)
 		{
-			PQclear(result);
 			Py_DECREF(notify_result);
 			return NULL;
 		}
 
 		PyTuple_SET_ITEM(notify_result, 1, temp);
 		PQfreemem(notify);
+		return notify_result;
 	}
-	else
-	{
-		Py_INCREF(Py_None);
-		notify_result = Py_None;
-	}
-
-	PQclear(result);
-
-	/* returns result */
-	return notify_result;
 }
 
 /* source creation */

   http://www.druid.net/pygresql/viewcvs.cgi/cvs/pygresql/module/test_pg.py.diff?r1=1.19&r2=1.20

Index: test_pg.py
===================================================================
RCS file: /usr/cvs/Public/pygresql/module/test_pg.py,v
retrieving revision 1.19
retrieving revision 1.20
diff -u -r1.19 -r1.20
--- test_pg.py	21 Nov 2008 23:38:17 -0000	1.19
+++ test_pg.py	23 Nov 2008 14:07:35 -0000	1.20
@@ -4,7 +4,7 @@
 #
 # Written by Christoph Zwerschke
 #
-# $Id: test_pg.py,v 1.19 2008/11/21 23:38:17 cito Exp $
+# $Id: test_pg.py,v 1.20 2008/11/23 14:07:35 cito Exp $
 #
 
 """Test the classic PyGreSQL interface in the pg module.
@@ -579,6 +579,21 @@
             '2|xyz  |uvw  ',
             '(2 rows)'])
 
+    def testGetNotify(self):
+        self.assert_(self.c.getnotify() is None)
+        self.c.query('listen test_notify')
+        try:
+            self.assert_(self.c.getnotify() is None)
+            self.c.query('notify test_notify')
+            r = self.c.getnotify()
+            self.assert_(isinstance(r, tuple))
+            self.assertEqual(len(r), 2)
+            self.assert_(isinstance(r[0], str))
+            self.assert_(isinstance(r[1], int))
+            self.assertEqual(r[0], 'test_notify')
+        finally:
+            self.c.query('unlisten test_notify')
+
 
 class TestInserttable(unittest.TestCase):
     """"Test inserttable method."""



More information about the PyGreSQL mailing list