Author: cito
Date: Tue Feb 9 05:51:46 2016
New Revision: 847
Log:
Fix issue with arrays and records containing unicode
Arrays and records with non-ascii unicode elements
did not work properly in Python 2.
Modified:
trunk/pgdb.py
trunk/tests/test_dbapi20.py
Modified: trunk/pgdb.py
==============================================================================
--- trunk/pgdb.py Tue Feb 9 02:20:10 2016 (r846)
+++ trunk/pgdb.py Tue Feb 9 05:51:46 2016 (r847)
@@ -773,7 +773,10 @@
if not value: # exception for empty array
return "'{}'"
q = self._quote
- return 'ARRAY[%s]' % ','.join(str(q(v)) for v in value)
+ try:
+ return 'ARRAY[%s]' % ','.join(str(q(v)) for v in value)
+ except UnicodeEncodeError: # Python 2 with non-ascii values
+ return u'ARRAY[%s]' % ','.join(unicode(q(v)) for v in value)
if isinstance(value, tuple):
# Quote as a ROW constructor. This is better than using a record
# literal because it carries the information that this is a record
@@ -781,7 +784,10 @@
# this usable with the IN syntax as well. It is only necessary
# when the records has a single column which is not really useful.
q = self._quote
- return '(%s)' % ','.join(str(q(v)) for v in value)
+ try:
+ return '(%s)' % ','.join(str(q(v)) for v in value)
+ except UnicodeEncodeError: # Python 2 with non-ascii values
+ return u'(%s)' % ','.join(unicode(q(v)) for v in value)
try:
value = value.__pg_repr__()
except AttributeError:
Modified: trunk/tests/test_dbapi20.py
==============================================================================
--- trunk/tests/test_dbapi20.py Tue Feb 9 02:20:10 2016 (r846)
+++ trunk/tests/test_dbapi20.py Tue Feb 9 05:51:46 2016 (r847)
@@ -733,6 +733,23 @@
con.close()
self.assertEqual(row, values)
+ def test_unicode_list_and_tuple(self):
+ value = (u'Käse', u'Würstchen')
+ con = self._connect()
+ try:
+ cur = con.cursor()
+ try:
+ cur.execute("select %s, %s", value)
+ except pgdb.DatabaseError:
+ self.skipTest('database does not support latin-1')
+ row = cur.fetchone()
+ cur.execute("select %s, %s", (list(value), tuple(value)))
+ as_list, as_tuple = cur.fetchone()
+ finally:
+ con.close()
+ self.assertEqual(as_list, list(row))
+ self.assertEqual(as_tuple, tuple(row))
+
def test_insert_record(self):
values = [('John', 61), ('Jane', 63),
('Fred', None), ('Wilma', None),
_______________________________________________
PyGreSQL mailing list
[email protected]
https://mail.vex.net/mailman/listinfo.cgi/pygresql