Changeset 635


Ignore:
Timestamp:
Nov 26, 2015, 1:33:40 PM (4 years ago)
Author:
cito
Message:

Add getter functions where we only had setters

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

Location:
trunk/module
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/module/TEST_PyGreSQL_classic_connection.py

    r634 r635  
    14601460        self.assertEqual(r, bad_money)
    14611461
     1462    def testGetDecimal(self):
     1463        decimal_class = pg.get_decimal()
     1464        # error if a parameter is passed
     1465        self.assertRaises(TypeError, pg.get_decimal, decimal_class)
     1466        self.assertIs(decimal_class, pg.Decimal)  # the default setting
     1467        pg.set_decimal(int)
     1468        try:
     1469            r = pg.get_decimal()
     1470        finally:
     1471            pg.set_decimal(decimal_class)
     1472        self.assertIs(r, int)
     1473        r = pg.get_decimal()
     1474        self.assertIs(r, decimal_class)
     1475
    14621476    def testSetDecimal(self):
    1463         d = pg.Decimal
     1477        decimal_class = pg.get_decimal()
     1478        # error if no parameter is passed
     1479        self.assertRaises(TypeError, pg.set_decimal)
    14641480        query = self.c.query
    14651481        try:
     
    14681484            self.skipTest('database does not support numeric')
    14691485        r = r.getresult()[0][0]
    1470         self.assertIsInstance(r, d)
    1471         self.assertEqual(r, d('3425'))
     1486        self.assertIsInstance(r, decimal_class)
     1487        self.assertEqual(r, decimal_class('3425'))
    14721488        r = query("select 3425::numeric")
    1473         pg.set_decimal(long)
     1489        pg.set_decimal(int)
    14741490        try:
    14751491            r = r.getresult()[0][0]
    14761492        finally:
    1477             pg.set_decimal(d)
    1478         self.assertNotIsInstance(r, d)
    1479         self.assertIsInstance(r, long)
    1480         self.assertEqual(r, long(3425))
     1493            pg.set_decimal(decimal_class)
     1494        self.assertNotIsInstance(r, decimal_class)
     1495        self.assertIsInstance(r, int)
     1496        self.assertEqual(r, int(3425))
    14811497
    14821498    def testGetBool(self):
     
    15171533    def testSetBool(self):
    15181534        use_bool = pg.get_bool()
     1535        # error if no parameter is passed
     1536        self.assertRaises(TypeError, pg.set_bool)
    15191537        query = self.c.query
    15201538        try:
     
    15421560        self.assertIs(r, 't')
    15431561
     1562    def testGetBool(self):
     1563        namedresult = pg.get_namedresult()
     1564        # error if a parameter is passed
     1565        self.assertRaises(TypeError, pg.get_namedresult, namedresult)
     1566        self.assertIs(namedresult, pg._namedresult)  # the default setting
     1567
    15441568    def testSetNamedresult(self):
     1569        namedresult = pg.get_namedresult()
     1570        self.assertTrue(callable(namedresult))
     1571
    15451572        query = self.c.query
    15461573
     
    15531580        self.assertEqual(r.__class__.__name__, 'Row')
    15541581
    1555         _namedresult = pg._namedresult
    1556         self.assertTrue(callable(_namedresult))
    1557         pg.set_namedresult(_namedresult)
    1558 
    1559         r = query("select 1 as x, 2 as y").namedresult()[0]
    1560         self.assertIsInstance(r, tuple)
    1561         self.assertEqual(r, (1, 2))
    1562         self.assertIsNot(type(r), tuple)
    1563         self.assertEqual(r._fields, ('x', 'y'))
    1564         self.assertEqual(r._asdict(), {'x': 1, 'y': 2})
    1565         self.assertEqual(r.__class__.__name__, 'Row')
    1566 
    1567         def _listresult(q):
     1582        def listresult(q):
    15681583            return [list(row) for row in q.getresult()]
    15691584
    1570         pg.set_namedresult(_listresult)
    1571 
    1572         try:
     1585        pg.set_namedresult(listresult)
     1586        try:
     1587            r = pg.get_namedresult()
     1588            self.assertIs(r, listresult)
    15731589            r = query("select 1 as x, 2 as y").namedresult()[0]
    15741590            self.assertIsInstance(r, list)
     
    15781594            self.assertNotEqual(r.__class__.__name__, 'Row')
    15791595        finally:
    1580             pg.set_namedresult(_namedresult)
     1596            pg.set_namedresult(namedresult)
     1597
     1598        r = pg.get_namedresult()
     1599        self.assertIs(r, namedresult)
    15811600
    15821601
  • trunk/module/TEST_PyGreSQL_classic_functions.py

    r634 r635  
    339339        self.assertIsInstance(r, str)
    340340        self.assertEqual(r, '*')
     341        r = pg.get_decimal_point()
     342        self.assertIsInstance(r, str)
     343        self.assertEqual(r, point)
     344
     345    def testGetDecimal(self):
     346        r = pg.get_decimal()
     347        self.assertIs(r, pg.Decimal)
    341348
    342349    def testSetDecimal(self):
    343350        decimal_class = pg.Decimal
    344         pg.set_decimal(long)
     351        pg.set_decimal(int)
     352        r = pg.get_decimal()
    345353        pg.set_decimal(decimal_class)
     354        self.assertIs(r, int)
     355        r = pg.get_decimal()
     356        self.assertIs(r, decimal_class)
    346357
    347358    def testGetBool(self):
     
    362373        self.assertIsInstance(r, bool)
    363374        self.assertIs(r, False)
     375        r = pg.get_bool()
     376        self.assertIsInstance(r, bool)
     377        self.assertIs(r, use_bool)
     378
     379    def testGetNamedresult(self):
     380        r = pg.get_namedresult()
     381        self.assertIs(r, pg._namedresult)
    364382
    365383    def testSetNamedresult(self):
    366         pg.set_namedresult(lambda q: q.getresult())
    367         pg.set_namedresult(pg._namedresult)
     384        namedresult = pg.get_namedresult()
     385        f = lambda q: q.getresult()
     386        pg.set_namedresult(f)
     387        r = pg.get_namedresult()
     388        pg.set_namedresult(namedresult)
     389        self.assertIs(r, f)
     390        r = pg.get_namedresult()
     391        self.assertIs(r, namedresult)
    368392
    369393
  • trunk/module/pgmodule.c

    r634 r635  
    40554055}
    40564056
    4057 /* set decimal point */
    4058 static char pgSetDecimalPoint__doc__[] =
    4059 "set_decimal_point() -- set decimal point to be used for money values.";
    4060 
    4061 static PyObject *
    4062 pgSetDecimalPoint(PyObject *self, PyObject * args)
    4063 {
    4064         PyObject *ret = NULL;
    4065         char *s = NULL;
    4066 
    4067         /* gets arguments */
    4068         if (PyArg_ParseTuple(args, "z", &s))
    4069         {
    4070                 if (!s)
    4071                         s = "\0";
    4072                 else if (*s && (*(s+1) || !strchr(".,;: '*/_`|", *s)))
    4073                         s = NULL;
    4074         }
    4075 
    4076         if (s)
    4077         {
    4078                 decimal_point = *s;
    4079                 Py_INCREF(Py_None); ret = Py_None;
    4080         } else {
    4081                 PyErr_SetString(PyExc_TypeError,
    4082                         "set_decimal_point() expects a decimal mark character");
    4083         }
    4084 
    4085         return ret;
    4086 }
    4087 
    40884057/* get decimal point */
    40894058static char pgGetDecimalPoint__doc__[] =
     
    41154084}
    41164085
    4117 /* set decimal */
     4086/* set decimal point */
     4087static char pgSetDecimalPoint__doc__[] =
     4088"set_decimal_point(char) -- set decimal point to be used for money values.";
     4089
     4090static PyObject *
     4091pgSetDecimalPoint(PyObject *self, PyObject * args)
     4092{
     4093        PyObject *ret = NULL;
     4094        char *s = NULL;
     4095
     4096        /* gets arguments */
     4097        if (PyArg_ParseTuple(args, "z", &s))
     4098        {
     4099                if (!s)
     4100                        s = "\0";
     4101                else if (*s && (*(s+1) || !strchr(".,;: '*/_`|", *s)))
     4102                        s = NULL;
     4103        }
     4104
     4105        if (s)
     4106        {
     4107                decimal_point = *s;
     4108                Py_INCREF(Py_None); ret = Py_None;
     4109        } else {
     4110                PyErr_SetString(PyExc_TypeError,
     4111                        "set_decimal_point() expects a decimal mark character");
     4112        }
     4113
     4114        return ret;
     4115}
     4116
     4117/* get decimal type */
     4118static char pgGetDecimal__doc__[] =
     4119"get_decimal() -- set a decimal type to be used for numeric values.";
     4120
     4121static PyObject *
     4122pgGetDecimal(PyObject *self, PyObject *args)
     4123{
     4124        PyObject *ret = NULL;
     4125
     4126        if (PyArg_ParseTuple(args, ""))
     4127        {
     4128                ret = decimal ? decimal : Py_None;
     4129                Py_INCREF(ret);
     4130        }
     4131
     4132        return ret;
     4133}
     4134
     4135/* set decimal type */
    41184136static char pgSetDecimal__doc__[] =
    41194137"set_decimal(cls) -- set a decimal type to be used for numeric values.";
     
    41414159                                "decimal type must be None or callable");
    41424160        }
     4161
     4162        return ret;
     4163}
     4164
     4165/* get usage of bool values */
     4166static char pgGetBool__doc__[] =
     4167"get_bool() -- check whether boolean values are converted to bool.";
     4168
     4169static PyObject *
     4170pgGetBool(PyObject *self, PyObject * args)
     4171{
     4172        PyObject *ret = NULL;
     4173
     4174        if (PyArg_ParseTuple(args, ""))
     4175        {
     4176                ret = use_bool ? Py_True : Py_False;
     4177                Py_INCREF(ret);
     4178        }
     4179
    41434180        return ret;
    41444181}
     
    41464183/* set usage of bool values */
    41474184static char pgSetBool__doc__[] =
    4148 "set_bool() -- set whether boolean values should be converted to bool.";
     4185"set_bool(bool) -- set whether boolean values should be converted to bool.";
    41494186
    41504187static PyObject *
     
    41644201}
    41654202
    4166 /* ge usage of bool values */
    4167 static char pgGetBool__doc__[] =
    4168 "get_bool() -- check whether boolean values are converted to bool.";
    4169 
    4170 static PyObject *
    4171 pgGetBool(PyObject *self, PyObject * args)
     4203/* get named result factory */
     4204static char pgGetNamedresult__doc__[] =
     4205"get_namedresult(cls) -- get the function used for getting named results.";
     4206
     4207static PyObject *
     4208pgGetNamedresult(PyObject *self, PyObject *args)
    41724209{
    41734210        PyObject *ret = NULL;
     
    41754212        if (PyArg_ParseTuple(args, ""))
    41764213        {
    4177                 ret = use_bool ? Py_True : Py_False;
     4214                ret = namedresult ? namedresult : Py_None;
    41784215                Py_INCREF(ret);
    41794216        }
     
    41824219}
    41834220
    4184 /* set named result */
     4221/* set named result factory */
    41854222static char pgSetNamedresult__doc__[] =
    41864223"set_namedresult(cls) -- set a function to be used for getting named results.";
     
    42024239                        PyErr_SetString(PyExc_TypeError, "parameter must be callable");
    42034240        }
     4241
    42044242        return ret;
    42054243}
     
    45574595        {"unescape_bytea", (PyCFunction) pgUnescapeBytea, METH_VARARGS,
    45584596                        pgUnescapeBytea__doc__},
     4597        {"get_decimal_point", (PyCFunction) pgGetDecimalPoint, METH_VARARGS,
     4598                        pgGetDecimalPoint__doc__},
    45594599        {"set_decimal_point", (PyCFunction) pgSetDecimalPoint, METH_VARARGS,
    45604600                        pgSetDecimalPoint__doc__},
    4561         {"get_decimal_point", (PyCFunction) pgGetDecimalPoint, METH_VARARGS,
    4562                         pgGetDecimalPoint__doc__},
     4601        {"get_decimal", (PyCFunction) pgGetDecimal, METH_VARARGS,
     4602                        pgGetDecimal__doc__},
    45634603        {"set_decimal", (PyCFunction) pgSetDecimal, METH_VARARGS,
    45644604                        pgSetDecimal__doc__},
     4605        {"get_bool", (PyCFunction) pgGetBool, METH_VARARGS, pgGetBool__doc__},
    45654606        {"set_bool", (PyCFunction) pgSetBool, METH_VARARGS, pgSetBool__doc__},
    4566         {"get_bool", (PyCFunction) pgGetBool, METH_VARARGS, pgGetBool__doc__},
     4607        {"get_namedresult", (PyCFunction) pgGetNamedresult, METH_VARARGS,
     4608                        pgGetNamedresult__doc__},
    45674609        {"set_namedresult", (PyCFunction) pgSetNamedresult, METH_VARARGS,
    45684610                        pgSetNamedresult__doc__},
Note: See TracChangeset for help on using the changeset viewer.