Author: cito
Date: Mon Feb 1 01:38:05 2016
New Revision: 806
Log:
Return bools from Postgres as Python bools by default
This has been done by the DB-API 2 module already and was configurable
in the classic module since a long time, but by default it was disabled.
It is now time to flip the switch and have this activated by default in
version 5.0. You can still restore the old behavior with set_bool(False).
Modified:
trunk/docs/contents/changelog.rst
trunk/docs/contents/pg/module.rst
trunk/pgmodule.c
trunk/tests/test_classic_connection.py
trunk/tests/test_classic_functions.py
Modified: trunk/docs/contents/changelog.rst
==============================================================================
--- trunk/docs/contents/changelog.rst Mon Feb 1 00:56:27 2016 (r805)
+++ trunk/docs/contents/changelog.rst Mon Feb 1 01:38:05 2016 (r806)
@@ -55,8 +55,11 @@
conversion of arrays to lists can be disabled with set_array(False).
- The pkey() method of the classic interface now returns tuples instead
of frozenset. The order of the tuples is like in the primary key index.
+ - Like the DB-API 2 module, the classic module now also returns bool values
+ from the database as Python bool objects instead of strings. You can
+ still restore the old behavior by calling set_bool(False).
- Like the DB-API 2 module, the classic module now also returns bytea
- columns fetched from the database as byte strings, so you don't need to
+ data fetched from the database as byte strings, so you don't need to
call unescape_bytea() any more. This has been made configurable though,
and you can restore the old behavior by calling set_bytea_escaped(True).
- A method set_jsondecode() has been added for changing or removing the
Modified: trunk/docs/contents/pg/module.rst
==============================================================================
--- trunk/docs/contents/pg/module.rst Mon Feb 1 00:56:27 2016 (r805)
+++ trunk/docs/contents/pg/module.rst Mon Feb 1 01:38:05 2016 (r806)
@@ -408,9 +408,9 @@
This function checks whether PyGreSQL returns PostgreSQL boolean
values converted to Python bool objects, or as ``'f'`` and ``'t'``
-strings which are the values used internally by PostgreSQL. By default,
-conversion to bool objects is not activated, but you can enable
-this with the :func:`set_bool` function.
+strings which are the values used internally by PostgreSQL. By default,
+conversion to bool objects is activated, but you can disable this with
+the :func:`set_bool` function.
.. versionadded:: 4.2
@@ -422,12 +422,15 @@
This function can be used to specify whether PyGreSQL shall return
PostgreSQL boolean values converted to Python bool objects, or as
-``'f'`` and ``'t'`` strings which are the values used internally by PostgreSQL.
-By default, conversion to bool objects is not activated, but you can
-enable this by calling ``set_bool(True)``.
+``'f'`` and ``'t'`` strings which are the values used internally by
+PostgreSQL. By default, conversion to bool objects is activated,
+but you can disable this by calling ``set_bool(True)``.
.. versionadded:: 4.2
+.. versionchanged:: 5.0
+ Boolean values had been returned as string by default in earlier versions.
+
get/set_array -- whether arrays are returned as list objects
-------------------------------------------------------------
@@ -459,7 +462,7 @@
.. versionadded:: 5.0
.. versionchanged:: 5.0
- Arrays had been returned as text strings only in earlier versions.
+ Arrays had been always returned as text strings only in earlier versions.
get/set_bytea_escaped -- whether bytea data is returned escaped
---------------------------------------------------------------
@@ -492,7 +495,7 @@
.. versionadded:: 5.0
.. versionchanged:: 5.0
- Bytea data had been returned in escaped form in earlier versions.
+ Bytea data had been returned in escaped form by default in earlier versions.
get/set_jsondecode -- decoding JSON format
------------------------------------------
@@ -519,7 +522,7 @@
.. versionadded:: 5.0
.. versionchanged:: 5.0
- JSON data had been returned in as text strings in earlier versions.
+ JSON data had been always returned as text strings in earlier versions.
get/set_cast_hook -- fallback typecast function
-----------------------------------------------
Modified: trunk/pgmodule.c
==============================================================================
--- trunk/pgmodule.c Mon Feb 1 00:56:27 2016 (r805)
+++ trunk/pgmodule.c Mon Feb 1 01:38:05 2016 (r806)
@@ -96,7 +96,7 @@
*namedresult = NULL, /* function for getting
named results */
*jsondecode = NULL; /* function for decoding
json strings */
static char decimal_point = '.'; /* decimal point used in money values */
-static int bool_as_text = 1; /* whether bool shall be returned as text */
+static int bool_as_text = 0; /* whether bool shall be returned as text */
static int array_as_text = 0; /* whether arrays shall be returned as text */
static int bytea_escaped = 0; /* whether bytea shall be returned escaped */
Modified: trunk/tests/test_classic_connection.py
==============================================================================
--- trunk/tests/test_classic_connection.py Mon Feb 1 00:56:27 2016
(r805)
+++ trunk/tests/test_classic_connection.py Mon Feb 1 01:38:05 2016
(r806)
@@ -752,13 +752,13 @@
self.assertEqual(self.c.query("select $1::text", [[None]]
).getresult(), [(None,)])
- def testQueryWithBoolParams(self, use_bool=None):
+ def testQueryWithBoolParams(self, bool_enabled=None):
query = self.c.query
- if use_bool is not None:
- use_bool_default = pg.get_bool()
- pg.set_bool(use_bool)
+ if bool_enabled is not None:
+ bool_enabled_default = pg.get_bool()
+ pg.set_bool(bool_enabled)
try:
- v_false, v_true = (False, True) if use_bool else 'ft'
+ v_false, v_true = (False, True) if bool_enabled else 'ft'
r_false, r_true = [(v_false,)], [(v_true,)]
self.assertEqual(query("select false").getresult(), r_false)
self.assertEqual(query("select true").getresult(), r_true)
@@ -775,11 +775,11 @@
self.assertEqual(query(q, (False,)).getresult(), r_false)
self.assertEqual(query(q, (True,)).getresult(), r_true)
finally:
- if use_bool is not None:
- pg.set_bool(use_bool_default)
+ if bool_enabled is not None:
+ pg.set_bool(bool_enabled_default)
- def testQueryWithBoolParamsAndUseBool(self):
- self.testQueryWithBoolParams(use_bool=True)
+ def testQueryWithBoolParamsNotDefault(self):
+ self.testQueryWithBoolParams(bool_enabled=not pg.get_bool())
def testQueryWithIntParams(self):
query = self.c.query
Modified: trunk/tests/test_classic_functions.py
==============================================================================
--- trunk/tests/test_classic_functions.py Mon Feb 1 00:56:27 2016
(r805)
+++ trunk/tests/test_classic_functions.py Mon Feb 1 01:38:05 2016
(r806)
@@ -712,20 +712,20 @@
def testGetBool(self):
r = pg.get_bool()
self.assertIsInstance(r, bool)
- self.assertIs(r, False)
+ self.assertIs(r, True)
def testSetBool(self):
use_bool = pg.get_bool()
try:
- pg.set_bool(True)
+ pg.set_bool(False)
r = pg.get_bool()
pg.set_bool(use_bool)
self.assertIsInstance(r, bool)
- self.assertIs(r, True)
- pg.set_bool(False)
+ self.assertIs(r, False)
+ pg.set_bool(True)
r = pg.get_bool()
self.assertIsInstance(r, bool)
- self.assertIs(r, False)
+ self.assertIs(r, True)
finally:
pg.set_bool(use_bool)
r = pg.get_bool()
_______________________________________________
PyGreSQL mailing list
[email protected]
https://mail.vex.net/mailman/listinfo.cgi/pygresql