diff --git a/NEWS b/NEWS index 02953649..8ce77c49 100644 --- a/NEWS +++ b/NEWS @@ -1,6 +1,15 @@ Current release --------------- +What's new in psycopg 2.6 +------------------------- + +Bug fixes: + +- Json apapter's `!str()` returns the adapted content instead of the `!repr()` + (:ticket:`#191`). + + What's new in psycopg 2.5.3 ^^^^^^^^^^^^^^^^^^^^^^^^^^^ diff --git a/lib/_json.py b/lib/_json.py index 536dd58b..3a4361e8 100644 --- a/lib/_json.py +++ b/lib/_json.py @@ -92,6 +92,14 @@ class Json(object): s = self.dumps(self.adapted) return QuotedString(s).getquoted() + if sys.version_info < (3,): + def __str__(self): + return self.getquoted() + else: + def __str__(self): + # getquoted is binary in Py3 + return self.getquoted().decode('ascii', errors='replace') + def register_json(conn_or_curs=None, globally=False, loads=None, oid=None, array_oid=None): diff --git a/tests/test_types_extras.py b/tests/test_types_extras.py index 96ffcd3c..4fedd5ea 100755 --- a/tests/test_types_extras.py +++ b/tests/test_types_extras.py @@ -1055,6 +1055,17 @@ class JsonTestCase(ConnectingTestCase): self.assertEqual(data['a'], 100) self.assertEqual(data['b'], None) + @skip_if_no_json_module + def test_str(self): + snowman = u"\u2603" + obj = {'a': [1, 2, snowman]} + j = psycopg2.extensions.adapt(psycopg2.extras.Json(obj)) + s = str(j) + self.assert_(isinstance(s, str)) + # no pesky b's + self.assert_(s.startswith("'")) + self.assert_(s.endswith("'")) + class RangeTestCase(unittest.TestCase): def test_noparam(self):