Changeset 806


Ignore:
Timestamp:
Feb 1, 2016, 1:38:05 AM (4 years ago)
Author:
cito
Message:

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).

Location:
trunk
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • trunk/docs/contents/changelog.rst

    r804 r806  
    5656    - The pkey() method of the classic interface now returns tuples instead
    5757      of frozenset. The order of the tuples is like in the primary key index.
     58    - Like the DB-API 2 module, the classic module now also returns bool values
     59      from the database as Python bool objects instead of strings.  You can
     60      still restore the old behavior by calling set_bool(False).
    5861    - Like the DB-API 2 module, the classic module now also returns bytea
    59       columns fetched from the database as byte strings, so you don't need to
     62      data fetched from the database as byte strings, so you don't need to
    6063      call unescape_bytea() any more.  This has been made configurable though,
    6164      and you can restore the old behavior by calling set_bytea_escaped(True).
  • trunk/docs/contents/pg/module.rst

    r804 r806  
    409409This function checks whether PyGreSQL returns PostgreSQL boolean
    410410values converted to Python bool objects, or as ``'f'`` and ``'t'``
    411 strings which are the values used internally by PostgreSQL. By default,
    412 conversion to bool objects is not activated, but you can enable
    413 this with the :func:`set_bool` function.
     411strings which are the values used internally by PostgreSQL.  By default,
     412conversion to bool objects is activated, but you can disable this with
     413the :func:`set_bool` function.
    414414
    415415.. versionadded:: 4.2
     
    423423This function can be used to specify whether PyGreSQL shall return
    424424PostgreSQL boolean values converted to Python bool objects, or as
    425 ``'f'`` and ``'t'`` strings which are the values used internally by PostgreSQL.
    426 By default, conversion to bool objects is not activated, but you can
    427 enable this by calling ``set_bool(True)``.
     425``'f'`` and ``'t'`` strings which are the values used internally by
     426PostgreSQL.  By default, conversion to bool objects is activated,
     427but you can disable this by calling ``set_bool(True)``.
    428428
    429429.. versionadded:: 4.2
     430
     431.. versionchanged:: 5.0
     432   Boolean values had been returned as string by default in earlier versions.
    430433
    431434get/set_array -- whether arrays are returned as list objects
     
    460463
    461464.. versionchanged:: 5.0
    462    Arrays had been returned as text strings only in earlier versions.
     465   Arrays had been always returned as text strings only in earlier versions.
    463466
    464467get/set_bytea_escaped -- whether bytea data is returned escaped
     
    493496
    494497.. versionchanged:: 5.0
    495    Bytea data had been returned in escaped form in earlier versions.
     498   Bytea data had been returned in escaped form by default in earlier versions.
    496499
    497500get/set_jsondecode -- decoding JSON format
     
    520523
    521524.. versionchanged:: 5.0
    522    JSON data had been returned in as text strings in earlier versions.
     525   JSON data had been always returned as text strings in earlier versions.
    523526
    524527get/set_cast_hook -- fallback typecast function
  • trunk/pgmodule.c

    r804 r806  
    9797                                *jsondecode = NULL; /* function for decoding json strings */
    9898static char decimal_point = '.'; /* decimal point used in money values */
    99 static int bool_as_text = 1; /* whether bool shall be returned as text */
     99static int bool_as_text = 0; /* whether bool shall be returned as text */
    100100static int array_as_text = 0; /* whether arrays shall be returned as text */
    101101static int bytea_escaped = 0; /* whether bytea shall be returned escaped */
  • trunk/tests/test_classic_connection.py

    r801 r806  
    753753            ).getresult(), [(None,)])
    754754
    755     def testQueryWithBoolParams(self, use_bool=None):
     755    def testQueryWithBoolParams(self, bool_enabled=None):
    756756        query = self.c.query
    757         if use_bool is not None:
    758             use_bool_default = pg.get_bool()
    759             pg.set_bool(use_bool)
    760         try:
    761             v_false, v_true = (False, True) if use_bool else 'ft'
     757        if bool_enabled is not None:
     758            bool_enabled_default = pg.get_bool()
     759            pg.set_bool(bool_enabled)
     760        try:
     761            v_false, v_true = (False, True) if bool_enabled else 'ft'
    762762            r_false, r_true = [(v_false,)], [(v_true,)]
    763763            self.assertEqual(query("select false").getresult(), r_false)
     
    776776            self.assertEqual(query(q, (True,)).getresult(), r_true)
    777777        finally:
    778             if use_bool is not None:
    779                 pg.set_bool(use_bool_default)
    780 
    781     def testQueryWithBoolParamsAndUseBool(self):
    782         self.testQueryWithBoolParams(use_bool=True)
     778            if bool_enabled is not None:
     779                pg.set_bool(bool_enabled_default)
     780
     781    def testQueryWithBoolParamsNotDefault(self):
     782        self.testQueryWithBoolParams(bool_enabled=not pg.get_bool())
    783783
    784784    def testQueryWithIntParams(self):
  • trunk/tests/test_classic_functions.py

    r802 r806  
    713713        r = pg.get_bool()
    714714        self.assertIsInstance(r, bool)
    715         self.assertIs(r, False)
     715        self.assertIs(r, True)
    716716
    717717    def testSetBool(self):
    718718        use_bool = pg.get_bool()
    719719        try:
    720             pg.set_bool(True)
     720            pg.set_bool(False)
    721721            r = pg.get_bool()
    722722            pg.set_bool(use_bool)
    723723            self.assertIsInstance(r, bool)
    724             self.assertIs(r, True)
    725             pg.set_bool(False)
     724            self.assertIs(r, False)
     725            pg.set_bool(True)
    726726            r = pg.get_bool()
    727727            self.assertIsInstance(r, bool)
    728             self.assertIs(r, False)
     728            self.assertIs(r, True)
    729729        finally:
    730730            pg.set_bool(use_bool)
Note: See TracChangeset for help on using the changeset viewer.