Menu

#77 __unicodeConvert doesn't support PgArray of Unicode Strings

closed-fixed
nobody
PgSQL (41)
5
2005-02-27
2004-10-21
No

If we have PgArray of Unicode Strings, PyPgSQL fails.

E.g.

c = db.cursor()
query = "CREATE TABLE test (id TEXT, values TEXT[]);
c.execute(query)

query = "INSERT INTO test values (%s, %s);"
values = []
values.append(1)
values.append([u"abc", u"dé"])
c.execute(query, values)
c.close()
db.commit()

File "D:\Soft\Python23\lib\site-
packages\pyPgSQL\PgSQL.py", line 3054, in execute
parms = _quoteall(parms[0])
File "D:\Soft\Python23\lib\site-
packages\pyPgSQL\PgSQL.py", line 2286, in _quoteall
t = tuple(map(_quote, vdict))
File "D:\Soft\Python23\lib\site-
packages\pyPgSQL\PgSQL.py", line 2255, in _quote
return value._quote()
File "D:\Soft\Python23\lib\site-
packages\pyPgSQL\PgSQL.py", line 1175, in _quote
return _handleArray(self.value)
File "D:\Soft\Python23\lib\site-
packages\pyPgSQL\PgSQL.py", line 2238, in _handleArray
_j = '%s%s,' % (_j, PgQuoteString(str(_i), 1))
UnicodeEncodeError: 'ascii' codec can't encode
character u'\x82' in position 0: ordinal not in range(128)

The cause is that the routine __unicodeConvert doesn't
take into account PgArray of Unicode Strings

If we modify the __unicodeConvert and add the two
marked line then there is no more error:

def __unicodeConvert(self, obj):
if type(obj) is StringType:
return obj
elif type(obj) is UnicodeType:
return obj.encode(*self.conn.client_encoding)
elif type(obj) in (ListType, TupleType):
converted_obj = []
for item in obj:
if type(item) is UnicodeType:
converted_obj.append(item.encode
(*self.conn.client_encoding))
elif isinstance(item, PgArray): # !!!
converted_obj.append(PgArray(self.__unicodeConvert
(item.value))) # !!!
else:
converted_obj.append(item)
return converted_obj

Discussion

  • Gerhard Häring

    Gerhard Häring - 2005-02-27
    • status: open --> open-fixed
     
  • Gerhard Häring

    Gerhard Häring - 2005-02-27

    Logged In: YES
    user_id=163326

    Fixed in CVS.

     
  • Gerhard Häring

    Gerhard Häring - 2005-02-27
    • status: open-fixed --> closed-fixed
     

Log in to post a comment.