Author: cito
Date: Thu Nov 26 13:33:40 2015
New Revision: 635

Log:
Add getter functions where we only had setters

set_namedresult() and set_decimal() did not have associated getters.

Modified:
   trunk/module/TEST_PyGreSQL_classic_connection.py
   trunk/module/TEST_PyGreSQL_classic_functions.py
   trunk/module/pgmodule.c

Modified: trunk/module/TEST_PyGreSQL_classic_connection.py
==============================================================================
--- trunk/module/TEST_PyGreSQL_classic_connection.py    Thu Nov 26 12:02:32 
2015        (r634)
+++ trunk/module/TEST_PyGreSQL_classic_connection.py    Thu Nov 26 13:33:40 
2015        (r635)
@@ -1459,25 +1459,41 @@
             pg.set_decimal_point(point)
         self.assertEqual(r, bad_money)
 
+    def testGetDecimal(self):
+        decimal_class = pg.get_decimal()
+        # error if a parameter is passed
+        self.assertRaises(TypeError, pg.get_decimal, decimal_class)
+        self.assertIs(decimal_class, pg.Decimal)  # the default setting
+        pg.set_decimal(int)
+        try:
+            r = pg.get_decimal()
+        finally:
+            pg.set_decimal(decimal_class)
+        self.assertIs(r, int)
+        r = pg.get_decimal()
+        self.assertIs(r, decimal_class)
+
     def testSetDecimal(self):
-        d = pg.Decimal
+        decimal_class = pg.get_decimal()
+        # error if no parameter is passed
+        self.assertRaises(TypeError, pg.set_decimal)
         query = self.c.query
         try:
             r = query("select 3425::numeric")
         except pg.ProgrammingError:
             self.skipTest('database does not support numeric')
         r = r.getresult()[0][0]
-        self.assertIsInstance(r, d)
-        self.assertEqual(r, d('3425'))
+        self.assertIsInstance(r, decimal_class)
+        self.assertEqual(r, decimal_class('3425'))
         r = query("select 3425::numeric")
-        pg.set_decimal(long)
+        pg.set_decimal(int)
         try:
             r = r.getresult()[0][0]
         finally:
-            pg.set_decimal(d)
-        self.assertNotIsInstance(r, d)
-        self.assertIsInstance(r, long)
-        self.assertEqual(r, long(3425))
+            pg.set_decimal(decimal_class)
+        self.assertNotIsInstance(r, decimal_class)
+        self.assertIsInstance(r, int)
+        self.assertEqual(r, int(3425))
 
     def testGetBool(self):
         use_bool = pg.get_bool()
@@ -1516,6 +1532,8 @@
 
     def testSetBool(self):
         use_bool = pg.get_bool()
+        # error if no parameter is passed
+        self.assertRaises(TypeError, pg.set_bool)
         query = self.c.query
         try:
             r = query("select true::bool")
@@ -1541,20 +1559,17 @@
         self.assertIsInstance(r, str)
         self.assertIs(r, 't')
 
-    def testSetNamedresult(self):
-        query = self.c.query
+    def testGetBool(self):
+        namedresult = pg.get_namedresult()
+        # error if a parameter is passed
+        self.assertRaises(TypeError, pg.get_namedresult, namedresult)
+        self.assertIs(namedresult, pg._namedresult)  # the default setting
 
-        r = query("select 1 as x, 2 as y").namedresult()[0]
-        self.assertIsInstance(r, tuple)
-        self.assertEqual(r, (1, 2))
-        self.assertIsNot(type(r), tuple)
-        self.assertEqual(r._fields, ('x', 'y'))
-        self.assertEqual(r._asdict(), {'x': 1, 'y': 2})
-        self.assertEqual(r.__class__.__name__, 'Row')
+    def testSetNamedresult(self):
+        namedresult = pg.get_namedresult()
+        self.assertTrue(callable(namedresult))
 
-        _namedresult = pg._namedresult
-        self.assertTrue(callable(_namedresult))
-        pg.set_namedresult(_namedresult)
+        query = self.c.query
 
         r = query("select 1 as x, 2 as y").namedresult()[0]
         self.assertIsInstance(r, tuple)
@@ -1564,12 +1579,13 @@
         self.assertEqual(r._asdict(), {'x': 1, 'y': 2})
         self.assertEqual(r.__class__.__name__, 'Row')
 
-        def _listresult(q):
+        def listresult(q):
             return [list(row) for row in q.getresult()]
 
-        pg.set_namedresult(_listresult)
-
+        pg.set_namedresult(listresult)
         try:
+            r = pg.get_namedresult()
+            self.assertIs(r, listresult)
             r = query("select 1 as x, 2 as y").namedresult()[0]
             self.assertIsInstance(r, list)
             self.assertEqual(r, [1, 2])
@@ -1577,7 +1593,10 @@
             self.assertFalse(hasattr(r, '_fields'))
             self.assertNotEqual(r.__class__.__name__, 'Row')
         finally:
-            pg.set_namedresult(_namedresult)
+            pg.set_namedresult(namedresult)
+
+        r = pg.get_namedresult()
+        self.assertIs(r, namedresult)
 
 
 class TestStandaloneEscapeFunctions(unittest.TestCase):

Modified: trunk/module/TEST_PyGreSQL_classic_functions.py
==============================================================================
--- trunk/module/TEST_PyGreSQL_classic_functions.py     Thu Nov 26 12:02:32 
2015        (r634)
+++ trunk/module/TEST_PyGreSQL_classic_functions.py     Thu Nov 26 13:33:40 
2015        (r635)
@@ -338,11 +338,22 @@
         pg.set_decimal_point(point)
         self.assertIsInstance(r, str)
         self.assertEqual(r, '*')
+        r = pg.get_decimal_point()
+        self.assertIsInstance(r, str)
+        self.assertEqual(r, point)
+
+    def testGetDecimal(self):
+        r = pg.get_decimal()
+        self.assertIs(r, pg.Decimal)
 
     def testSetDecimal(self):
         decimal_class = pg.Decimal
-        pg.set_decimal(long)
+        pg.set_decimal(int)
+        r = pg.get_decimal()
         pg.set_decimal(decimal_class)
+        self.assertIs(r, int)
+        r = pg.get_decimal()
+        self.assertIs(r, decimal_class)
 
     def testGetBool(self):
         r = pg.get_bool()
@@ -361,10 +372,23 @@
         pg.set_bool(use_bool)
         self.assertIsInstance(r, bool)
         self.assertIs(r, False)
+        r = pg.get_bool()
+        self.assertIsInstance(r, bool)
+        self.assertIs(r, use_bool)
+
+    def testGetNamedresult(self):
+        r = pg.get_namedresult()
+        self.assertIs(r, pg._namedresult)
 
     def testSetNamedresult(self):
-        pg.set_namedresult(lambda q: q.getresult())
-        pg.set_namedresult(pg._namedresult)
+        namedresult = pg.get_namedresult()
+        f = lambda q: q.getresult()
+        pg.set_namedresult(f)
+        r = pg.get_namedresult()
+        pg.set_namedresult(namedresult)
+        self.assertIs(r, f)
+        r = pg.get_namedresult()
+        self.assertIs(r, namedresult)
 
 
 class TestModuleConstants(unittest.TestCase):

Modified: trunk/module/pgmodule.c
==============================================================================
--- trunk/module/pgmodule.c     Thu Nov 26 12:02:32 2015        (r634)
+++ trunk/module/pgmodule.c     Thu Nov 26 13:33:40 2015        (r635)
@@ -4054,9 +4054,38 @@
        return to_obj;
 }
 
+/* get decimal point */
+static char pgGetDecimalPoint__doc__[] =
+"get_decimal_point() -- get decimal point to be used for money values.";
+
+static PyObject *
+pgGetDecimalPoint(PyObject *self, PyObject * args)
+{
+       PyObject *ret = NULL;
+       char s[2];
+
+       if (PyArg_ParseTuple(args, ""))
+       {
+               if (decimal_point)
+               {
+                       s[0] = decimal_point; s[1] = '\0';
+                       ret = PyStr_FromString(s);
+               } else {
+                       Py_INCREF(Py_None); ret = Py_None;
+               }
+       }
+       else
+       {
+               PyErr_SetString(PyExc_TypeError,
+                       "get_decimal_point() takes no parameter");
+       }
+
+       return ret;
+}
+
 /* set decimal point */
 static char pgSetDecimalPoint__doc__[] =
-"set_decimal_point() -- set decimal point to be used for money values.";
+"set_decimal_point(char) -- set decimal point to be used for money values.";
 
 static PyObject *
 pgSetDecimalPoint(PyObject *self, PyObject * args)
@@ -4085,36 +4114,25 @@
        return ret;
 }
 
-/* get decimal point */
-static char pgGetDecimalPoint__doc__[] =
-"get_decimal_point() -- get decimal point to be used for money values.";
+/* get decimal type */
+static char pgGetDecimal__doc__[] =
+"get_decimal() -- set a decimal type to be used for numeric values.";
 
 static PyObject *
-pgGetDecimalPoint(PyObject *self, PyObject * args)
+pgGetDecimal(PyObject *self, PyObject *args)
 {
        PyObject *ret = NULL;
-       char s[2];
 
        if (PyArg_ParseTuple(args, ""))
        {
-               if (decimal_point)
-               {
-                       s[0] = decimal_point; s[1] = '\0';
-                       ret = PyStr_FromString(s);
-               } else {
-                       Py_INCREF(Py_None); ret = Py_None;
-               }
-       }
-       else
-       {
-               PyErr_SetString(PyExc_TypeError,
-                       "get_decimal_point() takes no parameter");
+               ret = decimal ? decimal : Py_None;
+               Py_INCREF(ret);
        }
 
        return ret;
 }
 
-/* set decimal */
+/* set decimal type */
 static char pgSetDecimal__doc__[] =
 "set_decimal(cls) -- set a decimal type to be used for numeric values.";
 
@@ -4140,12 +4158,31 @@
                        PyErr_SetString(PyExc_TypeError,
                                "decimal type must be None or callable");
        }
+
+       return ret;
+}
+
+/* get usage of bool values */
+static char pgGetBool__doc__[] =
+"get_bool() -- check whether boolean values are converted to bool.";
+
+static PyObject *
+pgGetBool(PyObject *self, PyObject * args)
+{
+       PyObject *ret = NULL;
+
+       if (PyArg_ParseTuple(args, ""))
+       {
+               ret = use_bool ? Py_True : Py_False;
+               Py_INCREF(ret);
+       }
+
        return ret;
 }
 
 /* set usage of bool values */
 static char pgSetBool__doc__[] =
-"set_bool() -- set whether boolean values should be converted to bool.";
+"set_bool(bool) -- set whether boolean values should be converted to bool.";
 
 static PyObject *
 pgSetBool(PyObject *self, PyObject * args)
@@ -4163,25 +4200,25 @@
        return ret;
 }
 
-/* ge usage of bool values */
-static char pgGetBool__doc__[] =
-"get_bool() -- check whether boolean values are converted to bool.";
+/* get named result factory */
+static char pgGetNamedresult__doc__[] =
+"get_namedresult(cls) -- get the function used for getting named results.";
 
 static PyObject *
-pgGetBool(PyObject *self, PyObject * args)
+pgGetNamedresult(PyObject *self, PyObject *args)
 {
        PyObject *ret = NULL;
 
        if (PyArg_ParseTuple(args, ""))
        {
-               ret = use_bool ? Py_True : Py_False;
+               ret = namedresult ? namedresult : Py_None;
                Py_INCREF(ret);
        }
 
        return ret;
 }
 
-/* set named result */
+/* set named result factory */
 static char pgSetNamedresult__doc__[] =
 "set_namedresult(cls) -- set a function to be used for getting named results.";
 
@@ -4201,6 +4238,7 @@
                else
                        PyErr_SetString(PyExc_TypeError, "parameter must be 
callable");
        }
+
        return ret;
 }
 
@@ -4556,14 +4594,18 @@
                        pgEscapeBytea__doc__},
        {"unescape_bytea", (PyCFunction) pgUnescapeBytea, METH_VARARGS,
                        pgUnescapeBytea__doc__},
-       {"set_decimal_point", (PyCFunction) pgSetDecimalPoint, METH_VARARGS,
-                       pgSetDecimalPoint__doc__},
        {"get_decimal_point", (PyCFunction) pgGetDecimalPoint, METH_VARARGS,
                        pgGetDecimalPoint__doc__},
+       {"set_decimal_point", (PyCFunction) pgSetDecimalPoint, METH_VARARGS,
+                       pgSetDecimalPoint__doc__},
+       {"get_decimal", (PyCFunction) pgGetDecimal, METH_VARARGS,
+                       pgGetDecimal__doc__},
        {"set_decimal", (PyCFunction) pgSetDecimal, METH_VARARGS,
                        pgSetDecimal__doc__},
-       {"set_bool", (PyCFunction) pgSetBool, METH_VARARGS, pgSetBool__doc__},
        {"get_bool", (PyCFunction) pgGetBool, METH_VARARGS, pgGetBool__doc__},
+       {"set_bool", (PyCFunction) pgSetBool, METH_VARARGS, pgSetBool__doc__},
+       {"get_namedresult", (PyCFunction) pgGetNamedresult, METH_VARARGS,
+                       pgGetNamedresult__doc__},
        {"set_namedresult", (PyCFunction) pgSetNamedresult, METH_VARARGS,
                        pgSetNamedresult__doc__},
 
_______________________________________________
PyGreSQL mailing list
[email protected]
https://mail.vex.net/mailman/listinfo.cgi/pygresql

Reply via email to